MilvusとPhidataの統合
Phidataは、インテリジェントエージェントとワークフローを構築するための強力なフレームワークです。テキスト、画像、音声、動画を理解し、様々なツールやナレッジソースを活用して複雑なタスクを遂行するマルチモーダルエージェントを作成することができます。Phidataはマルチエージェントのオーケストレーションをサポートし、エージェントチームが協力して問題を解決することを可能にします。また、エージェントと対話するための美しいエージェントUIも提供します。
Milvusベクトルデータベースは、エンベッディングとして情報の効率的な保存と検索を可能にします。MilvusとPhidataを利用することで、知識をエージェントのワークフローに簡単に統合することができます。このドキュメントは、MilvusとPhidataの統合の基本的な使い方を説明するものです。
準備
必要な依存関係をインストールします:
$ pip install --upgrade phidata pymilvus openai
Google Colabを使用している場合、インストールした依存関係を有効にするには、ランタイムを再起動する必要があります(画面上部の "Runtime "メニューをクリックし、ドロップダウンメニューから "Restart session "を選択してください)。
この例では、LLMとしてOpenAIを使います。環境変数としてapi key OPENAI_API_KEY
を用意してください。
import os
os.environ["OPENAI_API_KEY"] = "sk-***********"
Milvusの初期化
パッケージをインポートし、Milvusベクトルデータベースインスタンスを初期化します。
from phi.agent import Agent
from phi.knowledge.pdf import PDFUrlKnowledgeBase
from phi.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の認証機能を有効にしている場合は、トークンに"<your_username>:<your_password>"を使用します。MilvusのフルマネージドクラウドサービスであるZilliz Cloudをご利用の場合は、Zilliz CloudのPublic EndpointとAPI keyに対応する
uri
、token
。
データのロード
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 コレクションの作成
INFO ナレッジベースの読み込み
INFO 読み込み中: https://phi-public.s3.amazonaws.com/recipes/ThaiRecipes.pdf
INFO ナレッジベースに0ドキュメントを追加
エージェントを使って質問に答える
ナレッジベースをエージェントに統合し、エージェントに質問して応答を得ることができます。
# Create and use the agent
agent = Agent(knowledge_base=knowledge_base, use_tools=True, 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 f┃
┃ 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 separ ┃
┃ • 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! ┃
┃ ┃
┗━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━┛
おめでとうございます、あなたはPhidataでMilvusを使用する基本を学びました。Phidataの使い方をもっと知りたい場合は、公式ドキュメントを参照してください。