From dca42e574268ce8227e6aea935688e04abfd312a Mon Sep 17 00:00:00 2001 From: Yan Qiao Date: Tue, 8 Apr 2025 23:04:35 -0700 Subject: [PATCH] feat: add code samples for model optimizer --- .../model_optimizer_textgen_with_txt.py | 37 +++++++++++++++++++ .../test_text_generation_examples.py | 7 +++- 2 files changed, 43 insertions(+), 1 deletion(-) create mode 100644 genai/text_generation/model_optimizer_textgen_with_txt.py diff --git a/genai/text_generation/model_optimizer_textgen_with_txt.py b/genai/text_generation/model_optimizer_textgen_with_txt.py new file mode 100644 index 00000000000..c6ae8651f05 --- /dev/null +++ b/genai/text_generation/model_optimizer_textgen_with_txt.py @@ -0,0 +1,37 @@ +# Copyright 2025 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 +# +# https://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 generate_content() -> str: + # [START googlegenaisdk_model_optimizer_textgen_with_txt] + from google import genai + from google.genai.types import HttpOptions + + client = genai.Client(http_options=HttpOptions(api_version="v1")) + response = client.models.generate_content( + model="model-optimizer-exp-04-09", + contents="How does AI work?", + ) + print(response.text) + # Example response: + # Okay, let's break down how AI works. It's a broad field, so I'll focus on the ... + # + # Here's a simplified overview: + # ... + # [END googlegenaisdk_model_optimizer_textgen_with_txt] + return response.text + + +if __name__ == "__main__": + generate_content() diff --git a/genai/text_generation/test_text_generation_examples.py b/genai/text_generation/test_text_generation_examples.py index a652806be39..703cb63c0b0 100644 --- a/genai/text_generation/test_text_generation_examples.py +++ b/genai/text_generation/test_text_generation_examples.py @@ -18,6 +18,7 @@ import os +import model_optimizer_textgen_with_txt import textgen_async_with_txt import textgen_chat_stream_with_txt import textgen_chat_with_txt @@ -37,7 +38,6 @@ import textgen_with_youtube_video import thinking_textgen_with_txt - os.environ["GOOGLE_GENAI_USE_VERTEXAI"] = "True" os.environ["GOOGLE_CLOUD_LOCATION"] = "us-central1" # The project name is included in the CICD pipeline @@ -135,3 +135,8 @@ def test_textgen_with_local_video() -> None: def test_textgen_with_youtube_video() -> None: response = textgen_with_youtube_video.generate_content() assert response + + +def test_model_optimizer_textgen_with_txt() -> None: + response = model_optimizer_textgen_with_txt.generate_content() + assert response