22
33from typing import TYPE_CHECKING , Any
44
5- from any_llm . any_llm import AnyLLM
5+ from . base import BaseAnthropicProvider
66
77MISSING_PACKAGES_ERROR = None
88try :
99 from anthropic import AsyncAnthropic
10-
11- from .utils import (
12- _convert_models_list ,
13- _convert_params ,
14- _convert_response ,
15- _create_openai_chunk_from_anthropic_chunk ,
16- )
1710except ImportError as e :
1811 MISSING_PACKAGES_ERROR = e
1912
2013if TYPE_CHECKING :
21- from collections .abc import AsyncIterator , Sequence
22-
23- from anthropic .types import Message
24- from anthropic .types .model_info import ModelInfo as AnthropicModelInfo
14+ from collections .abc import Sequence
2515
26- from any_llm .types .completion import ChatCompletion , ChatCompletionChunk , CompletionParams , CreateEmbeddingResponse
2716 from any_llm .types .model import Model
2817
2918
30- class AnthropicProvider (AnyLLM ):
19+ class AnthropicProvider (BaseAnthropicProvider ):
3120 """
3221 Anthropic Provider using enhanced Provider framework.
3322
@@ -38,15 +27,7 @@ class AnthropicProvider(AnyLLM):
3827 ENV_API_KEY_NAME = "ANTHROPIC_API_KEY"
3928 PROVIDER_DOCUMENTATION_URL = "https://docs.anthropic.com/en/home"
4029
41- SUPPORTS_COMPLETION_STREAMING = True
42- SUPPORTS_COMPLETION = True
43- SUPPORTS_RESPONSES = False
44- SUPPORTS_COMPLETION_REASONING = True
45- SUPPORTS_COMPLETION_IMAGE = True
46- SUPPORTS_COMPLETION_PDF = False
47- SUPPORTS_EMBEDDING = False
4830 SUPPORTS_LIST_MODELS = True
49- SUPPORTS_BATCH = False
5031
5132 MISSING_PACKAGES_ERROR = MISSING_PACKAGES_ERROR
5233
@@ -59,62 +40,6 @@ def _init_client(self, api_key: str | None = None, api_base: str | None = None,
5940 ** kwargs ,
6041 )
6142
62- @staticmethod
63- def _convert_completion_params (params : CompletionParams , ** kwargs : Any ) -> dict [str , Any ]:
64- """Convert CompletionParams to kwargs for Anthropic API."""
65- return _convert_params (params , ** kwargs )
66-
67- @staticmethod
68- def _convert_completion_response (response : Message ) -> ChatCompletion :
69- """Convert Anthropic Message to OpenAI ChatCompletion format."""
70- return _convert_response (response )
71-
72- @staticmethod
73- def _convert_completion_chunk_response (response : Any , ** kwargs : Any ) -> ChatCompletionChunk :
74- """Convert Anthropic streaming chunk to OpenAI ChatCompletionChunk format."""
75- model_id = kwargs .get ("model_id" , "unknown" )
76- return _create_openai_chunk_from_anthropic_chunk (response , model_id )
77-
78- @staticmethod
79- def _convert_embedding_params (params : Any , ** kwargs : Any ) -> dict [str , Any ]:
80- """Anthropic does not support embeddings."""
81- msg = "Anthropic does not support embeddings"
82- raise NotImplementedError (msg )
83-
84- @staticmethod
85- def _convert_embedding_response (response : Any ) -> CreateEmbeddingResponse :
86- """Anthropic does not support embeddings."""
87- msg = "Anthropic does not support embeddings"
88- raise NotImplementedError (msg )
89-
90- @staticmethod
91- def _convert_list_models_response (response : list [AnthropicModelInfo ]) -> Sequence [Model ]:
92- """Convert Anthropic models list to OpenAI format."""
93- return _convert_models_list (response )
94-
95- async def _stream_completion_async (self , ** kwargs : Any ) -> AsyncIterator [ChatCompletionChunk ]:
96- """Handle streaming completion - extracted to avoid generator issues."""
97- async with self .client .messages .stream (
98- ** kwargs ,
99- ) as anthropic_stream :
100- async for event in anthropic_stream :
101- yield self ._convert_completion_chunk_response (event , model_id = kwargs .get ("model" , "unknown" ))
102-
103- async def _acompletion (
104- self ,
105- params : CompletionParams ,
106- ** kwargs : Any ,
107- ) -> ChatCompletion | AsyncIterator [ChatCompletionChunk ]:
108- kwargs ["provider_name" ] = self .PROVIDER_NAME
109- converted_kwargs = self ._convert_completion_params (params , ** kwargs )
110-
111- if converted_kwargs .pop ("stream" , False ):
112- return self ._stream_completion_async (** converted_kwargs )
113-
114- message = await self .client .messages .create (** converted_kwargs )
115-
116- return self ._convert_completion_response (message )
117-
11843 async def _alist_models (self , ** kwargs : Any ) -> Sequence [Model ]:
11944 models_list = await self .client .models .list (** kwargs )
12045 return self ._convert_list_models_response (models_list .data )
0 commit comments