Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Add Amazon Bedrock Claude3 Sonnet support #19

Open
wants to merge 2 commits into
base: main
Choose a base branch
from
Open
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
4 changes: 2 additions & 2 deletions requirements.txt
Original file line number Diff line number Diff line change
@@ -1,8 +1,8 @@
aiohttp==3.8.6
aiosignal==1.3.1
annotated-types==0.6.0
anthropic==0.16.0
anthropic-bedrock==0.8.0
anthropic==0.19.1
boto3==1.34.59
anyio==3.7.1
async-timeout==4.0.3
attrs==23.1.0
Expand Down
10 changes: 6 additions & 4 deletions tool_use_package/tool_user.py
Original file line number Diff line number Diff line change
@@ -1,5 +1,5 @@
from anthropic import Anthropic
from anthropic_bedrock import AnthropicBedrock
from anthropic import AnthropicBedrock
import re
import builtins
import ast
Expand Down Expand Up @@ -43,10 +43,12 @@ def __init__(self, tools, temperature=0, max_retries=3, first_party=True, model=
self.model=model
self.client = Anthropic()
else:
if model == "anthropic.claude-v2:1" or model == "default":
if model == "anthropic.claude-v2:1" or model == "default" :
self.model = "anthropic.claude-v2:1"
elif "anthropic.claude-3" in model :
self.model = model
else:
raise ValueError("Only Claude 2.1 is currently supported when working with bedrock in this sdk. If you'd like to use another model, please use the first party anthropic API (and set first_party=true).")
raise ValueError("Only Claude 2.1 and Claude 3 Sonnet is currently supported when working with bedrock in this sdk. If you'd like to use another model, please use the first party anthropic API (and set first_party=true).")
self.client = AnthropicBedrock()
self.current_prompt = None
self.current_num_retries = 0
Expand Down Expand Up @@ -203,7 +205,7 @@ def _construct_next_injection(self, invoke_results):
raise ValueError(f"Unrecognized status from invoke_results, {invoke_results['status']}.")

def _complete(self, prompt, max_tokens_to_sample, temperature):
if self.first_party:
if self.first_party or "anthropic.claude-3" in self.model:
return self._messages_complete(prompt, max_tokens_to_sample, temperature)
else:
return self._completions_complete(prompt, max_tokens_to_sample, temperature)
Expand Down
65 changes: 65 additions & 0 deletions tools_example.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,65 @@
import requests

from tool_use_package.tools.base_tool import BaseTool
from tool_use_package.tool_user import ToolUser

# 1. Define the Tool
class GetLatitudeAndLongitude(BaseTool):
"""Returns the latitude and longitude for a given place name."""

def use_tool(self,place):

url = "https://nominatim.openstreetmap.org/search"
params = {'q': place, 'format': 'json', 'limit': 1}
response = requests.get(url, params=params).json()
if response:
lat = response[0]["lat"]
lon = response[0]["lon"]
print(f"invoke lat and lon tools {place}, {lat},{lon}")
return {"latitude": lat, "longitude": lon}
else:
return None

class GetWeatherTool(BaseTool):
"""Returns weather data for a given latitude and longitude."""

def use_tool(self,latitude: str, longitude: str):
url = f"https://api.open-meteo.com/v1/forecast?latitude={latitude}&longitude={longitude}&current_weather=true"
response = requests.get(url)
result = response.json()
print(f"invoke getweather tools {latitude}, {longitude},{result}")
return result





# 2. Tool Description
getweather_tool_name = "perform_getweather"
getweather_tool_description = """Returns weather data for a given latitude and longitude.
Use this tool WHENEVER you need to perform any getweather calculation, as it will ensure your answer is precise."""
getweather_tool_parameters = [
{"name": "latitude", "type": "str", "description": "latitude."},
{"name": "longitude", "type": "str", "description": "longitude."}
]

getlat_and_lon_tool_name = "perform_getweather_tools"
getlat_and_lon_tool_description = """Returns the latitude and longitude for a given place name..
Use this tool WHENEVER you need to perform any getweather calculation, as it will ensure your answer is precise."""
getlat_and_lon_tool_parameters = [
{"name": "place", "type": "str", "description": "place name."},
]

getweather_tool = GetWeatherTool(getweather_tool_name, getweather_tool_description, getweather_tool_parameters)
getlatitude_longitude_tool = GetLatitudeAndLongitude(getlat_and_lon_tool_name ,getlat_and_lon_tool_description,getlat_and_lon_tool_parameters)

# 3. Assign Tool and Ask Claude
tool_user = ToolUser([getlatitude_longitude_tool,getweather_tool],first_party=False , model="anthropic.claude-3-sonnet-20240229-v1:0")
messages = [
{
"role":"user",
"content":"""
Can you check the weather for me in Paris, France?"""
}
]
print(tool_user.use_tools(messages, execution_mode="automatic"))