-
Notifications
You must be signed in to change notification settings - Fork 894
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
feat(agents-api): Add support for claude computer-use (#787)
```python from anthropic import Anthropic from anthropic.types.beta.beta_message import BetaMessage from litellm import ChatCompletionMessageToolCall, Function, Message from litellm.types.utils import Choices, ModelResponse # ... # ... # Check if the model is Anthropic and bash/editor/computer use tool is included if "claude" in agent_model.lower() and any( tool.type in ["bash_20241022", "text_editor_20241022", "computer_20241022"] for tool in agent_tools ): # Retrieve the API key from the environment variable betas = [COMPUTER_USE_BETA_FLAG] # Use Anthropic API directly client = Anthropic(api_key=anthropic_api_key) # Claude Response claude_response: BetaMessage = await client.beta.messages.create( model=agent_model, messages=prompt, tools=formatted_agent_tools, max_tokens=1024, betas=betas, ) # Claude returns [ToolUse | TextBlock] # We need to convert tool_use to tool_calls # And set content = TextBlock.text # But we need to ensure no more than one text block is returned if ( len([block for block in claude_response.content if block.type == "text"]) > 1 ): raise ApplicationError("Claude should only return one message") text_block = next( (block for block in claude_response.content if block.type == "text"), None, ) stop_reason = claude_response.stop_reason if stop_reason == "tool_use": choice = Choices( message=Message( role="assistant", content=text_block.text if text_block else None, tool_calls=[ ChatCompletionMessageToolCall( type="function", function=Function( name=block.name, arguments=block.input, ), ) for block in claude_response.content if block.type == "tool_use" ], ), finish_reason="tool_calls", ) else: assert text_block, "Claude should always return a text block for stop_reason=stop" choice = Choices( message=Message( role="assistant", content=text_block.text, ), finish_reason="stop", ) ``` <!-- ELLIPSIS_HIDDEN --> ---- > [!IMPORTANT] > Adds support for Anthropic Claude model with new tool types, updates models, and fixes a bug in `execute_system.py`. > > - **Behavior**: > - Adds support for Anthropic Claude model with `computer_20241022`, `text_editor_20241022`, and `bash_20241022` tools in `prompt_step.py`. > - Handles tool use in Claude responses, raising an error if multiple messages are returned. > - **Models**: > - Adds `type` field to `Tool`, `CreateToolRequest`, `PatchToolRequest`, and `UpdateToolRequest` models. > - Updates `ToolType` enum to include new tool types. > - **Misc**: > - Fixes a bug in `execute_system.py` related to `delete` operation handling. > - Imports `AsyncAnthropic` in `prompt_step.py` for asynchronous API calls. > > <sup>This description was created by </sup>[<img alt="Ellipsis" src="https://img.shields.io/badge/Ellipsis-blue?color=175173">](https://www.ellipsis.dev?ref=julep-ai%2Fjulep&utm_source=github&utm_medium=referral)<sup> for 5867ecb. It will automatically update as commits are pushed.</sup> <!-- ELLIPSIS_HIDDEN --> --------- Co-authored-by: vedantsahai18 <[email protected]> Co-authored-by: Vedantsahai18 <[email protected]>
- Loading branch information
1 parent
29c2674
commit b63327b
Showing
7 changed files
with
226 additions
and
38 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
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
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
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
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
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
Oops, something went wrong.