AI Toolkit for Engineers
Project description
phidata
Build AI Assistants using language models
โจ What is phidata?
A toolkit for building AI Assistants using language models.
Assistants are trained to perform specific tasks and come with built-in memory, knowledge base, storage and tools. Examples:
- PDF Assistants: Answer questions using PDFs.
- Data Assistants: Analyze data by running SQL queries.
- Python Assistants: Perform tasks by running python code.
- Stock Assistants: Analyze stocks and research companies.
- Marketing Assistants: Provide marketing insights, copywriting and content ideas.
We can serve the assistants using Streamlit, FastApi or Django to build AI products.
๐ฉโ๐ป Getting Started
Installation
- Open the
Terminal
and create anai
directory with a python virtual environment.
mkdir ai && cd ai
python3 -m venv aienv
source aienv/bin/activate
- Install phidata
pip install -U phidata
Create a Conversation
Conversations are a human-like interface to language models and come with built-in memory, knowledge, storage and access to tools. Giving LLMs the ability to have long-term, knowledge-based Conversations is the first step in our journey to AGI.
- Create a file
conversation.py
and install openai usingpip install openai
from phi.conversation import Conversation
conversation = Conversation()
conversation.print_response('Share a quick healthy breakfast recipe.')
- Run the
conversation.py
file
python conversation.py
- See a simple conversation in action
โญโโโโโโโโโโโฌโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโฎ
โ Message โ Share a quick healthy breakfast recipe. โ
โโโโโโโโโโโโผโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโค
โ Response โ Absolutely! Here's a quick and healthy breakfast โ
โ (2.1s) โ recipe for a yogurt parfait: โ
โ โ โ
โ โ Healthy Yogurt Parfait โ
โ โ โ
โ โ Ingredients: โ
โ โ โ
โ โ โข Greek yogurt โ
โ โ โข Fresh berries (e.g., strawberries, blueberries, โ
โ โ raspberries) โ
โ โ โข Granola โ
โ โ โข Honey or maple syrup (optional) โ
โ โ โข Chia seeds (optional) โ
โ โ โ
โ โ Instructions: โ
โ โ โ
โ โ 1 In a clear glass or bowl, layer Greek yogurt, fresh โ
โ โ berries, and granola. โ
โ โ 2 Repeat the layers until the glass is filled. โ
โ โ 3 Drizzle with honey or maple syrup for sweetness, if โ
โ โ desired. โ
โ โ 4 Optional: Sprinkle with chia seeds for added โ
โ โ nutritional benefits. โ
โ โ โ
โ โ Enjoy your nutritious and delicious yogurt parfait! โ
โฐโโโโโโโโโโโดโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโฏ
Create a Python Engineer
We can have Conversations with Agents
designed for specific tasks. For example: the PythonAgent
can perform virtually any task using python code.
- Create a file
python_agent.py
and install pandas usingpip install pandas
from phi.agent.python import PythonAgent
from phi.file.local.csv import CsvFile
python_agent = PythonAgent(
files=[
CsvFile(
path="https://phidata-public.s3.amazonaws.com/demo_data/IMDB-Movie-Data.csv",
description="Contains information about movies from IMDB.",
)
],
pip_install=True,
show_function_calls=True,
)
python_agent.print_response("What is the average rating of movies?")
- Run the
python_agent.py
file
python python_agent.py
- See it work through the problem
WARNING PythonTools can run arbitrary code, please provide human supervision.
INFO Saved: .../average_rating.py
INFO Running .../average_rating.py
โญโโโโโโโโโโโฌโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโฎ
โ Message โ What is the average rating of movies? โ
โโโโโโโโโโโโผโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโค
โ Response โ โ
โ (4.1s) โ โข Running: โ
โ โ save_to_file_and_run(file_name=average_rating, โ
โ โ code=..., variable_to_return=average_rating) โ
โ โ โ
โ โ The average rating of the movies is approximately โ
โ โ 6.72. โ
โฐโโโโโโโโโโโดโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโฏ
Create a Data Analyst
Use the DuckDbAgent
to perform data analysis using SQL queries.
- Create a file
data_analyst.py
and install duckdb usingpip install duckdb
import json
from phi.agent.duckdb import DuckDbAgent
duckdb_agent = DuckDbAgent(
semantic_model=json.dumps({
"tables": [
{
"name": "movies",
"description": "Contains information about movies from IMDB.",
"path": "https://phidata-public.s3.amazonaws.com/demo_data/IMDB-Movie-Data.csv",
}
]
}),
)
duckdb_agent.print_response("What is the average rating of movies? Show me the SQL.")
- Run the
data_analyst.py
file
python data_analyst.py
- See it work through the problem
INFO Running: SHOW TABLES
INFO Running: CREATE TABLE IF NOT EXISTS 'movies'
AS SELECT * FROM
'https://phidata-public.s3.amazonaws.com/demo_
data/IMDB-Movie-Data.csv'
INFO Running: DESCRIBE movies
INFO Running: SELECT AVG(Rating) AS average_rating
FROM movies
โญโโโโโโโโโโโฌโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโฎ
โ Message โ What is the average rating of movies? Show me the SQL. โ
โโโโโโโโโโโโผโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโค
โ Response โ The average rating of movies in the dataset is 6.72. โ
โ (7.6s) โ โ
โ โ Here is the SQL query used to calculate the average โ
โ โ rating: โ
โ โ โ
โ โ โ
โ โ SELECT AVG(Rating) AS average_rating โ
โ โ FROM movies; โ
โ โ โ
โฐโโโโโโโโโโโดโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโฏ
Return a Pydantic Model as output
One of our favorite features is generating structured data from sparse information.
Meaning we can use LLMs to fill in pydantic models and generate content which previously could not be possible.
In this example, we generate an object of the MovieScript
class.
- Create a file
movie_generator.py
from typing import List
from pydantic import BaseModel, Field
from phi.conversation import Conversation
from rich.pretty import pprint
class MovieScript(BaseModel):
setting: str = Field(..., description="Setting of the movie. If not available, provide a random setting.")
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="2 sentence story of the movie.")
movie_generator = Conversation(
system_prompt="Generate a movie",
output_model=MovieScript,
)
pprint(movie_generator.run("New York"))
- Run the
movie_generator.py
file
python movie_generator.py
- See how the conversation generates a structured output
MovieScript(
โ setting='New York',
โ ending='happy ending',
โ genre='romantic comedy',
โ name='Love in the City',
โ characters=['Emma', 'Jack', 'Olivia', 'Michael'],
โ storyline="In the bustling streets of New York, Emma, an ambitious young woman, meets Jack, a charming but jobless artist. As they navigate the city's challenges, their bond grows stronger, leading to unexpected romance and heartwarming adventures."
)
๐ Examples
Create a PDF Assistant with Knowledge & Storage
- Knowledge Base: information that the AI can search to improve its responses, typically provided by a vector db.
- Storage: provides long term memory for
Conversations
, typically provided by a database.
Let's run PgVector
as it can provide both, knowledge and storage for our Conversations.
- Install docker desktop for running PgVector in a container.
- Create a file
resources.py
with the following contents
from phi.docker.app.postgres import PgVectorDb
from phi.docker.resources import DockerResources
# -*- PgVector running on port 5432:5432
vector_db = PgVectorDb(
pg_user="llm",
pg_password="llm",
pg_database="llm",
debug_mode=True,
)
# -*- DockerResources
dev_docker_resources = DockerResources(apps=[vector_db])
- Start
PgVector
using
phi start resources.py
- Create a file
pdf_assistant.py
and install libraries usingpip install pgvector pypdf psycopg sqlalchemy
import typer
from rich.prompt import Prompt
from typing import Optional, List
from phi.conversation import Conversation
from phi.storage.conversation.postgres import PgConversationStorage
from phi.knowledge.pdf import PDFUrlKnowledgeBase
from phi.vectordb.pgvector import PgVector
from resources import vector_db
knowledge_base = PDFUrlKnowledgeBase(
urls=["https://www.family-action.org.uk/content/uploads/2019/07/meals-more-recipes.pdf"],
vector_db=PgVector(
collection="recipes",
db_url=vector_db.get_db_connection_local(),
),
)
storage = PgConversationStorage(
table_name="recipe_conversations",
db_url=vector_db.get_db_connection_local(),
)
def llm_app(new: bool = False, user: str = "user"):
conversation_id: Optional[str] = None
if not new:
existing_conversation_ids: List[str] = storage.get_all_conversation_ids(user)
if len(existing_conversation_ids) > 0:
conversation_id = existing_conversation_ids[0]
conversation = Conversation(
user_name=user,
id=conversation_id,
knowledge_base=knowledge_base,
storage=storage,
# Uncomment the following line to use traditional RAG
# add_references_to_prompt=True,
function_calls=True,
show_function_calls=True,
)
if conversation_id is None:
conversation_id = conversation.id
print(f"Started Conversation: {conversation_id}\n")
else:
print(f"Continuing Conversation: {conversation_id}\n")
conversation.knowledge_base.load(recreate=False)
while True:
message = Prompt.ask(f"[bold] :sunglasses: {user} [/bold]")
if message in ("exit", "bye"):
break
conversation.print_response(message)
if __name__ == "__main__":
typer.run(llm_app)
- Run the
pdf_assistant.py
file
python pdf_assistant.py
- Ask a question:
How do I make chicken tikka salad?
- Message
bye
to exit, start the app again and ask:
What was my last message?
See how the app maintains storage across sessions.
- Run the
pdf_assistant.py
file with the--new
flag to start a new conversation.
python pdf_assistant.py --new
- Stop PgVector
Play around and then stop PgVector
using phi stop resources.py
phi stop resources.py
Build an AI App using Streamlit, FastApi and PgVector
Phidata provides pre-built templates for AI Apps that you can use as a starting point. The general workflow is:
- Create your codebase using a template:
phi ws create
- Run your app locally:
phi ws up dev:docker
- Run your app on AWS:
phi ws up prd:aws
Let's build an AI App using GPT-4 as the LLM, Streamlit as the chat interface, FastApi as the backend and PgVector for knowledge and storage. Read the full tutorial here.
Step 1: Create your codebase
Create your codebase using the llm-app
template pre-configured with FastApi, Streamlit and PgVector.
phi ws create -t llm-app -n llm-app
This will create a folder llm-app
with a pre-built LLM App that you can customize and make your own.
Step 2: Serve your App using Streamlit
Streamlit allows us to build micro front-ends and is extremely useful for building basic applications in pure python. Start the app
group using:
phi ws up --group app
Press Enter to confirm and give a few minutes for the image to download.
-
Chat with PDFs
-
Open localhost:8501 to view streamlit apps that you can customize and make your own.
-
Click on Chat with PDFs in the sidebar
-
Enter a username and wait for the knowledge base to load.
-
Choose the
RAG
orAutonomous
Conversation type. -
Ask "How do I make chicken curry?"
-
Upload PDFs and ask questions
Step 3: Serve your App using FastApi
Streamlit is great for building micro front-ends but any production application will be built using a front-end framework like next.js
backed by a RestApi built using a framework like FastApi
.
Your LLM App comes ready-to-use with FastApi endpoints, start the api
group using:
phi ws up --group api
Press Enter to confirm and give a few minutes for the image to download.
-
View API Endpoints
-
Open localhost:8000/docs to view the API Endpoints.
-
Load the knowledge base using
/v1/pdf/conversation/load-knowledge-base
-
Test the
v1/pdf/conversation/chat
endpoint with{"message": "How do I make chicken curry?"}
-
The Api comes pre-built with endpoints that you can integrate with your front-end.
Optional: Run Jupyterlab
A jupyter notebook is a must-have for AI development and your llm-app
comes with a notebook pre-installed with the required dependencies. Enable it by updating the workspace/settings.py
file:
...
ws_settings = WorkspaceSettings(
...
# Uncomment the following line
dev_jupyter_enabled=True,
...
Start jupyter
using:
phi ws up --group jupyter
Press Enter to confirm and give a few minutes for the image to download (only the first time). Verify container status and view logs on the docker dashboard.
View Jupyterlab UI
-
Open localhost:8888 to view the Jupyterlab UI. Password: admin
-
Play around with cookbooks in the
notebooks
folder. -
Delete local resources
Step 4: Stop the workspace
Play around and stop the workspace using:
phi ws down
Step 5: Run your LLM App on AWS
Read how to run your LLM App on AWS.
๐ Resources
- Read the documentation
- Chat with us on discord
- Email us at help@phidata.com
Project details
Release history Release notifications | RSS feed
Download files
Download the file for your platform. If you're not sure which to choose, learn more about installing packages.
Source Distribution
Built Distribution
Hashes for phidata-2.2.0.dev2-py3-none-any.whl
Algorithm | Hash digest | |
---|---|---|
SHA256 | 26ccb0f4b52dee4e0bcda5203d84a01d68350529af5bfcf7e9347d2f7559eace |
|
MD5 | 5618471c436fe9ffc8602414cae46c76 |
|
BLAKE2b-256 | ff39457c5d81bd77bfb78f053c417212f7032baa96d7cc2e37ea673e1286cd9b |