-
Notifications
You must be signed in to change notification settings - Fork 6.6k
docs(generative_ai): Update Chat Completions API samples #13088
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
Changes from all commits
Commits
Show all changes
10 commits
Select commit
Hold shift + click to select a range
f17f755
docs(generative_ai): Update Chat Completions API samples
holtskinner 161b975
Fix lint error
holtskinner 4b6cbd6
Update model endpoint id
holtskinner c4036ec
Update generative_ai/chat_completions/chat_completions_authentication.py
holtskinner 75eccb5
Update generative_ai/chat_completions/chat_completions_authentication.py
holtskinner 706b9ea
Update generative_ai/chat_completions/chat_completions_authentication.py
holtskinner b20542e
Update Test to include new model Endpoint ID
holtskinner af4d5a2
Update Ednpoint ID
holtskinner d7cd2a1
Change Model ID
holtskinner bd7500b
Update endpoint to v1
holtskinner File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
There are no files selected for viewing
50 changes: 50 additions & 0 deletions
50
generative_ai/chat_completions/chat_completions_authentication.py
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,50 @@ | ||
# 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_text(project_id: str, location: str = "us-central1") -> object: | ||
# [START generativeaionvertexai_gemini_chat_completions_authentication] | ||
import openai | ||
|
||
from google.auth import default | ||
import google.auth.transport.requests | ||
|
||
# TODO(developer): Update and un-comment below lines | ||
# project_id = "PROJECT_ID" | ||
# location = "us-central1" | ||
|
||
# Programmatically get an access token | ||
credentials, _ = default(scopes=["https://www.googleapis.com/auth/cloud-platform"]) | ||
credentials.refresh(google.auth.transport.requests.Request()) | ||
# 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. | ||
|
||
############################## | ||
# Choose one of the following: | ||
############################## | ||
|
||
# If you are calling a Gemini model, set the ENDPOINT_ID variable to use openapi. | ||
ENDPOINT_ID = "openapi" | ||
|
||
# If you are calling a self-deployed model from Model Garden, set the | ||
# ENDPOINT_ID variable and set the client's base URL to use your endpoint. | ||
# ENDPOINT_ID = "YOUR_ENDPOINT_ID" | ||
|
||
# OpenAI Client | ||
client = openai.OpenAI( | ||
base_url=f"https://{location}-aiplatform.googleapis.com/v1/projects/{project_id}/locations/{location}/endpoints/{ENDPOINT_ID}", | ||
api_key=credentials.token, | ||
) | ||
# [END generativeaionvertexai_gemini_chat_completions_authentication] | ||
|
||
return client |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
52 changes: 52 additions & 0 deletions
52
generative_ai/chat_completions/chat_completions_non_streaming_text_self_deployed.py
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,52 @@ | ||
# 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_text( | ||
project_id: str, | ||
location: str = "us-central1", | ||
model_id: str = "gemma-2-9b-it", | ||
endpoint_id: str = "YOUR_ENDPOINT_ID", | ||
) -> object: | ||
# [START generativeaionvertexai_gemini_chat_completions_non_streaming_self_deployed] | ||
from google.auth import default | ||
import google.auth.transport.requests | ||
|
||
import openai | ||
|
||
# TODO(developer): Update and un-comment below lines | ||
# project_id = "PROJECT_ID" | ||
# location = "us-central1" | ||
# model_id = "gemma-2-9b-it" | ||
# endpoint_id = "YOUR_ENDPOINT_ID" | ||
|
||
# Programmatically get an access token | ||
credentials, _ = default(scopes=["https://www.googleapis.com/auth/cloud-platform"]) | ||
credentials.refresh(google.auth.transport.requests.Request()) | ||
|
||
# OpenAI Client | ||
client = openai.OpenAI( | ||
base_url=f"https://{location}-aiplatform.googleapis.com/v1/projects/{project_id}/locations/{location}/endpoints/{endpoint_id}", | ||
api_key=credentials.token, | ||
) | ||
|
||
response = client.chat.completions.create( | ||
model=model_id, | ||
messages=[{"role": "user", "content": "Why is the sky blue?"}], | ||
) | ||
print(response) | ||
|
||
# [END generativeaionvertexai_gemini_chat_completions_non_streaming_self_deployed] | ||
|
||
return response |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
54 changes: 54 additions & 0 deletions
54
generative_ai/chat_completions/chat_completions_streaming_text_self_deployed.py
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,54 @@ | ||
# 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_text( | ||
project_id: str, | ||
location: str = "us-central1", | ||
model_id: str = "gemma-2-9b-it", | ||
endpoint_id: str = "YOUR_ENDPOINT_ID", | ||
) -> object: | ||
# [START generativeaionvertexai_gemini_chat_completions_streaming_self_deployed] | ||
from google.auth import default | ||
import google.auth.transport.requests | ||
|
||
import openai | ||
|
||
# TODO(developer): Update and un-comment below lines | ||
# project_id = "PROJECT_ID" | ||
# location = "us-central1" | ||
# model_id = "gemma-2-9b-it" | ||
# endpoint_id = "YOUR_ENDPOINT_ID" | ||
|
||
# Programmatically get an access token | ||
credentials, _ = default(scopes=["https://www.googleapis.com/auth/cloud-platform"]) | ||
credentials.refresh(google.auth.transport.requests.Request()) | ||
|
||
# OpenAI Client | ||
client = openai.OpenAI( | ||
base_url=f"https://{location}-aiplatform.googleapis.com/v1/projects/{project_id}/locations/{location}/endpoints/{endpoint_id}", | ||
api_key=credentials.token, | ||
) | ||
|
||
response = client.chat.completions.create( | ||
model=model_id, | ||
messages=[{"role": "user", "content": "Why is the sky blue?"}], | ||
stream=True, | ||
) | ||
for chunk in response: | ||
print(chunk) | ||
|
||
# [END generativeaionvertexai_gemini_chat_completions_streaming_self_deployed] | ||
|
||
return response |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1,4 +1,4 @@ | ||
backoff==2.2.1 | ||
google-api-core==2.19.0 | ||
google-api-core==2.24.0 | ||
pytest==8.2.0 | ||
pytest-asyncio==0.23.6 |
Oops, something went wrong.
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
Uh oh!
There was an error while loading. Please reload this page.