將 Milvus 與 Agno 整合
Agno(前身為 Phidata) 是用於建立多模態代理的輕量級函式庫。它允許您建立能理解文字、圖像、音訊和視訊的多模態代理,並利用各種工具和知識來源來完成複雜的任務。Agno 支援多代理協調,讓代理團隊能夠協同合作,共同解決問題。它也提供了美觀的 Agent UI 用來與您的代理進行互動。
Milvus 向量資料庫可有效儲存及檢索嵌入的資訊。有了 Milvus 和 Agno,您可以輕鬆地將您的知識整合到您的 Agent 工作流程中。本文件是如何使用 Milvus 與 Agno 整合的基本指南。
準備工作
安裝必要的相依性:
$ pip install --upgrade agno pymilvus milvus-lite openai
如果您使用的是 Google Colab,為了啟用剛安裝的相依性,您可能需要重新啟動執行時(點選畫面上方的「Runtime」功能表,並從下拉式功能表中選擇「Restart session」)。
在本範例中,我們將使用 OpenAI 作為 LLM。您應該準備api key OPENAI_API_KEY 作為環境變數。
import os
os.environ["OPENAI_API_KEY"] = "sk-xxxx"
初始化 Milvus
匯入套件並初始化 Milvus 向量資料庫實例。
from agno.agent import Agent
from agno.knowledge.pdf_url import PDFUrlKnowledgeBase
from agno.vectordb.milvus import Milvus
# Initialize Milvus
vector_db = Milvus(
collection="recipes",
uri="./milvus.db",
)
為您的 Milvus 伺服器指定集合名稱、uri 和 token(選項)。
以下是如何設定 uri 和 token:
- 如果你只需要一個本機向量資料庫來進行小規模的資料或原型設計,將 uri 設定為一個本機檔案,例如
./milvus.db,是最方便的方法,因為它會自動利用Milvus Lite將所有資料儲存到這個檔案中。 - 如果您有大規模的資料,例如超過一百萬個向量,您可以在Docker 或 Kubernetes 上架設效能更高的 Milvus 伺服器。在此設定中,請使用伺服器位址和連接埠作為您的 uri,例如
http://localhost:19530。如果您在 Milvus 上啟用認證功能,請使用 ": " 作為令牌,否則請勿設定令牌。 - 如果您使用Zilliz Cloud,Milvus 的完全管理雲端服務,請調整
uri和token,它們對應於 Zilliz Cloud 中的Public Endpoint 和 API key。
載入資料
建立 PDF url 知識庫實例,並將資料載入實例。我們使用公開食譜 pdf 資料作為範例。
# Create knowledge base
knowledge_base = PDFUrlKnowledgeBase(
urls=["https://phi-public.s3.amazonaws.com/recipes/ThaiRecipes.pdf"],
vector_db=vector_db,
)
knowledge_base.load(recreate=False) # Comment out after first run
INFO Creating
INFO Loading knowledge
INFO Reading: https://phi-public.s3.amazonaws.com/recipes/ThaiRecipes.pdf
INFO Added documents to knowledge base
使用代理回應問題
將知識庫整合到代理中,然後我們就可以向代理提出問題並獲得回應。
# Create and use the agent
agent = Agent(knowledge=knowledge_base, show_tool_calls=True)
# Query the agent
agent.print_response("How to make Tom Kha Gai", markdown=True)
Output()
┏━ Message ━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━┓
┃ ┃
┃ How to make Tom Kha Gai ┃
┃ ┃
┗━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━┛
┏━ Response (6.9s) ━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━┓
┃ ┃
┃ Running: ┃
┃ ┃
┃ • search_knowledge_base(query=Tom Kha Gai recipe) ┃
┃ ┃
┃ Here's a recipe for Tom Kha Gai, a delicious Thai chicken and galangal soup made with coconut milk: ┃
┃ ┃
┃ Ingredients (One serving): ┃
┃ ┃
┃ • 150 grams chicken, cut into bite-size pieces ┃
┃ • 50 grams sliced young galangal ┃
┃ • 100 grams lightly crushed lemongrass, julienned ┃
┃ • 100 grams straw mushrooms ┃
┃ • 250 grams coconut milk ┃
┃ • 100 grams chicken stock ┃
┃ • 3 tbsp lime juice ┃
┃ • 3 tbsp fish sauce ┃
┃ • 2 leaves kaffir lime, shredded ┃
┃ • 1-2 bird’s eye chilies, pounded ┃
┃ • 3 leaves coriander ┃
┃ ┃
┃ Directions: ┃
┃ ┃
┃ 1 Bring the chicken stock and coconut milk to a slow boil. ┃
┃ 2 Add galangal, lemongrass, chicken, and mushrooms. Once the soup returns to a boil, season it with fish sauce. ┃
┃ 3 Wait until the chicken is cooked, then add the kaffir lime leaves and bird’s eye chilies. ┃
┃ 4 Remove the pot from heat and add lime juice. ┃
┃ 5 Garnish with coriander leaves. ┃
┃ ┃
┃ Tips: ┃
┃ ┃
┃ • Keep the heat low throughout the cooking process to prevent the oil in the coconut milk from separating. ┃
┃ • If using mature galangal, reduce the amount. ┃
┃ • Adding lime juice after removing the pot from heat makes it more aromatic. ┃
┃ • Reduce the number of chilies for a milder taste. ┃
┃ ┃
┃ Enjoy making and savoring this flavorful Thai soup! ┃
┃ ┃
┗━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━┛
恭喜您,您已經了解了在 Agno 中使用 Milvus 的基本知識。如果您想了解更多如何使用 Agno,請參考官方說明文件。