Skip to content
Merged
Show file tree
Hide file tree
Changes from 3 commits
Commits
Show all changes
23 commits
Select commit Hold shift + click to select a range
5698195
fix(anthropic/chat/transformation.py): support anthropic disable_para…
Oct 28, 2024
e7f0674
feat(anthropic/chat/transformation.py): support anthropic computer to…
Oct 28, 2024
c4ee99c
fix(vertex_ai/common_utils.py): parse out '$schema' when calling vert…
Oct 28, 2024
f49ef06
fix(main.py): add 'extra_headers' support for azure on all translatio…
Oct 28, 2024
dda729e
fix: fix linting errors
Oct 29, 2024
d702f62
fix(transformation.py): handle no beta headers for anthropic
Oct 29, 2024
30c83d8
test: cleanup test
Oct 29, 2024
df95f77
fix: fix linting error
Oct 29, 2024
c2a0348
fix: fix linting errors
Oct 29, 2024
5cc16ed
fix: fix linting errors
Oct 29, 2024
6358d71
fix(transformation.py): handle dummy tool call
Oct 29, 2024
3223772
fix(main.py): fix linting error
Oct 29, 2024
69c9c9b
fix(azure.py): pass required param
Oct 29, 2024
0cc883d
LiteLLM Minor Fixes & Improvements (10/24/2024) (#6441)
Oct 28, 2024
1362916
Litellm dev 10 26 2024 (#6472)
Oct 28, 2024
b829310
(Testing) Add unit testing for DualCache - ensure in memory cache is …
ishaan-jaff Oct 29, 2024
143283c
redis otel tracing + async support for latency routing (#6452)
Oct 29, 2024
0bd495a
fix(dual_cache.py): set default value for parent_otel_span
Oct 29, 2024
9aad9e3
fix(transformation.py): support 'response_format' for anthropic calls
Oct 29, 2024
df06f25
fix(transformation.py): check for cache_control inside 'function' block
Oct 29, 2024
fc836cf
fix: fix linting error
Oct 29, 2024
4d405bb
Merge branch 'main' into litellm_dev_10_28_2024
Oct 29, 2024
18e1f0d
fix: fix linting errors
Oct 29, 2024
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
31 changes: 31 additions & 0 deletions docs/my-website/docs/providers/anthropic.md
Original file line number Diff line number Diff line change
Expand Up @@ -721,6 +721,37 @@ except Exception as e:

s/o @[Shekhar Patnaik](https://www.linkedin.com/in/patnaikshekhar) for requesting this!

### Computer Tools

```python
from litellm import completion

tools = [
{
"type": "computer_20241022",
"function": {
"name": "computer",
"parameters": {
"display_height_px": 100,
"display_width_px": 100,
"display_number": 1,
},
},
}
]
model = "claude-3-5-sonnet-20241022"
messages = [{"role": "user", "content": "Save a picture of a cat to my desktop."}]

resp = completion(
model=model,
messages=messages,
tools=tools,
# headers={"anthropic-beta": "computer-use-2024-10-22"},
)

print(resp)
```

## Usage - Vision

```python
Expand Down
35 changes: 23 additions & 12 deletions litellm/llms/anthropic/chat/handler.py
Original file line number Diff line number Diff line change
Expand Up @@ -29,6 +29,7 @@
get_async_httpx_client,
)
from litellm.types.llms.anthropic import (
AllAnthropicToolsValues,
AnthropicChatCompletionUsageBlock,
ContentBlockDelta,
ContentBlockStart,
Expand All @@ -53,26 +54,30 @@

# makes headers for API call
def validate_environment(
api_key, user_headers, model, messages: List[AllMessageValues]
api_key,
user_headers,
model,
messages: List[AllMessageValues],
tools: Optional[List[AllAnthropicToolsValues]],
anthropic_version: Optional[str] = None,
):
cache_headers = {}

if api_key is None:
raise litellm.AuthenticationError(
message="Missing Anthropic API Key - A call is being made to anthropic but no key is set either in the environment variables or via params. Please set `ANTHROPIC_API_KEY` in your environment vars",
llm_provider="anthropic",
model=model,
)

if AnthropicConfig().is_cache_control_set(messages=messages):
cache_headers = AnthropicConfig().get_cache_control_headers()
headers = {
"accept": "application/json",
"anthropic-version": "2023-06-01",
"content-type": "application/json",
"x-api-key": api_key,
}
prompt_caching_set = AnthropicConfig().is_cache_control_set(messages=messages)
computer_tool_used = AnthropicConfig().is_computer_tool_used(tools=tools)

headers.update(cache_headers)
headers = AnthropicConfig().get_anthropic_headers(
anthropic_version=anthropic_version,
computer_tool_used=computer_tool_used,
prompt_caching_set=prompt_caching_set,
api_key=api_key,
)

if user_headers is not None and isinstance(user_headers, dict):
headers = {**headers, **user_headers}
Expand Down Expand Up @@ -440,7 +445,13 @@ def completion(
headers={},
client=None,
):
headers = validate_environment(api_key, headers, model, messages=messages)
headers = validate_environment(
api_key,
headers,
model,
messages=messages,
tools=optional_params.get("tools"),
)
_is_function_call = False
messages = copy.deepcopy(messages)
optional_params = copy.deepcopy(optional_params)
Expand Down
182 changes: 155 additions & 27 deletions litellm/llms/anthropic/chat/transformation.py
Original file line number Diff line number Diff line change
Expand Up @@ -4,6 +4,9 @@
import litellm
from litellm.llms.prompt_templates.factory import anthropic_messages_pt
from litellm.types.llms.anthropic import (
AllAnthropicToolsValues,
AnthropicComputerTool,
AnthropicHostedTools,
AnthropicMessageRequestBase,
AnthropicMessagesRequest,
AnthropicMessagesTool,
Expand Down Expand Up @@ -84,6 +87,7 @@ def get_supported_openai_params(self):
"tools",
"tool_choice",
"extra_headers",
"parallel_tool_calls",
]

def get_cache_control_headers(self) -> dict:
Expand All @@ -92,6 +96,137 @@ def get_cache_control_headers(self) -> dict:
"anthropic-beta": "prompt-caching-2024-07-31",
}

def get_anthropic_headers(
self,
api_key: str,
anthropic_version: Optional[str] = None,
computer_tool_used: bool = False,
prompt_caching_set: bool = False,
) -> dict:
import json

betas = []
if prompt_caching_set:
betas.append("prompt-caching-2024-07-31")
if computer_tool_used:
betas.append("computer-use-2024-10-22")
betas_str = ",".join(betas)
headers = {
"anthropic-version": anthropic_version or "2023-06-01",
"anthropic-beta": betas_str,
"x-api-key": api_key,
"accept": "application/json",
"content-type": "application/json",
}
return headers

def _map_tool_choice(
self, tool_choice: Optional[str], disable_parallel_tool_use: Optional[bool]
) -> Optional[AnthropicMessagesToolChoice]:
_tool_choice: Optional[AnthropicMessagesToolChoice] = None
if tool_choice == "auto":
_tool_choice = AnthropicMessagesToolChoice(
type="auto",
)
elif tool_choice == "required":
_tool_choice = AnthropicMessagesToolChoice(type="any")
elif isinstance(tool_choice, dict):
_tool_name = tool_choice.get("function", {}).get("name")
_tool_choice = AnthropicMessagesToolChoice(type="tool")
if _tool_name is not None:
_tool_choice["name"] = _tool_name

if disable_parallel_tool_use is not None:
if _tool_choice is not None:
_tool_choice["disable_parallel_tool_use"] = disable_parallel_tool_use
else: # use anthropic defaults and make sure to send the disable_parallel_tool_use flag
_tool_choice = AnthropicMessagesToolChoice(
type="auto",
disable_parallel_tool_use=disable_parallel_tool_use,
)
return _tool_choice

def _map_tool_helper(
self, tool: ChatCompletionToolParam
) -> AllAnthropicToolsValues:
returned_tool: Optional[AllAnthropicToolsValues] = None
if tool["type"] == "function" or tool["type"] == "custom":
_tool = AnthropicMessagesTool(
name=tool["function"]["name"],
input_schema=tool["function"].get(
"parameters",
{
"type": "object",
"properties": {},
},
),
)

_description = tool["function"].get("description")
if _description is not None:
_tool["description"] = _description

returned_tool = _tool

elif tool["type"].startswith("computer_"):
## check if all required 'display_' params are given
if "parameters" not in tool["function"]:
raise ValueError("Missing required parameter: parameters")
required_display_params = ["display_width_px", "display_height_px"]

_display_width_px: Optional[int] = tool["function"]["parameters"].get(
"display_width_px"
)
_display_height_px: Optional[int] = tool["function"]["parameters"].get(
"display_height_px"
)
if _display_width_px is None or _display_height_px is None:
raise ValueError(
"Missing required parameter: display_width_px or display_height_px"
)

_computer_tool = AnthropicComputerTool(
type=tool["type"],
name=tool["function"].get("name", "computer"),
display_width_px=_display_width_px,
display_height_px=_display_height_px,
)

_display_number = tool["function"]["parameters"].get("display_number")
if _display_number is not None:
_computer_tool["display_number"] = _display_number

returned_tool = _computer_tool
elif tool["type"].startswith("bash_") or tool["type"].startswith(
"text_editor_"
):
function_name = tool["function"].get("name")
if function_name is None:
raise ValueError("Missing required parameter: name")

returned_tool = AnthropicHostedTools(
type=tool["type"],
name=function_name,
)
if returned_tool is None:
raise ValueError(f"Unsupported tool type: {tool['type']}")

if "cache_control" in tool:
returned_tool["cache_control"] = tool["cache_control"]

return returned_tool

def _map_tools(self, tools: List) -> List[AllAnthropicToolsValues]:
anthropic_tools = []
for tool in tools:
if "input_schema" in tool: # assume in anthropic format
anthropic_tools.append(tool)
else: # assume openai tool call
new_tool = self._map_tool_helper(tool)

anthropic_tools.append(new_tool)
return anthropic_tools

def map_openai_params(
self,
non_default_params: dict,
Expand All @@ -104,15 +239,16 @@ def map_openai_params(
if param == "max_completion_tokens":
optional_params["max_tokens"] = value
if param == "tools":
optional_params["tools"] = value
if param == "tool_choice":
_tool_choice: Optional[AnthropicMessagesToolChoice] = None
if value == "auto":
_tool_choice = {"type": "auto"}
elif value == "required":
_tool_choice = {"type": "any"}
elif isinstance(value, dict):
_tool_choice = {"type": "tool", "name": value["function"]["name"]}
optional_params["tools"] = self._map_tools(value)
if param == "tool_choice" or param == "parallel_tool_calls":
_tool_choice: Optional[AnthropicMessagesToolChoice] = (
self._map_tool_choice(
tool_choice=non_default_params.get("tool_choice"),
disable_parallel_tool_use=non_default_params.get(
"parallel_tool_calls"
),
)
)

if _tool_choice is not None:
optional_params["tool_choice"] = _tool_choice
Expand Down Expand Up @@ -180,6 +316,16 @@ def is_cache_control_set(self, messages: List[AllMessageValues]) -> bool:

return False

def is_computer_tool_used(
self, tools: Optional[List[AllAnthropicToolsValues]]
) -> bool:
if tools is None:
return False
for tool in tools:
if "type" in tool and tool["type"].startswith("computer_"):
return True
return False

def translate_system_message(
self, messages: List[AllMessageValues]
) -> List[AnthropicSystemMessageContent]:
Expand Down Expand Up @@ -274,24 +420,6 @@ def _transform_request(
## Handle Tool Calling
if "tools" in optional_params:
_is_function_call = True
anthropic_tools = []
for tool in optional_params["tools"]:
if "input_schema" in tool: # assume in anthropic format
anthropic_tools.append(tool)
else: # assume openai tool call
new_tool = tool["function"]
parameters = new_tool.pop(
"parameters",
{
"type": "object",
"properties": {},
},
)
new_tool["input_schema"] = parameters # rename key
if "cache_control" in tool:
new_tool["cache_control"] = tool["cache_control"]
anthropic_tools.append(new_tool)
optional_params["tools"] = anthropic_tools

data = {
"messages": anthropic_messages,
Expand Down
15 changes: 10 additions & 5 deletions litellm/llms/vertex_ai_and_google_ai_studio/common_utils.py
Original file line number Diff line number Diff line change
Expand Up @@ -164,7 +164,12 @@ def _build_vertex_schema(parameters: dict):
# 4. Suppress unnecessary title generation:
# * https://github.com/pydantic/pydantic/issues/1051
# * http://cl/586221780
strip_titles(parameters)
strip_field(parameters, field_name="title")

strip_field(
parameters, field_name="$schema"
) # 5. Remove $schema - json schema value, not supported by OpenAPI - causes vertex errors.

return parameters


Expand Down Expand Up @@ -244,14 +249,14 @@ def add_object_type(schema):
add_object_type(items)


def strip_titles(schema):
schema.pop("title", None)
def strip_field(schema, field_name: str):
schema.pop(field_name, None)

properties = schema.get("properties", None)
if properties is not None:
for name, value in properties.items():
strip_titles(value)
strip_field(value, field_name)

items = schema.get("items", None)
if items is not None:
strip_titles(items)
strip_field(items, field_name)
Original file line number Diff line number Diff line change
Expand Up @@ -394,10 +394,7 @@ def map_tool_choice_values(
def _map_function(self, value: List[dict]) -> List[Tools]:
gtool_func_declarations = []
googleSearchRetrieval: Optional[dict] = None
# remove 'additionalProperties' from tools
value = _remove_additional_properties(value)
# remove 'strict' from tools
value = _remove_strict_from_schema(value)

for tool in value:
openai_function_object: Optional[ChatCompletionToolParamFunctionChunk] = (
None
Expand All @@ -406,6 +403,15 @@ def _map_function(self, value: List[dict]) -> List[Tools]:
openai_function_object = ChatCompletionToolParamFunctionChunk( # type: ignore
**tool["function"]
)

if (
"parameters" in openai_function_object
and openai_function_object["parameters"] is not None
): # OPENAI accepts JSON Schema, Google accepts OpenAPI schema.
openai_function_object["parameters"] = _build_vertex_schema(
openai_function_object["parameters"]
)

elif "name" in tool: # functions list
openai_function_object = ChatCompletionToolParamFunctionChunk(**tool) # type: ignore

Expand Down
Loading