diff --git a/cookbook/reasoning/README.md b/cookbook/reasoning/README.md new file mode 100644 index 000000000..47ed031ce --- /dev/null +++ b/cookbook/reasoning/README.md @@ -0,0 +1,44 @@ +# Agentic Reasoning + +> WARNING: Reasoning is an experimental feature and may not work as expected. + +### Create and activate a virtual environment + +```shell +python3 -m venv ~/.venvs/aienv +source ~/.venvs/aienv/bin/activate +``` + +### Install libraries + +```shell +pip install -U openai phidata +``` + +### Export your `OPENAI_API_KEY` + +```shell +export OPENAI_API_KEY=*** +``` + +### Run a reasoning agent that DOES NOT WORK + +```shell +python cookbook/reasoning/strawberry.py +``` + +### Run other examples of reasoning agents + +```shell +python cookbook/reasoning/logical_puzzle.py +``` + +```shell +python cookbook/reasoning/ethical_dilemma.py +``` + +### Run reasoning agent with tools + +```shell +python cookbook/reasoning/finance_agent.py +``` diff --git a/cookbook/reasoning/strawberry.py b/cookbook/reasoning/strawberry.py index c491a483c..dc6db1acf 100644 --- a/cookbook/reasoning/strawberry.py +++ b/cookbook/reasoning/strawberry.py @@ -4,7 +4,7 @@ from phi.model.openai import OpenAIChat from phi.cli.console import console -task = "How many 'r' are in the word 'strawberry'? Count alphabet by alphabet." +task = "How many 'r' are in the word 'strawberry'?" regular_agent = Agent(model=OpenAIChat(id="gpt-4o"), markdown=True) reasoning_agent = Agent(model=OpenAIChat(id="gpt-4o"), reasoning=True, markdown=True, structured_outputs=True) @@ -13,15 +13,10 @@ async def main(): console.rule("[bold blue]Counting 'r's in 'strawberry'[/bold blue]") - console.rule("[bold green]Regular Agent Stream)[/bold green]") + console.rule("[bold green]Regular Agent[/bold green]") await regular_agent.aprint_response(task, stream=True) - console.rule("[bold yellow]Reasoning Agent Stream)[/bold yellow]") + console.rule("[bold yellow]Reasoning Agent[/bold yellow]") await reasoning_agent.aprint_response(task, stream=True) - console.rule("[bold green]Regular Agent)[/bold green]") - await regular_agent.aprint_response(task) - console.rule("[bold yellow]Reasoning Agent)[/bold yellow]") - await reasoning_agent.aprint_response(task) - asyncio.run(main()) diff --git a/phi/agent/agent.py b/phi/agent/agent.py index fa05bf072..a14789a28 100644 --- a/phi/agent/agent.py +++ b/phi/agent/agent.py @@ -1181,10 +1181,12 @@ def save_run_response_to_file(self, message: Optional[Union[str, List, Dict, Mes def get_reasoning_agent(self, model: Optional[Model] = None) -> Agent: return Agent( model=model, - description="You are a meticulous and thoughtful agent that solves problems by working through them step-by-step.", + description="You are a meticulous and thoughtful assistant that solves a problem by thinking through it step-by-step.", instructions=[ - "First - Analyze the task by carefully examining the request and develop a step-by-step plan to solve it.", - "Then work through the plan step-by-step, executing any tools as needed. For each step, provide:\n" + "First - Carefully analyze the task by spelling it out loud.", + "Then, break down the problem by thinking through it step by step and develop multiple strategies to solve the problem." + "Then, examine the users intent develop a step by step plan to solve the problem.", + "Work through your plan step-by-step, executing any tools as needed. For each step, provide:\n" " 1. Title: A clear, concise title that encapsulates the step's main focus or objective.\n" " 2. Action: Describe the action you will take in the first person (e.g., 'I will...').\n" " 3. Result: Execute the action by running any necessary tools or providing an answer. Summarize the outcome.\n" @@ -1203,6 +1205,10 @@ def get_reasoning_agent(self, model: Optional[Model] = None) -> Agent: " - If next_action is continue, proceed to the next step in your analysis.\n" " - If next_action is validate, validate the result and provide the final answer.\n" " - If next_action is final_answer, stop reasoning.", + "Remember - If next_action is validate, you must validate your result\n" + " - Ensure the answer resolves the original request.\n" + " - Validate your result using any necessary tools or methods.\n" + " - If there is another method to solve the task, use that to validate the result.\n" "Ensure your analysis is:\n" " - Complete: Validate results and run all necessary tools.\n" " - Comprehensive: Consider multiple angles and potential outcomes.\n" @@ -1213,7 +1219,7 @@ def get_reasoning_agent(self, model: Optional[Model] = None) -> Agent: " - Remember to run any tools you need to solve the problem.\n" f" - Take at least {self.reasoning_min_steps} steps to solve the problem.\n" " - If you have all the information you need, provide the final answer.\n" - " - Remember to run any tools you need to run to solve the problem.", + " - IMPORTANT: IF AT ANY TIME THE RESULT IS WRONG, RESET AND START OVER.", ], tools=self.tools, show_tool_calls=False, diff --git a/pyproject.toml b/pyproject.toml index 9aa8e916b..7d16fd087 100644 --- a/pyproject.toml +++ b/pyproject.toml @@ -1,6 +1,6 @@ [project] name = "phidata" -version = "2.5.8" +version = "2.5.9" description = "Build AI Agents with memory, knowledge and tools." requires-python = ">=3.7" readme = "README.md"