|
| 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 | +# Test file: https://storage.googleapis.com/generativeai-downloads/data/16000.wav |
| 15 | +# Install helpers for converting files: pip install librosa soundfile |
| 16 | + |
| 17 | +from pydantic import BaseModel |
| 18 | + |
| 19 | + |
| 20 | +class CalendarEvent(BaseModel): |
| 21 | + name: str |
| 22 | + date: str |
| 23 | + participants: list[str] |
| 24 | + |
| 25 | + |
| 26 | +def generate_content() -> CalendarEvent: |
| 27 | + # [START googlegenaisdk_live_structured_ouput_with_txt] |
| 28 | + import os |
| 29 | + |
| 30 | + import google.auth.transport.requests |
| 31 | + import openai |
| 32 | + from google.auth import default |
| 33 | + from openai.types.chat import (ChatCompletionSystemMessageParam, |
| 34 | + ChatCompletionUserMessageParam) |
| 35 | + |
| 36 | + project_id = os.environ["GOOGLE_CLOUD_PROJECT"] |
| 37 | + location = "us-central1" |
| 38 | + |
| 39 | + # Programmatically get an access token |
| 40 | + credentials, _ = default(scopes=["https://www.googleapis.com/auth/cloud-platform"]) |
| 41 | + credentials.refresh(google.auth.transport.requests.Request()) |
| 42 | + # Note: the credential lives for 1 hour by default (https://cloud.google.com/docs/authentication/token-types#at-lifetime); after expiration, it must be refreshed. |
| 43 | + |
| 44 | + ############################## |
| 45 | + # Choose one of the following: |
| 46 | + ############################## |
| 47 | + |
| 48 | + # If you are calling a Gemini model, set the ENDPOINT_ID variable to use openapi. |
| 49 | + ENDPOINT_ID = "openapi" |
| 50 | + |
| 51 | + # If you are calling a self-deployed model from Model Garden, set the |
| 52 | + # ENDPOINT_ID variable and set the client's base URL to use your endpoint. |
| 53 | + # ENDPOINT_ID = "YOUR_ENDPOINT_ID" |
| 54 | + |
| 55 | + # OpenAI Client |
| 56 | + client = openai.OpenAI( |
| 57 | + base_url=f"https://{location}-aiplatform.googleapis.com/v1/projects/{project_id}/locations/{location}/endpoints/{ENDPOINT_ID}", |
| 58 | + api_key=credentials.token, |
| 59 | + ) |
| 60 | + |
| 61 | + completion = client.beta.chat.completions.parse( |
| 62 | + model="google/gemini-2.0-flash-001", |
| 63 | + messages=[ |
| 64 | + ChatCompletionSystemMessageParam( |
| 65 | + role="system", content="Extract the event information." |
| 66 | + ), |
| 67 | + ChatCompletionUserMessageParam( |
| 68 | + role="user", |
| 69 | + content="Alice and Bob are going to a science fair on Friday.", |
| 70 | + ), |
| 71 | + ], |
| 72 | + response_format=CalendarEvent, |
| 73 | + ) |
| 74 | + |
| 75 | + response = completion.choices[0].message.parsed |
| 76 | + print(response) |
| 77 | + |
| 78 | + # System message: Extract the event information. |
| 79 | + # User message: Alice and Bob are going to a science fair on Friday. |
| 80 | + # Output message: name='science fair' date='Friday' participants=['Alice', 'Bob'] |
| 81 | + # [END googlegenaisdk_live_structured_ouput_with_txt] |
| 82 | + return response |
| 83 | + |
| 84 | + |
| 85 | +if __name__ == "__main__": |
| 86 | + generate_content() |
0 commit comments