Skip to content

Commit

Permalink
Add tool use sample for Claude model (#11807)
Browse files Browse the repository at this point in the history
* add sample for anthropic tool use

* lint

---------

Co-authored-by: Huy Ngo <huyngo@google.com>
  • Loading branch information
huythngo and huyngo-google authored May 31, 2024
1 parent 20a3d4c commit 9833dea
Show file tree
Hide file tree
Showing 2 changed files with 102 additions and 0 deletions.
68 changes: 68 additions & 0 deletions generative_ai/anthropic_claude_3_tool_use.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,68 @@
# 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.

# [START aiplatform_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


def tool_use(project_id: str, region: str) -> object:
client = AnthropicVertex(region=region, project_id=project_id)
message = client.messages.create(
model="claude-3-opus@20240229",
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))
return message


# [END aiplatform_claude_3_tool_use]


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

import backoff
from google.api_core.exceptions import ResourceExhausted

import anthropic_claude_3_tool_use

_PROJECT_ID = os.getenv("GOOGLE_CLOUD_PROJECT")
_LOCATION = "us-east5"


@backoff.on_exception(backoff.expo, ResourceExhausted, max_time=10)
def tool_use_test() -> None:
response = anthropic_claude_3_tool_use.tool_use(
project_id=_PROJECT_ID, region=_LOCATION
)
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

0 comments on commit 9833dea

Please sign in to comment.