|
| 1 | +from typing import Any, Dict, Optional |
| 2 | + |
| 3 | +from litellm import AdapterCompletionStreamWrapper, ChatCompletionRequest, ModelResponse |
| 4 | +from litellm.adapters.anthropic_adapter import ( |
| 5 | + AnthropicAdapter as LitellmAnthropicAdapter, |
| 6 | +) |
| 7 | +from litellm.types.llms.anthropic import AnthropicResponse |
| 8 | + |
| 9 | +from ..base import StreamGenerator |
| 10 | +from ..litellmshim import anthropic_stream_generator |
| 11 | +from ..litellmshim.litellmshim import BaseAdapter |
| 12 | + |
| 13 | + |
| 14 | +class AnthropicAdapter(BaseAdapter): |
| 15 | + """ |
| 16 | + LiteLLM's adapter class interface is used to translate between the Anthropic data |
| 17 | + format and the underlying model. The AnthropicAdapter class contains the actual |
| 18 | + implementation of the interface methods, we just forward the calls to it. |
| 19 | + """ |
| 20 | + def __init__(self, stream_generator: StreamGenerator = anthropic_stream_generator): |
| 21 | + self.litellm_anthropic_adapter = LitellmAnthropicAdapter() |
| 22 | + super().__init__(stream_generator) |
| 23 | + |
| 24 | + def translate_completion_input_params( |
| 25 | + self, |
| 26 | + completion_request: Dict, |
| 27 | + ) -> Optional[ChatCompletionRequest]: |
| 28 | + return self.litellm_anthropic_adapter.translate_completion_input_params( |
| 29 | + completion_request |
| 30 | + ) |
| 31 | + |
| 32 | + def translate_completion_output_params( |
| 33 | + self, response: ModelResponse |
| 34 | + ) -> Optional[AnthropicResponse]: |
| 35 | + return self.litellm_anthropic_adapter.translate_completion_output_params( |
| 36 | + response |
| 37 | + ) |
| 38 | + |
| 39 | + def translate_completion_output_params_streaming( |
| 40 | + self, completion_stream: Any |
| 41 | + ) -> AdapterCompletionStreamWrapper | None: |
| 42 | + return ( |
| 43 | + self.litellm_anthropic_adapter.translate_completion_output_params_streaming( |
| 44 | + completion_stream |
| 45 | + ) |
| 46 | + ) |
0 commit comments