MyGit
๐Ÿšฉๆ”ถๅˆฐGitHubไป“ๅบ“็š„ๆ›ดๆ–ฐ้€š็Ÿฅ

phidatahq/phidata

Fork: 250 Star: 2451 (ๆ›ดๆ–ฐไบŽ 2024-02-28 04:08:12)

license: MPL-2.0

Language: Python .

Build AI Assistants using function calling

ๆœ€ๅŽๅ‘ๅธƒ็‰ˆๆœฌ๏ผš v2.3.44 ( 2024-02-27 20:23:33)

ๅฎ˜ๆ–น็ฝ‘ๅ€ GitHub็ฝ‘ๅ€

โœจๅ…่ดน็”ณ่ฏท็ฝ‘็ซ™SSL่ฏไนฆ๏ผŒๆ”ฏๆŒๅคšๅŸŸๅๅ’Œๆณ›ๅŸŸๅ๏ผŒ็‚นๅ‡ปๆŸฅ็œ‹

phidata

Function calling is all you need

version pythonversion downloads build-status

What is phidata?

Phidata is a toolkit for building AI Assistants using function calling.

Function calling enables LLMs to achieve tasks by calling functions and intelligently choosing their next step based on the response, just like how humans solve problems.

assistants

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

Installation

pip install -U phidata

Example 1: Assistant that can search the web

Create a file assistant.py

from phi.assistant import Assistant

assistant = Assistant(description="You help people with their health and fitness goals.")
assistant.print_response("Share a quick healthy breakfast recipe.", markdown=True)

Install openai and run the Assistant

pip install openai

python assistant.py

Add DuckDuckGo functions to let the Assistant search the web

from phi.assistant import Assistant
from phi.tools.duckduckgo import DuckDuckGo

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

Install duckduckgo-search and run the Assistant

pip install duckduckgo-search

python assistant.py

Example 2: Assistant that can write and run python code

The PythonAssistant can achieve tasks using python code. Create a file python_assistant.py

from phi.assistant.python import PythonAssistant
from phi.file.local.csv import CsvFile

python_assistant = PythonAssistant(
    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_tool_calls=True,
)

python_assistant.print_response("What is the average rating of movies?", markdown=True)

Install pandas and run the python_assistant.py

pip install pandas

python python_assistant.py

Example 3: Assistant that can analyze data using SQL

The DuckDbAssistant can perform data analysis using SQL. Create a file data_assistant.py

import json
from phi.assistant.duckdb import DuckDbAssistant

duckdb_assistant = DuckDbAssistant(
    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_assistant.print_response("What is the average rating of movies? Show me the SQL.", markdown=True)

Install duckdb and run the data_assistant.py file

pip install duckdb

python data_assistant.py

Demos

Checkout these AI apps showcasing the advantage of function calling:

  • PDF AI that summarizes and answers questions from PDFs.
  • ArXiv AI that answers questions about ArXiv papers using the ArXiv API.
  • HackerNews AI that interacts with the HN API to summarize stories, users, find out what's trending, summarize topics.
  • Demo Streamlit App serving a PDF, Image and Website Assistant (password: admin)
  • Demo FastApi serving a PDF Assistant.

Tutorials

Build an AI App in 3 steps

Build an AI App

Build a Local RAG AI App using OpenHermes and Ollama

Local AI App

Examples

Assistant that calls the HackerNews API

  • Create a file api_assistant.py that can call the HackerNews API to get top stories.
import json
import httpx

from phi.assistant import Assistant


def get_top_hackernews_stories(num_stories: int = 10) -> str:
    """Use this function to get top stories from Hacker News.

    Args:
        num_stories (int): Number of stories to return. Defaults to 10.

    Returns:
        str: JSON string of top stories.
    """

    # Fetch top story IDs
    response = httpx.get('https://hacker-news.firebaseio.com/v0/topstories.json')
    story_ids = response.json()

    # Fetch story details
    stories = []
    for story_id in story_ids[:num_stories]:
        story_response = httpx.get(f'https://hacker-news.firebaseio.com/v0/item/{story_id}.json')
        story = story_response.json()
        if "text" in story:
            story.pop("text", None)
        stories.append(story)
    return json.dumps(stories)

assistant = Assistant(tools=[get_top_hackernews_stories], show_tool_calls=True)
assistant.print_response("Summarize the top stories on hackernews?", markdown=True)
  • Run the api_assistant.py file
python api_assistant.py
  • See it work through the problem
โ•ญโ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”ฌโ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ•ฎ
โ”‚ Message  โ”‚ Summarize the top stories on hackernews?                          โ”‚
โ”œโ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”ผโ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”ค
โ”‚ Response โ”‚                                                                   โ”‚
โ”‚ (51.1s)  โ”‚  โ€ข Running: get_top_hackernews_stories(num_stories=5)             โ”‚
โ”‚          โ”‚                                                                   โ”‚
โ”‚          โ”‚ Here's a summary of the top stories on Hacker News:               โ”‚
โ”‚          โ”‚                                                                   โ”‚
โ”‚          โ”‚  1 Boeing Whistleblower: Max 9 Production Line Has "Enormous      โ”‚
โ”‚          โ”‚    Volume of Defects" A whistleblower has revealed that Boeing's  โ”‚
โ”‚          โ”‚    Max 9 production line is riddled with an "enormous volume of   โ”‚
โ”‚          โ”‚    defects," with instances where bolts were not installed. The   โ”‚
โ”‚          โ”‚    story has garnered attention with a score of 140. Read more    โ”‚
โ”‚          โ”‚  2 Arno A. Penzias, 90, Dies; Nobel Physicist Confirmed Big Bang  โ”‚
โ”‚          โ”‚    Theory Arno A. Penzias, a Nobel Prize-winning physicist known  โ”‚
โ”‚          โ”‚    for his work that confirmed the Big Bang Theory, has passed    โ”‚
โ”‚          โ”‚    away at the age of 90. His contributions to science have been  โ”‚
โ”‚          โ”‚    significant, leading to discussions and tributes in the        โ”‚
โ”‚          โ”‚    scientific community. The news has a score of 207. Read more   โ”‚
โ”‚          โ”‚  3 Why the fuck are we templating YAML? (2019) This provocative   โ”‚
โ”‚          โ”‚    article from 2019 questions the proliferation of YAML          โ”‚
โ”‚          โ”‚    templating in software, sparking a larger conversation about   โ”‚
โ”‚          โ”‚    the complexities and potential pitfalls of this practice. With โ”‚
โ”‚          โ”‚    a substantial score of 149, it remains a hot topic of debate.  โ”‚
โ”‚          โ”‚    Read more                                                      โ”‚
โ”‚          โ”‚  4 Forging signed commits on GitHub Researchers have discovered a โ”‚
โ”‚          โ”‚    method for forging signed commits on GitHub which is causing   โ”‚
โ”‚          โ”‚    concern within the tech community about the implications for   โ”‚
โ”‚          โ”‚    code security and integrity. The story has a current score of  โ”‚
โ”‚          โ”‚    94. Read more                                                  โ”‚
โ”‚          โ”‚  5 Qdrant, the Vector Search Database, raised $28M in a Series A  โ”‚
โ”‚          โ”‚    round Qdrant, a company specializing in vector search          โ”‚
โ”‚          โ”‚    databases, has successfully raised $28 million in a Series A   โ”‚
โ”‚          โ”‚    funding round. This financial milestone indicates growing      โ”‚
โ”‚          โ”‚    interest and confidence in their technology. The story has     โ”‚
โ”‚          โ”‚    attracted attention with a score of 55. Read more              โ”‚
โ•ฐโ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”ดโ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ•ฏ

Assistant that generates pydantic models

One of our favorite features is generating structured data (i.e. a pydantic model) from sparse information. Meaning we can use Assistants to return pydantic models and generate content which previously could not be possible. In this example, our movie assistant generates an object of the MovieScript class.

  • Create a file pydantic_assistant.py
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 people write movie ideas.",
    output_model=MovieScript,
)

pprint(movie_assistant.run("New York"))
  • Run the pydantic_assistant.py file
python pydantic_assistant.py
  • See how the assistant generates a structured output
MovieScript(
โ”‚   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.'
)

A PDF Assistant with Knowledge & Storage

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 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="ai",
    pg_password="ai",
    pg_database="ai",
    debug_mode=True,
)

# -*- DockerResources
dev_docker_resources = DockerResources(apps=[vector_db])
  • Start PgVector using
phi start resources.py -y
  1. Create PDF Assistant
  • Create a file pdf_assistant.py
import typer
from rich.prompt import Prompt
from typing import Optional, List
from phi.assistant import Assistant
from phi.storage.assistant.postgres import PgAssistantStorage
from phi.knowledge.pdf import PDFUrlKnowledgeBase
from phi.vectordb.pgvector import PgVector2

from resources import vector_db

knowledge_base = PDFUrlKnowledgeBase(
    urls=["https://phi-public.s3.amazonaws.com/recipes/ThaiRecipes.pdf"],
    vector_db=PgVector2(
        collection="recipes",
        db_url=vector_db.get_db_connection_local(),
    ),
)
# Comment out after first run
knowledge_base.load(recreate=False)

storage = PgAssistantStorage(
    table_name="pdf_assistant",
    db_url=vector_db.get_db_connection_local(),
)


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(
        run_id=run_id,
        user_id=user,
        knowledge_base=knowledge_base,
        storage=storage,
        # use_tools=True adds functions to
        # search the knowledge base and chat history
        use_tools=True,
        show_tool_calls=True,
        # Uncomment the following line to use traditional RAG
        # add_references_to_prompt=True,
    )
    if run_id is None:
        run_id = assistant.run_id
        print(f"Started Run: {run_id}\n")
    else:
        print(f"Continuing Run: {run_id}\n")

    while True:
        message = Prompt.ask(f"[bold] :sunglasses: {user} [/bold]")
        if message in ("exit", "bye"):
            break
        assistant.print_response(message, markdown=True)

if __name__ == "__main__":
    typer.run(pdf_assistant)
  1. Install libraries
pip install -U pgvector pypdf psycopg sqlalchemy
  1. Run PDF Assistant
python pdf_assistant.py
  • Ask a question:
How do I make pad thai?
  • See how the Assistant searches the knowledge base and returns a response.
Show output
Started Run: d28478ea-75ed-4710-8191-22564ebfb140

INFO     Loading knowledge base
INFO     Reading:
         https://www.family-action.org.uk/content/uploads/2019/07/meals-more-recipes.pdf
INFO     Loaded 82 documents to knowledge base
 ๐Ÿ˜Ž user : How do I make chicken tikka salad?
โ•ญโ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”ฌโ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ•ฎ
โ”‚ Message  โ”‚ How do I make chicken tikka salad?                                              โ”‚
โ”œโ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”ผโ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”ค
โ”‚ Response โ”‚                                                                                 โ”‚
โ”‚ (7.2s)   โ”‚  โ€ข Running: search_knowledge_base(query=chicken tikka salad)                    โ”‚
โ”‚          โ”‚                                                                                 โ”‚
โ”‚          โ”‚ I found a recipe for Chicken Tikka Salad that serves 2. Here are the            โ”‚
โ”‚          โ”‚ ingredients and steps:                                                          โ”‚
โ”‚          โ”‚                                                                                 โ”‚
โ”‚          โ”‚ Ingredients:                                                                    โ”‚

...
  • Message bye to exit, start the assistant again using python pdf_assistant.py and ask:
What was my last message?

See how the assistant now maintains storage across sessions.

  • Run the pdf_assistant.py file with the --new flag to start a new run.
python pdf_assistant.py --new
  1. Stop PgVector

Play around and then stop PgVector using phi stop resources.py

phi stop resources.py -y

Build an AI App using Streamlit, FastApi and PgVector

Let's build an AI App using GPT-4 as the LLM, Streamlit as the chat interface, FastApi as the API and PgVector for knowledge and storage. Read the full tutorial here.

Create your codebase

Create your codebase using the ai-app template

phi ws create -t ai-app -n ai-app

This will create a folder ai-app with a pre-built AI App that you can customize and make your own.

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.

PDF Assistant

  • Open localhost:8501 to view streamlit apps that you can customize and make your own.
  • Click on PDF Assistant in the sidebar
  • Enter a username and wait for the knowledge base to load.
  • Choose either the RAG or Autonomous Assistant type.
  • Ask "How do I make pad thai?"
  • Upload PDFs and ask questions

We provide a default PDF of ThaiRecipes that you can clear using the Clear Knowledge Base button. The PDF is only for testing.

chat-with-pdf

Optional: 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 AI App comes ready-to-use with FastApi endpoints.

  • Update the workspace/settings.py file and set dev_api_enabled=True
...
ws_settings = WorkspaceSettings(
    ...
    # Uncomment the following line
    dev_api_enabled=True,
...
  • 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/assitants/load-knowledge-base

  • Test the v1/assitants/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 ai-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

Stop the workspace

Play around and stop the workspace using:

phi ws down

Run your AI App on AWS

Read how to run your AI App on AWS.

Checkout the cookbook for more examples

Documentation

AI Applications

After building an Assistant, serve it using Streamlit, FastApi or Django to build your AI application. Instead of wiring tools manually, phidata provides pre-built templates for AI Apps that you can run locally or deploy to AWS with 1 command. Here's how they work:

  • Create your AI App using a template: phi ws create
  • Run your app locally: phi ws up
  • Run your app on AWS: phi ws up prd:aws

Building AI for your product?

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

  1. Train an assistant with proprietary data to perform tasks specific to your product.
  2. Connect your product to the assistant via an API.
  3. Customize, Monitor and Improve the AI.

We provide dedicated support and development for AI products. Book a call to get started.

Contributions

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.

Roadmap

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

ๆœ€่ฟ‘็‰ˆๆœฌๆ›ดๆ–ฐ:(ๆ•ฐๆฎๆ›ดๆ–ฐไบŽ 2024-02-28 04:07:56)

2024-02-27 20:23:33 v2.3.44

2024-02-27 03:57:46 v2.3.43

2024-02-22 06:12:55 v2.3.42

2024-02-20 22:49:30 v2.3.41.1

2024-02-20 20:53:42 v2.3.41

2024-02-19 21:52:55 v2.3.40

2024-02-15 08:06:46 v2.3.39

2024-02-15 04:59:49 v2.3.38

2024-02-14 04:51:58 v2.3.37

2024-02-13 00:25:22 v2.3.36

ไธป้ข˜(topics):

ai, aws, data-engineering, developer-tools, docker, gpt-4, llm, llmops, python

phidatahq/phidataๅŒ่ฏญ่จ€ Pythonๆœ€่ฟ‘ๆ›ดๆ–ฐไป“ๅบ“

2024-03-05 01:41:56 langgenius/dify

2024-03-04 20:42:01 jxxghp/MoviePilot

2024-03-04 12:37:13 Azure/azure-sdk-for-python

2024-03-03 18:39:59 xtekky/gpt4free

2024-03-03 17:25:46 linruowuyin/Fhoe-Rail

2024-03-02 23:26:36 python-poetry/poetry