Skip to content

Commit 1e22407

Browse files
docs(genai): Text Embedding Sample for Gen AI SDK (#13172)
--------- Co-authored-by: Sampath Kumar <[email protected]>
1 parent 258aa4e commit 1e22407

File tree

6 files changed

+166
-0
lines changed

6 files changed

+166
-0
lines changed
Lines changed: 45 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,45 @@
1+
# Copyright 2025 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+
# https://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+
def embed_content() -> str:
17+
# [START googlegenaisdk_embeddings_docretrieval_with_txt]
18+
from google import genai
19+
from google.genai.types import EmbedContentConfig
20+
21+
client = genai.Client()
22+
response = client.models.embed_content(
23+
model="text-embedding-005",
24+
contents=[
25+
"How do I get a driver's license/learner's permit?",
26+
"How do I renew my driver's license?",
27+
"How do I change my address on my driver's license?",
28+
],
29+
config=EmbedContentConfig(
30+
task_type="RETRIEVAL_DOCUMENT", # Optional
31+
output_dimensionality=768, # Optional
32+
title="Driver's License", # Optional
33+
),
34+
)
35+
print(response)
36+
# Example response:
37+
# embeddings=[ContentEmbedding(values=[-0.06302902102470398, 0.00928034819662571, 0.014716853387653828, -0.028747491538524628, ... ],
38+
# statistics=ContentEmbeddingStatistics(truncated=False, token_count=13.0))]
39+
# metadata=EmbedContentMetadata(billable_character_count=112)
40+
# [END googlegenaisdk_embeddings_docretrieval_with_txt]
41+
return response
42+
43+
44+
if __name__ == "__main__":
45+
embed_content()
Lines changed: 45 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,45 @@
1+
# Copyright 2025 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+
# https://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+
def embed_content() -> str:
17+
# [START googlegenaisdk_embeddings_docretrieval_with_txt]
18+
from google import genai
19+
from google.genai.types import EmbedContentConfig
20+
21+
client = genai.Client()
22+
response = client.models.embed_content(
23+
model="text-embedding-005",
24+
contents=[
25+
"How do I get a driver's license/learner's permit?",
26+
"How do I renew my driver's license?",
27+
"How do I change my address on my driver's license?",
28+
],
29+
config=EmbedContentConfig(
30+
task_type="RETRIEVAL_DOCUMENT", # Optional
31+
output_dimensionality=768, # Optional
32+
title="Driver's License", # Optional
33+
),
34+
)
35+
print(response)
36+
# Example response:
37+
# embeddings=[ContentEmbedding(values=[-0.06302902102470398, 0.00928034819662571, 0.014716853387653828, -0.028747491538524628, ... ],
38+
# statistics=ContentEmbeddingStatistics(truncated=False, token_count=13.0))]
39+
# metadata=EmbedContentMetadata(billable_character_count=112)
40+
# [END googlegenaisdk_embeddings_docretrieval_with_txt]
41+
return response
42+
43+
44+
if __name__ == "__main__":
45+
embed_content()

genai/embeddings/noxfile_config.py

Lines changed: 42 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,42 @@
1+
# Copyright 2021 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+
# Default TEST_CONFIG_OVERRIDE for python repos.
16+
17+
# You can copy this file into your directory, then it will be imported from
18+
# the noxfile.py.
19+
20+
# The source of truth:
21+
# https://github.com/GoogleCloudPlatform/python-docs-samples/blob/main/noxfile_config.py
22+
23+
TEST_CONFIG_OVERRIDE = {
24+
# You can opt out from the test for specific Python versions.
25+
"ignored_versions": ["2.7", "3.7", "3.8", "3.10", "3.11", "3.13"],
26+
# Old samples are opted out of enforcing Python type hints
27+
# All new samples should feature them
28+
"enforce_type_hints": True,
29+
# An envvar key for determining the project id to use. Change it
30+
# to 'BUILD_SPECIFIC_GCLOUD_PROJECT' if you want to opt in using a
31+
# build specific Cloud project. You can also use your own string
32+
# to use your own Cloud project.
33+
"gcloud_project_env": "GOOGLE_CLOUD_PROJECT",
34+
# 'gcloud_project_env': 'BUILD_SPECIFIC_GCLOUD_PROJECT',
35+
# If you need to use a specific version of pip,
36+
# change pip_version_override to the string representation
37+
# of the version number, for example, "20.2.4"
38+
"pip_version_override": None,
39+
# A dictionary you want to inject into your test. Don't put any
40+
# secrets here. These values will override predefined values.
41+
"envs": {},
42+
}
Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,2 @@
1+
google-api-core==2.24.0
2+
pytest==8.2.0

genai/embeddings/requirements.txt

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1 @@
1+
google-genai==1.2.0
Lines changed: 31 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,31 @@
1+
# Copyright 2025 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+
# https://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+
# Using Google Cloud Vertex AI to test the code samples.
17+
#
18+
19+
import os
20+
21+
import embeddings_docretrieval_with_txt
22+
23+
os.environ["GOOGLE_GENAI_USE_VERTEXAI"] = "True"
24+
os.environ["GOOGLE_CLOUD_LOCATION"] = "us-central1"
25+
# The project name is included in the CICD pipeline
26+
# os.environ['GOOGLE_CLOUD_PROJECT'] = "add-your-project-name"
27+
28+
29+
def test_embeddings_docretrieval_with_txt() -> None:
30+
response = embeddings_docretrieval_with_txt.embed_content()
31+
assert response

0 commit comments

Comments
 (0)