From b2c1babc00f9fe26c4055b62581cdc545a5d101a Mon Sep 17 00:00:00 2001 From: Thoughtseize1 Date: Tue, 17 Sep 2024 16:25:50 +0200 Subject: [PATCH] New Samples for Model Garden --- .../claude_3_streaming_example.py | 64 +++++++++++++++ .../model_garden/claude_3_tool_example.py | 80 +++++++++++++++++++ .../model_garden/claude_3_unary_example.py | 56 +++++++++++++ .../model_garden/model_garden_test.py | 42 ++++++++++ 4 files changed, 242 insertions(+) create mode 100644 generative_ai/model_garden/claude_3_streaming_example.py create mode 100644 generative_ai/model_garden/claude_3_tool_example.py create mode 100644 generative_ai/model_garden/claude_3_unary_example.py create mode 100644 generative_ai/model_garden/model_garden_test.py diff --git a/generative_ai/model_garden/claude_3_streaming_example.py b/generative_ai/model_garden/claude_3_streaming_example.py new file mode 100644 index 00000000000..3e817e9b584 --- /dev/null +++ b/generative_ai/model_garden/claude_3_streaming_example.py @@ -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() diff --git a/generative_ai/model_garden/claude_3_tool_example.py b/generative_ai/model_garden/claude_3_tool_example.py new file mode 100644 index 00000000000..4b5511df1b3 --- /dev/null +++ b/generative_ai/model_garden/claude_3_tool_example.py @@ -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() diff --git a/generative_ai/model_garden/claude_3_unary_example.py b/generative_ai/model_garden/claude_3_unary_example.py new file mode 100644 index 00000000000..9fde85cfafd --- /dev/null +++ b/generative_ai/model_garden/claude_3_unary_example.py @@ -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() diff --git a/generative_ai/model_garden/model_garden_test.py b/generative_ai/model_garden/model_garden_test.py new file mode 100644 index 00000000000..93cd90962ca --- /dev/null +++ b/generative_ai/model_garden/model_garden_test.py @@ -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)