chat() is the asynchronous BaseLLM method for conversational interaction with an LLM. Unlike complete(), which completes a prompt, it accepts a new input string and optional conversation history and tools. It returns a pair of ChatMessage objects: the user message derived from the input and the model’s response. section 3.1 section 3.2.1
In OllamaLLM, chat() converts the history and new user message to Ollama messages and converts supplied tools to Ollama tools. It calls Ollama’s chat interface, then converts the response back to a ChatMessage. The framework method is distinct from ollama.chat(), the underlying provider interface. section 3.2.1
Figure 3.15 Integrating with Ollama’s chat interface to implement chat(). The user’s input and chat history are converted to Ollama messages, and tools are converted to Ollama tools. Next, ollama.chat() is invoked and its response is then converted back to a ChatMessage. This ChatMessage is returned along with another one derived from the user’s input.

Follow the message and tool conversions across the Ollama integration. section 3.2.1
A response can contain tool-call requests; chat() does not complete the tool-calling process by itself. Run the requested tool with its arguments, then pass the resulting ToolCallResult and the user and assistant messages to continue_chat_with_tool_results(). That method returns tool-result messages and a subsequent model response. section 3.2.2
Request a Hailstone step
Wrap hailstone_step_func in a SimpleFunctionTool and pass it in tools to chat(). A request for the next step from 3 can produce a tool call with x=3. Execute that call and continue the conversation with its result; the example’s final response is 10. section 3.2.2
# Included in chapter-notebooks/ch03/ch03.ipynb #A
from llm_agents_from_scratch.data_structures.llm import ChatMessage
from llm_agents_from_scratch.tools import SimpleFunctionTool
hailstone_tool = SimpleFunctionTool(hailstone_step_func) #B
user_input = (
"What is the result of taking the next step of the " #C
"Hailstone sequence on the number 3? (use the tool.)\n\n"
"Be very succinct in your response."
)
user_msg, response_msg = await llm.chat( #D
user_input,
tools=[hailstone_tool], #E
)See the input and tool supplied to chat() in the Hailstone example. section 3.2.2
