Skip to content

用记忆、知识和工具构建人工智能助手

License

Notifications You must be signed in to change notification settings

yuanxiaoming8899/phidata

 
 

Repository files navigation

菲数据

用记忆、知识和工具构建人工智能助手

图像

什么是phidata?

Phidata 是一个用于构建自主助手(又名代理)的框架,该助手具有长期记忆、上下文知识以及使用函数调用采取行动的能力。

为什么是phidata?

问题:法学硕士的背景有限,无法采取行动。

解决方案:添加记忆、知识和工具。

  • 内存:将聊天记录存储在数据库中,使法学硕士能够进行长期对话。
  • 知识:将信息存储在矢量数据库中,并为法学硕士提供业务背景
  • 工具:使法学硕士能够采取从 API 提取数据、发送电子邮件或查询数据库等操作。

怎么运行的

  • 第 1 步:创建一个Assistant
  • 步骤2:添加工具(功能)、知识(vectordb)和存储(数据库)
  • 第 3 步:使用 Streamlit、FastApi 或 Django 构建您的 AI 应用程序

安装

pip install -U phidata

快速入门:可以搜索网页的助手

创建文件assistant.py

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)

  <svg aria-hidden="true" height="16" viewBox="0 0 16 16" version="1.1" width="16" data-view-component="true" class="octicon octicon-copy js-clipboard-copy-icon">
<path d="M0 6.75C0 5.784.784 5 1.75 5h1.5a.75.75 0 0 1 0 1.5h-1.5a.25.25 0 0 0-.25.25v7.5c0 .138.112.25.25.25h7.5a.25.25 0 0 0 .25-.25v-1.5a.75.75 0 0 1 1.5 0v1.5A1.75 1.75 0 0 1 9.25 16h-7.5A1.75 1.75 0 0 1 0 14.25Z"></path><path d="M5 1.75C5 .784 5.784 0 6.75 0h7.5C15.216 0 16 .784 16 1.75v7.5A1.75 1.75 0 0 1 14.25 11h-7.5A1.75 1.75 0 0 1 5 9.25Zm1.75-.25a.25.25 0 0 0-.25.25v7.5c0 .138.112.25.25.25h7.5a.25.25 0 0 0 .25-.25v-7.5a.25.25 0 0 0-.25-.25Z"></path>

文档和支持

例子

  • LLM OS:使用LLM作为新兴操作系统的CPU。
  • 自主 RAG:为法学硕士提供搜索其知识、网络或聊天历史记录的工具。
  • 本地 RAG:带有 Ollama 和 PgVector 的完全本地 RAG。
  • 投资研究员:使用 Llama3 和 Groq 生成股票投资报告。
  • 新闻文章:使用 Llama3 和 Groq 撰写新闻文章。
  • 视频摘要:使用 Llama3 和 Groq 的 YouTube 视频摘要。
  • 研究助理:使用 Llama3 和 Groq 撰写研究报告。

可以编写和运行Python代码的助手

显示详细资料

可以PythonAssistant通过编写和运行Python代码来完成任务。

  • 创建文件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)

<clipboard-copy aria-label="Copy" class="ClipboardButton btn btn-invisible js-clipboard-copy m-2 p-0 tooltipped-no-delay d-flex flex-justify-center flex-items-center" data-copy-feedback="Copied!" data-tooltip-direction="w" value="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)" tabindex="0" role="button">

  • 安装 pandas 并运行python_assistant.py
pip install pandas

python python_assistant.py

<clipboard-copy aria-label="Copy" class="ClipboardButton btn btn-invisible js-clipboard-copy m-2 p-0 tooltipped-no-delay d-flex flex-justify-center flex-items-center" data-copy-feedback="Copied!" data-tooltip-direction="w" value="pip install pandas

python python_assistant.py" tabindex="0" role="button">

可以使用SQL分析数据的助手

显示详细资料

可以DuckDbAssistant使用 SQL 执行数据分析。

  • 创建文件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)

<clipboard-copy aria-label="Copy" class="ClipboardButton btn btn-invisible js-clipboard-copy m-2 p-0 tooltipped-no-delay d-flex flex-justify-center flex-items-center" data-copy-feedback="Copied!" data-tooltip-direction="w" value="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)" tabindex="0" role="button">

  • 安装duckdb并运行data_assistant.py文件
pip install duckdb

python data_assistant.py

<clipboard-copy aria-label="Copy" class="ClipboardButton btn btn-invisible js-clipboard-copy m-2 p-0 tooltipped-no-delay d-flex flex-justify-center flex-items-center" data-copy-feedback="Copied!" data-tooltip-direction="w" value="pip install duckdb

python data_assistant.py" tabindex="0" role="button">

可以生成pydantic模型的助手

显示详细资料

我们最喜欢的法学硕士功能之一是从文本生成结构化数据(即 pydantic 模型)。使用此功能可以提取特征、生成电影脚本、产生假数据等。

让我们创建一个电影助手来MovieScript为我们编写一个。

  • 创建文件movie_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 write movie scripts.", output_model=MovieScript, )

pprint(movie_assistant.run("New York"))

  <svg aria-hidden="true" height="16" viewBox="0 0 16 16" version="1.1" width="16" data-view-component="true" class="octicon octicon-copy js-clipboard-copy-icon">
<path d="M0 6.75C0 5.784.784 5 1.75 5h1.5a.75.75 0 0 1 0 1.5h-1.5a.25.25 0 0 0-.25.25v7.5c0 .138.112.25.25.25h7.5a.25.25 0 0 0 .25-.25v-1.5a.75.75 0 0 1 1.5 0v1.5A1.75 1.75 0 0 1 9.25 16h-7.5A1.75 1.75 0 0 1 0 14.25Z"></path><path d="M5 1.75C5 .784 5.784 0 6.75 0h7.5C15.216 0 16 .784 16 1.75v7.5A1.75 1.75 0 0 1 14.25 11h-7.5A1.75 1.75 0 0 1 5 9.25Zm1.75-.25a.25.25 0 0 0-.25.25v7.5c0 .138.112.25.25.25h7.5a.25.25 0 0 0 .25-.25v-7.5a.25.25 0 0 0-.25-.25Z"></path>
  • 运行movie_assistant.py文件
python movie_assistant.py
  • 输出是该类的一个对象MovieScript,如下所示:
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.'
)

PDF 知识和存储助手

显示详细资料

让我们创建一个可以回答 PDF 问题的 PDF 助手。我们将用于PgVector知识和存储。

知识库:助手可以搜索以改进其响应的信息(使用矢量数据库)。

存储:为助手提供长期记忆(使用数据库)。

  1. 运行 PgVector

安装docker 桌面并使用以下命令在端口5532上运行PgVector

docker run -d \
  -e POSTGRES_DB=ai \
  -e POSTGRES_USER=ai \
  -e POSTGRES_PASSWORD=ai \
  -e PGDATA=/var/lib/postgresql/data/pgdata \
  -v pgvolume:/var/lib/postgresql/data \
  -p 5532:5432 \
  --name pgvector \
  phidata/pgvector:16

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

<span class="pl-k">if</span> <span class="pl-c1">not</span> <span class="pl-s1">new</span>:
    <span class="pl-s1">existing_run_ids</span>: <span class="pl-v">List</span>[<span class="pl-s1">str</span>] <span class="pl-c1">=</span> <span class="pl-s1">storage</span>.<span class="pl-en">get_all_run_ids</span>(<span class="pl-s1">user</span>)
    <span class="pl-k">if</span> <span class="pl-en">len</span>(<span class="pl-s1">existing_run_ids</span>) <span class="pl-c1">&gt;</span> <span class="pl-c1">0</span>:
        <span class="pl-s1">run_id</span> <span class="pl-c1">=</span> <span class="pl-s1">existing_run_ids</span>[<span class="pl-c1">0</span>]

<span class="pl-s1">assistant</span> <span class="pl-c1">=</span> <span class="pl-v">Assistant</span>(
    <span class="pl-s1">run_id</span><span class="pl-c1">=</span><span class="pl-s1">run_id</span>,
    <span class="pl-s1">user_id</span><span class="pl-c1">=</span><span class="pl-s1">user</span>,
    <span class="pl-s1">knowledge_base</span><span class="pl-c1">=</span><span class="pl-s1">knowledge_base</span>,
    <span class="pl-s1">storage</span><span class="pl-c1">=</span><span class="pl-s1">storage</span>,
    <span class="pl-c"># Show tool calls in the response</span>
    <span class="pl-s1">show_tool_calls</span><span class="pl-c1">=</span><span class="pl-c1">True</span>,
    <span class="pl-c"># Enable the assistant to search the knowledge base</span>
    <span class="pl-s1">search_knowledge</span><span class="pl-c1">=</span><span class="pl-c1">True</span>,
    <span class="pl-c"># Enable the assistant to read the chat history</span>
    <span class="pl-s1">read_chat_history</span><span class="pl-c1">=</span><span class="pl-c1">True</span>,
)
<span class="pl-k">if</span> <span class="pl-s1">run_id</span> <span class="pl-c1">is</span> <span class="pl-c1">None</span>:
    <span class="pl-s1">run_id</span> <span class="pl-c1">=</span> <span class="pl-s1">assistant</span>.<span class="pl-s1">run_id</span>
    <span class="pl-en">print</span>(<span class="pl-s">f"Started Run: <span class="pl-s1"><span class="pl-kos">{</span><span class="pl-s1">run_id</span><span class="pl-kos">}</span></span><span class="pl-cce">\n</span>"</span>)
<span class="pl-k">else</span>:
    <span class="pl-en">print</span>(<span class="pl-s">f"Continuing Run: <span class="pl-s1"><span class="pl-kos">{</span><span class="pl-s1">run_id</span><span class="pl-kos">}</span></span><span class="pl-cce">\n</span>"</span>)

<span class="pl-c"># Runs the assistant as a cli app</span>
<span class="pl-s1">assistant</span>.<span class="pl-en">cli_app</span>(<span class="pl-s1">markdown</span><span class="pl-c1">=</span><span class="pl-c1">True</span>)

if name == "main": typer.run(pdf_assistant)

<clipboard-copy aria-label="Copy" class="ClipboardButton btn btn-invisible js-clipboard-copy m-2 p-0 tooltipped-no-delay d-flex flex-justify-center flex-items-center" data-copy-feedback="Copied!" data-tooltip-direction="w" value="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

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

knowledge_base = PDFUrlKnowledgeBase( urls=["https://phi-public.s3.amazonaws.com/recipes/ThaiRecipes.pdf"], vector_db=PgVector2(collection="recipes", db_url=db_url), )

Comment out after first run

knowledge_base.load()

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(
    run_id=run_id,
    user_id=user,
    knowledge_base=knowledge_base,
    storage=storage,
    # Show tool calls in the response
    show_tool_calls=True,
    # Enable the assistant to search the knowledge base
    search_knowledge=True,
    # Enable the assistant to read the chat history
    read_chat_history=True,
)
if run_id is None:
    run_id = assistant.run_id
    print(f&quot;Started Run: {run_id}\n&quot;)
else:
    print(f&quot;Continuing Run: {run_id}\n&quot;)

# Runs the assistant as a cli app
assistant.cli_app(markdown=True)

if name == "main": typer.run(pdf_assistant)" tabindex="0" role="button">

  1. 安装库
pip install -U pgvector pypdf "psycopg[binary]" sqlalchemy
  1. 运行PDF助手
python pdf_assistant.py
  • 问一个问题:
How do I make pad thai?
  • 了解助手如何搜索知识库并返回响应。

  • 消息bye退出,再次启动助手python pdf_assistant.py并询问:

What was my last message?

了解助手现在如何跨会话维护存储。

  • 运行pdf_assistant.py带有--new标志的文件以开始新的运行。
python pdf_assistant.py --new

查看食谱以获取更多示例。

下一步

  1. 阅读基础知识以了解有关 phidata 的更多信息。
  2. 了解助手以及如何自定义它们。
  3. 查看食谱以获取深入的示例和代码。

演示

查看以下使用 phidata 构建的 AI 应用程序:

  • PDF AI总结并回答 PDF 中的问题。
  • ArXiv AI使用 ArXiv API 回答有关 ArXiv 论文的问题。
  • HackerNews AI总结故事、用户并分享 HackerNews 的最新动态。

教程

使用 gpt-4o 构建 LLM 操作系统

自主RAG

本地 RAG 与 Llama3

由 Groq 提供支持的 Llama3 研究助理

想要构建人工智能产品?

我们已经帮助很多公司打造了AI产品,大致流程是:

  1. 使用专有数据构建助手来执行特定于您的产品的任务。
  2. 通过 API将您的产品连接到 Assistant。
  3. 监控和改进您的人工智能产品。

我们还提供专门的支持和开发,请致电预订即可开始。

贡献

我们是一个开源项目,欢迎贡献,请阅读贡献指南以获取更多信息。

请求功能

  • 如果您有功能请求,请提出问题或提出拉取请求。
  • 如果您对我们如何改进有任何想法,请发起讨论。

路线图

我们的路线图可以在这里找到。如果您有功能请求,请打开问题/讨论。

About

用记忆、知识和工具构建人工智能助手

Resources

License

Code of conduct

Stars

Watchers

Forks

Releases

No releases published

Packages

No packages published

Languages

  • Python 99.6%
  • Other 0.4%