Phidata 是一个用于构建多模态代理的框架,使用 phidata 可以:使用内存、知识、工具和推理构建多模式代理。建立可以协同工作解决问题的代理团队。使用漂亮的 Agent UI 与您的代理聊天。
16200 Stars 2200 Forks 28 Issues 82 贡献者 MPL-2.0 License Python 语言
代码: GitHub - phidatahq/phidata: Build multi-modal Agents with memory, knowledge, tools and reasoning. Chat with them using a beautiful Agent UI.
主页: https://docs.phidata.com/
AI开源软件站:AI开源 - 小众AI
主要功能
- 简单而优雅
- 强大且灵活
- 默认为 Multi-Modal
- 多代理编排
- 漂亮的代理 UI,与您的代理聊天
- 内置 Agentic RAG
- 结构化输出
- 推理代理
- 内置监控和调试功能
安装和使用
简单而优雅
Phidata 代理简单而优雅,从而产生最小、美观的代码。
例如,您可以在 10 行代码中创建一个 Web 搜索代理,创建一个文件web_search.py
from phi.agent import Agent
from phi.model.openai import OpenAIChat
from phi.tools.duckduckgo import DuckDuckGo
web_agent = Agent(
model=OpenAIChat(id="gpt-4o"),
tools=[DuckDuckGo()],
instructions=["Always include sources"],
show_tool_calls=True,
markdown=True,
)
web_agent.print_response("Tell me about OpenAI Sora?", stream=True)
安装库,导出并运行代理:OPENAI_API_KEY
pip install phidata openai duckduckgo-search
export OPENAI_API_KEY=sk-xxxx
python web_search.py
强大且灵活
Phidata 代理可以使用多种工具并按照说明完成复杂的任务。
例如,您可以使用查询财务数据的工具创建财务代理,创建文件finance_agent.py
from phi.agent import Agent
from phi.model.openai import OpenAIChat
from phi.tools.yfinance import YFinanceTools
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("Summarize analyst recommendations for NVDA", stream=True)
安装库并运行代理:
pip install yfinance
python finance_agent.py
默认为 Multi-Modal
Phidata 代理支持文本、图像、音频和视频。
例如,您可以创建一个可以理解图像并根据需要进行工具调用的映像代理,创建一个文件image_agent.py
from phi.agent import Agent
from phi.model.openai import OpenAIChat
from phi.tools.duckduckgo import DuckDuckGo
agent = Agent(
model=OpenAIChat(id="gpt-4o"),
tools=[DuckDuckGo()],
markdown=True,
)
agent.print_response(
"Tell me about this image and give me the latest news about it.",
images=["https://upload.wikimedia.org/wikipedia/commons/b/bf/Krakow_-_Kosciol_Mariacki.jpg"],
stream=True,
)
运行代理:
python image_agent.py
多代理编排
Phidata 代理可以作为一个团队一起工作来完成复杂的任务,创建文件agent_team.py
from phi.agent import Agent
from phi.model.openai import OpenAIChat
from phi.tools.duckduckgo import DuckDuckGo
from phi.tools.yfinance import YFinanceTools
web_agent = Agent(
name="Web Agent",
role="Search the web for information",
model=OpenAIChat(id="gpt-4o"),
tools=[DuckDuckGo()],
instructions=["Always include sources"],
show_tool_calls=True,
markdown=True,
)
finance_agent = Agent(
name="Finance Agent",
role="Get financial data",
model=OpenAIChat(id="gpt-4o"),
tools=[YFinanceTools(stock_price=True, analyst_recommendations=True, company_info=True)],
instructions=["Use tables to display data"],
show_tool_calls=True,
markdown=True,
)
agent_team = Agent(
team=[web_agent, finance_agent],
model=OpenAIChat(id="gpt-4o"),
instructions=["Always include sources", "Use tables to display data"],
show_tool_calls=True,
markdown=True,
)
agent_team.print_response("Summarize analyst recommendations and share the latest news for NVDA", stream=True)
运行 Agent 团队:
python agent_team.py
漂亮的代理 UI,与您的代理聊天
Phidata 提供了一个漂亮的 UI,用于与您的代理交互。让我们试一试,创建一个文件playground.py
注意
Phidata 不存储任何数据,所有代理数据都存储在本地 sqlite 数据库中。
from phi.agent import Agent
from phi.model.openai import OpenAIChat
from phi.storage.agent.sqlite import SqlAgentStorage
from phi.tools.duckduckgo import DuckDuckGo
from phi.tools.yfinance import YFinanceTools
from phi.playground import Playground, serve_playground_app
web_agent = Agent(
name="Web Agent",
model=OpenAIChat(id="gpt-4o"),
tools=[DuckDuckGo()],
instructions=["Always include sources"],
storage=SqlAgentStorage(table_name="web_agent", db_file="agents.db"),
add_history_to_messages=True,
markdown=True,
)
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"],
storage=SqlAgentStorage(table_name="finance_agent", db_file="agents.db"),
add_history_to_messages=True,
markdown=True,
)
app = Playground(agents=[finance_agent, web_agent]).get_app()
if __name__ == "__main__":
serve_playground_app("playground:app", reload=True)
通过运行以下命令使用 phidata 进行身份验证:
phi auth
或者从 中导出 for your workspace phidata.appPHI_API_KEY
export PHI_API_KEY=phi-***
安装依赖项并运行 Agent Playground:
pip install 'fastapi[standard]' sqlalchemy
python playground.py
- 打开提供的链接或导航到http://phidata.app/playground
- 选择终端节点并开始与您的代理聊天!localhost:7777
- AgentPlayground.mp4
代理 RAG
我们是第一个使用我们的 Auto-RAG 范式开创 Agentic RAG 的公司。使用 Agentic RAG(或 auto-rag),Agent 可以在其知识库(向量数据库)中搜索完成任务所需的特定信息,而不是总是在提示中插入 “context”。
这样可以节省令牌并提高响应质量。创建文件rag_agent.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
# 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()
agent = Agent(
model=OpenAIChat(id="gpt-4o"),
# Add the knowledge base to the agent
knowledge=knowledge_base,
show_tool_calls=True,
markdown=True,
)
agent.print_response("How do I make chicken and galangal in coconut milk soup", stream=True)
安装库并运行代理:
pip install lancedb tantivy pypdf sqlalchemy
python rag_agent.py
结构化输出
代理可以将其输出以结构化格式作为 Pydantic 模型返回。
创建文件structured_output.py
from typing import List
from pydantic import BaseModel, Field
from phi.agent import Agent
from phi.model.openai import OpenAIChat
# Define a Pydantic model to enforce the structure of the output
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!")
# Agent that uses JSON mode
json_mode_agent = Agent(
model=OpenAIChat(id="gpt-4o"),
description="You write movie scripts.",
response_model=MovieScript,
)
# Agent that uses structured outputs
structured_output_agent = Agent(
model=OpenAIChat(id="gpt-4o"),
description="You write movie scripts.",
response_model=MovieScript,
structured_outputs=True,
)
json_mode_agent.print_response("New York")
structured_output_agent.print_response("New York")
- 运行文件structured_output.py
python structured_output.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.'
)
推理代理(实验性)
推理可帮助代理逐步解决问题,根据需要回溯和纠正。创建文件 .reasoning_agent.py
from phi.agent import Agent
from phi.model.openai import OpenAIChat
task = (
"Three missionaries and three cannibals need to cross a river. "
"They have a boat that can carry up to two people at a time. "
"If, at any time, the cannibals outnumber the missionaries on either side of the river, the cannibals will eat the missionaries. "
"How can all six people get across the river safely? Provide a step-by-step solution and show the solutions as an ascii diagram"
)
reasoning_agent = Agent(model=OpenAIChat(id="gpt-4o"), reasoning=True, markdown=True, structured_outputs=True)
reasoning_agent.print_response(task, stream=True, show_full_reasoning=True)
运行 Reasoning Agent:
python reasoning_agent.py
警告
Reasoning 是一个实验性功能,在 ~20% 的时间内会中断。**它不是 o1 的替代品。**
这是一个由好奇心推动的实验,结合了 COT 和工具使用。对于此初始版本,请将您的期望设置得非常低。例如:它将无法计算 'strawberry' 中的 'r'。