Skip to content Skip to footer

Phidata Tutorial: Building AI Agents with Phidata

Phidata is a framework for building multi-modal agents and workflows. AI Agents are autonomous agents based on Large Language Models (LLM’s) which can perform tasks autononomously.

What are AI Agents?

AI agents are based on an LLM. With Phidata you can work with almost any Large Language Model, such as OpenAI, Anthropic, but also local models with Ollama (such as LLama3.3 for example).

Ok, so how are they different from models such as ChatGPT? AI agents are build for a specific purpose, such as researching stock prices or handling customer service. And they can use resources designed for this purpose.

AI Agents can use the following:

  • Memory: to keep track of conversations and tasks they’re working on they can use short-term and long-term memory (just like yourself).
  • Knowledge: knowledge is different than memory. Knowledge can be research papers, recipes, manuals, etc.
  • Tools: AI agents can also execute tasks by using tools. They can add meetings to your calendar, use search engines, scrape data from websites etc. And they can combine these tools in a clever way to perform a task.

AI Agents and their capabilities, Source: Phidata

Let’s build our first AI Agent!

Let’s start building our first AI agent with Phidata!

Let’s open VS Code (or any other code editor), create a new project, and execute the following commands to install all the required libraries:

pip install openai yfinance phidata python-dotenv lancedb tantivy pypdf sqlalchemy httpx duckdb duckduckgo-search fastapi uvicorn

Now it’s time to get access to a Large Language Model. For the next step you need an OpenAI API key. Follow this tutorial if you don’t know how to get one.

Now create a file called .env in VS Code and add your API key here:

OPENAI_API_KEY = "[YOUR API KEY HERE]"

Let’s start with building an agent which helps you to make investment decisions. Add a file finance-agent.py:

finance-agent.py

from phi.agent import Agent
from phi.model.openai import OpenAIChat
from phi.tools.yfinance import YFinanceTools

from dotenv import load_dotenv

load_dotenv()

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("Which stock is a better investment, NVDA or META?", stream=True)

agentic-rag.py

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

from dotenv import load_dotenv

load_dotenv()

# Create a knowledge base from a PDF
knowledge_base = PDFUrlKnowledgeBase(
    urls=["https://phi-public.s3.amazonaws.com/recipes/ThaiRecipes.pdf"],
    # Use LanceDB as the vector database
    vector_db=LanceDb(
        table_name="recipes",
        uri="tmp/lancedb",
        search_type=SearchType.vector,
        embedder=OpenAIEmbedder(model="text-embedding-3-small"),
    ),
)
# Comment out after first run as the knowledge base is loaded
knowledge_base.load()

prompt = """
"You only answer with information from your RAG database.
You don't use your internal knowledge.
If you can't answer with the database, simple return 'I don't know'"
"""

agent = Agent(
    model=OpenAIChat(id="gpt-4o"),
    # Add the knowledge base to the agent
    knowledge=knowledge_base,
    show_tool_calls=True,
    markdown=True,
    instructions=[prompt],
)
agent.print_response("How do I make Pad Thai Goong Sod", stream=True)

multi-agent.py

import httpx
from pathlib import Path
from phi.agent import Agent
from phi.tools.csv_tools import CsvTools
from phi.tools.duckduckgo import DuckDuckGo
import os

from dotenv import load_dotenv

load_dotenv()

### SEARCH CUSTOMER DATABASE AGENT ####################################################

imdb_csv = Path(__file__).parent.joinpath("wip").joinpath("IMDB-Movie-Data.csv")

if not os.path.exists(imdb_csv):
    url = "https://phidata-public.s3.amazonaws.com/demo_data/IMDB-Movie-Data.csv"

    response = httpx.get(url)

    imdb_csv.parent.mkdir(parents=True, exist_ok=True)
    imdb_csv.write_bytes(response.content)



imdb_csv_agent = Agent(
    name="IMDB CSV Agent",
    tools=[CsvTools(csvs=[imdb_csv])],
    markdown=True,
    show_tool_calls=True,
    debug_mode=True,
    instructions=[
        "First always get the list of files",
        "Then check the columns in the file",
        "Then run the query to answer the question",
    ],
)

### SEARCH THE WEB ##################################################################

web_search_agent = Agent(
    name="Web Search Agent",
    tools=[DuckDuckGo()],
    markdown=True,
    show_tool_calls=True,
)

### MAIN AGENT #####################################################################

agent_team = Agent(
    name="Agent Team",
    team=[web_search_agent, imdb_csv_agent],
    instructions=[
        "You are an AI agent which knows everything about movies",
        "You can refer questions to two different agents: Web Search Agent and IMDB CSV Agent",
        "First you always use the IMDB agent, if it cannot answer your question you use the Web Search Agent",
    ],
    show_tool_calls=True,
    markdown=True,
)

agent_team.cli_app(stream=False)

Leave a comment

Receive my Python cheatsheet today!

Do you want to become a Python expert? I summarized all my expertise in a 3 pages cheatsheet, so you never have to Google again :)

Socials

Tom’s Tech Academy © 2025. All Rights Reserved.