mirror of
https://github.com/BerriAI/litellm.git
synced 2026-10-07 02:59:05 +00:00
feat: Add synchronous methods for format conversion compatible with Anthropic and Gemini.
Co-authored-by: MrrDrr <l.tingting@pku.edu.cn>
This commit is contained in:
parent
c89496f378
commit
070446e490
6 changed files with 40 additions and 7 deletions
|
|
@ -10,7 +10,7 @@ This is an __init__.py file to allow the following interface
|
|||
|
||||
"""
|
||||
|
||||
from typing import Any, AsyncIterator, Coroutine, Dict, List, Optional, Union
|
||||
from typing import Any, AsyncIterator, Coroutine, Dict, List, Optional, Union,Iterator
|
||||
|
||||
from litellm.llms.anthropic.experimental_pass_through.messages.handler import (
|
||||
anthropic_messages as _async_anthropic_messages,
|
||||
|
|
@ -39,7 +39,7 @@ async def acreate(
|
|||
top_p: Optional[float] = None,
|
||||
container: Optional[Dict] = None,
|
||||
**kwargs
|
||||
) -> Union[AnthropicMessagesResponse, AsyncIterator]:
|
||||
) -> Union[AnthropicMessagesResponse, AsyncIterator, Iterator]:
|
||||
"""
|
||||
Async wrapper for Anthropic's messages API
|
||||
|
||||
|
|
@ -101,10 +101,11 @@ def create(
|
|||
) -> Union[
|
||||
AnthropicMessagesResponse,
|
||||
AsyncIterator[Any],
|
||||
Iterator[Any],
|
||||
Coroutine[Any, Any, Union[AnthropicMessagesResponse, AsyncIterator[Any]]],
|
||||
]:
|
||||
"""
|
||||
Async wrapper for Anthropic's messages API
|
||||
Sync wrapper for Anthropic's messages API
|
||||
|
||||
Args:
|
||||
max_tokens (int): Maximum tokens to generate (required)
|
||||
|
|
|
|||
|
|
@ -1,4 +1,4 @@
|
|||
from typing import Any, AsyncIterator, Coroutine, Dict, List, Optional, Union, cast
|
||||
from typing import Any, Iterator, AsyncIterator, Coroutine, Dict, List, Optional, Union, cast
|
||||
|
||||
import litellm
|
||||
from litellm.types.router import GenericLiteLLMParams
|
||||
|
|
@ -121,6 +121,7 @@ class GenerateContentToCompletionHandler:
|
|||
) -> Union[
|
||||
Dict[str, Any],
|
||||
AsyncIterator[bytes],
|
||||
Iterator[bytes],
|
||||
Coroutine[Any, Any, Union[Dict[str, Any], AsyncIterator[bytes]]],
|
||||
]:
|
||||
"""Handle generate_content call using completion adapter"""
|
||||
|
|
|
|||
|
|
@ -321,6 +321,16 @@ class GoogleGenAIAdapter:
|
|||
)
|
||||
# Return the SSE-wrapped version for proper event formatting
|
||||
return google_genai_wrapper.async_google_genai_sse_wrapper()
|
||||
|
||||
def sync_translate_completion_output_params_streaming(
|
||||
self,
|
||||
completion_stream: Any,
|
||||
) -> Union[Iterator[bytes], None]:
|
||||
"""Sync version of transform streaming completion output to Google GenAI format"""
|
||||
google_genai_wrapper = GoogleGenAIStreamWrapper(
|
||||
completion_stream=completion_stream
|
||||
)
|
||||
return google_genai_wrapper.google_genai_sse_wrapper()
|
||||
|
||||
def _transform_google_genai_tools_to_openai(
|
||||
self,
|
||||
|
|
|
|||
|
|
@ -2,6 +2,7 @@ from typing import (
|
|||
TYPE_CHECKING,
|
||||
Any,
|
||||
AsyncIterator,
|
||||
Iterator,
|
||||
Coroutine,
|
||||
Dict,
|
||||
List,
|
||||
|
|
@ -292,6 +293,7 @@ class LiteLLMMessagesToCompletionTransformationHandler:
|
|||
) -> Union[
|
||||
AnthropicMessagesResponse,
|
||||
AsyncIterator[Any],
|
||||
Iterator[Any],
|
||||
Coroutine[Any, Any, Union[AnthropicMessagesResponse, AsyncIterator[Any]]],
|
||||
]:
|
||||
"""Handle non-Anthropic models using the adapter."""
|
||||
|
|
@ -339,7 +341,7 @@ class LiteLLMMessagesToCompletionTransformationHandler:
|
|||
|
||||
if stream:
|
||||
transformed_stream = (
|
||||
ANTHROPIC_ADAPTER.translate_completion_output_params_streaming(
|
||||
ANTHROPIC_ADAPTER.sync_translate_completion_output_params_streaming(
|
||||
completion_response,
|
||||
model=model,
|
||||
tool_name_mapping=tool_name_mapping,
|
||||
|
|
|
|||
|
|
@ -5,6 +5,7 @@ from typing import (
|
|||
TYPE_CHECKING,
|
||||
Any,
|
||||
AsyncIterator,
|
||||
Iterator,
|
||||
Dict,
|
||||
List,
|
||||
Literal,
|
||||
|
|
@ -228,6 +229,23 @@ class AnthropicAdapter:
|
|||
)
|
||||
# Return the SSE-wrapped version for proper event formatting
|
||||
return anthropic_wrapper.async_anthropic_sse_wrapper()
|
||||
|
||||
def sync_translate_completion_output_params_streaming(
|
||||
self,
|
||||
completion_stream: Any,
|
||||
model: str,
|
||||
tool_name_mapping: Optional[Dict[str, str]] = None,
|
||||
) -> Union[Iterator[bytes], None]:
|
||||
"""
|
||||
Synchronous version of translate_completion_output_params_streaming.
|
||||
"""
|
||||
anthropic_wrapper = AnthropicStreamWrapper(
|
||||
completion_stream=completion_stream,
|
||||
model=model,
|
||||
tool_name_mapping=tool_name_mapping,
|
||||
)
|
||||
# Use the synchronous wrapper for synchronous iteration
|
||||
return anthropic_wrapper.anthropic_sse_wrapper()
|
||||
|
||||
|
||||
class LiteLLMAnthropicMessagesAdapter:
|
||||
|
|
|
|||
|
|
@ -8,7 +8,7 @@
|
|||
import asyncio
|
||||
import contextvars
|
||||
from functools import partial
|
||||
from typing import Any, AsyncIterator, Coroutine, Dict, List, Optional, Union, cast
|
||||
from typing import Any, Iterator, AsyncIterator, Coroutine, Dict, List, Optional, Union, cast
|
||||
|
||||
import litellm
|
||||
from litellm.litellm_core_utils.litellm_logging import Logging as LiteLLMLoggingObj
|
||||
|
|
@ -183,7 +183,7 @@ async def anthropic_messages(
|
|||
client: Optional[AsyncHTTPHandler] = None,
|
||||
custom_llm_provider: Optional[str] = None,
|
||||
**kwargs,
|
||||
) -> Union[AnthropicMessagesResponse, AsyncIterator]:
|
||||
) -> Union[AnthropicMessagesResponse, AsyncIterator, Iterator]:
|
||||
"""
|
||||
Async: Make llm api request in Anthropic /messages API spec
|
||||
"""
|
||||
|
|
@ -308,6 +308,7 @@ def anthropic_messages_handler(
|
|||
) -> Union[
|
||||
AnthropicMessagesResponse,
|
||||
AsyncIterator[Any],
|
||||
Iterator[Any],
|
||||
Coroutine[Any, Any, Union[AnthropicMessagesResponse, AsyncIterator[Any]]],
|
||||
]:
|
||||
"""
|
||||
|
|
|
|||
Loading…
Add table
Reference in a new issue