|
| 1 | +# Copyright 2026 Google LLC |
| 2 | +# |
| 3 | +# Licensed under the Apache License, Version 2.0 (the "License"); |
| 4 | +# you may not use this file except in compliance with the License. |
| 5 | +# You may obtain a copy of the License at |
| 6 | +# |
| 7 | +# http://www.apache.org/licenses/LICENSE-2.0 |
| 8 | +# |
| 9 | +# Unless required by applicable law or agreed to in writing, software |
| 10 | +# distributed under the License is distributed on an "AS IS" BASIS, |
| 11 | +# WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. |
| 12 | +# See the License for the specific language governing permissions and |
| 13 | +# limitations under the License. |
| 14 | + |
| 15 | +"""Sample Milvus RAG Agent. |
| 16 | +
|
| 17 | +This sample demonstrates how to build a knowledge base agent using Milvus |
| 18 | +as the vector database for retrieval-augmented generation (RAG). |
| 19 | +
|
| 20 | +Prerequisites: |
| 21 | + 1. A running Milvus instance (or use Milvus Lite with a local file path). |
| 22 | + 2. A Google GenAI API key set in the GOOGLE_API_KEY environment variable. |
| 23 | + 3. Install dependencies: pip install "google-adk[milvus]" |
| 24 | +
|
| 25 | +Usage: |
| 26 | + adk run contributing/samples/milvus_rag_agent |
| 27 | +""" |
| 28 | + |
| 29 | +import os |
| 30 | + |
| 31 | +from dotenv import load_dotenv |
| 32 | +from google.adk.agents.llm_agent import LlmAgent |
| 33 | +from google.adk.tools.milvus.milvus_toolset import MilvusToolset |
| 34 | +from google.adk.tools.milvus.settings import MilvusToolSettings |
| 35 | +from google.adk.tools.milvus.settings import MilvusVectorStoreSettings |
| 36 | +from google.genai import Client |
| 37 | + |
| 38 | +load_dotenv() |
| 39 | + |
| 40 | +# --- Embedding function using Google GenAI --- |
| 41 | +genai_client = Client() |
| 42 | + |
| 43 | + |
| 44 | +def embedding_fn(texts: list[str]) -> list[list[float]]: |
| 45 | + response = genai_client.models.embed_content( |
| 46 | + model="text-embedding-004", |
| 47 | + contents=texts, |
| 48 | + ) |
| 49 | + return [list(e.values) for e in response.embeddings] |
| 50 | + |
| 51 | + |
| 52 | +# --- Milvus vector store settings --- |
| 53 | +# Replace these with your own Milvus connection and collection settings. |
| 54 | +vector_store_settings = MilvusVectorStoreSettings( |
| 55 | + # Use a remote Milvus instance or a local Milvus Lite file path. |
| 56 | + uri=os.environ.get("MILVUS_URI", "http://localhost:19530"), |
| 57 | + token=os.environ.get("MILVUS_TOKEN", None), |
| 58 | + collection_name=os.environ.get("MILVUS_COLLECTION", "knowledge_base"), |
| 59 | + dimension=768, |
| 60 | + metric_type="COSINE", |
| 61 | + top_k=5, |
| 62 | +) |
| 63 | + |
| 64 | +# --- Milvus toolset --- |
| 65 | +milvus_toolset = MilvusToolset( |
| 66 | + milvus_tool_settings=MilvusToolSettings( |
| 67 | + vector_store_settings=vector_store_settings, |
| 68 | + ), |
| 69 | + embedding_fn=embedding_fn, |
| 70 | + tool_filter=["similarity_search"], |
| 71 | +) |
| 72 | + |
| 73 | +# --- Agent definition --- |
| 74 | +root_agent = LlmAgent( |
| 75 | + model="gemini-2.5-flash", |
| 76 | + name="milvus_knowledge_agent", |
| 77 | + description="Agent that answers questions using a Milvus knowledge base.", |
| 78 | + instruction=""" |
| 79 | + You are a helpful assistant with access to a knowledge base. |
| 80 | + 1. Always use the `similarity_search` tool to find relevant information. |
| 81 | + 2. Present the search results naturally in your response. |
| 82 | + 3. If no results are found, say you don't know. |
| 83 | + """, |
| 84 | + tools=[milvus_toolset], |
| 85 | +) |
0 commit comments