Skip to content

Commit 2c06813

Browse files
feat(genai, generative_ai): Migration of Gemini samples to 2.X models (#13267)
* feat(genai): update the gemini model name * feat(genai): cleanup * feat(genai): cleanup * feat(genai): cleanup * feat(genai): update * Update discoveryengine/answer_query_sample.py * feat(genai): update * feat(genai): update * feat(genai): cleanup * feat(genai): cleanup * re-add context caching samples with Gemini 2.0 * Re-add Basic Example for text generation with Vertex AI SDK for Gemini 2.0 Flash Used in https://cloud.google.com/vertex-ai/generative-ai/docs/migrate-to-v2 * Move chat completions function calling sample to chat_completions directory * Restore Reasoning Engine/Agent Engine * feat(genai): add 2.0 chat function calling examples * feat(genai): update chat function calling examples location as per snippet bot test * fix: update requirements.txt * clean: comment out un-used samples * clean: comment out un-used samples --------- Co-authored-by: Holt Skinner <[email protected]> Co-authored-by: Holt Skinner <[email protected]>
1 parent 47d7704 commit 2c06813

File tree

173 files changed

+422
-6822
lines changed

Some content is hidden

Large Commits have some content hidden by default. Use the searchbox below for content that may be hidden.

173 files changed

+422
-6822
lines changed

discoveryengine/answer_query_sample.py

+1-3
Original file line numberDiff line numberDiff line change
@@ -11,9 +11,7 @@
1111
# WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
1212
# See the License for the specific language governing permissions and
1313
# limitations under the License.
14-
#
1514

16-
# NOTE: This snippet has been partially generated by `gemini-1.5-pro-001`
1715

1816
# [START genappbuilder_answer_query]
1917
from google.api_core.client_options import ClientOptions
@@ -71,7 +69,7 @@ def answer_query_sample(
7169
ignore_non_answer_seeking_query=False, # Optional: Ignore non-answer seeking query
7270
ignore_low_relevant_content=False, # Optional: Return fallback answer when content is not relevant
7371
model_spec=discoveryengine.AnswerQueryRequest.AnswerGenerationSpec.ModelSpec(
74-
model_version="gemini-1.5-flash-001/answer_gen/v2", # Optional: Model to use for answer generation
72+
model_version="gemini-2.0-flash-001/answer_gen/v1", # Optional: Model to use for answer generation
7573
),
7674
prompt_spec=discoveryengine.AnswerQueryRequest.AnswerGenerationSpec.PromptSpec(
7775
preamble="Give a detailed answer.", # Optional: Natural language instructions for customizing the answer.

discoveryengine/session_sample.py

-2
Original file line numberDiff line numberDiff line change
@@ -12,9 +12,7 @@
1212
# WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
1313
# See the License for the specific language governing permissions and
1414
# limitations under the License.
15-
#
1615

17-
# NOTE: This snippet has been partially generated by `gemini-1.5-pro-001`
1816

1917
# [START genappbuilder_create_session]
2018
from google.cloud import discoveryengine_v1 as discoveryengine
+128-128
Original file line numberDiff line numberDiff line change
@@ -1,128 +1,128 @@
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-
17-
def create_target_site(
18-
project_id: str,
19-
location: str,
20-
data_store_id: str,
21-
uri_pattern: str,
22-
):
23-
# [START genappbuilder_create_target_site]
24-
from google.api_core.client_options import ClientOptions
25-
26-
from google.cloud import discoveryengine_v1 as discoveryengine
27-
28-
# TODO(developer): Uncomment these variables before running the sample.
29-
# project_id = "YOUR_PROJECT_ID"
30-
# location = "YOUR_LOCATION" # Values: "global"
31-
# data_store_id = "YOUR_DATA_STORE_ID"
32-
# NOTE: Do not include http or https protocol in the URI pattern
33-
# uri_pattern = "cloud.google.com/generative-ai-app-builder/docs/*"
34-
35-
# For more information, refer to:
36-
# https://cloud.google.com/generative-ai-app-builder/docs/locations#specify_a_multi-region_for_your_data_store
37-
client_options = (
38-
ClientOptions(api_endpoint=f"{location}-discoveryengine.googleapis.com")
39-
if location != "global"
40-
else None
41-
)
42-
43-
# Create a client
44-
client = discoveryengine.SiteSearchEngineServiceClient(
45-
client_options=client_options
46-
)
47-
48-
# The full resource name of the data store
49-
# e.g. projects/{project}/locations/{location}/dataStores/{data_store_id}
50-
site_search_engine = client.site_search_engine_path(
51-
project=project_id, location=location, data_store=data_store_id
52-
)
53-
54-
# Target Site to index
55-
target_site = discoveryengine.TargetSite(
56-
provided_uri_pattern=uri_pattern,
57-
# Options: INCLUDE, EXCLUDE
58-
type_=discoveryengine.TargetSite.Type.INCLUDE,
59-
exact_match=False,
60-
)
61-
62-
# Make the request
63-
operation = client.create_target_site(
64-
parent=site_search_engine,
65-
target_site=target_site,
66-
)
67-
68-
print(f"Waiting for operation to complete: {operation.operation.name}")
69-
response = operation.result()
70-
71-
# After the operation is complete,
72-
# get information from operation metadata
73-
metadata = discoveryengine.CreateTargetSiteMetadata(operation.metadata)
74-
75-
# Handle the response
76-
print(response)
77-
print(metadata)
78-
# [END genappbuilder_create_target_site]
79-
80-
return response
81-
82-
83-
def delete_target_site(
84-
project_id: str,
85-
location: str,
86-
data_store_id: str,
87-
target_site_id: str,
88-
):
89-
# [START genappbuilder_delete_target_site]
90-
from google.api_core.client_options import ClientOptions
91-
92-
from google.cloud import discoveryengine_v1 as discoveryengine
93-
94-
# TODO(developer): Uncomment these variables before running the sample.
95-
# project_id = "YOUR_PROJECT_ID"
96-
# location = "YOUR_LOCATION" # Values: "global"
97-
# data_store_id = "YOUR_DATA_STORE_ID"
98-
# target_site_id = "YOUR_TARGET_SITE_ID"
99-
100-
# For more information, refer to:
101-
# https://cloud.google.com/generative-ai-app-builder/docs/locations#specify_a_multi-region_for_your_data_store
102-
client_options = (
103-
ClientOptions(api_endpoint=f"{location}-discoveryengine.googleapis.com")
104-
if location != "global"
105-
else None
106-
)
107-
108-
# Create a client
109-
client = discoveryengine.SiteSearchEngineServiceClient(
110-
client_options=client_options
111-
)
112-
113-
# The full resource name of the data store
114-
# e.g. projects/{project}/locations/{location}/collections/{collection}/dataStores/{data_store_id}/siteSearchEngine/targetSites/{target_site}
115-
name = client.target_site_path(
116-
project=project_id,
117-
location=location,
118-
data_store=data_store_id,
119-
target_site=target_site_id,
120-
)
121-
122-
# Make the request
123-
operation = client.delete_target_site(name=name)
124-
125-
print(f"Operation: {operation.operation.name}")
126-
# [END genappbuilder_delete_target_site]
127-
128-
return operation.operation.name
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+
#
17+
# def create_target_site(
18+
# project_id: str,
19+
# location: str,
20+
# data_store_id: str,
21+
# uri_pattern: str,
22+
# ):
23+
# # [START genappbuilder_create_target_site]
24+
# from google.api_core.client_options import ClientOptions
25+
#
26+
# from google.cloud import discoveryengine_v1 as discoveryengine
27+
#
28+
# # TODO(developer): Uncomment these variables before running the sample.
29+
# # project_id = "YOUR_PROJECT_ID"
30+
# # location = "YOUR_LOCATION" # Values: "global"
31+
# # data_store_id = "YOUR_DATA_STORE_ID"
32+
# # NOTE: Do not include http or https protocol in the URI pattern
33+
# # uri_pattern = "cloud.google.com/generative-ai-app-builder/docs/*"
34+
#
35+
# # For more information, refer to:
36+
# # https://cloud.google.com/generative-ai-app-builder/docs/locations#specify_a_multi-region_for_your_data_store
37+
# client_options = (
38+
# ClientOptions(api_endpoint=f"{location}-discoveryengine.googleapis.com")
39+
# if location != "global"
40+
# else None
41+
# )
42+
#
43+
# # Create a client
44+
# client = discoveryengine.SiteSearchEngineServiceClient(
45+
# client_options=client_options
46+
# )
47+
#
48+
# # The full resource name of the data store
49+
# # e.g. projects/{project}/locations/{location}/dataStores/{data_store_id}
50+
# site_search_engine = client.site_search_engine_path(
51+
# project=project_id, location=location, data_store=data_store_id
52+
# )
53+
#
54+
# # Target Site to index
55+
# target_site = discoveryengine.TargetSite(
56+
# provided_uri_pattern=uri_pattern,
57+
# # Options: INCLUDE, EXCLUDE
58+
# type_=discoveryengine.TargetSite.Type.INCLUDE,
59+
# exact_match=False,
60+
# )
61+
#
62+
# # Make the request
63+
# operation = client.create_target_site(
64+
# parent=site_search_engine,
65+
# target_site=target_site,
66+
# )
67+
#
68+
# print(f"Waiting for operation to complete: {operation.operation.name}")
69+
# response = operation.result()
70+
#
71+
# # After the operation is complete,
72+
# # get information from operation metadata
73+
# metadata = discoveryengine.CreateTargetSiteMetadata(operation.metadata)
74+
#
75+
# # Handle the response
76+
# print(response)
77+
# print(metadata)
78+
# # [END genappbuilder_create_target_site]
79+
#
80+
# return response
81+
#
82+
#
83+
# def delete_target_site(
84+
# project_id: str,
85+
# location: str,
86+
# data_store_id: str,
87+
# target_site_id: str,
88+
# ):
89+
# # [START genappbuilder_delete_target_site]
90+
# from google.api_core.client_options import ClientOptions
91+
#
92+
# from google.cloud import discoveryengine_v1 as discoveryengine
93+
#
94+
# # TODO(developer): Uncomment these variables before running the sample.
95+
# # project_id = "YOUR_PROJECT_ID"
96+
# # location = "YOUR_LOCATION" # Values: "global"
97+
# # data_store_id = "YOUR_DATA_STORE_ID"
98+
# # target_site_id = "YOUR_TARGET_SITE_ID"
99+
#
100+
# # For more information, refer to:
101+
# # https://cloud.google.com/generative-ai-app-builder/docs/locations#specify_a_multi-region_for_your_data_store
102+
# client_options = (
103+
# ClientOptions(api_endpoint=f"{location}-discoveryengine.googleapis.com")
104+
# if location != "global"
105+
# else None
106+
# )
107+
#
108+
# # Create a client
109+
# client = discoveryengine.SiteSearchEngineServiceClient(
110+
# client_options=client_options
111+
# )
112+
#
113+
# # The full resource name of the data store
114+
# # e.g. projects/{project}/locations/{location}/collections/{collection}/dataStores/{data_store_id}/siteSearchEngine/targetSites/{target_site}
115+
# name = client.target_site_path(
116+
# project=project_id,
117+
# location=location,
118+
# data_store=data_store_id,
119+
# target_site=target_site_id,
120+
# )
121+
#
122+
# # Make the request
123+
# operation = client.delete_target_site(name=name)
124+
#
125+
# print(f"Operation: {operation.operation.name}")
126+
# # [END genappbuilder_delete_target_site]
127+
#
128+
# return operation.operation.name
Original file line numberDiff line numberDiff line change
@@ -1,43 +1,43 @@
1-
# Copyright 2024 Google LLC
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+
# #
215
#
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
16+
# import os
17+
# import re
618
#
7-
# http://www.apache.org/licenses/LICENSE-2.0
19+
# from discoveryengine import site_search_engine_sample
820
#
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.
21+
# project_id = os.environ["GOOGLE_CLOUD_PROJECT"]
22+
# location = "global"
23+
# data_store_id = "site-search-data-store"
1424
#
15-
16-
import os
17-
import re
18-
19-
from discoveryengine import site_search_engine_sample
20-
21-
project_id = os.environ["GOOGLE_CLOUD_PROJECT"]
22-
location = "global"
23-
data_store_id = "site-search-data-store"
24-
25-
26-
def test_create_target_site():
27-
response = site_search_engine_sample.create_target_site(
28-
project_id,
29-
location,
30-
data_store_id,
31-
uri_pattern="cloud.google.com/generative-ai-app-builder/docs/*",
32-
)
33-
assert response, response
34-
match = re.search(r"\/targetSites\/([^\/]+)", response.name)
35-
36-
if match:
37-
target_site = match.group(1)
38-
site_search_engine_sample.delete_target_site(
39-
project_id=project_id,
40-
location=location,
41-
data_store_id=data_store_id,
42-
target_site_id=target_site,
43-
)
25+
#
26+
# def test_create_target_site():
27+
# response = site_search_engine_sample.create_target_site(
28+
# project_id,
29+
# location,
30+
# data_store_id,
31+
# uri_pattern="cloud.google.com/generative-ai-app-builder/docs/*",
32+
# )
33+
# assert response, response
34+
# match = re.search(r"\/targetSites\/([^\/]+)", response.name)
35+
#
36+
# if match:
37+
# target_site = match.group(1)
38+
# site_search_engine_sample.delete_target_site(
39+
# project_id=project_id,
40+
# location=location,
41+
# data_store_id=data_store_id,
42+
# target_site_id=target_site,
43+
# )

genai/content_cache/contentcache_create_with_txt_gcs_pdf.py

+1-1
Original file line numberDiff line numberDiff line change
@@ -42,7 +42,7 @@ def create_content_cache() -> str:
4242
]
4343

4444
content_cache = client.caches.create(
45-
model="gemini-1.5-pro-002",
45+
model="gemini-2.0-flash-001",
4646
config=CreateCachedContentConfig(
4747
contents=contents,
4848
system_instruction=system_instruction,

genai/content_cache/contentcache_use_with_txt.py

+1-1
Original file line numberDiff line numberDiff line change
@@ -23,7 +23,7 @@ def generate_content(cache_name: str) -> str:
2323
# Use content cache to generate text response
2424
# E.g cache_name = 'projects/111111111111/locations/us-central1/cachedContents/1111111111111111111'
2525
response = client.models.generate_content(
26-
model="gemini-1.5-pro-002",
26+
model="gemini-2.0-flash-001",
2727
contents="Summarize the pdfs",
2828
config=GenerateContentConfig(
2929
cached_content=cache_name,

0 commit comments

Comments
 (0)