Skip to content

Commit

Permalink
update
Browse files Browse the repository at this point in the history
  • Loading branch information
ysolanky committed Sep 19, 2024
1 parent 2f4e25c commit b8d47fd
Show file tree
Hide file tree
Showing 11 changed files with 47 additions and 76 deletions.
18 changes: 9 additions & 9 deletions cookbook/providers/claude/README.md
Original file line number Diff line number Diff line change
Expand Up @@ -23,42 +23,42 @@ export ANTHROPIC_API_KEY=xxx
pip install -U anthropic duckduckgo-search duckdb yfinance exa_py phidata
```

### 4. Run Assistant
### 4. Run Agent

- stream on

```shell
python cookbook/llms/claude/assistant.py
python cookbook/providers/claude/assistant.py
```

- stream off

```shell
python cookbook/llms/claude/assistant_stream_off.py
python cookbook/providers/claude/assistant_stream_off.py
```

### 5. Run Assistant with Tools
### 5. Run Agent with Tools

- YFinance

```shell
python cookbook/llms/claude/finance.py
python cookbook/providers/claude/finance.py
```

- Data Analyst

```shell
python cookbook/llms/claude/data_analyst.py
python cookbook/providers/claude/data_analyst.py
```

- Exa Search

```shell
python cookbook/llms/claude/exa_search.py
python cookbook/providers/claude/exa_search.py
```

### 6. Run Assistant with Structured output
### 6. Run Agent with Structured output

```shell
python cookbook/llms/claude/structured_output.py
python cookbook/providers/claude/structured_output.py
```
5 changes: 3 additions & 2 deletions cookbook/providers/claude/agent.py
Original file line number Diff line number Diff line change
Expand Up @@ -2,9 +2,10 @@
from phi.tools.duckduckgo import DuckDuckGo
from phi.model.anthropic import Claude

assistant = Agent(
agent = Agent(
model=Claude(model="claude-3-5-sonnet-20240620"),
tools=[DuckDuckGo()],
show_tool_calls=True,
# debug_mode=True,
)
assistant.print_response("Whats happening in France", markdown=True)
agent.print_response("Whats happening in France", markdown=True, stream=True)
11 changes: 2 additions & 9 deletions cookbook/providers/claude/agent_stream_off.py
Original file line number Diff line number Diff line change
@@ -1,4 +1,3 @@
from rich.pretty import pprint
from phi.agent import Agent
from phi.tools.duckduckgo import DuckDuckGo
from phi.model.anthropic import Claude
Expand All @@ -7,12 +6,6 @@
model=Claude(model="claude-3-5-sonnet-20240620"),
tools=[DuckDuckGo()],
show_tool_calls=True,
debug_mode=True,
# debug_mode=True,
)
# agent.print_response("Whats happening in France?", markdown=True, stream=False)

response = agent.run("Whats happening in France?", markdown=True, stream=False)

pprint(response)


agent.print_response("Whats happening in France?", markdown=True, stream=False)
9 changes: 0 additions & 9 deletions cookbook/providers/claude/basic.py
Original file line number Diff line number Diff line change
@@ -1,17 +1,8 @@
from phi.agent import Agent
from phi.model.anthropic import Claude

# agent = Agent(
# llm=Claude(model="claude-3-haiku-20240307"),
# description="You help people with their health and fitness goals.",
# debug_mode=True,
# )
# agent.print_response("Share a quick healthy breakfast recipe.", markdown=True)


agent = Agent(
model=Claude(model="claude-3-5-sonnet-20240620"),
description="You help people with their health and fitness goals.",
debug_mode=True,
)
agent.print_response("Share a quick healthy breakfast recipe.", markdown=True)
9 changes: 1 addition & 8 deletions cookbook/providers/claude/basic_stream_off.py
Original file line number Diff line number Diff line change
@@ -1,15 +1,8 @@
from rich.pretty import pprint
from phi.agent import Agent
from phi.model.anthropic import Claude

agent = Agent(
model=Claude(model="claude-3-5-sonnet-20240620"),
description="You help people with their health and fitness goals.",
debug_mode=True,
)
# agent.print_response("Share a quick healthy breakfast recipe.", markdown=True, stream=False)


response = agent.run("Share a quick healthy breakfast recipe.", markdown=True, stream=False)

pprint(response)
agent.print_response("Share a quick healthy breakfast recipe.", markdown=True, stream=False)
10 changes: 5 additions & 5 deletions cookbook/providers/claude/data_analyst.py
Original file line number Diff line number Diff line change
@@ -1,14 +1,14 @@
from phi.assistant import Assistant
from phi.llm.anthropic import Claude
from phi.agent import Agent
from phi.model.anthropic import Claude
from phi.tools.duckdb import DuckDbTools

duckdb_tools = DuckDbTools(create_tables=False, export_tables=False, summarize_tables=False)
duckdb_tools.create_table_from_path(
path="https://phidata-public.s3.amazonaws.com/demo_data/IMDB-Movie-Data.csv", table="movies"
)

assistant = Assistant(
llm=Claude(model="claude-3-opus-20240229"),
agent = Agent(
model=Claude(model="claude-3-5-sonnet-20240620"),
tools=[duckdb_tools],
show_tool_calls=True,
add_to_system_prompt="""
Expand All @@ -17,4 +17,4 @@
""",
# debug_mode=True,
)
assistant.print_response("What is the average rating of movies?", markdown=True, stream=False)
agent.print_response("What is the average rating of movies?", markdown=True, stream=False)
10 changes: 6 additions & 4 deletions cookbook/providers/claude/exa_search.py
Original file line number Diff line number Diff line change
@@ -1,10 +1,12 @@
from phi.assistant import Assistant
from phi.agent import Agent
from phi.tools.exa import ExaTools
from phi.tools.website import WebsiteTools
from phi.llm.anthropic import Claude
from phi.model.anthropic import Claude

assistant = Assistant(llm=Claude(), tools=[ExaTools(), WebsiteTools()], show_tool_calls=True)
assistant.print_response(
agent = Agent(
model=Claude(model="claude-3-5-sonnet-20240620"), tools=[ExaTools(), WebsiteTools()], show_tool_calls=True
)
agent.print_response(
"Produce this table: research chromatic homotopy theory."
"Access each link in the result outputting the summary for that article, its link, and keywords; "
"After the table output make conceptual ascii art of the overarching themes and constructions",
Expand Down
12 changes: 6 additions & 6 deletions cookbook/providers/claude/finance.py
Original file line number Diff line number Diff line change
@@ -1,15 +1,15 @@
from phi.assistant import Assistant
from phi.agent import Agent
from phi.tools.yfinance import YFinanceTools
from phi.llm.anthropic import Claude
from phi.model.anthropic import Claude

assistant = Assistant(
llm=Claude(model="claude-3-5-sonnet-20240620"),
agent = Agent(
model=Claude(model="claude-3-5-sonnet-20240620"),
tools=[YFinanceTools(stock_price=True, analyst_recommendations=True, stock_fundamentals=True)],
show_tool_calls=True,
description="You are an investment analyst that researches stock prices, analyst recommendations, and stock fundamentals.",
instructions=["Use tables to display data where possible."],
markdown=True,
# debug_mode=True,
)
# assistant.print_response("Share the NVDA stock price and analyst recommendations")
assistant.print_response("Summarize fundamentals for TSLA")
agent.print_response("Summarize fundamentals for TSLA")
# agent.print_response("Share the NVDA stock price and analyst recommendations")
12 changes: 6 additions & 6 deletions cookbook/providers/claude/structured_output.py
Original file line number Diff line number Diff line change
@@ -1,8 +1,8 @@
from typing import List
from pydantic import BaseModel, Field
from rich.pretty import pprint
from phi.assistant import Assistant
from phi.llm.anthropic import Claude
from phi.agent import Agent
from phi.model.anthropic import Claude


class MovieScript(BaseModel):
Expand All @@ -16,11 +16,11 @@ class MovieScript(BaseModel):
storyline: str = Field(..., description="3 sentence storyline for the movie. Make it exciting!")


movie_assistant = Assistant(
llm=Claude(model="claude-3-opus-20240229"),
movie_agent = Agent(
model=Claude(model="claude-3-5-sonnet-20240620"),
description="You help people write movie scripts.",
output_model=MovieScript,
# debug_mode=True,
debug_mode=True,
)

pprint(movie_assistant.run("New York"))
pprint(movie_agent.run("New York", stream=True))
5 changes: 0 additions & 5 deletions cookbook/providers/openai/agent_stream_off.py
Original file line number Diff line number Diff line change
@@ -1,4 +1,3 @@
from rich.pretty import pprint
from phi.agent import Agent
from phi.model.openai import OpenAIChat
from phi.tools.duckduckgo import DuckDuckGo
Expand All @@ -10,7 +9,3 @@
debug_mode=True,
)
agent.print_response("Whats happening in France?", markdown=True, stream=False)

# response = agent.run("Whats happening in France?", markdown=True, stream=False)
#
# pprint(response)
22 changes: 9 additions & 13 deletions phi/model/anthropic/claude.py
Original file line number Diff line number Diff line change
Expand Up @@ -2,7 +2,7 @@
from typing import Optional, List, Iterator, Dict, Any, Union

from phi.model.base import Model
from phi.llm.message import Message
from phi.model.message import Message
from phi.model.response import ModelResponse
from phi.tools.function import FunctionCall
from phi.utils.log import logger
Expand Down Expand Up @@ -114,16 +114,12 @@ def invoke(self, messages: List[Message]) -> AnthropicMessage:
api_messages: List[dict] = []
system_messages: List[str] = []

logger.debug(f"Messages: {messages}")

for idx, message in enumerate(messages):
if message.role == "system" or (message.role != "user" and idx in [0, 1]):
system_messages.append(message.content) # type: ignore
else:
api_messages.append({"role": message.role, "content": message.content or ""})

logger.debug(f"System Messages: {system_messages}")

api_kwargs["system"] = " ".join(system_messages)

if self.tools:
Expand Down Expand Up @@ -287,7 +283,7 @@ def response(self, messages: List[Message]) -> ModelResponse:
logger.debug("---------- Claude Response End ----------")
return model_response

def response_stream(self, messages: List[Message]) -> Iterator[str]:
def response_stream(self, messages: List[Message]) -> Iterator[ModelResponse]:
logger.debug("---------- Claude Response Start ----------")
# -*- Log messages for debugging
for m in messages:
Expand All @@ -305,7 +301,7 @@ def response_stream(self, messages: List[Message]) -> Iterator[str]:
for delta in stream:
if isinstance(delta, RawContentBlockDeltaEvent):
if isinstance(delta.delta, TextDelta):
yield delta.delta.text
yield ModelResponse(content=delta.delta.text)
response_content_text += delta.delta.text

if isinstance(delta, ContentBlockStopEvent):
Expand All @@ -329,7 +325,7 @@ def response_stream(self, messages: List[Message]) -> Iterator[str]:
if isinstance(delta, MessageStopEvent):
response_usage = delta.message.usage

yield "\n\n"
yield ModelResponse(content="\n\n")

response_timer.stop()
logger.debug(f"Time to generate response: {response_timer.elapsed:.4f}s")
Expand Down Expand Up @@ -388,12 +384,12 @@ def response_stream(self, messages: List[Message]) -> Iterator[str]:

if self.show_tool_calls:
if len(function_calls_to_run) == 1:
yield f" - Running: {function_calls_to_run[0].get_call_str()}\n\n"
yield ModelResponse(content=f" - Running: {function_calls_to_run[0].get_call_str()}\n\n")
elif len(function_calls_to_run) > 1:
yield "Running:"
yield ModelResponse(content="Running:")
for _f in function_calls_to_run:
yield f"\n - {_f.get_call_str()}"
yield "\n\n"
yield ModelResponse(content="\n - {_f.get_call_str()}")
yield ModelResponse(content="\n\n")

function_call_results = self.run_function_calls(function_calls_to_run)
if len(function_call_results) > 0:
Expand All @@ -411,7 +407,7 @@ def response_stream(self, messages: List[Message]) -> Iterator[str]:
messages.append(Message(role="user", content=fc_responses))

# -*- Yield new response using results of tool calls
yield from self.response(messages=messages)
yield from self.response_stream(messages=messages)
logger.debug("---------- Claude Response End ----------")

def get_tool_call_prompt(self) -> Optional[str]:
Expand Down

0 comments on commit b8d47fd

Please sign in to comment.