MilvusとAgnoの統合
Agno(旧Phidata)は、マルチモーダルエージェントを構築するための軽量ライブラリです。テキスト、画像、音声、ビデオを理解し、様々なツールや知識ソースを活用して複雑なタスクを実行するマルチモーダルエージェントを作成することができます。Agnoは、マルチエージェントのオーケストレーションをサポートし、エージェントのチームが協力して問題を解決することを可能にします。また、エージェントと対話するための美しいエージェントUIを提供します。
Milvusベクトルデータベースは、エンベッディングとして情報の効率的な保存と検索を可能にします。MilvusとAgnoを利用することで、お客様のナレッジをAgentのワークフローに簡単に統合することができます。このドキュメントは、MilvusとAgnoの統合の基本的な使い方を説明したものです。
準備
必要な依存関係をインストールします:
$ pip install --upgrade agno pymilvus milvus-lite openai
Google Colabを使用している場合、インストールした依存関係を有効にするために、ランタイムを再起動する必要があるかもしれません(画面上部の "Runtime "メニューをクリックし、ドロップダウンメニューから "Restart session "を選択してください)。
この例では、LLMとしてOpenAIを使います。環境変数として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(optinal)を指定します。
uriとtokenの設定方法は以下の通りです:
- 小規模なデータやプロトタイピングのためにローカルのベクターデータベースが必要な場合、uriをローカルファイル、例えば
./milvus.dbに設定するのが最も便利です。 - もし、100万ベクトルを超えるような大規模なデータがある場合は、DockerやKubernetes上に、よりパフォーマンスの高いMilvusサーバを構築することができます。このセットアップでは、サーバのアドレスとポートをURIとして使用してください(例:
http://localhost:19530)。Milvusで認証機能を有効にしている場合は、トークンとして ": " を使用します。そうでない場合は、トークンを設定しないでください。 - MilvusのフルマネージドクラウドサービスであるZilliz Cloudをご利用の場合は、
uriとtokenをZilliz CloudのPublic EndpointとAPI keyに対応させてください。
データのロード
PDF url knowledageのベースインスタンスを作成し、インスタンスにデータをロードします。例として公開レシピの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の使い方についてもっと知りたい場合は、公式ドキュメントを参照してください。