phidatahq / phidata

Build AI Assistants with memory, knowledge and tools.

Home Page:

Geek Repo:Geek Repo

Github PK Tool:Github PK Tool


Build AI Assistants with memory, knowledge and tools


What is phidata?

Phidata is a framework for building Autonomous Assistants (aka Agents) that have long-term memory, contextual knowledge and the ability to take actions using function calling.

Use phidata to turn any LLM into an AI Assistant that can:

  • Search the web using DuckDuckGo, Google etc.
  • Analyze data using SQL, DuckDb, etc.
  • Conduct research and generate reports.
  • Answer questions from PDFs, APIs, etc.
  • Write scripts for movies, books, etc.
  • Summarize articles, videos, etc.
  • Perform tasks like sending emails, querying databases, etc.
  • And much more...

Why phidata?

Problem: We need to turn general-purpose LLMs into specialized assistants for our use-case.

Solution: Extend LLMs with memory, knowledge and tools:

  • Memory: Stores chat history in a database and enables LLMs to have long-term conversations.
  • Knowledge: Stores information in a vector database and provides LLMs with business context.
  • Tools: Enable LLMs to take actions like pulling data from an API, sending emails or querying a database.

Memory & knowledge make LLMs smarter while tools make them autonomous.

How it works

  • Step 1: Create an Assistant
  • Step 2: Add Tools (functions), Knowledge (vectordb) and Storage (database)
  • Step 3: Serve using Streamlit, FastApi or Django to build your AI application


pip install -U phidata


Assistant that can search the web

Create a file

from phi.assistant import Assistant
from import DuckDuckGo

assistant = Assistant(tools=[DuckDuckGo()], show_tool_calls=True)
assistant.print_response("Whats happening in France?", markdown=True)

Install libraries, export your OPENAI_API_KEY and run the Assistant

pip install openai duckduckgo-search

export OPENAI_API_KEY=sk-xxxx


Assistant that can query financial data

Create a file

from phi.assistant import Assistant
from phi.llm.openai import OpenAIChat
from import YFinanceTools

assistant = Assistant(
    tools=[YFinanceTools(stock_price=True, analyst_recommendations=True, company_info=True, company_news=True)],
assistant.print_response("What is the stock price of NVDA")
assistant.print_response("Write a comparison between NVDA and AMD, use all tools available.")

Install libraries and run the Assistant

pip install yfinance


More information


Assistant that can write and run python code

Show code

The PythonAssistant can achieve tasks by writing and running python code.

  • Create a file
from phi.assistant.python import PythonAssistant
from phi.file.local.csv import CsvFile

python_assistant = PythonAssistant(
            description="Contains information about movies from IMDB.",

python_assistant.print_response("What is the average rating of movies?", markdown=True)
  • Install pandas and run the
pip install pandas


Assistant that can analyze data using SQL

Show code

The DuckDbAssistant can perform data analysis using SQL.

  • Create a file
import json
from phi.assistant.duckdb import DuckDbAssistant

duckdb_assistant = DuckDbAssistant(
        "tables": [
                "name": "movies",
                "description": "Contains information about movies from IMDB.",
                "path": "",

duckdb_assistant.print_response("What is the average rating of movies? Show me the SQL.", markdown=True)
  • Install duckdb and run the file
pip install duckdb


Assistant that can generate pydantic models

Show code

One of our favorite LLM features is generating structured data (i.e. a pydantic model) from text. Use this feature to extract features, generate movie scripts, produce fake data etc.

Let's create a Movie Assistant to write a MovieScript for us.

  • Create a file
from typing import List
from pydantic import BaseModel, Field
from rich.pretty import pprint
from phi.assistant import Assistant

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!")

movie_assistant = Assistant(
    description="You help write movie scripts.",

pprint("New York"))
  • Run the file
  • The output is an object of the MovieScript class, here's how it looks:
│   setting='A bustling and vibrant New York City',
│   ending='The protagonist saves the city and reconciles with their estranged family.',
│   genre='action',
│   name='City Pulse',
│   characters=['Alex Mercer', 'Nina Castillo', 'Detective Mike Johnson'],
│   storyline='In the heart of New York City, a former cop turned vigilante, Alex Mercer, teams up with a street-smart activist, Nina Castillo, to take down a corrupt political figure who threatens to destroy the city. As they navigate through the intricate web of power and deception, they uncover shocking truths that push them to the brink of their abilities. With time running out, they must race against the clock to save New York and confront their own demons.'

PDF Assistant with Knowledge & Storage

Show code

Lets create a PDF Assistant that can answer questions from a PDF. We'll use PgVector for knowledge and storage.

Knowledge Base: information that the Assistant can search to improve its responses (uses a vector db).

Storage: provides long term memory for Assistants (uses a database).

  1. Run PgVector

Install docker desktop and run PgVector on port 5532 using:

docker run -d \
  -e POSTGRES_DB=ai \
  -e PGDATA=/var/lib/postgresql/data/pgdata \
  -v pgvolume:/var/lib/postgresql/data \
  -p 5532:5432 \
  --name pgvector \
  1. Create PDF Assistant
  • Create a file
import typer
from typing import Optional, List
from phi.assistant import Assistant
from import PgAssistantStorage
from phi.knowledge.pdf import PDFUrlKnowledgeBase
from phi.vectordb.pgvector import PgVector2

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

knowledge_base = PDFUrlKnowledgeBase(
    vector_db=PgVector2(collection="recipes", db_url=db_url),
# Comment out after first run

storage = PgAssistantStorage(table_name="pdf_assistant", db_url=db_url)

def pdf_assistant(new: bool = False, user: str = "user"):
    run_id: Optional[str] = None

    if not new:
        existing_run_ids: List[str] = storage.get_all_run_ids(user)
        if len(existing_run_ids) > 0:
            run_id = existing_run_ids[0]

    assistant = Assistant(
        # Show tool calls in the response
        # Enable the assistant to search the knowledge base
        # Enable the assistant to read the chat history
    if run_id is None:
        run_id = assistant.run_id
        print(f"Started Run: {run_id}\n")
        print(f"Continuing Run: {run_id}\n")

    # Runs the assistant as a cli app

if __name__ == "__main__":
  1. Install libraries
pip install -U pgvector pypdf "psycopg[binary]" sqlalchemy
  1. Run PDF Assistant
  • Ask a question:
How do I make pad thai?
  • See how the Assistant searches the knowledge base and returns a response.

  • Message bye to exit, start the assistant again using python and ask:

What was my last message?

See how the assistant now maintains storage across sessions.

  • Run the file with the --new flag to start a new run.
python --new

Checkout the cookbook for more examples.

Next Steps

  1. Read the basics to learn more about phidata.
  2. Read about Assistants and how to customize them.
  3. Checkout the cookbook for in-depth examples and code.


Checkout the following AI Applications built using phidata:

  • PDF AI that summarizes and answers questions from PDFs.
  • ArXiv AI that answers questions about ArXiv papers using the ArXiv API.
  • HackerNews AI summarize stories, users and shares what's new on HackerNews.


LLM OS with gpt-4o

Building the LLM OS with gpt-4o

Autonomous RAG

Autonomous RAG

Local RAG with Llama3

Local RAG with Llama3

Llama3 Research Assistant powered by Groq

Llama3 Research Assistant powered by Groq

Looking to build an AI product?

We've helped many companies build AI products, the general workflow is:

  1. Build an Assistant with proprietary data to perform tasks specific to your product.
  2. Connect your product to the Assistant via an API.
  3. Monitor and Improve your AI product.

We also provide dedicated support and development, book a call to get started.


We're an open-source project and welcome contributions, please read the contributing guide for more information.

Request a feature

  • If you have a feature request, please open an issue or make a pull request.
  • If you have ideas on how we can improve, please create a discussion.


Our roadmap is available here. If you have a feature request, please open an issue/discussion.


Build AI Assistants with memory, knowledge and tools.

License:Mozilla Public License 2.0


Language:Python 99.6%Language:Shell 0.3%Language:Batchfile 0.1%