From d1929be20c3dd1553f44e562945e37734f697e20 Mon Sep 17 00:00:00 2001 From: Kevin Guevara Date: Tue, 7 Jan 2025 17:45:35 -0500 Subject: [PATCH] fix: directory clean to use constants --- scope3ai/constants.py | 11 +++++++++++ scope3ai/lib.py | 14 ++------------ scope3ai/tracers/anthropic/chat.py | 13 +++++++------ scope3ai/tracers/cohere/chat.py | 4 +++- scope3ai/tracers/cohere/chat_v2.py | 3 ++- scope3ai/tracers/huggingface/chat.py | 3 ++- scope3ai/tracers/huggingface/speech_to_text.py | 3 ++- scope3ai/tracers/huggingface/text_to_image.py | 3 ++- scope3ai/tracers/huggingface/text_to_speech.py | 3 ++- scope3ai/tracers/huggingface/translation.py | 3 ++- scope3ai/tracers/litellm/chat.py | 4 ++-- .../tracers/{mistrarlai_v1 => mistralai}/chat.py | 3 ++- .../{mistrarlai_v1 => mistralai}/instrument.py | 2 +- scope3ai/tracers/openai/chat.py | 4 ++-- scope3ai/tracers/openai/speech_to_text.py | 3 ++- scope3ai/tracers/openai/text_to_speech.py | 4 +++- 16 files changed, 47 insertions(+), 33 deletions(-) create mode 100644 scope3ai/constants.py rename scope3ai/tracers/{mistrarlai_v1 => mistralai}/chat.py (98%) rename scope3ai/tracers/{mistrarlai_v1 => mistralai}/instrument.py (96%) diff --git a/scope3ai/constants.py b/scope3ai/constants.py new file mode 100644 index 0000000..81d4bf8 --- /dev/null +++ b/scope3ai/constants.py @@ -0,0 +1,11 @@ +from enum import Enum + + +class PROVIDERS(Enum): + ANTROPIC = "anthropic" + COHERE = "cohere" + OPENAI = "openai" + HUGGINGFACE_HUB = "huggingface_hub" + LITELLM = "litellm" + MISTRALAI = "mistralai" + RESPONSE = "response" diff --git a/scope3ai/lib.py b/scope3ai/lib.py index a850f78..3204520 100644 --- a/scope3ai/lib.py +++ b/scope3ai/lib.py @@ -3,7 +3,6 @@ import logging from contextlib import contextmanager from contextvars import ContextVar -from enum import Enum from functools import partial from os import getenv from typing import Optional, List @@ -14,6 +13,7 @@ from .api.tracer import Tracer from .api.types import ImpactRow, ImpactResponse, Scope3AIContext from .api.defaults import DEFAULT_API_URL +from .constants import PROVIDERS from .worker import BackgroundWorker @@ -62,7 +62,7 @@ def init_litellm_instrumentor() -> None: def init_mistral_v1_instrumentor() -> None: if importlib.util.find_spec("mistralai") is not None: - from scope3ai.tracers.mistrarlai_v1.instrument import MistralAIInstrumentor + from scope3ai.tracers.mistralai.instrument import MistralAIInstrumentor instrumentor = MistralAIInstrumentor() instrumentor.instrument() @@ -75,16 +75,6 @@ def init_response_instrumentor() -> None: instrumentor.instrument() -class PROVIDERS(Enum): - ANTROPIC = "anthropic" - COHERE = "cohere" - OPENAI = "openai" - HUGGINGFACE_HUB = "huggingface_hub" - LITELLM = "litellm" - MISTRALAI = "mistralai" - RESPONSE = "response" - - _INSTRUMENTS = { PROVIDERS.ANTROPIC.value: init_anthropic_instrumentor, PROVIDERS.COHERE.value: init_cohere_instrumentor, diff --git a/scope3ai/tracers/anthropic/chat.py b/scope3ai/tracers/anthropic/chat.py index 0b916c2..1fa228a 100644 --- a/scope3ai/tracers/anthropic/chat.py +++ b/scope3ai/tracers/anthropic/chat.py @@ -2,25 +2,26 @@ from collections.abc import AsyncIterator, Awaitable, Iterator from types import TracebackType from typing import Any, Callable, Generic, Optional, TypeVar, Union -from typing_extensions import override from anthropic import Anthropic, AsyncAnthropic from anthropic import Stream as _Stream, AsyncStream as _AsyncStream +from anthropic._streaming import _T from anthropic.lib.streaming import AsyncMessageStream as _AsyncMessageStream from anthropic.lib.streaming import MessageStream as _MessageStream from anthropic.types import Message as _Message from anthropic.types.message_delta_event import MessageDeltaEvent from anthropic.types.message_start_event import MessageStartEvent from anthropic.types.message_stop_event import MessageStopEvent -from anthropic.types.raw_message_stream_event import RawMessageStreamEvent -from anthropic.types.raw_message_start_event import RawMessageStartEvent from anthropic.types.raw_message_delta_event import RawMessageDeltaEvent -from anthropic._streaming import _T +from anthropic.types.raw_message_start_event import RawMessageStartEvent +from anthropic.types.raw_message_stream_event import RawMessageStreamEvent +from typing_extensions import override -from scope3ai.lib import Scope3AI from scope3ai.api.types import Scope3AIContext, Model, ImpactRow +from scope3ai.constants import PROVIDERS +from scope3ai.lib import Scope3AI -PROVIDER = "anthropic" +PROVIDER = PROVIDERS.ANTROPIC.value MessageStreamT = TypeVar("MessageStreamT", bound=_MessageStream) AsyncMessageStreamT = TypeVar("AsyncMessageStreamT", bound=_AsyncMessageStream) diff --git a/scope3ai/tracers/cohere/chat.py b/scope3ai/tracers/cohere/chat.py index e0cd51a..5d0bb1d 100644 --- a/scope3ai/tracers/cohere/chat.py +++ b/scope3ai/tracers/cohere/chat.py @@ -10,10 +10,12 @@ from cohere.types.streamed_chat_response import ( StreamEndStreamedChatResponse as _StreamEndStreamedChatResponse, ) + +from scope3ai.constants import PROVIDERS from scope3ai.lib import Scope3AI from scope3ai.api.types import Scope3AIContext, Model, ImpactRow -PROVIDER = "cohere" +PROVIDER = PROVIDERS.COHERE.value class NonStreamedChatResponse(_NonStreamedChatResponse): diff --git a/scope3ai/tracers/cohere/chat_v2.py b/scope3ai/tracers/cohere/chat_v2.py index 5f1f40f..9c2afb5 100644 --- a/scope3ai/tracers/cohere/chat_v2.py +++ b/scope3ai/tracers/cohere/chat_v2.py @@ -11,9 +11,10 @@ ) from scope3ai.api.types import ImpactRow, Model, Scope3AIContext +from scope3ai.constants import PROVIDERS from scope3ai.lib import Scope3AI -PROVIDER = "cohere" +PROVIDER = PROVIDERS.COHERE.value class ChatResponse(_ChatResponse): diff --git a/scope3ai/tracers/huggingface/chat.py b/scope3ai/tracers/huggingface/chat.py index c808495..3e04f51 100644 --- a/scope3ai/tracers/huggingface/chat.py +++ b/scope3ai/tracers/huggingface/chat.py @@ -10,10 +10,11 @@ from requests import Response from scope3ai.api.types import Scope3AIContext, Model, ImpactRow +from scope3ai.constants import PROVIDERS from scope3ai.lib import Scope3AI from scope3ai.response_interceptor.requests_interceptor import requests_response_capture -PROVIDER = "huggingface_hub" +PROVIDER = PROVIDERS.HUGGINGFACE_HUB.value @dataclass diff --git a/scope3ai/tracers/huggingface/speech_to_text.py b/scope3ai/tracers/huggingface/speech_to_text.py index 8d73966..3da386a 100644 --- a/scope3ai/tracers/huggingface/speech_to_text.py +++ b/scope3ai/tracers/huggingface/speech_to_text.py @@ -9,10 +9,11 @@ from scope3ai.api.types import Scope3AIContext, Model, ImpactRow from scope3ai.api.typesgen import Task +from scope3ai.constants import PROVIDERS from scope3ai.lib import Scope3AI from scope3ai.response_interceptor.requests_interceptor import requests_response_capture -PROVIDER = "huggingface_hub" +PROVIDER = PROVIDERS.HUGGINGFACE_HUB.value @dataclass diff --git a/scope3ai/tracers/huggingface/text_to_image.py b/scope3ai/tracers/huggingface/text_to_image.py index b574a78..ed1b877 100644 --- a/scope3ai/tracers/huggingface/text_to_image.py +++ b/scope3ai/tracers/huggingface/text_to_image.py @@ -9,11 +9,12 @@ from scope3ai.api.types import Scope3AIContext, Model, ImpactRow from scope3ai.api.typesgen import Task +from scope3ai.constants import PROVIDERS from scope3ai.lib import Scope3AI from scope3ai.response_interceptor.aiohttp_interceptor import aiohttp_response_capture from scope3ai.response_interceptor.requests_interceptor import requests_response_capture -PROVIDER = "huggingface_hub" +PROVIDER = PROVIDERS.HUGGINGFACE_HUB.value @dataclass diff --git a/scope3ai/tracers/huggingface/text_to_speech.py b/scope3ai/tracers/huggingface/text_to_speech.py index 89f28b8..0f31258 100644 --- a/scope3ai/tracers/huggingface/text_to_speech.py +++ b/scope3ai/tracers/huggingface/text_to_speech.py @@ -9,9 +9,10 @@ from scope3ai.api.types import Scope3AIContext, Model, ImpactRow from scope3ai.api.typesgen import Task +from scope3ai.constants import PROVIDERS from scope3ai.lib import Scope3AI -PROVIDER = "huggingface_hub" +PROVIDER = PROVIDERS.HUGGINGFACE_HUB.value @dataclass diff --git a/scope3ai/tracers/huggingface/translation.py b/scope3ai/tracers/huggingface/translation.py index aea2398..0fc13f1 100644 --- a/scope3ai/tracers/huggingface/translation.py +++ b/scope3ai/tracers/huggingface/translation.py @@ -9,11 +9,12 @@ from scope3ai.api.types import Scope3AIContext, Model, ImpactRow from scope3ai.api.typesgen import Task +from scope3ai.constants import PROVIDERS from scope3ai.lib import Scope3AI from scope3ai.response_interceptor.aiohttp_interceptor import aiohttp_response_capture from scope3ai.response_interceptor.requests_interceptor import requests_response_capture -PROVIDER = "huggingface_hub" +PROVIDER = PROVIDERS.HUGGINGFACE_HUB.value @dataclass diff --git a/scope3ai/tracers/litellm/chat.py b/scope3ai/tracers/litellm/chat.py index c749a98..d93de9f 100644 --- a/scope3ai/tracers/litellm/chat.py +++ b/scope3ai/tracers/litellm/chat.py @@ -7,9 +7,9 @@ from scope3ai import Scope3AI from scope3ai.api.types import Scope3AIContext, Model, ImpactRow +from scope3ai.constants import PROVIDERS - -PROVIDER = "litellm" +PROVIDER = PROVIDERS.LITELLM.value class ChatCompletion(ModelResponse): diff --git a/scope3ai/tracers/mistrarlai_v1/chat.py b/scope3ai/tracers/mistralai/chat.py similarity index 98% rename from scope3ai/tracers/mistrarlai_v1/chat.py rename to scope3ai/tracers/mistralai/chat.py index cd21a43..05b7fc4 100644 --- a/scope3ai/tracers/mistrarlai_v1/chat.py +++ b/scope3ai/tracers/mistralai/chat.py @@ -10,8 +10,9 @@ from scope3ai import Scope3AI from scope3ai.api.types import Scope3AIContext from scope3ai.api.typesgen import ImpactRow, Model +from scope3ai.constants import PROVIDERS -PROVIDER = "mistralai" +PROVIDER = PROVIDERS.MISTRALAI.value class ChatCompletionResponse(_ChatCompletionResponse): diff --git a/scope3ai/tracers/mistrarlai_v1/instrument.py b/scope3ai/tracers/mistralai/instrument.py similarity index 96% rename from scope3ai/tracers/mistrarlai_v1/instrument.py rename to scope3ai/tracers/mistralai/instrument.py index 628626a..b6f580b 100644 --- a/scope3ai/tracers/mistrarlai_v1/instrument.py +++ b/scope3ai/tracers/mistralai/instrument.py @@ -1,6 +1,6 @@ from wrapt import wrap_function_wrapper # type: ignore[import-untyped] -from scope3ai.tracers.mistrarlai_v1.chat import ( +from scope3ai.tracers.mistralai.chat import ( mistralai_v1_chat_wrapper, mistralai_v1_async_chat_wrapper, mistralai_v1_chat_wrapper_stream, diff --git a/scope3ai/tracers/openai/chat.py b/scope3ai/tracers/openai/chat.py index 29ed8ba..018db12 100644 --- a/scope3ai/tracers/openai/chat.py +++ b/scope3ai/tracers/openai/chat.py @@ -1,7 +1,7 @@ import time from typing import Any, Callable, Optional, Union - +from scope3ai.constants import PROVIDERS from scope3ai.lib import Scope3AI from scope3ai.api.types import Scope3AIContext, Model, ImpactRow @@ -19,7 +19,7 @@ _ChatCompletionChunk = object() -PROVIDER = "openai" +PROVIDER = PROVIDERS.OPENAI.value class ChatCompletion(_ChatCompletion): diff --git a/scope3ai/tracers/openai/speech_to_text.py b/scope3ai/tracers/openai/speech_to_text.py index 278a612..e10e662 100644 --- a/scope3ai/tracers/openai/speech_to_text.py +++ b/scope3ai/tracers/openai/speech_to_text.py @@ -16,9 +16,10 @@ ) from scope3ai.api.types import ImpactRow, Model, Scope3AIContext, Task +from scope3ai.constants import PROVIDERS from scope3ai.lib import Scope3AI -PROVIDER = "openai" +PROVIDER = PROVIDERS.OPENAI.value logger = logging.getLogger("scope3.tracers.openai.speech_to_text") diff --git a/scope3ai/tracers/openai/text_to_speech.py b/scope3ai/tracers/openai/text_to_speech.py index 60117a4..39490fc 100644 --- a/scope3ai/tracers/openai/text_to_speech.py +++ b/scope3ai/tracers/openai/text_to_speech.py @@ -8,6 +8,7 @@ from openai.resources.audio.speech import AsyncSpeech, Speech, _legacy_response from scope3ai.api.types import ImpactRow, Model, Scope3AIContext, Task +from scope3ai.constants import PROVIDERS from scope3ai.lib import Scope3AI @@ -19,7 +20,8 @@ def _imported(): return _imported -PROVIDER = "openai" +PROVIDER = PROVIDERS.OPENAI.value + MUTAGEN_MAPPING = { "mp3": _lazy_import("mutagen.mp3", "MP3"), "aac": _lazy_import("mutagen.aac", "AAC"),