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
64 changes: 64 additions & 0 deletions generative_ai/model_garden/claude_3_streaming_example.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,64 @@
# Copyright 2024 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.

import os

PROJECT_ID = os.getenv("GOOGLE_CLOUD_PROJECT")


def generate_text_streaming() -> str:
# [START generativeaionvertexai_claude_3_streaming]
# TODO(developer): Vertex AI SDK - uncomment below & run
# pip3 install --upgrade --user google-cloud-aiplatform
# gcloud auth application-default login
# pip3 install -U 'anthropic[vertex]'

# TODO(developer): Update and un-comment below line
# PROJECT_ID = "your-project-id"

from anthropic import AnthropicVertex

client = AnthropicVertex(project_id=PROJECT_ID, region="us-east5")
result = []

with client.messages.stream(
model="claude-3-5-sonnet@20240620",
max_tokens=1024,
messages=[
{
"role": "user",
"content": "Send me a recipe for banana bread.",
}
],
) as stream:
for text in stream.text_stream:
print(text, end="", flush=True)
result.append(text)

# Example response:
# Here's a simple recipe for delicious banana bread:
# Ingredients:
# - 2-3 ripe bananas, mashed
# - 1/3 cup melted butter
# ...
# ...
# 8. Bake for 50-60 minutes, or until a toothpick inserted into the center comes out clean.
# 9. Let cool in the pan for a few minutes, then remove and cool completely on a wire rack.

# [END generativeaionvertexai_claude_3_streaming]
return "".join(result)


if __name__ == "__main__":
generate_text_streaming()
80 changes: 80 additions & 0 deletions generative_ai/model_garden/claude_3_tool_example.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,80 @@
# Copyright 2024 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.
import os

PROJECT_ID = os.getenv("GOOGLE_CLOUD_PROJECT")


def tool_use() -> object:
# [START generativeaionvertexai_claude_3_tool_use]
# TODO(developer): Vertex AI SDK - uncomment below & run
# pip3 install --upgrade --user google-cloud-aiplatform
# gcloud auth application-default login
# pip3 install -U 'anthropic[vertex]'
from anthropic import AnthropicVertex

# TODO(developer): Update and un-comment below line
# PROJECT_ID = "your-project-id"

client = AnthropicVertex(project_id=PROJECT_ID, region="us-east5")
message = client.messages.create(
model="claude-3-5-sonnet@20240620",
max_tokens=1024,
tools=[
{
"name": "text_search_places_api",
"description": "returns information about a set of places based on a string",
"input_schema": {
"type": "object",
"properties": {
"textQuery": {
"type": "string",
"description": "The text string on which to search",
},
"priceLevels": {
"type": "array",
"description": "Price levels to query places, value can be one of [PRICE_LEVEL_INEXPENSIVE, PRICE_LEVEL_MODERATE, PRICE_LEVEL_EXPENSIVE, PRICE_LEVEL_VERY_EXPENSIVE]",
},
"openNow": {
"type": "boolean",
"description": "whether those places are open for business.",
},
},
"required": ["textQuery"],
},
}
],
messages=[
{
"role": "user",
"content": "What are some affordable and good Italian restaurants open now in San Francisco??",
}
],
)
print(message.model_dump_json(indent=2))
# Example response:
# {
# "id": "msg_vrtx_018pk1ykbbxAYhyWUdP1bJoQ",
# "content": [
# {
# "text": "To answer your question about affordable and good Italian restaurants
# that are currently open in San Francisco....
# ...

# [END generativeaionvertexai_claude_3_tool_use]
return message


if __name__ == "__main__":
tool_use()
56 changes: 56 additions & 0 deletions generative_ai/model_garden/claude_3_unary_example.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,56 @@
# Copyright 2024 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.
import os

PROJECT_ID = os.getenv("GOOGLE_CLOUD_PROJECT")


def generate_text() -> object:
# [START generativeaionvertexai_claude_3_unary]
# TODO(developer): Vertex AI SDK - uncomment below & run
# pip3 install --upgrade --user google-cloud-aiplatform
# gcloud auth application-default login
# pip3 install -U 'anthropic[vertex]'

# TODO(developer): Update and un-comment below line
# PROJECT_ID = "your-project-id"

from anthropic import AnthropicVertex

client = AnthropicVertex(project_id=PROJECT_ID, region="us-east5")
message = client.messages.create(
model="claude-3-5-sonnet@20240620",
max_tokens=1024,
messages=[
{
"role": "user",
"content": "Send me a recipe for banana bread.",
}
],
)
print(message.model_dump_json(indent=2))
# Example response:
# {
# "id": "msg_vrtx_0162rhgehxa9rvJM5BSVLZ9j",
# "content": [
# {
# "text": "Here's a simple recipe for delicious banana bread:\n\nIngredients:\n- 2-3 ripe bananas...
# ...

# [END generativeaionvertexai_claude_3_unary]
return message


if __name__ == "__main__":
generate_text()
42 changes: 42 additions & 0 deletions generative_ai/model_garden/model_garden_test.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,42 @@
# Copyright 2024 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.

import backoff

import claude_3_streaming_example
import claude_3_tool_example
import claude_3_unary_example

from google.api_core.exceptions import ResourceExhausted


@backoff.on_exception(backoff.expo, ResourceExhausted, max_time=10)
def test_generate_text_streaming() -> None:
responses = claude_3_streaming_example.generate_text_streaming()
assert "bread" in responses


@backoff.on_exception(backoff.expo, ResourceExhausted, max_time=10)
def test_tool_use() -> None:
response = claude_3_tool_example.tool_use()
json_response = response.model_dump_json(indent=2)
assert "restaurant" in json_response
assert "tool_use" in json_response
assert "text_search_places_api" in json_response


@backoff.on_exception(backoff.expo, ResourceExhausted, max_time=10)
def test_generate_text() -> None:
responses = claude_3_unary_example.generate_text()
assert "bread" in responses.model_dump_json(indent=2)
Loading