Skip to content

Agent

Agent

Bases: Structure

Source code in griptape/griptape/structures/agent.py
@define
class Agent(Structure):
    input_template: str = field(default=PromptTask.DEFAULT_INPUT_TEMPLATE)
    tools: list[BaseTool] = field(factory=list, kw_only=True)
    max_meta_memory_entries: int | None = field(default=20, kw_only=True)

    def __attrs_post_init__(self) -> None:
        if len(self.tasks) == 0:
            if self.tools:
                task = ToolkitTask(
                    self.input_template, tools=self.tools, max_meta_memory_entries=self.max_meta_memory_entries
                )
            else:
                task = PromptTask(self.input_template, max_meta_memory_entries=self.max_meta_memory_entries)

            self.add_task(task)

        super().__attrs_post_init__()

    @property
    def task(self) -> BaseTask:
        return self.tasks[0]

    def add_task(self, task: BaseTask) -> BaseTask:
        self.tasks.clear()

        task.preprocess(self)

        self.tasks.append(task)

        return task

    def add_tasks(self, *tasks: BaseTask) -> list[BaseTask]:
        if len(tasks) > 1:
            raise ValueError("Agents can only have one task.")
        return super().add_tasks(*tasks)

    def try_run(self, *args) -> Agent:
        self._execution_args = args

        self.task.reset()

        self.task.execute()

        if self.conversation_memory:
            if isinstance(self.task.input, tuple):
                input_text = self.task.input[0].to_text()
            else:
                input_text = self.task.input.to_text()

            run = Run(input=input_text, output=self.task.output.to_text())

            self.conversation_memory.add_run(run)

        return self

input_template: str = field(default=PromptTask.DEFAULT_INPUT_TEMPLATE) class-attribute instance-attribute

max_meta_memory_entries: int | None = field(default=20, kw_only=True) class-attribute instance-attribute

task: BaseTask property

tools: list[BaseTool] = field(factory=list, kw_only=True) class-attribute instance-attribute

__attrs_post_init__()

Source code in griptape/griptape/structures/agent.py
def __attrs_post_init__(self) -> None:
    if len(self.tasks) == 0:
        if self.tools:
            task = ToolkitTask(
                self.input_template, tools=self.tools, max_meta_memory_entries=self.max_meta_memory_entries
            )
        else:
            task = PromptTask(self.input_template, max_meta_memory_entries=self.max_meta_memory_entries)

        self.add_task(task)

    super().__attrs_post_init__()

add_task(task)

Source code in griptape/griptape/structures/agent.py
def add_task(self, task: BaseTask) -> BaseTask:
    self.tasks.clear()

    task.preprocess(self)

    self.tasks.append(task)

    return task

add_tasks(*tasks)

Source code in griptape/griptape/structures/agent.py
def add_tasks(self, *tasks: BaseTask) -> list[BaseTask]:
    if len(tasks) > 1:
        raise ValueError("Agents can only have one task.")
    return super().add_tasks(*tasks)

try_run(*args)

Source code in griptape/griptape/structures/agent.py
def try_run(self, *args) -> Agent:
    self._execution_args = args

    self.task.reset()

    self.task.execute()

    if self.conversation_memory:
        if isinstance(self.task.input, tuple):
            input_text = self.task.input[0].to_text()
        else:
            input_text = self.task.input.to_text()

        run = Run(input=input_text, output=self.task.output.to_text())

        self.conversation_memory.add_run(run)

    return self