Skip to content

Commit 8f2a0a1

Browse files
samples(documentai, discoveryengine): Add Samples for Vertex AI Search Standalone RAG APIs (GoogleCloudPlatform#11613)
--------- Co-authored-by: Owl Bot <gcf-owl-bot[bot]@users.noreply.github.com>
1 parent ea7e1ca commit 8f2a0a1

File tree

6 files changed

+222
-3
lines changed

6 files changed

+222
-3
lines changed

discoveryengine/requirements.txt

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,2 +1,2 @@
1-
google-cloud-discoveryengine==0.11.10
1+
google-cloud-discoveryengine==0.11.11
22
google-api-core==2.17.1
Lines changed: 125 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,125 @@
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+
from google.cloud import discoveryengine_v1alpha as discoveryengine
17+
18+
19+
def check_grounding_sample(
20+
project_id: str,
21+
) -> discoveryengine.CheckGroundingResponse:
22+
# [START genappbuilder_check_grounding]
23+
from google.cloud import discoveryengine_v1alpha as discoveryengine
24+
25+
# TODO(developer): Uncomment these variables before running the sample.
26+
# project_id = "YOUR_PROJECT_ID"
27+
28+
client = discoveryengine.GroundedGenerationServiceClient()
29+
30+
# The full resource name of the grounding config.
31+
# Format: projects/{project_id}/locations/{location}/groundingConfigs/default_grounding_config
32+
grounding_config = client.grounding_config_path(
33+
project=project_id,
34+
location="global",
35+
grounding_config="default_grounding_config",
36+
)
37+
38+
request = discoveryengine.CheckGroundingRequest(
39+
grounding_config=grounding_config,
40+
answer_candidate="Titanic was directed by James Cameron. It was released in 1997.",
41+
facts=[
42+
discoveryengine.GroundingFact(
43+
fact_text=(
44+
"Titanic is a 1997 American epic romantic disaster movie. It was directed, written,"
45+
" and co-produced by James Cameron. The movie is about the 1912 sinking of the"
46+
" RMS Titanic. It stars Kate Winslet and Leonardo DiCaprio. The movie was released"
47+
" on December 19, 1997. It received positive critical reviews. The movie won 11 Academy"
48+
" Awards, and was nominated for fourteen total Academy Awards."
49+
),
50+
attributes={"author": "Simple Wikipedia"},
51+
),
52+
discoveryengine.GroundingFact(
53+
fact_text=(
54+
'James Cameron\'s "Titanic" is an epic, action-packed romance'
55+
"set against the ill-fated maiden voyage of the R.M.S. Titanic;"
56+
"the pride and joy of the White Star Line and, at the time,"
57+
"the largest moving object ever built. "
58+
'She was the most luxurious liner of her era -- the "ship of dreams" -- '
59+
"which ultimately carried over 1,500 people to their death in the "
60+
"ice cold waters of the North Atlantic in the early hours of April 15, 1912."
61+
),
62+
attributes={"author": "Simple Wikipedia"},
63+
),
64+
],
65+
grounding_spec=discoveryengine.CheckGroundingSpec(citation_threshold=0.6),
66+
)
67+
68+
response = client.check_grounding(request=request)
69+
70+
# Handle the response
71+
print(response)
72+
# [END genappbuilder_check_grounding]
73+
74+
return response
75+
76+
77+
def rank_sample(
78+
project_id: str,
79+
) -> discoveryengine.RankResponse:
80+
# [START genappbuilder_rank]
81+
from google.cloud import discoveryengine_v1alpha as discoveryengine
82+
83+
# TODO(developer): Uncomment these variables before running the sample.
84+
# project_id = "YOUR_PROJECT_ID"
85+
86+
client = discoveryengine.RankServiceClient()
87+
88+
# The full resource name of the ranking config.
89+
# Format: projects/{project_id}/locations/{location}/rankingConfigs/default_ranking_config
90+
ranking_config = client.ranking_config_path(
91+
project=project_id,
92+
location="global",
93+
ranking_config="default_ranking_config",
94+
)
95+
request = discoveryengine.RankRequest(
96+
ranking_config=ranking_config,
97+
model="semantic-ranker-512@latest",
98+
top_n=10,
99+
query="What is Google Gemini?",
100+
records=[
101+
discoveryengine.RankingRecord(
102+
id="1",
103+
title="Gemini",
104+
content="The Gemini zodiac symbol often depicts two figures standing side-by-side.",
105+
),
106+
discoveryengine.RankingRecord(
107+
id="2",
108+
title="Gemini",
109+
content="Gemini is a cutting edge large language model created by Google.",
110+
),
111+
discoveryengine.RankingRecord(
112+
id="3",
113+
title="Gemini Constellation",
114+
content="Gemini is a constellation that can be seen in the night sky.",
115+
),
116+
],
117+
)
118+
119+
response = client.rank(request=request)
120+
121+
# Handle the response
122+
print(response)
123+
# [END genappbuilder_rank]
124+
125+
return response
Lines changed: 34 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,34 @@
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 standalone_apis_sample
19+
20+
project_id = os.environ["GOOGLE_CLOUD_PROJECT"]
21+
22+
23+
def test_check_grounding():
24+
response = standalone_apis_sample.check_grounding_sample(project_id)
25+
assert response
26+
assert response.support_score
27+
assert response.cited_chunks
28+
assert response.claims
29+
30+
31+
def test_rank():
32+
response = standalone_apis_sample.rank_sample(project_id)
33+
assert response
34+
assert response.records

documentai/snippets/handle_response_sample_test.py

Lines changed: 15 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -219,3 +219,18 @@ def test_process_document_summarizer(capsys):
219219
]
220220
for expected_string in expected_strings:
221221
assert expected_string in out
222+
223+
224+
def test_process_document_layout():
225+
document = handle_response_sample_v1beta3.process_document_layout_sample(
226+
project_id=os.environ["GOOGLE_CLOUD_PROJECT"],
227+
location="us",
228+
processor_id="85b02a52f356f564",
229+
processor_version="pretrained",
230+
file_path="resources/superconductivity.pdf",
231+
mime_type="application/pdf",
232+
)
233+
234+
assert document
235+
assert document.document_layout
236+
assert document.chunked_document

documentai/snippets/handle_response_sample_v1beta3.py

Lines changed: 45 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -30,8 +30,51 @@
3030
# mime_type = "application/pdf" # Refer to https://cloud.google.com/document-ai/docs/file-types for supported file types
3131

3232
# [END documentai_process_custom_extractor_document]
33+
# [END documentai_process_summarizer_document]
34+
3335

36+
# [START documentai_process_layout_document]
37+
def process_document_layout_sample(
38+
project_id: str,
39+
location: str,
40+
processor_id: str,
41+
processor_version: str,
42+
file_path: str,
43+
mime_type: str,
44+
) -> documentai.Document:
45+
process_options = documentai.ProcessOptions(
46+
layout_config=documentai.ProcessOptions.LayoutConfig(
47+
chunking_config=documentai.ProcessOptions.LayoutConfig.ChunkingConfig(
48+
chunk_size=1000,
49+
include_ancestor_headings=True,
50+
breakpoint_percentile_threshold=90,
51+
)
52+
)
53+
)
3454

55+
document = process_document(
56+
project_id,
57+
location,
58+
processor_id,
59+
processor_version,
60+
file_path,
61+
mime_type,
62+
process_options=process_options,
63+
)
64+
65+
print("Document Layout Blocks")
66+
for block in document.document_layout.blocks:
67+
print(block)
68+
69+
print("Document Chunks")
70+
for chunk in document.chunked_document.chunks:
71+
print(chunk)
72+
73+
# [END documentai_process_layout_document]
74+
return document
75+
76+
77+
# [START documentai_process_summarizer_document]
3578
def process_document_summarizer_sample(
3679
project_id: str,
3780
location: str,
@@ -175,6 +218,7 @@ def print_entity(entity: documentai.Document.Entity) -> None:
175218
print(f" * Normalized Value: {repr(normalized_value)}")
176219

177220

221+
# [START documentai_process_layout_document]
178222
def process_document(
179223
project_id: str,
180224
location: str,
@@ -217,5 +261,6 @@ def process_document(
217261
return result.document
218262

219263

264+
# [END documentai_process_layout_document]
220265
# [END documentai_process_summarizer_document]
221266
# [END documentai_process_custom_extractor_document]
Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1,2 +1,2 @@
1-
google-cloud-documentai==2.20.0
2-
google-cloud-storage==2.9.0
1+
google-cloud-documentai==2.27.0
2+
google-cloud-storage==2.16.0

0 commit comments

Comments
 (0)