A Task is a Pydantic data structure that pairs a required instruction with an identifier. Its generated identifier links it to the TaskResult produced when an LLMAgent executes it. section 4.1 section 4.1.1
Listing 4.1 Implementing Task and TaskResult
# llm_agents_from_scratch/data_structures/agent.py
import uuid
from typing import Literal
from pydantic import BaseModel, Field
class Task(BaseModel):
"""Represents a single task with an instruction.
Attributes:
id_: Identifier for task.
instruction: The instruction for the task.
"""
id_: str = Field(default_factory=lambda: str(uuid.uuid4()))
instruction: str
def __str__(self) -> str: #A
"""String representation of Task."""
return self.instruction
class TaskResult(BaseModel):
"""The result of the task execution.
Attributes:
task_id: The ID `Task` that was executed.
content: The content results of the task execution.
"""
task_id: str #B
content: str
def __str__(self) -> str:
"""String representation of TaskResult."""
return self.contentInspect the Task and TaskResult model definitions, including Task's generated identifier and string representation. section 4.1.1
Submitting a task
For a Hailstone sequence starting at 4, the instruction specifies the rules for generating the sequence. Passing the Task to LLMAgent.run() produces a TaskResult containing `4 -> 2 -> 1`. section 4.3
