forked from GoogleCloudPlatform/python-docs-samples
-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathanswer_query_sample.py
More file actions
100 lines (85 loc) · 4.63 KB
/
answer_query_sample.py
File metadata and controls
100 lines (85 loc) · 4.63 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
# Copyright 2024 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.
# [START genappbuilder_answer_query]
from google.api_core.client_options import ClientOptions
from google.cloud import discoveryengine_v1 as discoveryengine
# TODO(developer): Uncomment these variables before running the sample.
# project_id = "YOUR_PROJECT_ID"
# location = "YOUR_LOCATION" # Values: "global", "us", "eu"
# engine_id = "YOUR_APP_ID"
def answer_query_sample(
project_id: str,
location: str,
engine_id: str,
) -> discoveryengine.AnswerQueryResponse:
# For more information, refer to:
# https://cloud.google.com/generative-ai-app-builder/docs/locations#specify_a_multi-region_for_your_data_store
client_options = (
ClientOptions(api_endpoint=f"{location}-discoveryengine.googleapis.com")
if location != "global"
else None
)
# Create a client
client = discoveryengine.ConversationalSearchServiceClient(
client_options=client_options
)
# The full resource name of the Search serving config
serving_config = f"projects/{project_id}/locations/{location}/collections/default_collection/engines/{engine_id}/servingConfigs/default_serving_config"
# Optional: Options for query phase
# The `query_understanding_spec` below includes all available query phase options.
# For more details, refer to https://cloud.google.com/generative-ai-app-builder/docs/reference/rest/v1/QueryUnderstandingSpec
query_understanding_spec = discoveryengine.AnswerQueryRequest.QueryUnderstandingSpec(
query_rephraser_spec=discoveryengine.AnswerQueryRequest.QueryUnderstandingSpec.QueryRephraserSpec(
disable=False, # Optional: Disable query rephraser
max_rephrase_steps=1, # Optional: Number of rephrase steps
),
# Optional: Classify query types
query_classification_spec=discoveryengine.AnswerQueryRequest.QueryUnderstandingSpec.QueryClassificationSpec(
types=[
discoveryengine.AnswerQueryRequest.QueryUnderstandingSpec.QueryClassificationSpec.Type.ADVERSARIAL_QUERY,
discoveryengine.AnswerQueryRequest.QueryUnderstandingSpec.QueryClassificationSpec.Type.NON_ANSWER_SEEKING_QUERY,
] # Options: ADVERSARIAL_QUERY, NON_ANSWER_SEEKING_QUERY or both
),
)
# Optional: Options for answer phase
# The `answer_generation_spec` below includes all available query phase options.
# For more details, refer to https://cloud.google.com/generative-ai-app-builder/docs/reference/rest/v1/AnswerGenerationSpec
answer_generation_spec = discoveryengine.AnswerQueryRequest.AnswerGenerationSpec(
ignore_adversarial_query=False, # Optional: Ignore adversarial query
ignore_non_answer_seeking_query=False, # Optional: Ignore non-answer seeking query
ignore_low_relevant_content=False, # Optional: Return fallback answer when content is not relevant
model_spec=discoveryengine.AnswerQueryRequest.AnswerGenerationSpec.ModelSpec(
model_version="gemini-2.5-flash/answer_gen/v1", # Optional: Model to use for answer generation
),
prompt_spec=discoveryengine.AnswerQueryRequest.AnswerGenerationSpec.PromptSpec(
preamble="Give a detailed answer.", # Optional: Natural language instructions for customizing the answer.
),
include_citations=True, # Optional: Include citations in the response
answer_language_code="en", # Optional: Language code of the answer
)
# Initialize request argument(s)
request = discoveryengine.AnswerQueryRequest(
serving_config=serving_config,
query=discoveryengine.Query(text="What is Vertex AI Search?"),
session=None, # Optional: include previous session ID to continue a conversation
query_understanding_spec=query_understanding_spec,
answer_generation_spec=answer_generation_spec,
user_pseudo_id="user-pseudo-id", # Optional: Add user pseudo-identifier for queries.
)
# Make the request
response = client.answer_query(request)
# Handle the response
print(response)
return response
# [END genappbuilder_answer_query]