What is phidata?
Phidata is a framework for building agentic systems, use phidata to:
- Build Agents with memory, knowledge, tools and reasoning.
- Build teams of Agents that can work together.
- Chat with your Agents using a beautiful Agent UI.
- Monitor, evaluate and optimize your 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.
Reasoning Agent
Reasoning is an experimental feature that helps agents work through a problem step-by-step, backtracking and correcting as needed. Create a file reasoning_agent.py
.
Create a reasoning agent
from phi.agent import Agent
from phi.model.openai import OpenAIChat
task = (
"Three missionaries and three cannibals need to cross a river. "
"They have a boat that can carry up to two people at a time. "
"If, at any time, the cannibals outnumber the missionaries on either side of the river, the cannibals will eat the missionaries. "
"How can all six people get across the river safely? Provide a step-by-step solution and show the solutions as an ascii diagram"
)
reasoning_agent = Agent(model=OpenAIChat(id="gpt-4o"), reasoning=True, markdown=True, structured_outputs=True)
reasoning_agent.print_response(task, stream=True, show_full_reasoning=True)
Run the reasoning agent
python reasoning_agent.py
Reasoning is an experimental feature and will break ~20% of the time. It is not a replacement for o1. It is an experiment that combines COT and tool use. Set your expectations very low for this initial release. For example: It will not be able to count ‘r’s in ‘strawberry’.
RAG Agent
Instead of always inserting the “context” into the prompt, the RAG Agent can search its knowledge base (vector db) for the specific information it needs to achieve its task.
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
Use structured outputs to enforce the structure of the output from an agent and make the agent return a pydantic object. 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
Ready to build?
Start building your own agents or use a template to build a full-stack agentic system.
Getting help
Come chat with us on discord or post your questions on the community forum.
Looking for dedicated support?
We’ve helped many companies build AI products, the general workflow is:
- Build agents to perform tasks specific to your product.
- Serve your agents via an API and connect them to your product.
- Monitor, evaluate and improve your AI product.
We provide dedicated support and development, book a call to get started.