Skip to content
This repository was archived by the owner on Jun 5, 2025. It is now read-only.

Commit a81aa0f

Browse files
authored
Merge pull request #242 from stacklok/issue-187
fix: override translate method to return ChatCompletionRequest
2 parents d666bb6 + 01f9799 commit a81aa0f

File tree

1 file changed

+21
-1
lines changed

1 file changed

+21
-1
lines changed

src/codegate/providers/anthropic/adapter.py

Lines changed: 21 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,3 +1,4 @@
1+
from typing import Optional
12
from litellm.adapters.anthropic_adapter import (
23
AnthropicAdapter as LitellmAnthropicAdapter,
34
)
@@ -6,7 +7,25 @@
67
LiteLLMAdapterInputNormalizer,
78
LiteLLMAdapterOutputNormalizer,
89
)
10+
import litellm
11+
from litellm import ChatCompletionRequest
12+
from litellm.types.llms.anthropic import (
13+
AnthropicMessagesRequest,
14+
)
15+
916

17+
class AnthropicAdapter(LitellmAnthropicAdapter):
18+
def __init__(self) -> None:
19+
super().__init__()
20+
21+
def translate_completion_input_params(self, kwargs) -> Optional[ChatCompletionRequest]:
22+
request_body = AnthropicMessagesRequest(**kwargs) # type: ignore
23+
if not request_body.get("system"):
24+
request_body["system"] = "System prompt"
25+
translated_body = litellm.AnthropicExperimentalPassThroughConfig()\
26+
.translate_anthropic_to_openai(anthropic_message_request=request_body)
27+
return translated_body
28+
1029

1130
class AnthropicInputNormalizer(LiteLLMAdapterInputNormalizer):
1231
"""
@@ -16,7 +35,8 @@ class AnthropicInputNormalizer(LiteLLMAdapterInputNormalizer):
1635
"""
1736

1837
def __init__(self):
19-
super().__init__(LitellmAnthropicAdapter())
38+
self.adapter = AnthropicAdapter()
39+
super().__init__(self.adapter)
2040

2141

2242
class AnthropicOutputNormalizer(LiteLLMAdapterOutputNormalizer):

0 commit comments

Comments
 (0)