MLMs之Agent:Phidata的简介、安装和使用方法、案例应用之详细攻略
目录
Phidata简介
2023年11月,Phidata是一个用于构建多模态智能体的框架,它允许你构建具有记忆、知识、工具和推理能力的多模态智能体,并通过一个美观的Agent UI与它们进行交互。Phidata 的核心优势在于其简洁优雅的设计、强大的灵活性和多模态特性,支持文本、图像、音频和视频等多种数据类型。它还支持多智能体协同工作,内置Agentic RAG(基于智能体的检索增强生成)功能,并提供结构化输出和推理能力。Phidata 不存储任何数据,所有智能体数据都存储在本地 SQLite 数据库中。
Phidata 提供了一个功能强大且易于使用的框架,用于构建各种类型的多模态智能体,并支持多种高级功能,例如多 Agent 协同、Agentic RAG 和结构化输出。 其提供的示例和文档也十分完善,方便用户快速上手和进行开发。
GitHub地址:https://github.com/phidatahq/phidata
pip install -U phidata
Phidata安装和使用方法
1、安装
安装 Phidata 非常简单,只需使用 pip 命令:
pip install -U phidata
pip install phidata openai duckduckgo-search
2、使用方法
Phidata 的使用方法主要体现在构建和运行各种类型的 Agent。以下是一些示例:
(1)、认证
在使用 Phidata Playground 之前,需要进行身份验证。可以通过运行 phi auth 命令或从 phidata.app 导出 PHI_API_KEY 环境变量来完成身份验证。
(2)、创建 Agent
Phidata 提供了丰富的 API 来创建各种功能的 Agent。 核心在于使用 phi.agent.Agent 类,并配置其模型、工具、指令、存储等参数。例如,创建一个简单的网络搜索 Agent:
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)
这段代码创建了一个名为 web_agent 的 Agent,使用 OpenAI 的 gpt-4o 模型,并使用了 DuckDuckGo 作为搜索工具。instructions 指定了 Agent 的指令,show_tool_calls 和 markdown 控制输出格式。
(3)、运行 Agent
创建 Agent 后,可以直接运行其 print_response 方法来与 Agent 交互。 需要安装相应的库,例如 openai 和 duckduckgo-search。 运行代码前,请确保已设置 OPENAI_API_KEY 环境变量。
export OPENAI_API_KEY=sk-xxxx
python web_search.py
(4)、Agent Playground
Phidata 提供了一个 Agent Playground,用于方便地与多个 Agent 交互。 需要运行 playground.py 文件启动 Playground,然后访问指定的 URL (例如 http://localhost:7777) 与 Agent 进行交互。
Phidata 案例应用
Phidata 提供了多个案例,展示了其强大的功能:
1、多工具 Agent
Phidata Agent 可以使用多个工具并遵循指令来完成复杂的任务。例如,一个金融 Agent 可以使用 YFinanceTools 查询金融数据:
# 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)
2、多模态 Agent
Phidata Agent 默认支持文本、图像、音频和视频等多种模态。例如,一个图像 Agent 可以理解图像并根据需要进行工具调用:
# 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,
)
3、多 Agent 协同
Phidata Agent 可以协同工作以完成复杂的任务。例如,一个团队由网络搜索 Agent 和金融 Agent 组成,共同完成任务:
# 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)
4、Agentic RAG
Phidata 引入了 Agentic RAG,允许 Agent 在其知识库中搜索特定信息,从而提高响应质量并节省 token。
# 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)
5、结构化输出
Agent 可以返回结构化输出,例如使用 Pydantic 模型:
# structured_output.py 的代码 (已在问题描述中提供)
6、推理 Agent
Phidata 提供了实验性的推理功能,帮助 Agent 分步解决问题。
# reasoning_agent.py 的代码 (已在问题描述中提供)
7、Python 代码 Agent
PythonAgent 可以编写和运行 Python 代码来完成任务。
# python_agent.py 的代码 (已在问题描述中提供)
8、SQL 数据分析 Agent
DuckDbAgent 可以使用 SQL 进行数据分析。
# data_analyst.py 的代码 (已在问题描述中提供)
9、监控和调试
Phidata 内置了监控和调试功能,方便用户跟踪 Agent 的运行状态和调试问题。