Skip to content

Commit

Permalink
update-claude-phi-1742
Browse files Browse the repository at this point in the history
  • Loading branch information
ysolanky committed Oct 23, 2024
1 parent a478a33 commit 10cb0d9
Show file tree
Hide file tree
Showing 16 changed files with 129 additions and 20 deletions.
4 changes: 2 additions & 2 deletions cookbook/knowledge/custom_references.py
Original file line number Diff line number Diff line change
Expand Up @@ -11,13 +11,13 @@
from phi.agent import Agent
from phi.document import Document
from phi.knowledge.pdf import PDFUrlKnowledgeBase
from phi.vectordb.pgvector import PgVector2
from phi.vectordb.pgvector import PgVector

db_url = "postgresql+psycopg://ai:ai@localhost:5532/ai"

knowledge_base = PDFUrlKnowledgeBase(
urls=["https://phi-public.s3.amazonaws.com/recipes/ThaiRecipes.pdf"],
vector_db=PgVector2(collection="recipes", db_url=db_url),
vector_db=PgVector(table_name="recipes", db_url=db_url),
)
# Comment out after first run
# knowledge_base.load(recreate=False)
Expand Down
6 changes: 3 additions & 3 deletions cookbook/knowledge/json_kb.py
Original file line number Diff line number Diff line change
Expand Up @@ -2,15 +2,15 @@

from phi.agent import Agent
from phi.knowledge.json import JSONKnowledgeBase
from phi.vectordb.pgvector import PgVector2
from phi.vectordb.pgvector import PgVector

db_url = "postgresql+psycopg://ai:ai@localhost:5532/ai"

# Initialize the JSONKnowledgeBase
knowledge_base = JSONKnowledgeBase(
path=Path("data/docs"), # Table name: ai.json_documents
vector_db=PgVector2(
collection="json_documents",
vector_db=PgVector(
table_name="json_documents",
db_url=db_url,
),
num_documents=5, # Number of documents to return on search
Expand Down
6 changes: 3 additions & 3 deletions cookbook/knowledge/pdf.py
Original file line number Diff line number Diff line change
@@ -1,14 +1,14 @@
from phi.agent import Agent
from phi.knowledge.pdf import PDFKnowledgeBase, PDFReader
from phi.vectordb.pgvector import PgVector2
from phi.vectordb.pgvector import PgVector

db_url = "postgresql+psycopg://ai:ai@localhost:5532/ai"

# Create a knowledge base with the PDFs from the data/pdfs directory
knowledge_base = PDFKnowledgeBase(
path="data/pdfs",
vector_db=PgVector2(
collection="pdf_documents",
vector_db=PgVector(
table_name="pdf_documents",
# Can inspect database via psql e.g. "psql -h localhost -p 5432 -U ai -d ai"
db_url=db_url,
),
Expand Down
4 changes: 2 additions & 2 deletions cookbook/knowledge/pdf_url.py
Original file line number Diff line number Diff line change
@@ -1,12 +1,12 @@
from phi.agent import Agent
from phi.knowledge.pdf import PDFUrlKnowledgeBase
from phi.vectordb.pgvector import PgVector2
from phi.vectordb.pgvector import PgVector

db_url = "postgresql+psycopg://ai:ai@localhost:5532/ai"

knowledge_base = PDFUrlKnowledgeBase(
urls=["https://phi-public.s3.amazonaws.com/recipes/ThaiRecipes.pdf"],
vector_db=PgVector2(collection="recipes", db_url=db_url),
vector_db=PgVector(table_name="recipes", db_url=db_url),
)
knowledge_base.load(recreate=False) # Comment out after first run

Expand Down
6 changes: 3 additions & 3 deletions cookbook/knowledge/text.py
Original file line number Diff line number Diff line change
Expand Up @@ -2,16 +2,16 @@

from phi.agent import Agent
from phi.knowledge.text import TextKnowledgeBase
from phi.vectordb.pgvector import PgVector2
from phi.vectordb.pgvector import PgVector

db_url = "postgresql+psycopg://ai:ai@localhost:5532/ai"


# Initialize the TextKnowledgeBase
knowledge_base = TextKnowledgeBase(
path=Path("data/docs"), # Table name: ai.text_documents
vector_db=PgVector2(
collection="text_documents",
vector_db=PgVector(
table_name="text_documents",
db_url=db_url,
),
num_documents=5, # Number of documents to return on search
Expand Down
18 changes: 18 additions & 0 deletions cookbook/providers/claude/README.md
Original file line number Diff line number Diff line change
Expand Up @@ -74,3 +74,21 @@ python cookbook/providers/claude/finance.py
```shell
python cookbook/providers/claude/structured_output.py
```

### 7. Run Agent that uses memory

```shell
python cookbook/providers/claude/memory.py
```

### 8. Run Agent that uses storage

```shell
python cookbook/providers/claude/storage.py
```

### 9. Run Agent that uses knowledge

```shell
python cookbook/providers/claude/knowledge.py
```
2 changes: 1 addition & 1 deletion cookbook/providers/claude/agent.py
Original file line number Diff line number Diff line change
Expand Up @@ -5,7 +5,7 @@
from phi.tools.yfinance import YFinanceTools

agent = Agent(
model=Claude(id="claude-3-5-sonnet-20240620"),
model=Claude(id="claude-3-5-sonnet-20241022"),
tools=[YFinanceTools(stock_price=True)],
show_tool_calls=True,
markdown=True,
Expand Down
2 changes: 1 addition & 1 deletion cookbook/providers/claude/agent_stream.py
Original file line number Diff line number Diff line change
Expand Up @@ -6,7 +6,7 @@
from phi.tools.yfinance import YFinanceTools

agent = Agent(
model=Claude(id="claude-3-5-sonnet-20240620"),
model=Claude(id="claude-3-5-sonnet-20241022"),
tools=[YFinanceTools(stock_price=True)],
show_tool_calls=True,
markdown=True,
Expand Down
2 changes: 1 addition & 1 deletion cookbook/providers/claude/basic.py
Original file line number Diff line number Diff line change
@@ -1,7 +1,7 @@
from phi.agent import Agent, RunResponse # noqa
from phi.model.anthropic import Claude

agent = Agent(model=Claude(id="claude-3-5-sonnet-20240620"), markdown=True)
agent = Agent(model=Claude(id="claude-3-5-sonnet-20241022"), markdown=True)

# Get the response in a variable
# run: RunResponse = agent.run("Share a 2 sentence horror story")
Expand Down
2 changes: 1 addition & 1 deletion cookbook/providers/claude/basic_stream.py
Original file line number Diff line number Diff line change
Expand Up @@ -2,7 +2,7 @@
from phi.agent import Agent, RunResponse # noqa
from phi.model.anthropic import Claude

agent = Agent(model=Claude(id="claude-3-5-sonnet-20240620"), markdown=True)
agent = Agent(model=Claude(id="claude-3-5-sonnet-20241022"), markdown=True)

# Get the response in a variable
# run_response: Iterator[RunResponse] = agent.run("Share a 2 sentence horror story", stream=True)
Expand Down
2 changes: 1 addition & 1 deletion cookbook/providers/claude/data_analyst.py
Original file line number Diff line number Diff line change
Expand Up @@ -11,7 +11,7 @@
)

agent = Agent(
model=Claude(id="claude-3-5-sonnet-20240620"),
model=Claude(id="claude-3-5-sonnet-20241022"),
tools=[duckdb_tools],
markdown=True,
show_tool_calls=True,
Expand Down
2 changes: 1 addition & 1 deletion cookbook/providers/claude/finance_agent.py
Original file line number Diff line number Diff line change
Expand Up @@ -5,7 +5,7 @@
from phi.tools.yfinance import YFinanceTools

agent = Agent(
model=Claude(id="claude-3-5-sonnet-20240620"),
model=Claude(id="claude-3-5-sonnet-20241022"),
tools=[YFinanceTools(stock_price=True, analyst_recommendations=True, stock_fundamentals=True)],
show_tool_calls=True,
description="You are an investment analyst that researches stocks and helps users make informed decisions.",
Expand Down
23 changes: 23 additions & 0 deletions cookbook/providers/claude/knowledge.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,23 @@
"""Run `pip install duckduckgo-search sqlalchemy pgvector pypdf anthropic openai` to install dependencies."""

from phi.agent import Agent
from phi.model.anthropic import Claude
from phi.knowledge.pdf import PDFUrlKnowledgeBase
from phi.vectordb.pgvector import PgVector

db_url = "postgresql+psycopg://ai:ai@localhost:5532/ai"

knowledge_base = PDFUrlKnowledgeBase(
urls=["https://phi-public.s3.amazonaws.com/recipes/ThaiRecipes.pdf"],
vector_db=PgVector(table_name="recipes", db_url=db_url),
)
knowledge_base.load(recreate=True) # Comment out after first run

agent = Agent(
model=Claude(id="claude-3-5-sonnet-20241022"),
knowledge_base=knowledge_base,
use_tools=True,
show_tool_calls=True,
debug_mode=True,
)
agent.print_response("How to make Thai curry?", markdown=True)
51 changes: 51 additions & 0 deletions cookbook/providers/claude/memory.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,51 @@
"""
This recipe shows how to use personalized memories and summaries in an agent.
Steps:
1. Run: `./cookbook/run_pgvector.sh` to start a postgres and pgvector instance
2. Run: `pip install anthropic sqlalchemy 'psycopg[binary]' pgvector` to install the dependencies
3. Run: `python cookbook/agents/personalized_memories_and_summaries.py` to run the agent
"""

from rich.pretty import pprint

from phi.agent import Agent, AgentMemory
from phi.model.anthropic import Claude
from phi.memory.db.postgres import PgMemoryDb
from phi.storage.agent.postgres import PgAgentStorage

db_url = "postgresql+psycopg://ai:ai@localhost:5532/ai"
agent = Agent(
model=Claude(id="claude-3-5-sonnet-20241022"),
# Store the memories and summary in a database
memory=AgentMemory(
db=PgMemoryDb(table_name="agent_memory", db_url=db_url), create_user_memories=True, create_session_summary=True
),
# Store agent sessions in a database
storage=PgAgentStorage(table_name="personalized_agent_sessions", db_url=db_url),
# Show debug logs so you can see the memory being created
# debug_mode=True,
)

# -*- Share personal information
agent.print_response("My name is john billings?", stream=True)
# -*- Print memories
pprint(agent.memory.memories)
# -*- Print summary
pprint(agent.memory.summary)

# -*- Share personal information
agent.print_response("I live in nyc?", stream=True)
# -*- Print memories
pprint(agent.memory.memories)
# -*- Print summary
pprint(agent.memory.summary)

# -*- Share personal information
agent.print_response("I'm going to a concert tomorrow?", stream=True)
# -*- Print memories
pprint(agent.memory.memories)
# -*- Print summary
pprint(agent.memory.summary)

# Ask about the conversation
agent.print_response("What have we been talking about, do you know my name?", stream=True)
17 changes: 17 additions & 0 deletions cookbook/providers/claude/storage.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,17 @@
"""Run `pip install duckduckgo-search sqlalchemy anthropic` to install dependencies."""

from phi.agent import Agent
from phi.model.anthropic import Claude
from phi.tools.duckduckgo import DuckDuckGo
from phi.storage.agent.postgres import PgAgentStorage

db_url = "postgresql+psycopg://ai:ai@localhost:5532/ai"

agent = Agent(
model=Claude(),
storage=PgAgentStorage(table_name="agent_sessions", db_url=db_url),
tools=[DuckDuckGo()],
add_history_to_messages=True,
)
agent.print_response("How many people live in Canada?")
agent.print_response("What is their national anthem called?")
2 changes: 1 addition & 1 deletion phi/model/anthropic/claude.py
Original file line number Diff line number Diff line change
Expand Up @@ -72,7 +72,7 @@ class Claude(Model):
client (Optional[AnthropicClient]): A pre-configured instance of the Anthropic client.
"""

id: str = "claude-3-5-sonnet-20240620"
id: str = "claude-3-5-sonnet-20241022"
name: str = "Claude"
provider: str = "Anthropic"

Expand Down

0 comments on commit 10cb0d9

Please sign in to comment.