From 3640262dbfacccefd3f8b70ac2677a18b4e3a345 Mon Sep 17 00:00:00 2001 From: Ishaan Jaff Date: Wed, 26 Mar 2025 17:12:40 -0700 Subject: [PATCH] fix anthropic_messages implementation --- .../messages/handler.py | 21 +++- litellm/messages/__init__.py | 98 ++++++++++++++++++- 2 files changed, 111 insertions(+), 8 deletions(-) diff --git a/litellm/llms/anthropic/experimental_pass_through/messages/handler.py b/litellm/llms/anthropic/experimental_pass_through/messages/handler.py index 9b890db266..54826a38ba 100644 --- a/litellm/llms/anthropic/experimental_pass_through/messages/handler.py +++ b/litellm/llms/anthropic/experimental_pass_through/messages/handler.py @@ -6,7 +6,7 @@ """ import json -from typing import Any, AsyncIterator, Dict, Optional, Union, cast +from typing import AsyncIterator, Dict, List, Optional, Union, cast import httpx @@ -64,9 +64,20 @@ class AnthropicMessagesHandler: @client async def anthropic_messages( - api_key: str, + max_tokens: int, + messages: List[Dict[str, Union[str, List[Dict[str, str]]]]], model: str, - stream: bool = False, + metadata: Optional[Dict] = None, + stop_sequences: Optional[List[str]] = None, + stream: Optional[bool] = False, + system: Optional[str] = None, + temperature: Optional[float] = 1.0, + thinking: Optional[Dict] = None, + tool_choice: Optional[Dict] = None, + tools: Optional[List[Dict]] = None, + top_k: Optional[int] = None, + top_p: Optional[float] = None, + api_key: Optional[str] = None, api_base: Optional[str] = None, client: Optional[AsyncHTTPHandler] = None, custom_llm_provider: Optional[str] = None, @@ -133,7 +144,7 @@ async def anthropic_messages( litellm_logging_obj.model_call_details.update(kwargs) # Prepare request body - request_body = kwargs.copy() + request_body = locals().copy() request_body = { k: v for k, v in request_body.items() @@ -165,7 +176,7 @@ async def anthropic_messages( url=request_url, headers=headers, data=json.dumps(request_body), - stream=stream, + stream=stream or False, ) response.raise_for_status() diff --git a/litellm/messages/__init__.py b/litellm/messages/__init__.py index 1274f768ba..0a4ea92c2c 100644 --- a/litellm/messages/__init__.py +++ b/litellm/messages/__init__.py @@ -4,13 +4,105 @@ Interface for Anthropic's messages API Use this to call LLMs in Anthropic /messages Request/Response format """ +from typing import Dict, List, Optional, Union + from litellm.llms.anthropic.experimental_pass_through.handler import ( anthropic_messages as _async_anthropic_messages, ) -async def acreate(*args, **kwargs): +async def acreate( + max_tokens: int, + messages: List[Dict[str, Union[str, List[Dict[str, str]]]]], + model: str, + metadata: Optional[Dict] = None, + stop_sequences: Optional[List[str]] = None, + stream: Optional[bool] = False, + system: Optional[str] = None, + temperature: Optional[float] = 1.0, + thinking: Optional[Dict] = None, + tool_choice: Optional[Dict] = None, + tools: Optional[List[Dict]] = None, + top_k: Optional[int] = None, + top_p: Optional[float] = None, + **kwargs +) -> Dict: """ - Wrapper around Anthropic's messages API + Async wrapper for Anthropic's messages API + + Args: + max_tokens (int): Maximum tokens to generate (required) + messages (List[Dict]): List of message objects with role and content (required) + model (str): Model name to use (required) + metadata (Dict, optional): Request metadata + stop_sequences (List[str], optional): Custom stop sequences + stream (bool, optional): Whether to stream the response + system (str, optional): System prompt + temperature (float, optional): Sampling temperature (0.0 to 1.0) + thinking (Dict, optional): Extended thinking configuration + tool_choice (Dict, optional): Tool choice configuration + tools (List[Dict], optional): List of tool definitions + top_k (int, optional): Top K sampling parameter + top_p (float, optional): Nucleus sampling parameter + **kwargs: Additional arguments + + Returns: + Dict: Response from the API """ - return await _async_anthropic_messages(*args, **kwargs) + return await _async_anthropic_messages( + max_tokens=max_tokens, + messages=messages, + model=model, + metadata=metadata, + stop_sequences=stop_sequences, + stream=stream, + system=system, + temperature=temperature, + thinking=thinking, + tool_choice=tool_choice, + tools=tools, + top_k=top_k, + top_p=top_p, + **kwargs, + ) + + +async def create( + max_tokens: int, + messages: List[Dict[str, Union[str, List[Dict[str, str]]]]], + model: str, + metadata: Optional[Dict] = None, + stop_sequences: Optional[List[str]] = None, + stream: Optional[bool] = False, + system: Optional[str] = None, + temperature: Optional[float] = 1.0, + thinking: Optional[Dict] = None, + tool_choice: Optional[Dict] = None, + tools: Optional[List[Dict]] = None, + top_k: Optional[int] = None, + top_p: Optional[float] = None, + **kwargs +) -> Dict: + """ + Async wrapper for Anthropic's messages API + + Args: + max_tokens (int): Maximum tokens to generate (required) + messages (List[Dict]): List of message objects with role and content (required) + model (str): Model name to use (required) + metadata (Dict, optional): Request metadata + stop_sequences (List[str], optional): Custom stop sequences + stream (bool, optional): Whether to stream the response + system (str, optional): System prompt + temperature (float, optional): Sampling temperature (0.0 to 1.0) + thinking (Dict, optional): Extended thinking configuration + tool_choice (Dict, optional): Tool choice configuration + tools (List[Dict], optional): List of tool definitions + top_k (int, optional): Top K sampling parameter + top_p (float, optional): Nucleus sampling parameter + **kwargs: Additional arguments + + Returns: + Dict: Response from the API + """ + raise NotImplementedError("This function is not implemented")