Skip to content
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
Original file line number Diff line number Diff line change
Expand Up @@ -13,7 +13,7 @@
# limitations under the License.


def generate_text(project_id: str, location: str = "us-central1") -> object:
def generate_text(project_id: str, location: str = "global") -> object:
# [START generativeaionvertexai_gemini_chat_completions_authentication]
import openai

Expand All @@ -22,7 +22,7 @@ def generate_text(project_id: str, location: str = "us-central1") -> object:

# TODO(developer): Update and un-comment below lines
# project_id = "PROJECT_ID"
# location = "us-central1"
# location = "global"

# Programmatically get an access token
credentials, _ = default(scopes=["https://www.googleapis.com/auth/cloud-platform"])
Expand All @@ -42,7 +42,7 @@ def generate_text(project_id: str, location: str = "us-central1") -> object:

# OpenAI Client
client = openai.OpenAI(
base_url=f"https://{location}-aiplatform.googleapis.com/v1/projects/{project_id}/locations/{location}/endpoints/{ENDPOINT_ID}",
base_url=f"https://aiplatform.googleapis.com/v1/projects/{project_id}/locations/{location}/endpoints/{ENDPOINT_ID}",
api_key=credentials.token,
)
# [END generativeaionvertexai_gemini_chat_completions_authentication]
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -43,19 +43,19 @@ def __getattr__(self, name: str) -> Any:


# [END generativeaionvertexai_credentials_refresher]
def generate_text(project_id: str, location: str = "us-central1") -> object:
def generate_text(project_id: str, location: str = "global") -> object:
# [START generativeaionvertexai_credentials_refresher]

# TODO(developer): Update and un-comment below lines
# project_id = "PROJECT_ID"
# location = "us-central1"
# location = "global"

client = OpenAICredentialsRefresher(
base_url=f"https://{location}-aiplatform.googleapis.com/v1/projects/{project_id}/locations/{location}/endpoints/openapi",
base_url=f"https://aiplatform.googleapis.com/v1/projects/{project_id}/locations/{location}/endpoints/openapi",
)

response = client.chat.completions.create(
model="google/gemini-2.0-flash-001",
model="google/gemini-3.8-flash",
Comment thread
iennae marked this conversation as resolved.
messages=[{"role": "user", "content": "Why is the sky blue?"}],
)

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -19,22 +19,21 @@

def generate_text() -> object:
# [START generativeaionvertexai_gemini_chat_completions_function_calling_basic]
from google.auth import default
import google.auth.transport.requests
import openai

from google.auth import default, transport

# TODO(developer): Update & uncomment below line
# PROJECT_ID = "your-project-id"
location = "us-central1"
location = "global"

# Programmatically get an access token
credentials, _ = default(scopes=["https://www.googleapis.com/auth/cloud-platform"])
auth_request = transport.requests.Request()
credentials.refresh(auth_request)
credentials.refresh(google.auth.transport.requests.Request())

# # OpenAI Client
client = openai.OpenAI(
base_url=f"https://{location}-aiplatform.googleapis.com/v1beta1/projects/{PROJECT_ID}/locations/{location}/endpoints/openapi",
base_url=f"https://aiplatform.googleapis.com/v1beta1/projects/{PROJECT_ID}/locations/{location}/endpoints/openapi",
api_key=credentials.token,
)

Expand Down Expand Up @@ -68,7 +67,7 @@ def generate_text() -> object:
messages.append({"role": "user", "content": "What is the weather in Boston?"})

response = client.chat.completions.create(
model="google/gemini-2.0-flash-001",
model="google/gemini-3.8-flash",
Comment thread
iennae marked this conversation as resolved.
messages=messages,
tools=tools,
)
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -19,22 +19,21 @@

def generate_text() -> object:
# [START generativeaionvertexai_gemini_chat_completions_function_calling_config]
from google.auth import default
import google.auth.transport.requests
import openai

from google.auth import default, transport

# TODO(developer): Update & uncomment below line
# PROJECT_ID = "your-project-id"
location = "us-central1"
location = "global"

# Programmatically get an access token
credentials, _ = default(scopes=["https://www.googleapis.com/auth/cloud-platform"])
auth_request = transport.requests.Request()
credentials.refresh(auth_request)
credentials.refresh(google.auth.transport.requests.Request())

# OpenAI Client
client = openai.OpenAI(
base_url=f"https://{location}-aiplatform.googleapis.com/v1beta1/projects/{PROJECT_ID}/locations/{location}/endpoints/openapi",
base_url=f"https://aiplatform.googleapis.com/v1beta1/projects/{PROJECT_ID}/locations/{location}/endpoints/openapi",
api_key=credentials.token,
)

Expand Down Expand Up @@ -68,7 +67,7 @@ def generate_text() -> object:
messages.append({"role": "user", "content": "What is the weather in Boston, MA?"})

response = client.chat.completions.create(
model="google/gemini-2.0-flash-001",
model="google/gemini-3.8-flash",
Comment thread
iennae marked this conversation as resolved.
messages=messages,
tools=tools,
tool_choice="auto",
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -13,7 +13,7 @@
# limitations under the License.


def generate_text(project_id: str, location: str = "us-central1") -> object:
def generate_text(project_id: str, location: str = "global") -> object:
# [START generativeaionvertexai_gemini_chat_completions_non_streaming_image]

from google.auth import default
Expand All @@ -23,20 +23,20 @@ def generate_text(project_id: str, location: str = "us-central1") -> object:

# TODO(developer): Update and un-comment below lines
# project_id = "PROJECT_ID"
# location = "us-central1"
# location = "global"

# 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/openapi",
base_url=f"https://aiplatform.googleapis.com/v1/projects/{project_id}/locations/{location}/endpoints/openapi",
api_key=credentials.token,
)

response = client.chat.completions.create(
model="google/gemini-2.0-flash-001",
model="google/gemini-3.8-flash",
Comment thread
iennae marked this conversation as resolved.
messages=[
{
"role": "user",
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -13,7 +13,7 @@
# limitations under the License.


def generate_text(project_id: str, location: str = "us-central1") -> object:
def generate_text(project_id: str, location: str = "global") -> object:
# [START generativeaionvertexai_gemini_chat_completions_non_streaming]
from google.auth import default
import google.auth.transport.requests
Expand All @@ -22,20 +22,20 @@ def generate_text(project_id: str, location: str = "us-central1") -> object:

# TODO(developer): Update and un-comment below lines
# project_id = "PROJECT_ID"
# location = "us-central1"
# location = "global"

# 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/openapi",
base_url=f"https://aiplatform.googleapis.com/v1/projects/{project_id}/locations/{location}/endpoints/openapi",
api_key=credentials.token,
)

response = client.chat.completions.create(
model="google/gemini-2.0-flash-001",
model="google/gemini-3.8-flash",
Comment thread
iennae marked this conversation as resolved.
messages=[{"role": "user", "content": "Why is the sky blue?"}],
)

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -13,7 +13,7 @@
# limitations under the License.


def generate_text(project_id: str, location: str = "us-central1") -> object:
def generate_text(project_id: str, location: str = "global") -> object:
# [START generativeaionvertexai_gemini_chat_completions_streaming_image]
from google.auth import default
import google.auth.transport.requests
Expand All @@ -22,20 +22,20 @@ def generate_text(project_id: str, location: str = "us-central1") -> object:

# TODO(developer): Update and un-comment below lines
# project_id = "PROJECT_ID"
# location = "us-central1"
# location = "global"

# 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/openapi",
base_url=f"https://aiplatform.googleapis.com/v1/projects/{project_id}/locations/{location}/endpoints/openapi",
api_key=credentials.token,
)

response = client.chat.completions.create(
model="google/gemini-2.0-flash-001",
model="google/gemini-3.8-flash",
Comment thread
iennae marked this conversation as resolved.
messages=[
{
"role": "user",
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -13,7 +13,7 @@
# limitations under the License.


def generate_text(project_id: str, location: str = "us-central1") -> object:
def generate_text(project_id: str, location: str = "global") -> object:
# [START generativeaionvertexai_gemini_chat_completions_streaming]
from google.auth import default
import google.auth.transport.requests
Expand All @@ -22,20 +22,20 @@ def generate_text(project_id: str, location: str = "us-central1") -> object:

# TODO(developer): Update and un-comment below lines
# project_id = "PROJECT_ID"
# location = "us-central1"
# location = "global"

# 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/openapi",
base_url=f"https://aiplatform.googleapis.com/v1/projects/{project_id}/locations/{location}/endpoints/openapi",
api_key=credentials.token,
)

response = client.chat.completions.create(
model="google/gemini-2.0-flash-001",
model="google/gemini-3.8-flash",
Comment thread
iennae marked this conversation as resolved.
messages=[{"role": "user", "content": "Why is the sky blue?"}],
stream=True,
)
Expand Down
19 changes: 16 additions & 3 deletions generative_ai/chat_completions/chat_completions_test.py
Original file line number Diff line number Diff line change
Expand Up @@ -16,6 +16,8 @@

import chat_completions_authentication
import chat_completions_credentials_refresher
import chat_completions_function_calling_basic
import chat_completions_function_calling_config
import chat_completions_non_streaming_image
import chat_completions_non_streaming_text
import chat_completions_non_streaming_text_self_deployed
Expand All @@ -25,7 +27,8 @@


PROJECT_ID = os.getenv("GOOGLE_CLOUD_PROJECT")
LOCATION = "us-central1"
LOCATION = "global"
SELF_DEPLOYED_LOCATION = "us-central1"
SELF_HOSTED_MODEL_ID = "google/gemma-2-9b-it"
ENDPOINT_ID = "6714120476014149632"

Expand Down Expand Up @@ -62,15 +65,25 @@ def test_credentials_refresher() -> None:
assert response


def test_function_calling_basic() -> None:
response = chat_completions_function_calling_basic.generate_text()
assert response


def test_function_calling_config() -> None:
response = chat_completions_function_calling_config.generate_text()
assert response


def test_streaming_text_self_deployed() -> None:
response = chat_completions_streaming_text_self_deployed.generate_text(
PROJECT_ID, LOCATION, SELF_HOSTED_MODEL_ID, ENDPOINT_ID
PROJECT_ID, SELF_DEPLOYED_LOCATION, SELF_HOSTED_MODEL_ID, ENDPOINT_ID
)
assert response


def test_non_streaming_text_self_deployed() -> None:
response = chat_completions_non_streaming_text_self_deployed.generate_text(
PROJECT_ID, LOCATION, SELF_HOSTED_MODEL_ID, ENDPOINT_ID
PROJECT_ID, SELF_DEPLOYED_LOCATION, SELF_HOSTED_MODEL_ID, ENDPOINT_ID
)
assert response
2 changes: 1 addition & 1 deletion generative_ai/model_garden/claude_3_streaming_example.py
Original file line number Diff line number Diff line change
Expand Up @@ -33,7 +33,7 @@ def generate_text_streaming() -> str:
result = []

with client.messages.stream(
model="claude-3-5-sonnet-v2@20241022",
model="claude-sonnet-4-6",
Comment thread
iennae marked this conversation as resolved.
max_tokens=1024,
messages=[
{
Expand Down
2 changes: 1 addition & 1 deletion generative_ai/model_garden/claude_3_tool_example.py
Original file line number Diff line number Diff line change
Expand Up @@ -29,7 +29,7 @@ def tool_use() -> object:

client = AnthropicVertex(project_id=PROJECT_ID, region="us-east5")
message = client.messages.create(
model="claude-3-5-sonnet-v2@20241022",
model="claude-sonnet-4-6",
Comment thread
iennae marked this conversation as resolved.
max_tokens=1024,
tools=[
{
Expand Down
2 changes: 1 addition & 1 deletion generative_ai/model_garden/claude_3_unary_example.py
Original file line number Diff line number Diff line change
Expand Up @@ -30,7 +30,7 @@ def generate_text() -> object:

client = AnthropicVertex(project_id=PROJECT_ID, region="us-east5")
message = client.messages.create(
model="claude-3-5-sonnet-v2@20241022",
model="claude-sonnet-4-6",
Comment thread
iennae marked this conversation as resolved.
max_tokens=1024,
messages=[
{
Expand Down
2 changes: 1 addition & 1 deletion generative_ai/model_garden/requirements.txt
Original file line number Diff line number Diff line change
Expand Up @@ -5,7 +5,7 @@ pillow==12.3.0
google-cloud-aiplatform[full]==1.157.0
sentencepiece==0.2.1
google-auth==2.38.0
anthropic[vertex]==0.28.0
anthropic[vertex]==2.2.0
Comment thread
iennae marked this conversation as resolved.
langchain-core==1.4.0
langchain-google-vertexai==3.2.3
numpy<3
Expand Down
5 changes: 3 additions & 2 deletions generative_ai/rag/create_corpus_example.py
Original file line number Diff line number Diff line change
Expand Up @@ -34,8 +34,9 @@ def create_corpus(
# display_name = "test_corpus"
# description = "Corpus Description"

# Initialize Agent Platform client once per session
client = agentplatform.Client(project=PROJECT_ID, location="us-central1")
# Initialize Agent Platform client once per session.
# See https://docs.cloud.google.com/gemini-enterprise-agent-platform/build/rag-engine/rag-overview#supported-regions
client = agentplatform.Client(project=PROJECT_ID, location="us-east5")

# Configure project-level config
backend_config = types.RagVectorDbConfig(
Expand Down
5 changes: 3 additions & 2 deletions generative_ai/rag/create_corpus_feature_store_example.py
Original file line number Diff line number Diff line change
Expand Up @@ -36,8 +36,9 @@ def create_corpus_feature_store(
# display_name = "test_corpus"
# description = "Corpus Description"

# Initialize Agent Platform client once per session
client = agentplatform.Client(project=PROJECT_ID, location="us-central1")
# Initialize Agent Platform client once per session.
# See https://docs.cloud.google.com/gemini-enterprise-agent-platform/build/rag-engine/rag-overview#supported-regions
client = agentplatform.Client(project=PROJECT_ID, location="us-east5")

# Configure embedding model (Optional)
backend_config = types.RagVectorDbConfig(
Expand Down
13 changes: 9 additions & 4 deletions generative_ai/rag/create_corpus_pinecone_example.py
Original file line number Diff line number Diff line change
Expand Up @@ -20,7 +20,6 @@
PROJECT_ID = os.getenv("GOOGLE_CLOUD_PROJECT")



def create_corpus_pinecone(
pinecone_index_name: str,
pinecone_api_key_secret_manager_version: str,
Expand All @@ -38,8 +37,9 @@ def create_corpus_pinecone(
# display_name = "test_corpus"
# description = "Corpus Description"

# Initialize Agent Platform client once per session
client = agentplatform.Client(project=PROJECT_ID, location="us-central1")
# Initialize Agent Platform client once per session.
# See https://docs.cloud.google.com/gemini-enterprise-agent-platform/build/rag-engine/rag-overview#supported-regions
client = agentplatform.Client(project=PROJECT_ID, location="us-east5")

# Configure embedding model (Optional)
embedding_model_config = types.RagEmbeddingModelConfig(
Expand All @@ -51,7 +51,12 @@ def create_corpus_pinecone(
# Configure Vector DB
vector_db = types.RagVectorDbConfig(
pinecone=types.RagVectorDbConfigPinecone(
index_name=pinecone_index_name,
index_name=pinecone_index_name,
),
api_auth=types.ApiAuth(
api_key_config=types.ApiAuthApiKeyConfig(
api_key_secret_version=pinecone_api_key_secret_manager_version
)
),
rag_embedding_model_config=embedding_model_config,
)
Expand Down
Loading
Loading