Skip to content

Commit

Permalink
functionnal anthropic message create wrapper
Browse files Browse the repository at this point in the history
  • Loading branch information
LucBERTON committed Mar 6, 2024
1 parent 89d9f1c commit 47c8cfd
Showing 1 changed file with 7 additions and 9 deletions.
16 changes: 7 additions & 9 deletions genai_impact/client/anthropic_wrapper.py
Original file line number Diff line number Diff line change
Expand Up @@ -6,12 +6,10 @@

try:
from anthropic import Anthropic as _Anthropic
# from mistralai.models.chat_completion import (
# ChatCompletionResponse as _ChatCompletionResponse,
# )
from anthropic.types import Message as _Message
except ImportError:
_Anthropic = object()
# _ChatCompletionResponse = object()
_Message = object()


#model names found here: https://docs.anthropic.com/claude/docs/models-overview#model-recommendations
Expand All @@ -22,24 +20,24 @@
}


class ChatCompletionResponse(_ChatCompletionResponse):
class Message(_Message):
impacts: Impacts


def chat_wrapper(
wrapped: Callable, instance: _Anthropic, args: Any, kwargs: Any # noqa: ARG001
) -> ChatCompletionResponse:
) -> Message:
response = wrapped(*args, **kwargs)
model_size = _MODEL_SIZES.get(response.model)
output_tokens = response.usage.completion_tokens
output_tokens = response.usage.output_tokens
impacts = compute_llm_impact(
model_parameter_count=model_size, output_token_count=output_tokens
)
return ChatCompletionResponse(**response.model_dump(), impacts=impacts)
return Message(**response.model_dump(), impacts=impacts)


class Anthropic(_Anthropic):
def __init__(self, **kwargs: Any) -> None:
super().__init__(**kwargs)

wrap_function_wrapper("anthropic.messages.create", chat_wrapper)
wrap_function_wrapper("anthropic.resources", "Messages.create", chat_wrapper)

0 comments on commit 47c8cfd

Please sign in to comment.