Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Don't create subtasks when not using tools #1489

Merged
merged 1 commit into from
Dec 30, 2024
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
29 changes: 16 additions & 13 deletions griptape/tasks/prompt_task.py
Original file line number Diff line number Diff line change
Expand Up @@ -203,21 +203,24 @@
self.prompt_driver.tokenizer.stop_sequences.extend([self.response_stop_sequence])

result = self.prompt_driver.run(self.prompt_stack)
subtask = self.add_subtask(ActionsSubtask(result.to_artifact()))

while True:
if subtask.output is None:
if len(self.subtasks) >= self.max_subtasks:
subtask.output = ErrorArtifact(f"Exceeded tool limit of {self.max_subtasks} subtasks per task")
if self.tools:
subtask = self.add_subtask(ActionsSubtask(result.to_artifact()))

while True:
Comment on lines +206 to +209
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

You could avoid some nesting if you flip the condition:

if not self.tools:
    self.output = result.to_artifact()
    return self.output

while True:
    ...

Copy link
Member Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I'd rather keep the conditional positive and solve the ugly nested code separately here.

if subtask.output is None:
if len(self.subtasks) >= self.max_subtasks:
subtask.output = ErrorArtifact(f"Exceeded tool limit of {self.max_subtasks} subtasks per task")
else:
subtask.run()

result = self.prompt_driver.run(self.prompt_stack)
subtask = self.add_subtask(ActionsSubtask(result.to_artifact()))
else:
subtask.run()

result = self.prompt_driver.run(self.prompt_stack)
subtask = self.add_subtask(ActionsSubtask(result.to_artifact()))
else:
break
break

Check warning on line 219 in griptape/tasks/prompt_task.py

View check run for this annotation

Codecov / codecov/patch

griptape/tasks/prompt_task.py#L219

Added line #L219 was not covered by tests
Copy link
Member Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Not introduced by this change


self.output = subtask.output
self.output = subtask.output
else:
self.output = result.to_artifact()

return self.output

Expand Down
15 changes: 15 additions & 0 deletions tests/unit/tasks/test_prompt_task.py
Original file line number Diff line number Diff line change
Expand Up @@ -8,6 +8,7 @@
from griptape.structures import Pipeline
from griptape.tasks import PromptTask
from tests.mocks.mock_prompt_driver import MockPromptDriver
from tests.mocks.mock_tool.tool import MockTool


class TestPromptTask:
Expand Down Expand Up @@ -212,3 +213,17 @@ def test_conversation_memory(self):
task.run()

assert len(conversation_memory.runs) == 2

def test_subtasks(self):
task = PromptTask(
input="foo",
prompt_driver=MockPromptDriver(),
)

task.run()
assert len(task.subtasks) == 0

task = PromptTask(input="foo", prompt_driver=MockPromptDriver(use_native_tools=True), tools=[MockTool()])

task.run()
assert len(task.subtasks) == 2
Loading