Anthropic Claude 3で、VertexAIやBedrockでも、Tool use (Function calling) つかえるようになりました、
https://docs.anthropic.com/en/docs/tool-use
anthropic 0.28.0
知りたい地区の天気をLLMに聞くと,locationを返して、Google GenCode API, openweathermap APIで現在の天気を返せるものがサクッと作れるようになりました。
from anthropic import AnthropicVertex
import requests
import json
def main():
LOCATION = "us-central1"
client = AnthropicVertex(region=LOCATION, project_id="xxxxxxx")
response = client.messages.create(
max_tokens=1024,
tools=[
{
"name": "get_weather",
"description": "Get the current weather in a given location",
"input_schema": {
"type": "object",
"properties": {
"location": {
"type": "string",
"description": "The city and state, e.g. San Francisco, CA"
}
},
"required": ["location"]
}
}
],
messages=[{"role": "user","content": "柏の天気を教えて?"}],
model="claude-3-haiku@20240307",
)
# print(message)
print(response.content[-1].input)
payload = {'address': response.content[-1].input['location'], 'key': 'xxxxxxxxxxxxxxxx'}
r = requests.get('https://maps.googleapis.com/maps/api/geocode/json', params=payload)
# print(r.text['results'][0].geometry.location)
d=json.loads(r.text)
loc = d['results'][0]['geometry']['location']
print(loc)
# curl "https://api.openweathermap.org/data/3.0/onecall
payload2 = {'lat': loc['lat'], 'lon': loc['lng'], 'lang': 'ja', 'appid': 'xxxxxxxxxxxxxxxxxx'}
r2 = requests.get('https://api.openweathermap.org/data/3.0/onecall', params=payload2)
d2 = json.loads(r2.text)
print(d2['current']['weather'])
if __name__ == "__main__":
main()
{'location': '柏, 千葉'}
{'lat': 35.8675958, 'lng': 139.9757575}
[{'id': 500, 'main': 'Rain', 'description': '小雨', 'icon': '10d'}]