Chat
In this guide we’ll build a small chatbot that takes in user messages and generates responses.
chat-history.baml
class MyUserMessage {role "user" | "assistant"content string}function ChatWithLLM(messages: MyUserMessage[]) -> string {client "openai/gpt-5"prompt #"Answer the user's questions based on the chat history:{% for message in messages %}{{ _.role(message.role) }}{{ message.content }}{% endfor %}Answer:"#}test TestName {functions [ChatWithLLM]args {messages [{role "user"content "Hello!"}{role "assistant"content "Hi!"}]}}
Code
from baml_client import bfrom baml_client.types import MyUserMessagedef main():messages: list[MyUserMessage] = []while True:content = input("Enter your message (or 'quit' to exit): ")if content.lower() == 'quit':breakmessages.append(MyUserMessage(role="user", content=content))agent_response = b.ChatWithLLM(messages=messages)print(f"AI: {agent_response}")print()# Add the agent's response to the chat historymessages.append(MyUserMessage(role="assistant", content=agent_response))if __name__ == "__main__":main()