Skip to content

Commit ddfef1f

Browse files
committed
Add streaming sample
1 parent 68653cf commit ddfef1f

File tree

1 file changed

+48
-2
lines changed

1 file changed

+48
-2
lines changed

discoveryengine/standalone_apis_sample.py

Lines changed: 48 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -211,9 +211,9 @@ def grounded_generation_google_search_sample(
211211
request = discoveryengine.GenerateGroundedContentRequest(
212212
# The full resource name of the location.
213213
# Format: projects/{project_id}/locations/{location}
214-
location=client.common_location_path(project=project_id, location="us"),
214+
location=client.common_location_path(project=project_id, location="global"),
215215
generation_spec=discoveryengine.GenerateGroundedContentRequest.GenerationSpec(
216-
model_id="gemini-experimental",
216+
model_id="gemini-1.5-flash",
217217
),
218218
# Conversation between user and model
219219
contents=[
@@ -247,3 +247,49 @@ def grounded_generation_google_search_sample(
247247
# [END genappbuilder_grounded_generation_google_search]
248248

249249
return response
250+
251+
252+
def grounded_generation_streaming_sample(
253+
project_id: str,
254+
) -> discoveryengine.GenerateGroundedContentResponse:
255+
# [START genappbuilder_grounded_generation_streaming]
256+
from google.cloud import discoveryengine_v1 as discoveryengine
257+
258+
# TODO(developer): Uncomment these variables before running the sample.
259+
# project_id = "YOUR_PROJECT_ID"
260+
261+
client = discoveryengine.GroundedGenerationServiceClient()
262+
263+
request = discoveryengine.GenerateGroundedContentRequest(
264+
# 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"),
267+
generation_spec=discoveryengine.GenerateGroundedContentRequest.GenerationSpec(
268+
model_id="gemini-1.5-flash",
269+
),
270+
# Conversation between user and model
271+
contents=[
272+
discoveryengine.GroundedGenerationContent(
273+
role="user",
274+
parts=[
275+
discoveryengine.GroundedGenerationContent.Part(
276+
text="Summarize how to delete a data store in Vertex AI Agent Builder?"
277+
)
278+
],
279+
)
280+
],
281+
grounding_spec=discoveryengine.GenerateGroundedContentRequest.GroundingSpec(
282+
grounding_sources=[
283+
discoveryengine.GenerateGroundedContentRequest.GroundingSource(
284+
inline_source=discoveryengine.GenerateGroundedContentRequest.GroundingSource.GoogleSearch()
285+
),
286+
]
287+
),
288+
)
289+
response = client.stream_generate_grounded_content(request)
290+
291+
# Handle the response
292+
print(response)
293+
# [END genappbuilder_grounded_generation_streaming]
294+
295+
return response

0 commit comments

Comments
 (0)