Skip to content

LLM

Data Structures for LLMs.

ChatRole

Bases: str, Enum

Roles for chat messages.

Source code in src/llm_agents_from_scratch/data_structures/llm.py
class ChatRole(str, Enum):
    """Roles for chat messages."""

    USER = "user"
    ASSISTANT = "assistant"
    SYSTEM = "system"
    TOOL = "tool"

ChatMessage

Bases: BaseModel

The chat message data model.

Attributes:

Name Type Description
role ChatRole

The role of the message.

content str

The content of the message.

tool_calls list[ToolCall] | None

Tool calls associated with the message.

Source code in src/llm_agents_from_scratch/data_structures/llm.py
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

    @classmethod
    def from_tool_call_result(cls, tool_call_result: ToolCallResult) -> Self:
        """Create a ChatMessage from a ToolCallResult."""
        return cls(
            role=ChatRole.TOOL,
            content=tool_call_result.model_dump_json(indent=4),
        )

from_tool_call_result classmethod

from_tool_call_result(tool_call_result)

Create a ChatMessage from a ToolCallResult.

Source code in src/llm_agents_from_scratch/data_structures/llm.py
@classmethod
def from_tool_call_result(cls, tool_call_result: ToolCallResult) -> Self:
    """Create a ChatMessage from a ToolCallResult."""
    return cls(
        role=ChatRole.TOOL,
        content=tool_call_result.model_dump_json(indent=4),
    )

CompleteResult

Bases: BaseModel

The llm completion result data model.

Attributes:

Name Type Description
response str

The completion response provided by the LLM.

full_response str

Input prompt and completion text.

Source code in src/llm_agents_from_scratch/data_structures/llm.py
class CompleteResult(BaseModel):
    """The llm completion result data model.

    Attributes:
        response: The completion response provided by the LLM.
        full_response: Input prompt and completion text.
    """

    response: str
    prompt: str