Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

WIP: show-context-with-messages-phi-1668 #1454

Open
wants to merge 17 commits into
base: main
Choose a base branch
from
32 changes: 32 additions & 0 deletions cookbook/agents/24_agent_with_context.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,32 @@
import json
import httpx

from phi.agent import Agent


def get_top_hackernews_stories(num_stories: int = 5) -> str:
# Fetch top story IDs
response = httpx.get("https://hacker-news.firebaseio.com/v0/topstories.json")
story_ids = response.json()

# Fetch story details
stories = []
for story_id in story_ids[:num_stories]:
story_response = httpx.get(f"https://hacker-news.firebaseio.com/v0/item/{story_id}.json")
story = story_response.json()
if "text" in story:
story.pop("text", None)
stories.append(story)
return json.dumps(stories)


agent = Agent(
context={
"name": "John Doe",
"top_stories": lambda: get_top_hackernews_stories(3),
},
add_context=True,
markdown=True,
show_tool_calls=True,
)
agent.print_response("Who am I and what are the top stories?", stream=True)
14 changes: 14 additions & 0 deletions cookbook/agents/25_system_prompt_via_function.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,14 @@
from phi.agent import Agent


def get_system_prompt(agent: Agent) -> str:
return f"You are {agent.name}!"


agent = Agent(
name="AgentX",
system_prompt=get_system_prompt,
markdown=True,
show_tool_calls=True,
)
agent.print_response("Who are you?", stream=True)
16 changes: 16 additions & 0 deletions cookbook/agents/26_instructions_via_function.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,16 @@
from typing import List

from phi.agent import Agent


def get_instructions(agent: Agent) -> List[str]:
return ["Talk in haiku's!", "Use poetry to answer questions."]


agent = Agent(
name="AgentX",
instructions=get_instructions,
markdown=True,
show_tool_calls=True,
)
agent.print_response("Who are you?", stream=True)
34 changes: 34 additions & 0 deletions cookbook/agents/27_tool_calls_accesing_agent.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,34 @@
import json
import httpx

from phi.agent import Agent
from phi.utils.log import logger


def get_top_hackernews_stories(agent: Agent, num_stories: int = 5) -> str:
logger.info(f"Context: {agent.context}")

# Fetch top story IDs
response = httpx.get("https://hacker-news.firebaseio.com/v0/topstories.json")
story_ids = response.json()

# Fetch story details
stories = []
for story_id in story_ids[:num_stories]:
story_response = httpx.get(f"https://hacker-news.firebaseio.com/v0/item/{story_id}.json")
story = story_response.json()
if "text" in story:
story.pop("text", None)
stories.append(story)
return json.dumps(stories)


agent = Agent(
context={
"num_stories": 3,
},
tools=[get_top_hackernews_stories],
markdown=True,
show_tool_calls=True,
)
agent.print_response("What are the top hackernews stories?", stream=True)
8 changes: 4 additions & 4 deletions cookbook/integrations/pgvector/agent.py
Original file line number Diff line number Diff line change
@@ -1,15 +1,15 @@
from phi.agent import Agent
from phi.storage.agent.postgres import PgAgentStorage
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"

agent = Agent(
storage=PgAgentStorage(table_name="recipe_agent", db_url=db_url),
knowledge_base=PDFUrlKnowledgeBase(
knowledge=PDFUrlKnowledgeBase(
urls=["https://phi-public.s3.amazonaws.com/recipes/ThaiRecipes.pdf"],
vector_db=PgVector2(collection="recipe_documents", db_url=db_url),
vector_db=PgVector(table_name="recipe_documents", db_url=db_url),
),
# Show tool calls in the response
show_tool_calls=True,
Expand All @@ -19,6 +19,6 @@
read_chat_history=True,
)
# Comment out after first run
agent.knowledge_base.load(recreate=False) # type: ignore
agent.knowledge.load(recreate=False) # type: ignore

agent.print_response("How do I make pad thai?", markdown=True)
Loading