From 355203c8fd26b47e3839f037f465feea2b41b6f9 Mon Sep 17 00:00:00 2001 From: Steve Androulakis Date: Sun, 16 Feb 2025 07:14:49 -0800 Subject: [PATCH] prompt strings out of workflow file --- prompts/agent_prompt_generators.py | 40 ++++++++++++++++++++++++++++++ workflows/agent_goal_workflow.py | 21 ++++++---------- 2 files changed, 47 insertions(+), 14 deletions(-) diff --git a/prompts/agent_prompt_generators.py b/prompts/agent_prompt_generators.py index 4f0e390..205205a 100644 --- a/prompts/agent_prompt_generators.py +++ b/prompts/agent_prompt_generators.py @@ -108,3 +108,43 @@ def generate_genai_prompt( ) return "\n".join(prompt_lines) + +def generate_tool_completion_prompt(current_tool: str, dynamic_result: dict) -> str: + """ + Generates a prompt for handling tool completion and determining next steps. + + Args: + current_tool: The name of the tool that just completed + dynamic_result: The result data from the tool execution + + Returns: + str: A formatted prompt string for the agent to process the tool completion + """ + return ( + f"### The '{current_tool}' tool completed successfully with {dynamic_result}. " + "INSTRUCTIONS: Parse this tool result as plain text, and use the system prompt containing the list of tools in sequence and the conversation history (and previous tool_results) to figure out next steps, if any. " + "You will need to use the tool_results to auto-fill arguments for subsequent tools and also to figure out if all tools have been run." + '{"next": "", "tool": "", "args": {"": "", "": "}, "response": ""}' + "ONLY return those json keys (next, tool, args, response), nothing else." + 'Next should only be "done" if all tools have been run (use the system prompt to figure that out).' + 'Next should be "question" if the tool is not the last one in the sequence.' + 'Next should NOT be "confirm" at this point.' + ) + +def generate_missing_args_prompt(current_tool: str, tool_data: dict, missing_args: list[str]) -> str: + """ + Generates a prompt for handling missing arguments for a tool. + + Args: + current_tool: The name of the tool that needs arguments + tool_data: The current tool data containing the response + missing_args: List of argument names that are missing + + Returns: + str: A formatted prompt string for requesting missing arguments + """ + return ( + f"### INSTRUCTIONS set next='question', combine this response response='{tool_data.get('response')}' " + f"and following missing arguments for tool {current_tool}: {missing_args}. " + "Only provide a valid JSON response without any comments or metadata." + ) diff --git a/workflows/agent_goal_workflow.py b/workflows/agent_goal_workflow.py index 0eec89c..c4d940d 100644 --- a/workflows/agent_goal_workflow.py +++ b/workflows/agent_goal_workflow.py @@ -10,7 +10,11 @@ from models.data_types import ConversationHistory, NextStep, ValidationInput with workflow.unsafe.imports_passed_through(): from activities.tool_activities import ToolActivities - from prompts.agent_prompt_generators import generate_genai_prompt + from prompts.agent_prompt_generators import ( + generate_genai_prompt, + generate_tool_completion_prompt, + generate_missing_args_prompt, + ) from models.data_types import ( CombinedInput, AgentGoalWorkflowParams, @@ -78,16 +82,7 @@ class AgentGoalWorkflow: self.add_message("tool_result", dynamic_result) - self.prompt_queue.append( - f"### The '{current_tool}' tool completed successfully with {dynamic_result}. " - "INSTRUCTIONS: Parse this tool result as plain text, and use the system prompt containing the list of tools in sequence and the conversation history (and previous tool_results) to figure out next steps, if any. " - "You will need to use the tool_results to auto-fill arguments for subsequent tools and also to figure out if all tools have been run." - '{"next": "", "tool": "", "args": {"": "", "": "}, "response": ""}' - "ONLY return those json keys (next, tool, args, response), nothing else." - 'Next should only be "done" if all tools have been run (use the system prompt to figure that out).' - 'Next should be "question" if the tool is not the last one in the sequence.' - 'Next should NOT be "confirm" at this point.' - ) + self.prompt_queue.append(generate_tool_completion_prompt(current_tool, dynamic_result)) async def _handle_missing_args( self, current_tool: str, args: Dict[str, Any], tool_data: ToolData @@ -97,9 +92,7 @@ class AgentGoalWorkflow: if missing_args: self.prompt_queue.append( - f"### INSTRUCTIONS set next='question', combine this response response='{tool_data.get('response')}' " - f"and following missing arguments for tool {current_tool}: {missing_args}. " - "Only provide a valid JSON response without any comments or metadata." + generate_missing_args_prompt(current_tool, tool_data, missing_args) ) workflow.logger.info( f"Missing arguments for tool: {current_tool}: {' '.join(missing_args)}"