Skip to content

feat: add code samples for model optimizer #13296

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

Merged
merged 1 commit into from
Apr 10, 2025
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
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
37 changes: 37 additions & 0 deletions genai/text_generation/model_optimizer_textgen_with_txt.py
Original file line number Diff line number Diff line change
@@ -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()
7 changes: 6 additions & 1 deletion genai/text_generation/test_text_generation_examples.py
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand All @@ -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
Expand Down Expand Up @@ -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