What is phidata?
Phidata is a framework for building agentic systems, engineers use phidata to:
- Build Agents with memory, knowledge, tools and reasoning.
- Build teams of Agents that can work together.
- Chat with Agents using a beautiful Agent UI.
- Monitor, evaluate and optimize Agents.
- Build agentic systems i.e. applications with an API, database and vectordb.
Let’s build some agents
Setup your virtual environment
Export your OpenAI key
Phidata works with every LLM but for these examples let’s use OpenAI.
You can get an API key from here.
Web Search Agent
Let’s build a simple agent that can search the web, create a file web_search.py
Create a web search agent
from phi.agent import Agent
from phi.model.openai import OpenAIChat
from phi.tools.duckduckgo import DuckDuckGo
web_agent = Agent(
name="Web Agent",
model=OpenAIChat(id="gpt-4o"),
tools=[DuckDuckGo()],
instructions=["Always include sources"],
show_tool_calls=True,
markdown=True,
)
web_agent.print_response("Whats happening in France?", stream=True)
Run the agent
Install libraries
pip install duckduckgo-search
Run the agent
Financial Agent
Lets create another agent that can query financial data, create a file finance_agent.py
Create a finance agent
from phi.agent import Agent
from phi.model.openai import OpenAIChat
from phi.tools.yfinance import YFinanceTools
finance_agent = Agent(
name="Finance Agent",
model=OpenAIChat(id="gpt-4o"),
tools=[YFinanceTools(stock_price=True, analyst_recommendations=True, company_info=True, company_news=True)],
instructions=["Use tables to display data"],
show_tool_calls=True,
markdown=True,
)
finance_agent.print_response("Summarize analyst recommendations for NVDA", stream=True)
Run the agent
Install libraries
Run the agent
Team of Agents
A team of agents can work together to solve complex problems, create a file agent_team.py
Create an agent team
from phi.agent import Agent
from phi.model.openai import OpenAIChat
from phi.tools.duckduckgo import DuckDuckGo
from phi.tools.yfinance import YFinanceTools
web_agent = Agent(
name="Web Agent",
role="Search the web for information",
model=OpenAIChat(id="gpt-4o"),
tools=[DuckDuckGo()],
instructions=["Always include sources"],
show_tool_calls=True,
markdown=True,
)
finance_agent = Agent(
name="Finance Agent",
role="Get financial data",
model=OpenAIChat(id="gpt-4o"),
tools=[YFinanceTools(stock_price=True, analyst_recommendations=True, company_info=True)],
instructions=["Use tables to display data"],
show_tool_calls=True,
markdown=True,
)
agent_team = Agent(
team=[web_agent, finance_agent],
instructions=["Always include sources", "Use tables to display data"],
show_tool_calls=True,
markdown=True,
)
agent_team.print_response("Summarize analyst recommendations and share the latest news for NVDA", stream=True)
Agent teams are non-deterministic and are not recommended for production systems, we recommend using workflows instead.
Agentic RAG
Instead of always inserting the “context” into the prompt, we give our Agent a tool to search its knowledge base (vector db) for the information it needs.
This saves tokens and improves response quality. Create a file rag_agent.py
Create a RAG agent
from phi.agent import Agent
from phi.model.openai import OpenAIChat
from phi.embedder.openai import OpenAIEmbedder
from phi.knowledge.pdf import PDFUrlKnowledgeBase
from phi.vectordb.lancedb import LanceDb, SearchType
knowledge_base = PDFUrlKnowledgeBase(
urls=["https://phi-public.s3.amazonaws.com/recipes/ThaiRecipes.pdf"],
vector_db=LanceDb(
table_name="recipes",
uri="tmp/lancedb",
search_type=SearchType.vector,
embedder=OpenAIEmbedder(model="text-embedding-3-small"),
),
)
knowledge_base.load()
agent = Agent(
model=OpenAIChat(id="gpt-4o"),
knowledge=knowledge_base,
show_tool_calls=True,
markdown=True,
)
agent.print_response("How do I make chicken and galangal in coconut milk soup", stream=True)
Run the agent
Install libraries
pip install lancedb tantivy pypdf sqlalchemy
Run the agent
Structured Outputs
Agents can return their output in a structured format as a Pydantic model.
Create a file structured_output.py
Create a structured output agent
from typing import List
from pydantic import BaseModel, Field
from phi.agent import Agent
from phi.model.openai import OpenAIChat
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!")
json_mode_agent = Agent(
model=OpenAIChat(id="gpt-4o"),
description="You write movie scripts.",
response_model=MovieScript,
)
structured_output_agent = Agent(
model=OpenAIChat(id="gpt-4o-2024-08-06"),
description="You write movie scripts.",
response_model=MovieScript,
structured_outputs=True,
)
json_mode_agent.print_response("New York")
structured_output_agent.print_response("New York")
Run the agent
python structured_output.py
Next Steps
- Chat with your Agents using a beautiful Agent UI.
- Learn how to monitor and debug your Agents.
- For more advanced cases, build deterministic, stateful, multi-agent workflows.