|
| 1 | +# Copyright 2025 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 | +import json |
| 16 | +from typing import Optional |
| 17 | + |
| 18 | +from google.adk.agents import LlmAgent |
| 19 | +from google.adk.agents.callback_context import CallbackContext |
| 20 | +from google.adk.models import LlmResponse |
| 21 | +from google.adk.tools.vertex_ai_search_tool import VertexAiSearchTool |
| 22 | +from google.genai import types |
| 23 | + |
| 24 | +VERTEXAI_DATASTORE_ID = "projects/adk-agent-builder-assistant/locations/global/collections/default_collection/dataStores/adk-agent-builder-sample-datastore_1758230446136" |
| 25 | + |
| 26 | + |
| 27 | +def citation_retrieval_after_model_callback( |
| 28 | + callback_context: CallbackContext, |
| 29 | + llm_response: LlmResponse, |
| 30 | +) -> Optional[LlmResponse]: |
| 31 | + """Callback function to retrieve citations after model response is generated.""" |
| 32 | + grounding_metadata = llm_response.grounding_metadata |
| 33 | + if not grounding_metadata: |
| 34 | + return None |
| 35 | + |
| 36 | + content = llm_response.content |
| 37 | + if not llm_response.content: |
| 38 | + return None |
| 39 | + |
| 40 | + parts = content.parts |
| 41 | + if not parts: |
| 42 | + return None |
| 43 | + |
| 44 | + # Add citations to the response as JSON objects. |
| 45 | + parts.append(types.Part(text="References:\n")) |
| 46 | + for grounding_chunk in grounding_metadata.grounding_chunks: |
| 47 | + retrieved_context = grounding_chunk.retrieved_context |
| 48 | + if not retrieved_context: |
| 49 | + continue |
| 50 | + |
| 51 | + citation = { |
| 52 | + "title": retrieved_context.title, |
| 53 | + "uri": retrieved_context.uri, |
| 54 | + "snippet": retrieved_context.text, |
| 55 | + } |
| 56 | + parts.append(types.Part(text=json.dumps(citation))) |
| 57 | + |
| 58 | + return LlmResponse(content=types.Content(parts=parts)) |
| 59 | + |
| 60 | + |
| 61 | +root_agent = LlmAgent( |
| 62 | + name="adk_knowledge_agent", |
| 63 | + description=( |
| 64 | + "Agent for performing Vertex AI Search to find ADK knowledge and" |
| 65 | + " documentation" |
| 66 | + ), |
| 67 | + instruction="""You are a specialized search agent for an ADK knowledge base. |
| 68 | +
|
| 69 | + You can use the VertexAiSearchTool to search for ADK examples and documentation in the document store. |
| 70 | + """, |
| 71 | + model="gemini-2.5-flash", |
| 72 | + tools=[VertexAiSearchTool(data_store_id=VERTEXAI_DATASTORE_ID)], |
| 73 | + after_model_callback=citation_retrieval_after_model_callback, |
| 74 | +) |
0 commit comments