-
Notifications
You must be signed in to change notification settings - Fork 2.9k
feat: Add Milvus vector store integration for RAG tool and memory #4417
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
Closed
zc277584121
wants to merge
3
commits into
google:main
from
zc277584121:feat/milvus-vector-store-integration
Closed
Changes from all commits
Commits
Show all changes
3 commits
Select commit
Hold shift + click to select a range
File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
There are no files selected for viewing
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,86 @@ | ||
| # Milvus RAG Agent Sample | ||
|
|
||
| This sample demonstrates how to build a knowledge base agent using | ||
| [Milvus](https://milvus.io/) as the vector database for | ||
| retrieval-augmented generation (RAG) with Google ADK. | ||
|
|
||
| ## Prerequisites | ||
|
|
||
| 1. A running Milvus instance, or use Milvus Lite (local file path). | ||
| 2. A Google GenAI API key for embedding generation. | ||
| 3. Install dependencies: | ||
|
|
||
| ```bash | ||
| pip install "google-adk[milvus]" | ||
| ``` | ||
|
|
||
| ## Supported Milvus Backends | ||
|
|
||
| The integration works with all three Milvus deployment modes by changing | ||
| the `uri` (and optionally `token`) setting: | ||
|
|
||
| | Backend | `uri` | `token` | | ||
| |---------|-------|---------| | ||
| | **Milvus Lite** (local, no server needed) | `./milvus.db` | — | | ||
| | **Milvus Server** (self-hosted) | `http://localhost:19530` | — | | ||
| | **Zilliz Cloud** (fully managed) | `https://in01-xxx.serverless.gcp-us-west1.cloud.zilliz.com` | `your-api-key` | | ||
|
|
||
| > For Milvus Lite, install the extra package: `pip install "pymilvus[milvus_lite]"`. | ||
|
|
||
| ## Setup | ||
|
|
||
| ### Environment Variables | ||
|
|
||
| ```bash | ||
| export GOOGLE_API_KEY="your-google-api-key" | ||
|
|
||
| # Pick one of the following: | ||
| export MILVUS_URI="./milvus.db" # Milvus Lite | ||
| export MILVUS_URI="http://localhost:19530" # Milvus Server | ||
| export MILVUS_URI="https://in01-xxx.cloud.zilliz.com" # Zilliz Cloud | ||
|
|
||
| export MILVUS_COLLECTION="knowledge_base" | ||
| # Required for Zilliz Cloud only: | ||
| # export MILVUS_TOKEN="your-api-key" | ||
| ``` | ||
|
|
||
| ### Data Ingestion | ||
|
|
||
| Before running the agent, you need to populate the Milvus collection | ||
| with your knowledge base data: | ||
|
|
||
| ```python | ||
| from google.adk.tools.milvus.milvus_vector_store import MilvusVectorStore | ||
| from google.adk.tools.milvus.settings import MilvusToolSettings | ||
| from google.adk.tools.milvus.settings import MilvusVectorStoreSettings | ||
| from google.genai import Client | ||
|
|
||
| # Define your embedding function (example using Google GenAI). | ||
| genai_client = Client() | ||
|
|
||
| def embedding_fn(texts): | ||
| resp = genai_client.models.embed_content( | ||
| model="text-embedding-004", contents=texts) | ||
| return [list(e.values) for e in resp.embeddings] | ||
|
|
||
| settings = MilvusToolSettings( | ||
| vector_store_settings=MilvusVectorStoreSettings( | ||
| uri="http://localhost:19530", | ||
| collection_name="knowledge_base", | ||
| dimension=768, | ||
| ), | ||
| ) | ||
|
|
||
| store = MilvusVectorStore(settings=settings, embedding_fn=embedding_fn) | ||
zc277584121 marked this conversation as resolved.
Show resolved
Hide resolved
|
||
| store.setup() | ||
| store.add_contents([ | ||
| "Your document text here...", | ||
| "Another document...", | ||
| ]) | ||
| ``` | ||
|
|
||
| ## Run | ||
|
|
||
| ```bash | ||
| adk run contributing/samples/milvus_rag_agent | ||
| ``` | ||
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,15 @@ | ||
| # Copyright 2026 Google LLC | ||
| # | ||
| # Licensed under the Apache License, Version 2.0 (the "License"); | ||
| # you may not use this file except in compliance with the License. | ||
| # You may obtain a copy of the License at | ||
| # | ||
| # http://www.apache.org/licenses/LICENSE-2.0 | ||
| # | ||
| # Unless required by applicable law or agreed to in writing, software | ||
| # distributed under the License is distributed on an "AS IS" BASIS, | ||
| # WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. | ||
| # See the License for the specific language governing permissions and | ||
| # limitations under the License. | ||
|
|
||
| from . import agent |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,85 @@ | ||
| # Copyright 2026 Google LLC | ||
| # | ||
| # Licensed under the Apache License, Version 2.0 (the "License"); | ||
| # you may not use this file except in compliance with the License. | ||
| # You may obtain a copy of the License at | ||
| # | ||
| # http://www.apache.org/licenses/LICENSE-2.0 | ||
| # | ||
| # Unless required by applicable law or agreed to in writing, software | ||
| # distributed under the License is distributed on an "AS IS" BASIS, | ||
| # WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. | ||
| # See the License for the specific language governing permissions and | ||
| # limitations under the License. | ||
|
|
||
| """Sample Milvus RAG Agent. | ||
|
|
||
| This sample demonstrates how to build a knowledge base agent using Milvus | ||
| as the vector database for retrieval-augmented generation (RAG). | ||
|
|
||
| Prerequisites: | ||
| 1. A running Milvus instance (or use Milvus Lite with a local file path). | ||
| 2. A Google GenAI API key set in the GOOGLE_API_KEY environment variable. | ||
| 3. Install dependencies: pip install "google-adk[milvus]" | ||
|
|
||
| Usage: | ||
| adk run contributing/samples/milvus_rag_agent | ||
| """ | ||
|
|
||
| import os | ||
|
|
||
| from dotenv import load_dotenv | ||
| from google.adk.agents.llm_agent import LlmAgent | ||
| from google.adk.tools.milvus.milvus_toolset import MilvusToolset | ||
| from google.adk.tools.milvus.settings import MilvusToolSettings | ||
| from google.adk.tools.milvus.settings import MilvusVectorStoreSettings | ||
| from google.genai import Client | ||
|
|
||
| load_dotenv() | ||
|
|
||
| # --- Embedding function using Google GenAI --- | ||
| genai_client = Client() | ||
|
|
||
|
|
||
| def embedding_fn(texts: list[str]) -> list[list[float]]: | ||
| response = genai_client.models.embed_content( | ||
| model="text-embedding-004", | ||
| contents=texts, | ||
| ) | ||
| return [list(e.values) for e in response.embeddings] | ||
zc277584121 marked this conversation as resolved.
Show resolved
Hide resolved
|
||
|
|
||
|
|
||
| # --- Milvus vector store settings --- | ||
| # Replace these with your own Milvus connection and collection settings. | ||
| vector_store_settings = MilvusVectorStoreSettings( | ||
| # Use a remote Milvus instance or a local Milvus Lite file path. | ||
| uri=os.environ.get("MILVUS_URI", "http://localhost:19530"), | ||
| token=os.environ.get("MILVUS_TOKEN", None), | ||
| collection_name=os.environ.get("MILVUS_COLLECTION", "knowledge_base"), | ||
| dimension=768, | ||
| metric_type="COSINE", | ||
| top_k=5, | ||
| ) | ||
|
|
||
| # --- Milvus toolset --- | ||
| milvus_toolset = MilvusToolset( | ||
| milvus_tool_settings=MilvusToolSettings( | ||
| vector_store_settings=vector_store_settings, | ||
| ), | ||
| embedding_fn=embedding_fn, | ||
| tool_filter=["similarity_search"], | ||
| ) | ||
|
|
||
| # --- Agent definition --- | ||
| root_agent = LlmAgent( | ||
| model="gemini-2.5-flash", | ||
zc277584121 marked this conversation as resolved.
Show resolved
Hide resolved
|
||
| name="milvus_knowledge_agent", | ||
| description="Agent that answers questions using a Milvus knowledge base.", | ||
| instruction=""" | ||
| You are a helpful assistant with access to a knowledge base. | ||
| 1. Always use the `similarity_search` tool to find relevant information. | ||
| 2. Present the search results naturally in your response. | ||
| 3. If no results are found, say you don't know. | ||
| """, | ||
| tools=[milvus_toolset], | ||
| ) | ||
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Oops, something went wrong.
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
Uh oh!
There was an error while loading. Please reload this page.