|
| 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 Any |
| 17 | +from typing import Dict |
| 18 | +from typing import List |
| 19 | + |
| 20 | +from adk_answering_agent.settings import ADK_GCP_SA_KEY |
| 21 | +from adk_answering_agent.settings import GEMINI_API_DATASTORE_ID |
| 22 | +from adk_answering_agent.utils import error_response |
| 23 | +from google.adk.agents.llm_agent import Agent |
| 24 | +from google.api_core.exceptions import GoogleAPICallError |
| 25 | +from google.cloud import discoveryengine_v1beta as discoveryengine |
| 26 | +from google.oauth2 import service_account |
| 27 | + |
| 28 | + |
| 29 | +def search_gemini_api_docs(queries: List[str]) -> Dict[str, Any]: |
| 30 | + """Searches Gemini API docs using Vertex AI Search. |
| 31 | +
|
| 32 | + Args: |
| 33 | + queries: The list of queries to search. |
| 34 | +
|
| 35 | + Returns: |
| 36 | + A dictionary containing the status of the request and the list of search |
| 37 | + results, which contains the title, url and snippets. |
| 38 | + """ |
| 39 | + try: |
| 40 | + adk_gcp_sa_key_info = json.loads(ADK_GCP_SA_KEY) |
| 41 | + client = discoveryengine.SearchServiceClient( |
| 42 | + credentials=service_account.Credentials.from_service_account_info( |
| 43 | + adk_gcp_sa_key_info |
| 44 | + ) |
| 45 | + ) |
| 46 | + except (TypeError, ValueError) as e: |
| 47 | + return error_response(f"Error creating Vertex AI Search client: {e}") |
| 48 | + |
| 49 | + serving_config = f"{GEMINI_API_DATASTORE_ID}/servingConfigs/default_config" |
| 50 | + results = [] |
| 51 | + try: |
| 52 | + for query in queries: |
| 53 | + request = discoveryengine.SearchRequest( |
| 54 | + serving_config=serving_config, |
| 55 | + query=query, |
| 56 | + page_size=20, |
| 57 | + ) |
| 58 | + response = client.search(request=request) |
| 59 | + for item in response.results: |
| 60 | + snippets = [] |
| 61 | + for snippet in item.document.derived_struct_data.get("snippets", []): |
| 62 | + snippets.append(snippet.get("snippet")) |
| 63 | + |
| 64 | + results.append({ |
| 65 | + "title": item.document.derived_struct_data.get("title"), |
| 66 | + "url": item.document.derived_struct_data.get("link"), |
| 67 | + "snippets": snippets, |
| 68 | + }) |
| 69 | + except GoogleAPICallError as e: |
| 70 | + return error_response(f"Error from Vertex AI Search: {e}") |
| 71 | + return {"status": "success", "results": results} |
| 72 | + |
| 73 | + |
| 74 | +root_agent = Agent( |
| 75 | + model="gemini-2.5-pro", |
| 76 | + name="gemini_assistant", |
| 77 | + description="Answer questions about Gemini API.", |
| 78 | + instruction=""" |
| 79 | + You are a helpful assistant that responds to questions about Gemini API based on information |
| 80 | + found in the document store. You can access the document store using the `search_gemini_api_docs` tool. |
| 81 | +
|
| 82 | + When user asks a question, here are the steps: |
| 83 | + 1. Use the `search_gemini_api_docs` tool to find relevant information before answering. |
| 84 | + * You can call the tool with multiple queries to find all the relevant information. |
| 85 | + 2. Provide a response based on the information you found in the document store. Reference the source document in the response. |
| 86 | +
|
| 87 | + IMPORTANT: |
| 88 | + * Your response should be based on the information you found in the document store. Do not invent |
| 89 | + information that is not in the document store. Do not invent citations which are not in the document store. |
| 90 | + * If you can't find the answer or information in the document store, just respond with "I can't find the answer or information in the document store". |
| 91 | + * If you uses citation from the document store, please always provide a footnote referencing the source document format it as: "[1] URL of the document". |
| 92 | + """, |
| 93 | + tools=[search_gemini_api_docs], |
| 94 | +) |
0 commit comments