下面是一些使用phidata構建Agent的示例:

網頁搜索Agent

這個Agent可以在網上搜索信息來回答問題。核心代碼如下:

from phi.agent import Agent  
from phi.model.openai import OpenAIChat
from phi.tools.duckduckgo import DuckDuckGo

web_agent = Agent(
name="Web Agent",
role="Search the web for information",
model=OpenAIChat(id="gpt-4o"),
tools=[DuckDuckGo()],
markdown=True,
show_tool_calls=True,
)
web_agent.print_response("Whats happening in France?", stream=True)

通過組合OpenAI的語言模型和DuckDuckGo搜索引擎,就可以創建一個可以在網上查找信息的Agent。當詢問它”法國最近發生了什么”,它會通過搜索引擎查找相關新聞,再用自然語言回答。

金融數據Agent

這個Agent專門用于查詢金融數據,核心代碼:

from phi.agent import Agent
from phi.model.openai import OpenAIChat
from phi.tools.yfinance import YFinanceTools

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, company_news=True)],
instructions=["Always use tables to display data"],
markdown=True,
show_tool_calls=True,
)
finance_agent.print_response("Share analyst recommendations for NVDA", stream=True)

它使用yfinance庫獲取金融數據,當詢問”分享NVDA股票的分析師評級”時,它會查詢相關數據,并以表格的形式展示分析師的評級信息。

Agent協作

Phidata還支持多個Agent協同工作,解決更復雜的問題。比如我們可以讓上面的網頁搜索Agent和金融數據Agent組成一個團隊:

agent_team = Agent(
team=[web_agent, finance_agent],
show_tool_calls=True,
markdown=True,
)
agent_team.print_response("Research the web for NVDA and share analyst recommendations", stream=True)

當詢問”在網上搜索NVDA公司的信息,并分享分析師的評級”,這個Agent團隊會先用網頁搜索Agent查找NVDA公司的背景信息,再用金融數據Agent獲取分析師評級,最后整合成完整的回答。

基于知識庫的Agent

普通的Agent每次都需要將所有的背景知識放入prompt中,但這樣會占用大量token。phidata的RAG(Retrieval Augmented Generation)Agent可以將背景知識存入向量數據庫,每次只檢索最相關的少量知識,大大節省token用量,還能提高回答質量。

下面的例子展示了如何從一個菜譜PDF文件創建知識庫,構建一個美食問答Agent:

from phi.agent import Agent
from phi.model.openai import OpenAIChat
from phi.knowledge.pdf import PDFUrlKnowledgeBase
from phi.vectordb.lancedb import LanceDb, SearchType

db_uri = "tmp/lancedb"
# 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=db_uri, search_type=SearchType.vector),
)
# Load the knowledge base: Comment out after first run
knowledge_base.load(upsert=True)

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")

當詢問”如何制作椰汁雞肉湯”,Agent會從知識庫中檢索雞肉湯的菜譜,再根據菜譜步驟回答問題。這樣不僅節省token,回答的質量也更高。

其他功能

除了上述核心功能,phidata還提供了:

Phidata目前仍在快速迭代中,歡迎大家在GitHub上貢獻代碼,或在Discord社區中交流討論。

總之,phidata是一個功能強大,涵蓋全流程的智能Agent開發框架。對于想要快速構建和部署聊天機器人、智能助手、問答系統等AI應用的開發者非常友好,值得嘗試!

文章轉自微信公眾號@機器學習算法與Python實戰

上一篇:

OpenAI Assistants API 使用指南

下一篇:

原創 | OpenAPI 標準規范
#你可能也喜歡這些API文章!

我們有何不同?

API服務商零注冊

多API并行試用

數據驅動選型,提升決策效率

查看全部API→
??

熱門場景實測,選對API

#AI文本生成大模型API

對比大模型API的內容創意新穎性、情感共鳴力、商業轉化潛力

25個渠道
一鍵對比試用API 限時免費

#AI深度推理大模型API

對比大模型API的邏輯推理準確性、分析深度、可視化建議合理性

10個渠道
一鍵對比試用API 限時免費