Skip to content

Commit 989fc28

Browse files
committed
Update library imports to v1 and fix Grounded Generation Samples
1 parent 7c42d0e commit 989fc28

File tree

1 file changed

+34
-24
lines changed

1 file changed

+34
-24
lines changed

discoveryengine/standalone_apis_sample.py

Lines changed: 34 additions & 24 deletions
Original file line numberDiff line numberDiff line change
@@ -13,14 +13,14 @@
1313
# limitations under the License.
1414
#
1515

16-
from google.cloud import discoveryengine_v1alpha as discoveryengine
16+
from google.cloud import discoveryengine_v1 as discoveryengine
1717

1818

1919
def check_grounding_sample(
2020
project_id: str,
2121
) -> discoveryengine.CheckGroundingResponse:
2222
# [START genappbuilder_check_grounding]
23-
from google.cloud import discoveryengine_v1alpha as discoveryengine
23+
from google.cloud import discoveryengine_v1 as discoveryengine
2424

2525
# TODO(developer): Uncomment these variables before running the sample.
2626
# project_id = "YOUR_PROJECT_ID"
@@ -78,7 +78,7 @@ def rank_sample(
7878
project_id: str,
7979
) -> discoveryengine.RankResponse:
8080
# [START genappbuilder_rank]
81-
from google.cloud import discoveryengine_v1alpha as discoveryengine
81+
from google.cloud import discoveryengine_v1 as discoveryengine
8282

8383
# TODO(developer): Uncomment these variables before running the sample.
8484
# project_id = "YOUR_PROJECT_ID"
@@ -126,23 +126,22 @@ def rank_sample(
126126

127127

128128
def grounded_generation_inline_vais_sample(
129-
project_id: str,
130-
serving_config: str,
129+
project_number: str,
130+
engine_id: str,
131131
) -> discoveryengine.GenerateGroundedContentResponse:
132132
# [START genappbuilder_grounded_generation_inline_vais]
133133
from google.cloud import discoveryengine_v1 as discoveryengine
134134

135135
# TODO(developer): Uncomment these variables before running the sample.
136-
# project_id = "YOUR_PROJECT_ID"
137-
# The full resource name of the serving config for a Vertex AI Search App
138-
# serving_config = "projects/{project_id}/locations/global/collections/default_collection/engines/{engine_id}/servingConfigs/default_search"
136+
# project_number = "YOUR_PROJECT_NUMBER"
137+
# engine_id = "YOUR_ENGINE_ID"
139138

140139
client = discoveryengine.GroundedGenerationServiceClient()
141140

142141
request = discoveryengine.GenerateGroundedContentRequest(
143142
# The full resource name of the location.
144-
# Format: projects/{project_id}/locations/{location}
145-
location=client.common_location_path(project=project_id, location="global"),
143+
# Format: projects/{project_number}/locations/{location}
144+
location=client.common_location_path(project=project_number, location="global"),
146145
generation_spec=discoveryengine.GenerateGroundedContentRequest.GenerationSpec(
147146
model_id="gemini-1.5-flash",
148147
),
@@ -182,8 +181,11 @@ def grounded_generation_inline_vais_sample(
182181
]
183182
),
184183
),
185-
discoveryengine.GenerateGroundedContentRequest.GroundingSource.SearchSource(
186-
serving_config=serving_config,
184+
discoveryengine.GenerateGroundedContentRequest.GroundingSource(
185+
search_source=discoveryengine.GenerateGroundedContentRequest.GroundingSource.SearchSource(
186+
# The full resource name of the serving config for a Vertex AI Search App
187+
serving_config=f"projects/{project_number}/locations/global/collections/default_collection/engines/{engine_id}/servingConfigs/default_search",
188+
),
187189
),
188190
]
189191
),
@@ -198,20 +200,20 @@ def grounded_generation_inline_vais_sample(
198200

199201

200202
def grounded_generation_google_search_sample(
201-
project_id: str,
203+
project_number: str,
202204
) -> discoveryengine.GenerateGroundedContentResponse:
203205
# [START genappbuilder_grounded_generation_google_search]
204206
from google.cloud import discoveryengine_v1 as discoveryengine
205207

206208
# TODO(developer): Uncomment these variables before running the sample.
207-
# project_id = "YOUR_PROJECT_ID"
209+
# project_number = "YOUR_PROJECT_NUMBER"
208210

209211
client = discoveryengine.GroundedGenerationServiceClient()
210212

211213
request = discoveryengine.GenerateGroundedContentRequest(
212214
# The full resource name of the location.
213-
# Format: projects/{project_id}/locations/{location}
214-
location=client.common_location_path(project=project_id, location="global"),
215+
# Format: projects/{project_number}/locations/{location}
216+
location=client.common_location_path(project=project_number, location="global"),
215217
generation_spec=discoveryengine.GenerateGroundedContentRequest.GenerationSpec(
216218
model_id="gemini-1.5-flash",
217219
),
@@ -235,7 +237,14 @@ def grounded_generation_google_search_sample(
235237
grounding_spec=discoveryengine.GenerateGroundedContentRequest.GroundingSpec(
236238
grounding_sources=[
237239
discoveryengine.GenerateGroundedContentRequest.GroundingSource(
238-
inline_source=discoveryengine.GenerateGroundedContentRequest.GroundingSource.GoogleSearch()
240+
google_search_source=discoveryengine.GenerateGroundedContentRequest.GroundingSource.GoogleSearchSource(
241+
# Optional: For Dynamic Retrieval
242+
dynamic_retrieval_config=discoveryengine.GenerateGroundedContentRequest.DynamicRetrievalConfiguration(
243+
predictor=discoveryengine.GenerateGroundedContentRequest.DynamicRetrievalConfiguration.DynamicRetrievalPredictor(
244+
threshold=0.7
245+
)
246+
)
247+
)
239248
),
240249
]
241250
),
@@ -250,7 +259,7 @@ def grounded_generation_google_search_sample(
250259

251260

252261
def grounded_generation_streaming_sample(
253-
project_id: str,
262+
project_number: str,
254263
) -> discoveryengine.GenerateGroundedContentResponse:
255264
# [START genappbuilder_grounded_generation_streaming]
256265
from google.cloud import discoveryengine_v1 as discoveryengine
@@ -262,8 +271,8 @@ def grounded_generation_streaming_sample(
262271

263272
request = discoveryengine.GenerateGroundedContentRequest(
264273
# The full resource name of the location.
265-
# Format: projects/{project_id}/locations/{location}
266-
location=client.common_location_path(project=project_id, location="global"),
274+
# Format: projects/{project_number}/locations/{location}
275+
location=client.common_location_path(project=project_number, location="global"),
267276
generation_spec=discoveryengine.GenerateGroundedContentRequest.GenerationSpec(
268277
model_id="gemini-1.5-flash",
269278
),
@@ -281,15 +290,16 @@ def grounded_generation_streaming_sample(
281290
grounding_spec=discoveryengine.GenerateGroundedContentRequest.GroundingSpec(
282291
grounding_sources=[
283292
discoveryengine.GenerateGroundedContentRequest.GroundingSource(
284-
inline_source=discoveryengine.GenerateGroundedContentRequest.GroundingSource.GoogleSearch()
293+
google_search_source=discoveryengine.GenerateGroundedContentRequest.GroundingSource.GoogleSearchSource()
285294
),
286295
]
287296
),
288297
)
289-
response = client.stream_generate_grounded_content(request)
298+
responses = client.stream_generate_grounded_content(iter([request]))
290299

291-
# Handle the response
292-
print(response)
300+
for response in responses:
301+
# Handle the response
302+
print(response)
293303
# [END genappbuilder_grounded_generation_streaming]
294304

295305
return response

0 commit comments

Comments
 (0)