Skip to content

Commit

Permalink
chore: formatting
Browse files Browse the repository at this point in the history
  • Loading branch information
samuelrince committed Mar 24, 2024
1 parent facea59 commit b632c19
Show file tree
Hide file tree
Showing 5 changed files with 10 additions and 4 deletions.
3 changes: 2 additions & 1 deletion genai_impact/exceptions.py
Original file line number Diff line number Diff line change
@@ -1,6 +1,7 @@
class GenAIImpactError(Exception):
pass


class TracerInitializationError(GenAIImpactError):
"Tracer is initialized twice"
"""Tracer is initialized twice"""
pass
3 changes: 3 additions & 0 deletions genai_impact/tracer.py
Original file line number Diff line number Diff line change
Expand Up @@ -19,20 +19,23 @@ def init_instruments() -> None:
init_anthropic_instrumentor()
init_mistralai_instrumentor()


def init_openai_instrumentor() -> None:
if importlib.util.find_spec("openai") is not None:
from genai_impact.tracers.openai_tracer import OpenAIInstrumentor

instrumentor = OpenAIInstrumentor()
instrumentor.instrument()


def init_anthropic_instrumentor() -> None:
if importlib.util.find_spec("anthropic") is not None:
from genai_impact.tracers.anthropic_tracer import AnthropicInstrumentor

instrumentor = AnthropicInstrumentor()
instrumentor.instrument()


def init_mistralai_instrumentor() -> None:
if importlib.util.find_spec("mistralai") is not None:
from genai_impact.tracers.mistralai_tracer import MistralAIInstrumentor
Expand Down
2 changes: 1 addition & 1 deletion genai_impact/tracers/mistralai_tracer.py
Original file line number Diff line number Diff line change
Expand Up @@ -44,7 +44,7 @@ def mistralai_chat_wrapper(

async def mistralai_async_chat_wrapper(
wrapped: Callable,
instance: _MistralAsyncClient, # noqa: ARG001
instance: _MistralAsyncClient, # noqa: ARG001
args: Any,
kwargs: Any,
) -> ChatCompletionResponse:
Expand Down
3 changes: 2 additions & 1 deletion genai_impact/tracers/openai_tracer.py
Original file line number Diff line number Diff line change
Expand Up @@ -25,12 +25,14 @@ def compute_impacts_and_return_response(response: Any) -> ChatCompletion:
)
return ChatCompletion(**response.model_dump(), impacts=impacts)


def openai_chat_wrapper(
wrapped: Callable, instance: Completions, args: Any, kwargs: Any # noqa: ARG001
) -> ChatCompletion:
response = wrapped(*args, **kwargs)
return compute_impacts_and_return_response(response)


async def openai_async_chat_wrapper(
wrapped: Callable,
instance: AsyncCompletions, # noqa: ARG001
Expand All @@ -41,7 +43,6 @@ async def openai_async_chat_wrapper(
return compute_impacts_and_return_response(response)



class OpenAIInstrumentor:
def __init__(self) -> None:
self.wrapped_methods = [
Expand Down
3 changes: 2 additions & 1 deletion tests/test_tracer.py
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,7 @@
from genai_impact import Tracer
from genai_impact.exceptions import GenAIImpactError


def test_double_init(tracer_init):
with pytest.raises(GenAIImpactError) as e:
Tracer.init() # Second initialization
Tracer.init() # Second initialization

0 comments on commit b632c19

Please sign in to comment.