|
1 | | -# Copyright 2024 Google LLC |
| 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 | +# # |
2 | 15 | # |
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 |
| 16 | +# # NOTE: This snippet has been partially generated by `gemini-2.5-pro-exp-03-25` |
6 | 17 | # |
7 | | -# http://www.apache.org/licenses/LICENSE-2.0 |
| 18 | +# # [START genappbuilder_answer_query] |
| 19 | +# from google.api_core.client_options import ClientOptions |
| 20 | +# from google.cloud import discoveryengine_v1 as discoveryengine |
8 | 21 | # |
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. |
| 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" |
14 | 26 | # |
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 | | - # The `query_understanding_spec` below includes all available query phase options. |
51 | | - # For more details, refer to https://cloud.google.com/generative-ai-app-builder/docs/reference/rest/v1/QueryUnderstandingSpec |
52 | | - query_understanding_spec = discoveryengine.AnswerQueryRequest.QueryUnderstandingSpec( |
53 | | - query_rephraser_spec=discoveryengine.AnswerQueryRequest.QueryUnderstandingSpec.QueryRephraserSpec( |
54 | | - disable=False, # Optional: Disable query rephraser |
55 | | - max_rephrase_steps=1, # Optional: Number of rephrase steps |
56 | | - ), |
57 | | - # Optional: Classify query types |
58 | | - query_classification_spec=discoveryengine.AnswerQueryRequest.QueryUnderstandingSpec.QueryClassificationSpec( |
59 | | - types=[ |
60 | | - discoveryengine.AnswerQueryRequest.QueryUnderstandingSpec.QueryClassificationSpec.Type.ADVERSARIAL_QUERY, |
61 | | - discoveryengine.AnswerQueryRequest.QueryUnderstandingSpec.QueryClassificationSpec.Type.NON_ANSWER_SEEKING_QUERY, |
62 | | - ] # Options: ADVERSARIAL_QUERY, NON_ANSWER_SEEKING_QUERY or both |
63 | | - ), |
64 | | - ) |
65 | | - |
66 | | - # Optional: Options for answer phase |
67 | | - # The `answer_generation_spec` below includes all available query phase options. |
68 | | - # For more details, refer to https://cloud.google.com/generative-ai-app-builder/docs/reference/rest/v1/AnswerGenerationSpec |
69 | | - answer_generation_spec = discoveryengine.AnswerQueryRequest.AnswerGenerationSpec( |
70 | | - ignore_adversarial_query=False, # Optional: Ignore adversarial query |
71 | | - ignore_non_answer_seeking_query=False, # Optional: Ignore non-answer seeking query |
72 | | - ignore_low_relevant_content=False, # Optional: Return fallback answer when content is not relevant |
73 | | - model_spec=discoveryengine.AnswerQueryRequest.AnswerGenerationSpec.ModelSpec( |
74 | | - model_version="gemini-2.0-flash-001/answer_gen/v2", # Optional: Model to use for answer generation |
75 | | - ), |
76 | | - prompt_spec=discoveryengine.AnswerQueryRequest.AnswerGenerationSpec.PromptSpec( |
77 | | - preamble="Give a detailed answer.", # Optional: Natural language instructions for customizing the answer. |
78 | | - ), |
79 | | - include_citations=True, # Optional: Include citations in the response |
80 | | - answer_language_code="en", # Optional: Language code of the answer |
81 | | - ) |
82 | | - |
83 | | - # Initialize request argument(s) |
84 | | - request = discoveryengine.AnswerQueryRequest( |
85 | | - serving_config=serving_config, |
86 | | - query=discoveryengine.Query(text="What is Vertex AI Search?"), |
87 | | - session=None, # Optional: include previous session ID to continue a conversation |
88 | | - query_understanding_spec=query_understanding_spec, |
89 | | - answer_generation_spec=answer_generation_spec, |
90 | | - ) |
91 | | - |
92 | | - # Make the request |
93 | | - response = client.answer_query(request) |
94 | | - |
95 | | - # Handle the response |
96 | | - print(response) |
97 | | - |
98 | | - return response |
99 | | - |
100 | | - |
101 | | -# [END genappbuilder_answer_query] |
| 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 | +# # The `query_understanding_spec` below includes all available query phase options. |
| 51 | +# # For more details, refer to https://cloud.google.com/generative-ai-app-builder/docs/reference/rest/v1/QueryUnderstandingSpec |
| 52 | +# query_understanding_spec = discoveryengine.AnswerQueryRequest.QueryUnderstandingSpec( |
| 53 | +# query_rephraser_spec=discoveryengine.AnswerQueryRequest.QueryUnderstandingSpec.QueryRephraserSpec( |
| 54 | +# disable=False, # Optional: Disable query rephraser |
| 55 | +# max_rephrase_steps=1, # Optional: Number of rephrase steps |
| 56 | +# ), |
| 57 | +# # Optional: Classify query types |
| 58 | +# query_classification_spec=discoveryengine.AnswerQueryRequest.QueryUnderstandingSpec.QueryClassificationSpec( |
| 59 | +# types=[ |
| 60 | +# discoveryengine.AnswerQueryRequest.QueryUnderstandingSpec.QueryClassificationSpec.Type.ADVERSARIAL_QUERY, |
| 61 | +# discoveryengine.AnswerQueryRequest.QueryUnderstandingSpec.QueryClassificationSpec.Type.NON_ANSWER_SEEKING_QUERY, |
| 62 | +# ] # Options: ADVERSARIAL_QUERY, NON_ANSWER_SEEKING_QUERY or both |
| 63 | +# ), |
| 64 | +# ) |
| 65 | +# |
| 66 | +# # Optional: Options for answer phase |
| 67 | +# # The `answer_generation_spec` below includes all available query phase options. |
| 68 | +# # For more details, refer to https://cloud.google.com/generative-ai-app-builder/docs/reference/rest/v1/AnswerGenerationSpec |
| 69 | +# answer_generation_spec = discoveryengine.AnswerQueryRequest.AnswerGenerationSpec( |
| 70 | +# ignore_adversarial_query=False, # Optional: Ignore adversarial query |
| 71 | +# ignore_non_answer_seeking_query=False, # Optional: Ignore non-answer seeking query |
| 72 | +# ignore_low_relevant_content=False, # Optional: Return fallback answer when content is not relevant |
| 73 | +# model_spec=discoveryengine.AnswerQueryRequest.AnswerGenerationSpec.ModelSpec( |
| 74 | +# model_version="gemini-2.0-flash-001/answer_gen/v2", # Optional: Model to use for answer generation |
| 75 | +# ), |
| 76 | +# prompt_spec=discoveryengine.AnswerQueryRequest.AnswerGenerationSpec.PromptSpec( |
| 77 | +# preamble="Give a detailed answer.", # Optional: Natural language instructions for customizing the answer. |
| 78 | +# ), |
| 79 | +# include_citations=True, # Optional: Include citations in the response |
| 80 | +# answer_language_code="en", # Optional: Language code of the answer |
| 81 | +# ) |
| 82 | +# |
| 83 | +# # Initialize request argument(s) |
| 84 | +# request = discoveryengine.AnswerQueryRequest( |
| 85 | +# serving_config=serving_config, |
| 86 | +# query=discoveryengine.Query(text="What is Vertex AI Search?"), |
| 87 | +# session=None, # Optional: include previous session ID to continue a conversation |
| 88 | +# query_understanding_spec=query_understanding_spec, |
| 89 | +# answer_generation_spec=answer_generation_spec, |
| 90 | +# ) |
| 91 | +# |
| 92 | +# # Make the request |
| 93 | +# response = client.answer_query(request) |
| 94 | +# |
| 95 | +# # Handle the response |
| 96 | +# print(response) |
| 97 | +# |
| 98 | +# return response |
| 99 | +# |
| 100 | +# |
| 101 | +# # [END genappbuilder_answer_query] |
0 commit comments