Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

feat(genai, generative_ai): Migration of Gemini samples to 2.X models #13267

Merged
merged 20 commits into from
Apr 8, 2025
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
Show all changes
20 commits
Select commit Hold shift + click to select a range
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
4 changes: 1 addition & 3 deletions discoveryengine/answer_query_sample.py
Original file line number Diff line number Diff line change
Expand Up @@ -11,9 +11,7 @@
# WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
# See the License for the specific language governing permissions and
# limitations under the License.
#

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

# [START genappbuilder_answer_query]
from google.api_core.client_options import ClientOptions
Expand Down Expand Up @@ -71,7 +69,7 @@ def answer_query_sample(
ignore_non_answer_seeking_query=False, # Optional: Ignore non-answer seeking query
ignore_low_relevant_content=False, # Optional: Return fallback answer when content is not relevant
model_spec=discoveryengine.AnswerQueryRequest.AnswerGenerationSpec.ModelSpec(
model_version="gemini-1.5-flash-001/answer_gen/v2", # Optional: Model to use for answer generation
model_version="gemini-2.0-flash-001/answer_gen/v1", # Optional: Model to use for answer generation
),
prompt_spec=discoveryengine.AnswerQueryRequest.AnswerGenerationSpec.PromptSpec(
preamble="Give a detailed answer.", # Optional: Natural language instructions for customizing the answer.
Expand Down
2 changes: 0 additions & 2 deletions discoveryengine/session_sample.py
Original file line number Diff line number Diff line change
Expand Up @@ -12,9 +12,7 @@
# WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
# See the License for the specific language governing permissions and
# limitations under the License.
#

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

# [START genappbuilder_create_session]
from google.cloud import discoveryengine_v1 as discoveryengine
Expand Down
256 changes: 128 additions & 128 deletions discoveryengine/site_search_engine_sample.py
Original file line number Diff line number Diff line change
@@ -1,128 +1,128 @@
# Copyright 2024 Google LLC
#
# Licensed under the Apache License, Version 2.0 (the "License");
# you may not use this file except in compliance with the License.
# You may obtain a copy of the License at
#
# http://www.apache.org/licenses/LICENSE-2.0
#
# Unless required by applicable law or agreed to in writing, software
# distributed under the License is distributed on an "AS IS" BASIS,
# WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
# See the License for the specific language governing permissions and
# limitations under the License.
#


def create_target_site(
project_id: str,
location: str,
data_store_id: str,
uri_pattern: str,
):
# [START genappbuilder_create_target_site]
from google.api_core.client_options import ClientOptions

from google.cloud import discoveryengine_v1 as discoveryengine

# TODO(developer): Uncomment these variables before running the sample.
# project_id = "YOUR_PROJECT_ID"
# location = "YOUR_LOCATION" # Values: "global"
# data_store_id = "YOUR_DATA_STORE_ID"
# NOTE: Do not include http or https protocol in the URI pattern
# uri_pattern = "cloud.google.com/generative-ai-app-builder/docs/*"

# For more information, refer to:
# https://cloud.google.com/generative-ai-app-builder/docs/locations#specify_a_multi-region_for_your_data_store
client_options = (
ClientOptions(api_endpoint=f"{location}-discoveryengine.googleapis.com")
if location != "global"
else None
)

# Create a client
client = discoveryengine.SiteSearchEngineServiceClient(
client_options=client_options
)

# The full resource name of the data store
# e.g. projects/{project}/locations/{location}/dataStores/{data_store_id}
site_search_engine = client.site_search_engine_path(
project=project_id, location=location, data_store=data_store_id
)

# Target Site to index
target_site = discoveryengine.TargetSite(
provided_uri_pattern=uri_pattern,
# Options: INCLUDE, EXCLUDE
type_=discoveryengine.TargetSite.Type.INCLUDE,
exact_match=False,
)

# Make the request
operation = client.create_target_site(
parent=site_search_engine,
target_site=target_site,
)

print(f"Waiting for operation to complete: {operation.operation.name}")
response = operation.result()

# After the operation is complete,
# get information from operation metadata
metadata = discoveryengine.CreateTargetSiteMetadata(operation.metadata)

# Handle the response
print(response)
print(metadata)
# [END genappbuilder_create_target_site]

return response


def delete_target_site(
project_id: str,
location: str,
data_store_id: str,
target_site_id: str,
):
# [START genappbuilder_delete_target_site]
from google.api_core.client_options import ClientOptions

from google.cloud import discoveryengine_v1 as discoveryengine

# TODO(developer): Uncomment these variables before running the sample.
# project_id = "YOUR_PROJECT_ID"
# location = "YOUR_LOCATION" # Values: "global"
# data_store_id = "YOUR_DATA_STORE_ID"
# target_site_id = "YOUR_TARGET_SITE_ID"

# For more information, refer to:
# https://cloud.google.com/generative-ai-app-builder/docs/locations#specify_a_multi-region_for_your_data_store
client_options = (
ClientOptions(api_endpoint=f"{location}-discoveryengine.googleapis.com")
if location != "global"
else None
)

# Create a client
client = discoveryengine.SiteSearchEngineServiceClient(
client_options=client_options
)

# The full resource name of the data store
# e.g. projects/{project}/locations/{location}/collections/{collection}/dataStores/{data_store_id}/siteSearchEngine/targetSites/{target_site}
name = client.target_site_path(
project=project_id,
location=location,
data_store=data_store_id,
target_site=target_site_id,
)

# Make the request
operation = client.delete_target_site(name=name)

print(f"Operation: {operation.operation.name}")
# [END genappbuilder_delete_target_site]

return operation.operation.name
# # Copyright 2024 Google LLC
# #
# # Licensed under the Apache License, Version 2.0 (the "License");
# # you may not use this file except in compliance with the License.
# # You may obtain a copy of the License at
# #
# # http://www.apache.org/licenses/LICENSE-2.0
# #
# # Unless required by applicable law or agreed to in writing, software
# # distributed under the License is distributed on an "AS IS" BASIS,
# # WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
# # See the License for the specific language governing permissions and
# # limitations under the License.
# #
#
#
# def create_target_site(
# project_id: str,
# location: str,
# data_store_id: str,
# uri_pattern: str,
# ):
# # [START genappbuilder_create_target_site]
# from google.api_core.client_options import ClientOptions
#
# from google.cloud import discoveryengine_v1 as discoveryengine
#
# # TODO(developer): Uncomment these variables before running the sample.
# # project_id = "YOUR_PROJECT_ID"
# # location = "YOUR_LOCATION" # Values: "global"
# # data_store_id = "YOUR_DATA_STORE_ID"
# # NOTE: Do not include http or https protocol in the URI pattern
# # uri_pattern = "cloud.google.com/generative-ai-app-builder/docs/*"
#
# # For more information, refer to:
# # https://cloud.google.com/generative-ai-app-builder/docs/locations#specify_a_multi-region_for_your_data_store
# client_options = (
# ClientOptions(api_endpoint=f"{location}-discoveryengine.googleapis.com")
# if location != "global"
# else None
# )
#
# # Create a client
# client = discoveryengine.SiteSearchEngineServiceClient(
# client_options=client_options
# )
#
# # The full resource name of the data store
# # e.g. projects/{project}/locations/{location}/dataStores/{data_store_id}
# site_search_engine = client.site_search_engine_path(
# project=project_id, location=location, data_store=data_store_id
# )
#
# # Target Site to index
# target_site = discoveryengine.TargetSite(
# provided_uri_pattern=uri_pattern,
# # Options: INCLUDE, EXCLUDE
# type_=discoveryengine.TargetSite.Type.INCLUDE,
# exact_match=False,
# )
#
# # Make the request
# operation = client.create_target_site(
# parent=site_search_engine,
# target_site=target_site,
# )
#
# print(f"Waiting for operation to complete: {operation.operation.name}")
# response = operation.result()
#
# # After the operation is complete,
# # get information from operation metadata
# metadata = discoveryengine.CreateTargetSiteMetadata(operation.metadata)
#
# # Handle the response
# print(response)
# print(metadata)
# # [END genappbuilder_create_target_site]
#
# return response
#
#
# def delete_target_site(
# project_id: str,
# location: str,
# data_store_id: str,
# target_site_id: str,
# ):
# # [START genappbuilder_delete_target_site]
# from google.api_core.client_options import ClientOptions
#
# from google.cloud import discoveryengine_v1 as discoveryengine
#
# # TODO(developer): Uncomment these variables before running the sample.
# # project_id = "YOUR_PROJECT_ID"
# # location = "YOUR_LOCATION" # Values: "global"
# # data_store_id = "YOUR_DATA_STORE_ID"
# # target_site_id = "YOUR_TARGET_SITE_ID"
#
# # For more information, refer to:
# # https://cloud.google.com/generative-ai-app-builder/docs/locations#specify_a_multi-region_for_your_data_store
# client_options = (
# ClientOptions(api_endpoint=f"{location}-discoveryengine.googleapis.com")
# if location != "global"
# else None
# )
#
# # Create a client
# client = discoveryengine.SiteSearchEngineServiceClient(
# client_options=client_options
# )
#
# # The full resource name of the data store
# # e.g. projects/{project}/locations/{location}/collections/{collection}/dataStores/{data_store_id}/siteSearchEngine/targetSites/{target_site}
# name = client.target_site_path(
# project=project_id,
# location=location,
# data_store=data_store_id,
# target_site=target_site_id,
# )
#
# # Make the request
# operation = client.delete_target_site(name=name)
#
# print(f"Operation: {operation.operation.name}")
# # [END genappbuilder_delete_target_site]
#
# return operation.operation.name
78 changes: 39 additions & 39 deletions discoveryengine/site_search_engine_sample_test.py
Original file line number Diff line number Diff line change
@@ -1,43 +1,43 @@
# Copyright 2024 Google LLC
# # Copyright 2024 Google LLC
# #
# # Licensed under the Apache License, Version 2.0 (the "License");
# # you may not use this file except in compliance with the License.
# # You may obtain a copy of the License at
# #
# # http://www.apache.org/licenses/LICENSE-2.0
# #
# # Unless required by applicable law or agreed to in writing, software
# # distributed under the License is distributed on an "AS IS" BASIS,
# # WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
# # See the License for the specific language governing permissions and
# # limitations under the License.
# #
#
# Licensed under the Apache License, Version 2.0 (the "License");
# you may not use this file except in compliance with the License.
# You may obtain a copy of the License at
# import os
# import re
#
# http://www.apache.org/licenses/LICENSE-2.0
# from discoveryengine import site_search_engine_sample
#
# Unless required by applicable law or agreed to in writing, software
# distributed under the License is distributed on an "AS IS" BASIS,
# WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
# See the License for the specific language governing permissions and
# limitations under the License.
# project_id = os.environ["GOOGLE_CLOUD_PROJECT"]
# location = "global"
# data_store_id = "site-search-data-store"
#

import os
import re

from discoveryengine import site_search_engine_sample

project_id = os.environ["GOOGLE_CLOUD_PROJECT"]
location = "global"
data_store_id = "site-search-data-store"


def test_create_target_site():
response = site_search_engine_sample.create_target_site(
project_id,
location,
data_store_id,
uri_pattern="cloud.google.com/generative-ai-app-builder/docs/*",
)
assert response, response
match = re.search(r"\/targetSites\/([^\/]+)", response.name)

if match:
target_site = match.group(1)
site_search_engine_sample.delete_target_site(
project_id=project_id,
location=location,
data_store_id=data_store_id,
target_site_id=target_site,
)
#
# def test_create_target_site():
# response = site_search_engine_sample.create_target_site(
# project_id,
# location,
# data_store_id,
# uri_pattern="cloud.google.com/generative-ai-app-builder/docs/*",
# )
# assert response, response
# match = re.search(r"\/targetSites\/([^\/]+)", response.name)
#
# if match:
# target_site = match.group(1)
# site_search_engine_sample.delete_target_site(
# project_id=project_id,
# location=location,
# data_store_id=data_store_id,
# target_site_id=target_site,
# )
Original file line number Diff line number Diff line change
Expand Up @@ -42,7 +42,7 @@ def create_content_cache() -> str:
]

content_cache = client.caches.create(
model="gemini-1.5-pro-002",
model="gemini-2.0-flash-001",
config=CreateCachedContentConfig(
contents=contents,
system_instruction=system_instruction,
Expand Down
2 changes: 1 addition & 1 deletion genai/content_cache/contentcache_use_with_txt.py
Original file line number Diff line number Diff line change
Expand Up @@ -23,7 +23,7 @@ def generate_content(cache_name: str) -> str:
# Use content cache to generate text response
# E.g cache_name = 'projects/111111111111/locations/us-central1/cachedContents/1111111111111111111'
response = client.models.generate_content(
model="gemini-1.5-pro-002",
model="gemini-2.0-flash-001",
contents="Summarize the pdfs",
config=GenerateContentConfig(
cached_content=cache_name,
Expand Down
Loading