After an LLM requests a tool call and the tool executes, continue_chat_with_tool_results() sends the results back to the ongoing chat for synthesis. It extends chat() by accepting tool results instead of a new input string. section 3.1.2
The asynchronous BaseLLM method accepts:
- tool_call_results: a sequence of ToolCallResult objects produced by executing requested tools. section 3.1.2
- chat_history: the existing sequence of ChatMessage objects, including the assistant message that requested the tool calls. section 3.1.2 section 3.2.2
- tools: an optional sequence of tools, plus additional keyword arguments. section 3.1.2
The method returns a tuple: a list of TOOL-role ChatMessage objects derived from the results, followed by the LLM’s response as a ChatMessage. BaseLLM declares the method abstract, so subclasses provide the implementation. section 3.1.2
Listing 3.4 Implementing BaseLLM: continue_chat_with_tool_results()
# llm_agents_from_scratch/base/llm.py
from abc import ABC, abstractmethod
from llm_agents_from_scratch.base.tool import AsyncBaseTool, BaseTool
from llm_agents_from_scratch.data_structures import (
ChatMessage,
CompleteResult,
ToolCallResult,
)
from typing import Any
class BaseLLM(ABC):
"""Base LLM Class."""
… #A
@abstractmethod
async def continue_chat_with_tool_results(
self,
tool_call_results: Sequence[ToolCallResult], #B
chat_history: Sequence[ChatMessage], #C
tools: Sequence[Tool] | None = None,
**kwargs: Any,
) -> tuple[list[ChatMessage], ChatMessage]: #D
"""Continue a chat submitting tool call results."""The declaration specifies the asynchronous interface and its input and return types. section 3.1.2
In OllamaLLM, each result becomes a ChatMessage through ChatMessage.from_tool_call_result(). The implementation converts the history and result messages to Ollama messages, converts supplied tools to Ollama tools, and invokes ollama.chat(). It converts the response back to a ChatMessage and returns it alongside the result messages. section 3.2.1
Hailstone tool call
Ask chat() for the next Hailstone step from 3, then execute the requested tool call. Pass the resulting ToolCallResult and the user and assistant messages to continue_chat_with_tool_results(). In the demonstrated run, the tool result and final assistant response both contain 10. section 3.2.2
# Included in chapter-notebooks/ch03/ch03.ipynb #A
tools_msg, final_response = await llm.continue_chat_with_tool_results(
tool_call_results=[tool_call_result],
chat_history=[user_msg, response_msg],
)
print(final_response)The continuation call supplies the tool result and prior messages and receives both result messages and the final response. section 3.2.2
