-
Notifications
You must be signed in to change notification settings - Fork 6
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Merge pull request #703 from parea-ai/PAI-966-anthropic-beta-tool-use…
…-support Pai 966 anthropic beta tool use support
- Loading branch information
Showing
3 changed files
with
183 additions
and
4 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,124 @@ | ||
single_tool_use = { | ||
"model": "claude-3-opus-20240229", | ||
"max_tokens": 1024, | ||
"messages": [{"role": "user", "content": "What's the weather like in San Francisco?"}], | ||
"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"], | ||
}, | ||
} | ||
] | ||
} | ||
|
||
|
||
multiple_tool_use = { | ||
"model": "claude-3-opus-20240229", | ||
"max_tokens": 1024, | ||
"messages": [{"role": "user", "content": "What is the weather like right now in New York? Also what time is it there?"}], | ||
"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" | ||
}, | ||
"unit": { | ||
"type": "string", | ||
"enum": ["celsius", "fahrenheit"], | ||
"description": "The unit of temperature, either 'celsius' or 'fahrenheit'" | ||
} | ||
}, | ||
"required": ["location"] | ||
} | ||
}, | ||
{ | ||
"name": "get_time", | ||
"description": "Get the current time in a given time zone", | ||
"input_schema": { | ||
"type": "object", | ||
"properties": { | ||
"timezone": { | ||
"type": "string", | ||
"description": "The IANA time zone name, e.g. America/Los_Angeles" | ||
} | ||
}, | ||
"required": ["timezone"] | ||
} | ||
} | ||
] | ||
} | ||
|
||
|
||
missing_information = { | ||
"model": "claude-3-opus-20240229", | ||
"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" | ||
}, | ||
"unit": { | ||
"type": "string", | ||
"enum": ["celsius", "fahrenheit"], | ||
"description": "The unit of temperature, either \"celsius\" or \"fahrenheit\"" | ||
} | ||
}, | ||
"required": ["location"] | ||
} | ||
} | ||
], | ||
"messages": [ | ||
{ | ||
"role": "user", | ||
"content": "What is the weather like in San Francisco?" | ||
}, | ||
{ | ||
"role": "assistant", | ||
"content": [ | ||
{ | ||
"type": "text", | ||
"text": "<thinking>I need to use get_weather, and the user wants SF, which is likely San Francisco, CA.</thinking>" | ||
}, | ||
{ | ||
"type": "tool_use", | ||
"id": "toolu_01A09q90qw90lq917835lq9", | ||
"name": "get_weather", | ||
"input": { | ||
"location": "San Francisco, CA", | ||
"unit": "celsius" | ||
} | ||
} | ||
] | ||
}, | ||
{ | ||
"role": "user", | ||
"content": [ | ||
{ | ||
"type": "tool_result", | ||
"tool_use_id": "toolu_01A09q90qw90lq917835lq9", | ||
"content": "65 degrees" | ||
} | ||
] | ||
} | ||
] | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,36 @@ | ||
import asyncio | ||
import os | ||
|
||
import anthropic | ||
from dotenv import load_dotenv | ||
|
||
from parea import Parea | ||
from parea.cookbook.data.anthropic_tool_use_examples import missing_information, multiple_tool_use, single_tool_use | ||
|
||
load_dotenv() | ||
|
||
client = anthropic.Anthropic() | ||
aclient = anthropic.AsyncAnthropic() | ||
|
||
p = Parea(api_key=os.getenv("PAREA_API_KEY"), project_name="testing") | ||
p.wrap_anthropic_client(client) | ||
p.wrap_anthropic_client(aclient) | ||
|
||
|
||
def anthropic_sync(create_kwargs): | ||
message = client.beta.tools.messages.create(**create_kwargs) | ||
print(message.content) | ||
|
||
|
||
async def async_anthropic(create_kwargs): | ||
message = await aclient.beta.tools.messages.create(**create_kwargs) | ||
print(message.content) | ||
|
||
|
||
if __name__ == "__main__": | ||
anthropic_sync(single_tool_use) | ||
anthropic_sync(multiple_tool_use) | ||
anthropic_sync(missing_information) | ||
# asyncio.run(async_anthropic(single_tool_use)) | ||
# asyncio.run(async_anthropic(multiple_tool_use)) | ||
# asyncio.run(async_anthropic(missing_information)) |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters