Skip to content

Commit 965b317

Browse files
committed
Merge branch 'refs/heads/main' into queued-resources-create-spot-delete
2 parents 041e318 + 5add740 commit 965b317

26 files changed

+1075
-117
lines changed

.kokoro/tests/run_tests.sh

Lines changed: 7 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -98,6 +98,13 @@ fi
9898
# install nox for testing
9999
pip install --user -q nox
100100

101+
# Use secrets acessor service account to get secrets
102+
if [[ -f "${KOKORO_GFILE_DIR}/secrets_viewer_service_account.json" ]]; then
103+
gcloud auth activate-service-account \
104+
--key-file="${KOKORO_GFILE_DIR}/secrets_viewer_service_account.json" \
105+
--project="cloud-devrel-kokoro-resources"
106+
fi
107+
101108
# On kokoro, we should be able to use the default service account. We
102109
# need to somehow bootstrap the secrets on other CI systems.
103110
if [[ "${TRAMPOLINE_CI}" == "kokoro" ]]; then

generative_ai/README.md

Lines changed: 175 additions & 84 deletions
Original file line numberDiff line numberDiff line change
@@ -1,84 +1,175 @@
1-
# Generative AI on Google Cloud
2-
3-
* Product Page: https://cloud.google.com/ai/generative-ai?hl=en
4-
* Code samples: https://cloud.google.com/docs/samples?text=Generative%20AI
5-
6-
## Samples Style Guide
7-
8-
If you are interested in code sample contributions, see [Contributing Guide](https://github.com/GoogleCloudPlatform/python-docs-samples/blob/main/CONTRIBUTING.md).
9-
10-
Only for Generative AI Python samples, below style guide instructions take precedence over [Samples Style Guide](https://googlecloudplatform.github.io/samples-style-guide/).
11-
12-
### 1. Use Scripting format
13-
14-
Wrap the imports, sample code and the region tags to be with in one function definition.
15-
This is to keep the region tag section code to be in script format and also allowing you to write regular testcases.
16-
17-
> This change is motivated by the desire to provide code samples in a copy-paste-run
18-
format that is helpful for popular data science community tools like
19-
Google Colab, Jupyter Notebooks, and IPython shell.
20-
21-
Here is an example code.
22-
23-
```python
24-
def create_hello_world_file(filename):
25-
# <region tag: starts here>
26-
import os
27-
28-
# TODO(developer): Update and uncomment below code
29-
# filename = `/tmp/test.txt`
30-
31-
if os.path.isfile(filename):
32-
print(f'Overriding content in file(name: {filename})!')
33-
34-
with open(filename) as fp:
35-
fp.write('Hello world!')
36-
# <region tag: ends here>
37-
```
38-
39-
In Google Cloud documentation, this code sample will be shown as below
40-
41-
```python
42-
import os
43-
44-
# TODO(developer): Update and uncomment below code
45-
# filename = `/tmp/test.txt`
46-
47-
if os.path.isfile(filename):
48-
print(f'Overriding content in file(name: {filename})!')
49-
50-
with open(filename) as fp:
51-
fp.write('Hello world!')
52-
```
53-
54-
**Note:** In the above sample, `imports` are include and `TODO's` are provided to variable
55-
that need to update by users.
56-
57-
### 2. Avoid Hidden Variables
58-
59-
Suggestion to avoid hidden variables in code samples
60-
61-
* Use global variables, to defined common variables like PROJECT_ID, LOCATION.
62-
63-
* Keep the function definitions simple, with less or no arguments.
64-
* Ex: Use `def generate_text() -> str:` instead of `def generate_text(temperature=..image_path=..video_path=..) -> str:`
65-
66-
* Use descriptive variables names and if need, use long variable name.
67-
* Ex: Use `text_input` instead of `user_input` or `input_var` or `myvar` ...
68-
69-
* Resist the temptation to tell more
70-
* Ex: Don't define unused optional arguments
71-
* Ex: Use `Read more @ http://..` than explaining `video_config looks like {"foo": "bar",...}`
72-
73-
**Note**: Not all the samples are the same! "Avoid Hidden variables" is not same as "Dont/No Hidden Variables".
74-
75-
76-
## Conclusion
77-
78-
To summarize, it's crucial to maintain the simplicity and brevity of your code
79-
examples.
80-
> The ideal sample is one that appears self-evident and immediately
81-
comprehensible.
82-
83-
84-
1+
# Generative AI Samples on Google Cloud
2+
3+
Welcome to the Python samples folder for Generative AI on Vertex AI! In this folder, you can find the Python samples
4+
used in [Google Cloud Generative AI documentation](https://cloud.google.com/ai/generative-ai?hl=en).
5+
6+
If you are looking for colab notebook, then this [link](https://github.com/GoogleCloudPlatform/generative-ai/tree/main).
7+
8+
## Getting Started
9+
10+
To try and run these Code samples, we have following recommend using Google Cloud IDE or Google Colab.
11+
12+
Note: A Google Cloud Project is a pre-requisite.
13+
14+
### Feature folders
15+
16+
Browse the folders below to find the Generative AI capabilities you're interested in.
17+
18+
<table>
19+
<tr>
20+
<td><strong>Python Samples Folder</strong>
21+
</td>
22+
<td><strong>Google Cloud Product</strong>
23+
</td>
24+
<td><strong>Short Description (With the help of Gemini 1.5)</strong>
25+
</td>
26+
</tr>
27+
<tr>
28+
<td><a href="https://github.com/GoogleCloudPlatform/python-docs-samples/tree/main/generative_ai/context_caching">Context Caching</a>
29+
</td>
30+
<td><a href="https://cloud.google.com/vertex-ai/generative-ai/docs/context-cache/context-cache-overview">https://cloud.google.com/vertex-ai/generative-ai/docs/context-cache/context-cache-overview</a>
31+
</td>
32+
<td>Code samples demonstrating how to use context caching with Vertex AI's generative models. This allows for more consistent and relevant responses across multiple interactions by storing previous conversation history.
33+
</td>
34+
</tr>
35+
<tr>
36+
<td><a href="https://github.com/GoogleCloudPlatform/python-docs-samples/tree/main/generative_ai/controlled_generation">Controlled Generation</a>
37+
</td>
38+
<td><a href="https://cloud.google.com/vertex-ai/generative-ai/docs/multimodal/control-generated-output">https://cloud.google.com/vertex-ai/generative-ai/docs/multimodal/control-generated-output</a>
39+
</td>
40+
<td>Examples of how to control the output of generative models, such as specifying length, format, or sentiment.
41+
</td>
42+
</tr>
43+
<tr>
44+
<td><a href="https://github.com/GoogleCloudPlatform/python-docs-samples/tree/main/generative_ai/count_token">Count Token</a>
45+
</td>
46+
<td><a href="https://cloud.google.com/vertex-ai/generative-ai/docs/multimodal/list-token">https://cloud.google.com/vertex-ai/generative-ai/docs/multimodal/list-token</a>
47+
</td>
48+
<td>Code demonstrating how to count tokens in text, which is crucial for managing costs and understanding model limitations.
49+
</td>
50+
</tr>
51+
<tr>
52+
<td><a href="https://github.com/GoogleCloudPlatform/python-docs-samples/tree/main/generative_ai/embeddings">Embeddings</a>
53+
</td>
54+
<td><a href="https://cloud.google.com/vertex-ai/generative-ai/docs/embeddings">https://cloud.google.com/vertex-ai/generative-ai/docs/embeddings</a>
55+
</td>
56+
<td>Code showing how to generate and use embeddings from text or images. Embeddings can be used for tasks like semantic search, clustering, and classification.
57+
</td>
58+
</tr>
59+
<tr>
60+
<td><a href="https://github.com/GoogleCloudPlatform/python-docs-samples/tree/main/generative_ai/extensions">Extensions</a>
61+
</td>
62+
<td><a href="https://cloud.google.com/vertex-ai/generative-ai/docs/extensions/overview">https://cloud.google.com/vertex-ai/generative-ai/docs/extensions/overview</a>
63+
</td>
64+
<td>Demonstrations of how to use extensions with generative models, enabling them to access and process real-time information, use tools, and interact with external systems.
65+
</td>
66+
</tr>
67+
<tr>
68+
<td><a href="https://github.com/GoogleCloudPlatform/python-docs-samples/tree/main/generative_ai/function_calling">Function Calling</a>
69+
</td>
70+
<td><a href="https://cloud.google.com/vertex-ai/generative-ai/docs/multimodal/function-calling">https://cloud.google.com/vertex-ai/generative-ai/docs/multimodal/function-calling</a>
71+
</td>
72+
<td>Examples of how to use function calling to enable generative models to execute specific actions or retrieve information from external APIs.
73+
</td>
74+
</tr>
75+
<tr>
76+
<td><a href="https://github.com/GoogleCloudPlatform/python-docs-samples/tree/main/generative_ai/grounding">Grounding</a>
77+
</td>
78+
<td><a href="https://cloud.google.com/vertex-ai/generative-ai/docs/grounding/overview">https://cloud.google.com/vertex-ai/generative-ai/docs/grounding/overview</a>
79+
</td>
80+
<td>Code illustrating how to ground generative models with specific knowledge bases or data sources to improve the accuracy and relevance of their responses.
81+
</td>
82+
</tr>
83+
<tr>
84+
<td><a href="https://github.com/GoogleCloudPlatform/python-docs-samples/tree/main/generative_ai/image_generation">Image Generation</a>
85+
</td>
86+
<td><a href="https://cloud.google.com/vertex-ai/generative-ai/docs/image/overview">https://cloud.google.com/vertex-ai/generative-ai/docs/image/overview</a>
87+
</td>
88+
<td>Samples showcasing how to generate images from text prompts using models like Imagen.
89+
</td>
90+
</tr>
91+
<tr>
92+
<td><a href="https://github.com/GoogleCloudPlatform/python-docs-samples/tree/main/generative_ai/model_garden">Model Garden</a>
93+
</td>
94+
<td><a href="https://cloud.google.com/vertex-ai/generative-ai/docs/model-garden/explore-models">https://cloud.google.com/vertex-ai/generative-ai/docs/model-garden/explore-models</a>
95+
</td>
96+
<td>Resources related to exploring and utilizing pre-trained models available in Vertex AI's Model Garden.
97+
</td>
98+
</tr>
99+
<tr>
100+
<td><a href="https://github.com/GoogleCloudPlatform/python-docs-samples/tree/main/generative_ai/model_tuning">Model Tuning</a>
101+
</td>
102+
<td><a href="https://cloud.google.com/vertex-ai/generative-ai/docs/models/tune-models">https://cloud.google.com/vertex-ai/generative-ai/docs/models/tune-models</a>
103+
</td>
104+
<td>Code and guides for fine-tuning pre-trained generative models on specific datasets or for specific tasks.
105+
</td>
106+
</tr>
107+
<tr>
108+
<td><a href="https://github.com/GoogleCloudPlatform/python-docs-samples/tree/main/generative_ai/rag">RAG</a>
109+
</td>
110+
<td><a href="https://cloud.google.com/vertex-ai/generative-ai/docs/model-reference/rag-api">https://cloud.google.com/vertex-ai/generative-ai/docs/model-reference/rag-api</a>
111+
</td>
112+
<td>Information and resources about Retrieval Augmented Generation (RAG), which combines information retrieval with generative models.
113+
</td>
114+
</tr>
115+
<tr>
116+
<td><a href="https://github.com/GoogleCloudPlatform/python-docs-samples/tree/main/generative_ai/reasoning_engine">Reasoning Engine</a>
117+
</td>
118+
<td><a href="https://cloud.google.com/vertex-ai/generative-ai/docs/model-reference/reasoning-engine">https://cloud.google.com/vertex-ai/generative-ai/docs/model-reference/reasoning-engine</a>
119+
</td>
120+
<td>Details about the Reasoning Engine, which enables more complex reasoning and logical deduction in generative models.
121+
</td>
122+
</tr>
123+
<tr>
124+
<td><a href="https://github.com/GoogleCloudPlatform/python-docs-samples/tree/main/generative_ai/safety">Safety</a>
125+
</td>
126+
<td><a href="https://cloud.google.com/vertex-ai/generative-ai/docs/multimodal/configure-safety-attributes">https://cloud.google.com/vertex-ai/generative-ai/docs/multimodal/configure-safety-attributes</a>
127+
</td>
128+
<td>Examples of how to configure safety attributes and filters to mitigate risks and ensure responsible use of generative models.
129+
</td>
130+
</tr>
131+
<tr>
132+
<td><a href="https://github.com/GoogleCloudPlatform/python-docs-samples/tree/main/generative_ai/system_instructions">System Instructions</a>
133+
</td>
134+
<td><a href="https://cloud.google.com/vertex-ai/generative-ai/docs/learn/prompts/system-instructions?hl=en">https://cloud.google.com/vertex-ai/generative-ai/docs/learn/prompts/system-instructions?hl=en</a>
135+
</td>
136+
<td>Code demonstrating how to provide system instructions to guide the behavior and responses of generative models.
137+
</td>
138+
</tr>
139+
<tr>
140+
<td><a href="https://github.com/GoogleCloudPlatform/python-docs-samples/tree/main/generative_ai/text_generation">Text Generation</a>
141+
</td>
142+
<td><a href="https://cloud.google.com/vertex-ai/generative-ai/docs/multimodal/send-chat-prompts-gemini">https://cloud.google.com/vertex-ai/generative-ai/docs/multimodal/send-chat-prompts-gemini</a>
143+
</td>
144+
<td>Samples of how to generate text using Gemini models, including chat-based interactions and creative writing.
145+
</td>
146+
</tr>
147+
<tr>
148+
<td><a href="https://github.com/GoogleCloudPlatform/python-docs-samples/tree/main/generative_ai/audio">Understand Audio</a>
149+
</td>
150+
<td><a href="https://cloud.google.com/vertex-ai/generative-ai/docs/multimodal/audio-understanding">https://cloud.google.com/vertex-ai/generative-ai/docs/multimodal/audio-understanding</a>
151+
</td>
152+
<td>Examples of how to use generative models for audio understanding tasks, such as transcription and audio classification.
153+
</td>
154+
</tr>
155+
<tr>
156+
<td><a href="https://github.com/GoogleCloudPlatform/python-docs-samples/tree/main/generative_ai/video">Understand Video</a>
157+
</td>
158+
<td><a href="https://cloud.google.com/vertex-ai/generative-ai/docs/multimodal/video-understanding">https://cloud.google.com/vertex-ai/generative-ai/docs/multimodal/video-understanding</a>
159+
</td>
160+
<td>Samples showcasing how to use generative models for video understanding tasks, such as video summarization and content analysis.
161+
</td>
162+
</tr>
163+
</table>
164+
165+
## Contributing
166+
167+
Contributions welcome! See the [Contributing Guide](https://github.com/GoogleCloudPlatform/python-docs-samples/blob/main/CONTRIBUTING.md).
168+
169+
## Getting help
170+
171+
Please use the [issues page](https://github.com/GoogleCloudPlatform/python-docs-samples/issues) to provide suggestions, feedback or submit a bug report.
172+
173+
## Disclaimer
174+
175+
This repository itself is not an officially supported Google product. The code in this repository is for demonstrative purposes only.

generative_ai/batch_predict/gemini_batch_predict.py renamed to generative_ai/batch_predict/gemini_batch_predict_gcs.py

Lines changed: 6 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -13,7 +13,7 @@
1313
# limitations under the License.
1414
import os
1515

16-
from vertexai.preview.batch_prediction import BatchPredictionJob
16+
from vertexai.batch_prediction import BatchPredictionJob
1717

1818
PROJECT_ID = os.getenv("GOOGLE_CLOUD_PROJECT")
1919

@@ -23,11 +23,11 @@ def batch_predict_gemini_createjob(
2323
) -> BatchPredictionJob:
2424
"""Perform batch text prediction using a Gemini AI model.
2525
Args:
26-
input_uri (str): URI of the input file in BigQuery table or Google Cloud Storage.
27-
Example: "gs://[BUCKET]/[DATASET].jsonl" OR "bq://[PROJECT].[DATASET].[TABLE]"
26+
input_uri (str): URI of the input file in Google Cloud Storage.
27+
Example: "gs://[BUCKET]/[DATASET].jsonl"
2828
29-
output_uri (str): URI of the output folder, in BigQuery table or Google Cloud Storage.
30-
Example: "gs://[BUCKET]/[OUTPUT].jsonl" OR "bq://[PROJECT].[DATASET].[TABLE]"
29+
output_uri (str): URI of the output folder in Google Cloud Storage.
30+
Example: "gs://[BUCKET]/[OUTPUT].jsonl"
3131
Returns:
3232
batch_prediction_job: The batch prediction job object containing details of the job.
3333
"""
@@ -36,7 +36,7 @@ def batch_predict_gemini_createjob(
3636
import time
3737
import vertexai
3838

39-
from vertexai.preview.batch_prediction import BatchPredictionJob
39+
from vertexai.batch_prediction import BatchPredictionJob
4040

4141
# TODO(developer): Update and un-comment below lines
4242
# input_uri ="gs://[BUCKET]/[OUTPUT].jsonl" # Example
@@ -77,7 +77,6 @@ def batch_predict_gemini_createjob(
7777
# https://storage.googleapis.com/cloud-samples-data/batch/prompt_for_batch_gemini_predict.jsonl
7878

7979
return batch_prediction_job
80-
8180
# [END generativeaionvertexai_batch_predict_gemini_createjob]
8281

8382

generative_ai/batch_predict/requirements.txt

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -3,7 +3,7 @@ pandas==2.0.3; python_version == '3.8'
33
pandas==2.1.4; python_version > '3.8'
44
pillow==10.3.0; python_version < '3.8'
55
pillow==10.3.0; python_version >= '3.8'
6-
google-cloud-aiplatform[all]==1.69.0
6+
google-cloud-aiplatform[all]==1.71.0
77
sentencepiece==0.2.0
88
google-auth==2.29.0
99
anthropic[vertex]==0.28.0

generative_ai/batch_predict/test_batch_predict_examples.py

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -21,7 +21,7 @@
2121

2222
import batch_code_predict
2323
import batch_text_predict
24-
import gemini_batch_predict
24+
import gemini_batch_predict_gcs
2525

2626

2727
INPUT_BUCKET = "cloud-samples-data"
@@ -74,10 +74,10 @@ def test_batch_code_predict(output_folder: pytest.fixture()) -> None:
7474
assert OUTPUT_PATH in job.output_info.gcs_output_directory
7575

7676

77-
def test_batch_gemini_predict(output_folder: pytest.fixture()) -> None:
77+
def test_batch_gemini_predict_gcs(output_folder: pytest.fixture()) -> None:
7878
input_uri = f"gs://{INPUT_BUCKET}/batch/prompt_for_batch_gemini_predict.jsonl"
7979
job = _main_test(
80-
test_func=lambda: gemini_batch_predict.batch_predict_gemini_createjob(
80+
test_func=lambda: gemini_batch_predict_gcs.batch_predict_gemini_createjob(
8181
input_uri, output_folder
8282
)
8383
)

generative_ai/embeddings/code_retrieval_example.py

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -17,14 +17,14 @@
1717
# [START generativeaionvertexai_embedding_code_retrieval]
1818
from vertexai.language_models import TextEmbeddingInput, TextEmbeddingModel
1919

20-
MODEL_NAME = "text-embedding-preview-0815"
20+
MODEL_NAME = "text-embedding-005"
2121
DIMENSIONALITY = 256
2222

2323

2424
def embed_text(
2525
texts: list[str] = ["Retrieve a function that adds two numbers"],
2626
task: str = "CODE_RETRIEVAL_QUERY",
27-
model_name: str = "text-embedding-preview-0815",
27+
model_name: str = "text-embedding-005",
2828
dimensionality: int | None = 256,
2929
) -> list[list[float]]:
3030
"""Embeds texts with a pre-trained, foundational model."""

generative_ai/embeddings/document_retrieval_example.py

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -32,7 +32,7 @@ def embed_text() -> list[list[float]]:
3232
# The task type for embedding. Check the available tasks in the model's documentation.
3333
task = "RETRIEVAL_DOCUMENT"
3434

35-
model = TextEmbeddingModel.from_pretrained("text-embedding-004")
35+
model = TextEmbeddingModel.from_pretrained("text-embedding-005")
3636
inputs = [TextEmbeddingInput(text, task) for text in texts]
3737
kwargs = dict(output_dimensionality=dimensionality) if dimensionality else {}
3838
embeddings = model.get_embeddings(inputs, **kwargs)

generative_ai/embeddings/model_tuning_example.py

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -21,7 +21,7 @@
2121

2222
def tune_embedding_model(
2323
api_endpoint: str,
24-
base_model_name: str = "text-embedding-004",
24+
base_model_name: str = "text-embedding-005",
2525
corpus_path: str = "gs://cloud-samples-data/ai-platform/embedding/goog-10k-2024/r11/corpus.jsonl",
2626
queries_path: str = "gs://cloud-samples-data/ai-platform/embedding/goog-10k-2024/r11/queries.jsonl",
2727
train_label_path: str = "gs://cloud-samples-data/ai-platform/embedding/goog-10k-2024/r11/train.tsv",

0 commit comments

Comments
 (0)