Skip to content

Commit d84651b

Browse files
docs(samples): Added answer_query_sample.py (GoogleCloudPlatform#12564)
* docs(samples): Added answer_query_sample.py * 🦉 Updates from OwlBot post-processor See https://github.com/googleapis/repo-automation-bots/blob/main/packages/owl-bot/README.md * Fixed Sample to pass tests * Fixed test engine * Update answer_query_sample.py Added disclaimer about gemini generation --------- Co-authored-by: Owl Bot <gcf-owl-bot[bot]@users.noreply.github.com>
1 parent 1e214f2 commit d84651b

File tree

2 files changed

+128
-0
lines changed

2 files changed

+128
-0
lines changed
+97
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,97 @@
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]
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,31 @@
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+
import os
17+
18+
from discoveryengine import answer_query_sample
19+
20+
project_id = os.environ["GOOGLE_CLOUD_PROJECT"]
21+
22+
23+
def test_answer_query():
24+
response = answer_query_sample.answer_query_sample(
25+
project_id=project_id,
26+
location="global",
27+
engine_id="test-search-engine_1689960780551",
28+
)
29+
30+
assert response
31+
assert response.answer

0 commit comments

Comments
 (0)