mirror of
https://github.com/GoogleCloudPlatform/vertex-ai-samples.git
synced 2026-09-26 14:42:04 +00:00
* feat: Claude Fable 5.1 Launch * refactor: replace model/region if-elif chains with a dict lookup Addresses review feedback on both Select Claude model cells. The mapping is unchanged for all 20 models; only the lookup mechanism differs. * chore: apply nbfmt Runs the repo's own tensorflow-docs nbfmt over the notebook so the 'notebook format and lint' check passes.
38 KiB
38 KiB
In [ ]:
# Copyright 2026 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.In [ ]:
import sys
if "google.colab" in sys.modules:
from google.colab import auth
auth.authenticate_user()In [ ]:
MODEL = "claude-fable-5-1" # @param ["claude-fable-5-1","claude-opus-5","claude-sonnet-5","claude-fable-5","claude-opus-4-8","claude-opus-4-7","claude-sonnet-4-6","claude-opus-4-6","claude-opus-4-5","claude-haiku-4-5","claude-sonnet-4-5","claude-opus-4-1","claude-sonnet-4","claude-opus-4","claude-3-7-sonnet","claude-3-5-sonnet-v2","claude-3-5-haiku","claude-3-5-sonnet","claude-3-opus","claude-3-haiku"]
# Available regions per model.
MODEL_AVAILABLE_REGIONS = {
"claude-fable-5-1": ["global", "us", "eu"],
"claude-opus-5": ["global", "us", "eu"],
"claude-sonnet-5": ["global", "us", "eu"],
"claude-fable-5": ["global", "us", "eu"],
"claude-opus-4-8": ["global", "us", "eu"],
"claude-opus-4-7": ["global", "us", "eu"],
"claude-sonnet-4-6": ["us-east5", "europe-west1", "asia-southeast1", "global"],
"claude-opus-4-6": [
"us-east5",
"us-west4",
"us-east1",
"us-south1",
"europe-west1",
"europe-west4",
"europe-north1",
"asia-southeast1",
"global",
],
"claude-opus-4-5": ["us-east5", "europe-west1", "asia-southeast1", "global"],
"claude-haiku-4-5": ["us-east5", "europe-west1", "global"],
"claude-sonnet-4-5": ["us-east5", "europe-west1", "asia-southeast1", "global"],
"claude-opus-4-1": ["us-east5", "europe-west4", "global"],
"claude-sonnet-4": ["us-east5", "europe-west4", "global"],
"claude-opus-4": ["us-east5", "europe-west4", "global"],
"claude-3-7-sonnet": ["us-east5", "europe-west1", "europe-west4", "global"],
"claude-3-5-sonnet-v2": ["us-east5", "europe-west1", "global"],
"claude-3-5-haiku": ["us-east5"],
"claude-3-5-sonnet": ["us-east5", "europe-west1", "asia-southeast1"],
"claude-3-opus": ["us-east5"],
"claude-3-haiku": ["us-east5", "europe-west1", "asia-southeast1"],
}
available_regions = MODEL_AVAILABLE_REGIONS[MODEL]
In [ ]:
import ipywidgets as widgets
from IPython.display import display
dropdown = widgets.Dropdown(
options=available_regions,
description="Select a location:",
font_weight="bold",
style={"description_width": "initial"},
)
def dropdown_eventhandler(change):
global LOCATION
if change["type"] == "change" and change["name"] == "value":
LOCATION = change.new
print("Selected:", change.new)
LOCATION = dropdown.value
dropdown.observe(dropdown_eventhandler, names="value")
display(dropdown)In [ ]:
PROJECT_ID = "[your-project-id]" # @param {type:"string"}
MULTI_REGION_LIST = ["us", "eu"]
if LOCATION == "global":
ENDPOINT = "https://aiplatform.googleapis.com"
elif LOCATION in MULTI_REGION_LIST:
ENDPOINT = f"https://aiplatform.{LOCATION}.rep.googleapis.com"
else:
ENDPOINT = f"https://{LOCATION}-aiplatform.googleapis.com"
if not PROJECT_ID or PROJECT_ID == "[your-project-id]":
raise ValueError("Please set your PROJECT_ID")In [ ]:
import base64
import json
import requests
from IPython.display import ImageIn [ ]:
PAYLOAD = {
"anthropic_version": "vertex-2023-10-16",
"messages": [{"role": "user", "content": "Send me a recipe for banana bread."}],
"max_tokens": 100,
"stream": False,
}
request = json.dumps(PAYLOAD)
!curl -X POST -H "Authorization: Bearer $(gcloud auth print-access-token)" -H "Content-Type: application/json" {ENDPOINT}/v1/projects/{PROJECT_ID}/locations/{LOCATION}/publishers/anthropic/models/{MODEL}:rawPredict -d '{request}'In [ ]:
PAYLOAD = {
"anthropic_version": "vertex-2023-10-16",
"messages": [{"role": "user", "content": "Send me a recipe for banana bread."}],
"max_tokens": 100,
"stream": True,
}
request = json.dumps(PAYLOAD)
!curl -X POST -H "Authorization: Bearer $(gcloud auth print-access-token)" -H "Content-Type: application/json" {ENDPOINT}/v1/projects/{PROJECT_ID}/locations/{LOCATION}/publishers/anthropic/models/{MODEL}:streamRawPredict -d '{request}'In [ ]:
image_url = "https://upload.wikimedia.org/wikipedia/commons/a/a7/Camponotus_flavomarginatus_ant.jpg"
# Wikimedia Foundation blocks requests from default HTTP client libraries that
# do not specify a custom `User-Agent` header.
headers = {
"User-Agent": (
"Mozilla/5.0 (Windows NT 10.0; Win64; x64) AppleWebKit/537.36 (KHTML,"
" like Gecko) Chrome/115.0.0.0 Safari/537.36"
)
}
response = requests.get(url=image_url, headers=headers)
response.raise_for_status()
image_b64 = base64.b64encode(response.content).decode("utf-8")
display(Image(data=response.content, width=300))In [ ]:
PAYLOAD = {
"anthropic_version": "vertex-2023-10-16",
"messages": [
{
"role": "user",
"content": [
{
"type": "image",
"source": {
"type": "base64",
"media_type": "image/jpeg",
"data": image_b64,
},
},
{"type": "text", "text": "What is in this image?"},
],
}
],
"max_tokens": 100,
"stream": False,
}
# Save payload to request.json
with open("request.json", "w") as f:
json.dump(PAYLOAD, f)
# Pass the file to curl using -d @request.json
!curl -X POST \
-H "Authorization: Bearer $(gcloud auth print-access-token)" \
-H "Content-Type: application/json" \
"{ENDPOINT}/v1/projects/{PROJECT_ID}/locations/{LOCATION}/publishers/anthropic/models/{MODEL}:rawPredict" \
-d @request.jsonIn [ ]:
PAYLOAD = {
"anthropic_version": "vertex-2023-10-16",
"messages": [
{
"role": "user",
"content": [
{
"type": "image",
"source": {
"type": "base64",
"media_type": "image/jpeg",
"data": image_b64,
},
},
{"type": "text", "text": "What is in this image?"},
],
}
],
"max_tokens": 100,
"stream": True,
}
# Save payload to request.json
with open("request.json", "w") as f:
json.dump(PAYLOAD, f)
# Pass the file to curl using -d @request.json
!curl -X POST \
-H "Authorization: Bearer $(gcloud auth print-access-token)" \
-H "Content-Type: application/json" \
{ENDPOINT}/v1/projects/{PROJECT_ID}/locations/{LOCATION}/publishers/anthropic/models/{MODEL}:streamRawPredict \
-d @request.jsonIn [ ]:
! pip3 install -U -q 'anthropic[vertex]'In [ ]:
# Restart kernel after installs so that your environment can access the new packages
import sys
if "google.colab" in sys.modules:
import IPython
app = IPython.Application.instance()
app.kernel.do_shutdown(True)In [ ]:
import sys
if "google.colab" in sys.modules:
from google.colab import auth
auth.authenticate_user()In [ ]:
MODEL = "claude-fable-5-1" # @param ["claude-fable-5-1","claude-opus-5","claude-sonnet-5","claude-fable-5","claude-opus-4-8","claude-opus-4-7","claude-sonnet-4-6","claude-opus-4-6","claude-opus-4-5","claude-haiku-4-5","claude-sonnet-4-5","claude-opus-4-1","claude-sonnet-4","claude-opus-4","claude-3-7-sonnet","claude-3-5-sonnet-v2","claude-3-5-haiku","claude-3-5-sonnet","claude-3-opus","claude-3-haiku"]
# Available regions per model.
MODEL_AVAILABLE_REGIONS = {
"claude-fable-5-1": ["global", "us", "eu"],
"claude-opus-5": ["global", "us", "eu"],
"claude-sonnet-5": ["global", "us", "eu"],
"claude-fable-5": ["global", "us", "eu"],
"claude-opus-4-8": ["global", "us", "eu"],
"claude-opus-4-7": ["global", "us", "eu"],
"claude-sonnet-4-6": ["us-east5", "europe-west1", "asia-southeast1", "global"],
"claude-opus-4-6": [
"us-east5",
"us-west4",
"us-east1",
"us-south1",
"europe-west1",
"europe-west4",
"europe-north1",
"asia-southeast1",
"global",
],
"claude-opus-4-5": ["us-east5", "europe-west1", "asia-southeast1", "global"],
"claude-haiku-4-5": ["us-east5", "europe-west1", "global"],
"claude-sonnet-4-5": ["us-east5", "europe-west1", "asia-southeast1", "global"],
"claude-opus-4-1": ["us-east5", "europe-west4", "global"],
"claude-sonnet-4": ["us-east5", "europe-west4", "global"],
"claude-opus-4": ["us-east5", "europe-west4", "global"],
"claude-3-7-sonnet": ["us-east5", "europe-west1", "europe-west4", "global"],
"claude-3-5-sonnet-v2": ["us-east5", "europe-west1", "global"],
"claude-3-5-haiku": ["us-east5"],
"claude-3-5-sonnet": ["us-east5", "europe-west1", "asia-southeast1"],
"claude-3-opus": ["us-east5"],
"claude-3-haiku": ["us-east5", "europe-west1", "asia-southeast1"],
}
available_regions = MODEL_AVAILABLE_REGIONS[MODEL]
In [ ]:
import ipywidgets as widgets
from IPython.display import display
dropdown = widgets.Dropdown(
options=available_regions,
description="Select a location:",
font_weight="bold",
style={"description_width": "initial"},
)
def dropdown_eventhandler(change):
global LOCATION
if change["type"] == "change" and change["name"] == "value":
LOCATION = change.new
print("Selected:", change.new)
LOCATION = dropdown.value
dropdown.observe(dropdown_eventhandler, names="value")
display(dropdown)In [ ]:
PROJECT_ID = "[your-project-id]" # @param {type:"string"}
if not PROJECT_ID or PROJECT_ID == "[your-project-id]":
raise ValueError("Please set your PROJECT_ID")In [ ]:
import base64
import requests
from IPython.display import ImageIn [ ]:
from anthropic import AnthropicVertex
client = AnthropicVertex(region=LOCATION, project_id=PROJECT_ID)
message = client.messages.create(
max_tokens=1024,
messages=[
{
"role": "user",
"content": "Send me a recipe for banana bread.",
}
],
model=MODEL,
)
print(message.model_dump_json(indent=2))In [ ]:
from anthropic import AnthropicVertex
client = AnthropicVertex(region=LOCATION, project_id=PROJECT_ID)
with client.messages.stream(
max_tokens=1024,
messages=[
{
"role": "user",
"content": "Send me a recipe for banana bread.",
}
],
model=MODEL,
) as stream:
for text in stream.text_stream:
print(text, end="", flush=True)In [ ]:
image_url = "https://upload.wikimedia.org/wikipedia/commons/a/a7/Camponotus_flavomarginatus_ant.jpg"
image_media_type = "image/jpeg"
# Wikimedia Foundation blocks requests from default HTTP client libraries that
# do not specify a custom `User-Agent` header.
headers = {
"User-Agent": (
"Mozilla/5.0 (Windows NT 10.0; Win64; x64) AppleWebKit/537.36 (KHTML,"
" like Gecko) Chrome/115.0.0.0 Safari/537.36"
)
}
response = requests.get(url=image_url, headers=headers)
response.raise_for_status()
image_b64 = base64.b64encode(response.content).decode("utf-8")
display(Image(data=response.content, width=300))In [ ]:
from anthropic import AnthropicVertex
client = AnthropicVertex(region=LOCATION, project_id=PROJECT_ID)
message = client.messages.create(
max_tokens=1024,
messages=[
{
"role": "user",
"content": [
{
"type": "image",
"source": {
"type": "base64",
"media_type": image_media_type,
"data": image_b64,
},
},
{"type": "text", "text": "Describe this image."},
],
}
],
model=MODEL,
)
print(message.model_dump_json(indent=2))In [ ]:
from anthropic import AnthropicVertex
client = AnthropicVertex(region=LOCATION, project_id=PROJECT_ID)
with client.messages.stream(
max_tokens=1024,
messages=[
{
"role": "user",
"content": [
{
"type": "image",
"source": {
"type": "base64",
"media_type": image_media_type,
"data": image_b64,
},
},
{"type": "text", "text": "Describe this image."},
],
}
],
model=MODEL,
) as stream:
for text in stream.text_stream:
print(text, end="", flush=True)