-
Notifications
You must be signed in to change notification settings - Fork 1k
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Also, code to normalize response to OpenAI format.
- Loading branch information
1 parent
e940359
commit 9c2680e
Showing
2 changed files
with
81 additions
and
4 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,39 @@ | ||
import anthropic | ||
from provider import Provider | ||
|
||
class AnthropicProvider(Provider): | ||
def __init__(self, **config): | ||
""" | ||
Initialize the Anthropic provider with the given configuration. | ||
Pass the entire configuration dictionary to the Anthropic client constructor. | ||
""" | ||
|
||
self.client = anthropic.Anthropic(**config) | ||
|
||
def chat_completions_create(self, model, messages, **kwargs): | ||
# Check if the fist message is a system message | ||
if messages[0]["role"] == "system": | ||
system_message = messages[0]["content"] | ||
messages = messages[1:] | ||
else: | ||
system_message = None | ||
|
||
return self.normalize_response(self.client.messages.create( | ||
model=model, | ||
system=system_message, | ||
messages=messages, | ||
**kwargs | ||
)) | ||
|
||
def normalize_response(self, response): | ||
""" Normalize the response from the Anthropic API to match OpenAI's response format. """ | ||
return { | ||
"choices": [ | ||
{ | ||
"message": { | ||
"role": response.get("role", "assistant"), | ||
"content": response.get("content", ""), | ||
} | ||
} | ||
] | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters