Skip to content

Prompt task

PromptTask

Bases: BaseTextInputTask

Source code in griptape/griptape/tasks/prompt_task.py
@define
class PromptTask(BaseTextInputTask):
    prompt_driver: BasePromptDriver | None = field(default=None, kw_only=True)
    generate_system_template: Callable[[PromptTask], str] = field(
        default=Factory(lambda self: self.default_system_template_generator, takes_self=True), kw_only=True
    )

    output: TextArtifact | ErrorArtifact | InfoArtifact | None = field(default=None, init=False)

    @property
    def prompt_stack(self) -> PromptStack:
        stack = PromptStack()
        memory = self.structure.conversation_memory

        stack.add_system_input(self.generate_system_template(self))

        stack.add_user_input(self.input.to_text())

        if self.output:
            stack.add_assistant_input(self.output.to_text())

        if memory:
            # inserting at index 1 to place memory right after system prompt
            stack.add_conversation_memory(memory, 1)

        return stack

    def default_system_template_generator(self, _: PromptTask) -> str:
        return J2("tasks/prompt_task/system.j2").render(
            rulesets=J2("rulesets/rulesets.j2").render(rulesets=self.all_rulesets)
        )

    def run(self) -> TextArtifact | InfoArtifact | ErrorArtifact:
        self.output = self.active_driver().run(self.prompt_stack)

        return self.output

    def active_driver(self) -> BasePromptDriver:
        if self.prompt_driver is None:
            return self.structure.prompt_driver
        else:
            return self.prompt_driver

generate_system_template: Callable[[PromptTask], str] = field(default=Factory(lambda : self.default_system_template_generator, takes_self=True), kw_only=True) class-attribute instance-attribute

output: TextArtifact | ErrorArtifact | InfoArtifact | None = field(default=None, init=False) class-attribute instance-attribute

prompt_driver: BasePromptDriver | None = field(default=None, kw_only=True) class-attribute instance-attribute

prompt_stack: PromptStack property

active_driver()

Source code in griptape/griptape/tasks/prompt_task.py
def active_driver(self) -> BasePromptDriver:
    if self.prompt_driver is None:
        return self.structure.prompt_driver
    else:
        return self.prompt_driver

default_system_template_generator(_)

Source code in griptape/griptape/tasks/prompt_task.py
def default_system_template_generator(self, _: PromptTask) -> str:
    return J2("tasks/prompt_task/system.j2").render(
        rulesets=J2("rulesets/rulesets.j2").render(rulesets=self.all_rulesets)
    )

run()

Source code in griptape/griptape/tasks/prompt_task.py
def run(self) -> TextArtifact | InfoArtifact | ErrorArtifact:
    self.output = self.active_driver().run(self.prompt_stack)

    return self.output