ChatRole is a string-backed enumeration that limits a ChatMessage’s role to one of four named values. It identifies whether a message provides context, comes from the user or LLM, or carries a tool result. section 3.1.1 section 3.1
The roles distinguish these message functions:
- SYSTEM sets general context for the LLM or agent. section 3.1.1
- USER marks messages sent by the user. section 3.1.1
- ASSISTANT marks messages from the LLM. section 3.1.1
- TOOL marks messages carrying tool results back to the LLM. section 3.1.1
Tool-call exchange
A user input becomes a USER message; an LLM response becomes an ASSISTANT message. When the response carries a ToolCall, its content is an empty string. A ToolCallResult can be converted into a TOOL message whose content serializes the result. section 3.1.1 section 3.1
Listing 3.2 Implementing ChatMessage and ChatRole
# llm_agents_from_scratch/data_structures/llm.py
from pydantic import BaseModel
from typing_extensions import Self
from llm_agents_from_scratch.data_structures.tool import ToolCall
… #A
class ChatRole(str, Enum):
"""Roles for chat messages."""
USER = "user"
ASSISTANT = "assistant"
SYSTEM = "system"
TOOL = "tool"
class ChatMessage(BaseModel):
"""The chat message data model.
Attributes:
role: The role of the message.
content: The content of the message.
tool_calls: Tool calls associated with the message.
"""
model_config = ConfigDict(arbitrary_types_allowed=True)
role: ChatRole
content: str
tool_calls: list[ToolCall] | None = None #B
@classmethod #C
def from_tool_call_result(
cls,
tool_call_result: ToolCallResult
) -> Self:
"""Create a ChatMessage from a ToolCallResult."""
return cls(
role=ChatRole.TOOL, #D
content=tool_call_result.model_dump_json(indent=4), #E
)Inspect the ChatRole and ChatMessage implementation, including the role values and tool-result conversion method. section 3.1.1
