|
| 1 | +# Copyright 2024 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 | + |
| 16 | +# NOTE: This snippet has been partially generated by `gemini-1.5-pro-001` |
| 17 | + |
| 18 | +# [START genappbuilder_answer_query] |
| 19 | +from google.api_core.client_options import ClientOptions |
| 20 | +from google.cloud import discoveryengine_v1 as discoveryengine |
| 21 | + |
| 22 | +# TODO(developer): Uncomment these variables before running the sample. |
| 23 | +# project_id = "YOUR_PROJECT_ID" |
| 24 | +# location = "YOUR_LOCATION" # Values: "global", "us", "eu" |
| 25 | +# engine_id = "YOUR_APP_ID" |
| 26 | + |
| 27 | + |
| 28 | +def answer_query_sample( |
| 29 | + project_id: str, |
| 30 | + location: str, |
| 31 | + engine_id: str, |
| 32 | +) -> discoveryengine.AnswerQueryResponse: |
| 33 | + # For more information, refer to: |
| 34 | + # https://cloud.google.com/generative-ai-app-builder/docs/locations#specify_a_multi-region_for_your_data_store |
| 35 | + client_options = ( |
| 36 | + ClientOptions(api_endpoint=f"{location}-discoveryengine.googleapis.com") |
| 37 | + if location != "global" |
| 38 | + else None |
| 39 | + ) |
| 40 | + |
| 41 | + # Create a client |
| 42 | + client = discoveryengine.ConversationalSearchServiceClient( |
| 43 | + client_options=client_options |
| 44 | + ) |
| 45 | + |
| 46 | + # The full resource name of the Search serving config |
| 47 | + serving_config = f"projects/{project_id}/locations/{location}/collections/default_collection/engines/{engine_id}/servingConfigs/default_serving_config" |
| 48 | + |
| 49 | + # Optional: Options for query phase |
| 50 | + query_understanding_spec = discoveryengine.AnswerQueryRequest.QueryUnderstandingSpec( |
| 51 | + query_rephraser_spec=discoveryengine.AnswerQueryRequest.QueryUnderstandingSpec.QueryRephraserSpec( |
| 52 | + disable=False, # Optional: Disable query rephraser |
| 53 | + max_rephrase_steps=1, # Optional: Number of rephrase steps |
| 54 | + ), |
| 55 | + # Optional: Classify query types |
| 56 | + query_classification_spec=discoveryengine.AnswerQueryRequest.QueryUnderstandingSpec.QueryClassificationSpec( |
| 57 | + types=[ |
| 58 | + discoveryengine.AnswerQueryRequest.QueryUnderstandingSpec.QueryClassificationSpec.Type.ADVERSARIAL_QUERY, |
| 59 | + discoveryengine.AnswerQueryRequest.QueryUnderstandingSpec.QueryClassificationSpec.Type.NON_ANSWER_SEEKING_QUERY, |
| 60 | + ] # Options: ADVERSARIAL_QUERY, NON_ANSWER_SEEKING_QUERY or both |
| 61 | + ), |
| 62 | + ) |
| 63 | + |
| 64 | + # Optional: Options for answer phase |
| 65 | + answer_generation_spec = discoveryengine.AnswerQueryRequest.AnswerGenerationSpec( |
| 66 | + ignore_adversarial_query=False, # Optional: Ignore adversarial query |
| 67 | + ignore_non_answer_seeking_query=False, # Optional: Ignore non-answer seeking query |
| 68 | + ignore_low_relevant_content=False, # Optional: Return fallback answer when content is not relevant |
| 69 | + model_spec=discoveryengine.AnswerQueryRequest.AnswerGenerationSpec.ModelSpec( |
| 70 | + model_version="gemini-1.5-flash-001/answer_gen/v2", # Optional: Model to use for answer generation |
| 71 | + ), |
| 72 | + prompt_spec=discoveryengine.AnswerQueryRequest.AnswerGenerationSpec.PromptSpec( |
| 73 | + preamble="Give a detailed answer.", # Optional: Natural language instructions for customizing the answer. |
| 74 | + ), |
| 75 | + include_citations=True, # Optional: Include citations in the response |
| 76 | + answer_language_code="en", # Optional: Language code of the answer |
| 77 | + ) |
| 78 | + |
| 79 | + # Initialize request argument(s) |
| 80 | + request = discoveryengine.AnswerQueryRequest( |
| 81 | + serving_config=serving_config, |
| 82 | + query=discoveryengine.Query(text="What is Vertex AI Search?"), |
| 83 | + session=None, # Optional: include previous session ID to continue a conversation |
| 84 | + query_understanding_spec=query_understanding_spec, |
| 85 | + answer_generation_spec=answer_generation_spec, |
| 86 | + ) |
| 87 | + |
| 88 | + # Make the request |
| 89 | + response = client.answer_query(request) |
| 90 | + |
| 91 | + # Handle the response |
| 92 | + print(response) |
| 93 | + |
| 94 | + return response |
| 95 | + |
| 96 | + |
| 97 | +# [END genappbuilder_answer_query] |
0 commit comments