Skip to content

Commit

Permalink
anthropic-models-phi-1205
Browse files Browse the repository at this point in the history
  • Loading branch information
ysolanky committed Sep 19, 2024
1 parent b41e00f commit 2f4e25c
Show file tree
Hide file tree
Showing 15 changed files with 643 additions and 1 deletion.
1 change: 1 addition & 0 deletions cookbook/llms/claude/assistant_stream_off.py
Original file line number Diff line number Diff line change
Expand Up @@ -6,5 +6,6 @@
llm=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, stream=False)
64 changes: 64 additions & 0 deletions cookbook/providers/claude/README.md
Original file line number Diff line number Diff line change
@@ -0,0 +1,64 @@
# Anthropic Claude

[Models overview](https://docs.anthropic.com/claude/docs/models-overview)

> Note: Fork and clone this repository if needed
### 1. Create and activate a virtual environment

```shell
python3 -m venv ~/.venvs/aienv
source ~/.venvs/aienv/bin/activate
```

### 2. Set your `ANTHROPIC_API_KEY`

```shell
export ANTHROPIC_API_KEY=xxx
```

### 3. Install libraries

```shell
pip install -U anthropic duckduckgo-search duckdb yfinance exa_py phidata
```

### 4. Run Assistant

- stream on

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

- stream off

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

### 5. Run Assistant with Tools

- YFinance

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

- Data Analyst

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

- Exa Search

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

### 6. Run Assistant with Structured output

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

assistant = Agent(
model=Claude(model="claude-3-5-sonnet-20240620"),
tools=[DuckDuckGo()],
show_tool_calls=True,
)
assistant.print_response("Whats happening in France", markdown=True)
18 changes: 18 additions & 0 deletions cookbook/providers/claude/agent_stream_off.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,18 @@
from rich.pretty import pprint
from phi.agent import Agent
from phi.tools.duckduckgo import DuckDuckGo
from phi.model.anthropic import Claude

agent = Agent(
model=Claude(model="claude-3-5-sonnet-20240620"),
tools=[DuckDuckGo()],
show_tool_calls=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)


17 changes: 17 additions & 0 deletions cookbook/providers/claude/basic.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,17 @@
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)
15 changes: 15 additions & 0 deletions cookbook/providers/claude/basic_stream_off.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,15 @@
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)
20 changes: 20 additions & 0 deletions cookbook/providers/claude/data_analyst.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,20 @@
from phi.assistant import Assistant
from phi.llm.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"),
tools=[duckdb_tools],
show_tool_calls=True,
add_to_system_prompt="""
Here are the tables you have access to:
- movies: Contains information about movies from IMDB.
""",
# debug_mode=True,
)
assistant.print_response("What is the average rating of movies?", markdown=True, stream=False)
12 changes: 12 additions & 0 deletions cookbook/providers/claude/exa_search.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,12 @@
from phi.assistant import Assistant
from phi.tools.exa import ExaTools
from phi.tools.website import WebsiteTools
from phi.llm.anthropic import Claude

assistant = Assistant(llm=Claude(), tools=[ExaTools(), WebsiteTools()], show_tool_calls=True)
assistant.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",
markdown=True,
)
15 changes: 15 additions & 0 deletions cookbook/providers/claude/finance.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,15 @@
from phi.assistant import Assistant
from phi.tools.yfinance import YFinanceTools
from phi.llm.anthropic import Claude

assistant = Assistant(
llm=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")
26 changes: 26 additions & 0 deletions cookbook/providers/claude/structured_output.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,26 @@
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


class MovieScript(BaseModel):
setting: str = Field(..., description="Provide a nice setting for a blockbuster movie.")
ending: str = Field(..., description="Ending of the movie. If not available, provide a happy ending.")
genre: str = Field(
..., description="Genre of the movie. If not available, select action, thriller or romantic comedy."
)
name: str = Field(..., description="Give a name to this movie")
characters: List[str] = Field(..., description="Name of characters for this movie.")
storyline: str = Field(..., description="3 sentence storyline for the movie. Make it exciting!")


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

pprint(movie_assistant.run("New York"))
16 changes: 16 additions & 0 deletions cookbook/providers/openai/agent_stream_off.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,16 @@
from rich.pretty import pprint
from phi.agent import Agent
from phi.model.openai import OpenAIChat
from phi.tools.duckduckgo import DuckDuckGo

agent = Agent(
model=OpenAIChat(model="gpt-4o"),
tools=[DuckDuckGo()],
show_tool_calls=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)
3 changes: 3 additions & 0 deletions phi/llm/anthropic/claude.py
Original file line number Diff line number Diff line change
Expand Up @@ -119,6 +119,9 @@ def invoke(self, messages: List[Message]) -> AnthropicMessage:
else:
api_messages.append({"role": message.role, "content": message.content or ""})

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

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

if self.tools:
Expand Down
1 change: 1 addition & 0 deletions phi/model/anthropic/__init__.py
Original file line number Diff line number Diff line change
@@ -0,0 +1 @@
from phi.model.anthropic.claude import Claude
Loading

0 comments on commit 2f4e25c

Please sign in to comment.