Skip to content

Commit

Permalink
v2.5.9
Browse files Browse the repository at this point in the history
  • Loading branch information
ashpreetbedi committed Oct 20, 2024
1 parent ca86f34 commit cef9e2d
Show file tree
Hide file tree
Showing 4 changed files with 58 additions and 13 deletions.
44 changes: 44 additions & 0 deletions cookbook/reasoning/README.md
Original file line number Diff line number Diff line change
@@ -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
```
11 changes: 3 additions & 8 deletions cookbook/reasoning/strawberry.py
Original file line number Diff line number Diff line change
Expand Up @@ -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)
Expand All @@ -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())
14 changes: 10 additions & 4 deletions phi/agent/agent.py
Original file line number Diff line number Diff line change
Expand Up @@ -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"
Expand All @@ -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"
Expand All @@ -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,
Expand Down
2 changes: 1 addition & 1 deletion pyproject.toml
Original file line number Diff line number Diff line change
@@ -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"
Expand Down

0 comments on commit cef9e2d

Please sign in to comment.