Skip to content

Commit

Permalink
fix: get attr utils to solve no attribute error (#144)
Browse files Browse the repository at this point in the history
* get attr utils

* get attr in openai audio patchee

* get attr in all openai patchees
  • Loading branch information
yuanbohan authored May 7, 2024
1 parent fbd12e4 commit 47bebb8
Show file tree
Hide file tree
Showing 12 changed files with 126 additions and 71 deletions.
49 changes: 25 additions & 24 deletions src/greptimeai/patchee/openai_patchee/audio.py
Original file line number Diff line number Diff line change
Expand Up @@ -4,6 +4,7 @@
from openai import AsyncOpenAI, OpenAI

from greptimeai.patchee import Patchee
from greptimeai.utils.attr import get_attr, get_optional_attr

from . import (
_SPAN_NAME_SPEECH,
Expand All @@ -16,25 +17,25 @@
class _SpeechPatchees:
def __init__(self, client: Union[OpenAI, AsyncOpenAI, None] = None):
audio_speech_create = Patchee(
obj=client.audio.speech if client else openai.audio.speech,
obj=get_optional_attr([client, openai], ["audio", "speech"]),
method_name="create",
span_name=_SPAN_NAME_SPEECH,
event_name="audio.speech.create",
)

audio_raw_speech_create = Patchee(
obj=client.audio.with_raw_response.speech
if client
else openai.audio.with_raw_response.speech,
obj=get_optional_attr(
[client, openai], ["audio", "with_raw_response", "speech"]
),
method_name="create",
span_name=_SPAN_NAME_SPEECH,
event_name="audio.with_raw_response.speech.create",
)

audio_speech_raw_create = Patchee(
obj=client.audio.speech.with_raw_response
if client
else openai.audio.speech.with_raw_response,
obj=get_optional_attr(
[client, openai], ["audio", "speech", "with_raw_response"]
),
method_name="create",
span_name=_SPAN_NAME_SPEECH,
event_name="audio.speech.with_raw_response.create",
Expand All @@ -48,7 +49,7 @@ def __init__(self, client: Union[OpenAI, AsyncOpenAI, None] = None):

if client:
raw_audio_speech_create = Patchee(
obj=client.with_raw_response.audio.speech,
obj=get_attr(client, ["with_raw_response", "audio", "speech"]),
method_name="create",
span_name=_SPAN_NAME_SPEECH,
event_name="with_raw_response.audio.speech.create",
Expand All @@ -59,25 +60,25 @@ def __init__(self, client: Union[OpenAI, AsyncOpenAI, None] = None):
class _TranscriptionPatchees:
def __init__(self, client: Union[OpenAI, AsyncOpenAI, None] = None):
audio_transcriptions_create = Patchee(
obj=client.audio.transcriptions if client else openai.audio.transcriptions,
obj=get_optional_attr([client, openai], ["audio", "transcriptions"]),
method_name="create",
span_name=_SPAN_NAME_TRANSCRIPTION,
event_name="audio.transcriptions.create",
)

audio_raw_transcriptions_create = Patchee(
obj=client.audio.with_raw_response.transcriptions
if client
else openai.audio.with_raw_response.transcriptions,
obj=get_optional_attr(
[client, openai], ["audio", "with_raw_response", "transcriptions"]
),
method_name="create",
span_name=_SPAN_NAME_TRANSCRIPTION,
event_name="audio.with_raw_response.transcriptions.create",
)

audio_transcriptions_raw_create = Patchee(
obj=client.audio.transcriptions.with_raw_response
if client
else openai.audio.transcriptions.with_raw_response,
obj=get_optional_attr(
[client, openai], ["audio", "transcriptions", "with_raw_response"]
),
method_name="create",
span_name=_SPAN_NAME_TRANSCRIPTION,
event_name="audio.transcriptions.with_raw_response.create",
Expand All @@ -91,7 +92,7 @@ def __init__(self, client: Union[OpenAI, AsyncOpenAI, None] = None):

if client:
raw_audio_transcriptions_create = Patchee(
obj=client.with_raw_response.audio.transcriptions,
obj=get_attr(client, ["with_raw_response", "audio", "transcriptions"]),
method_name="create",
span_name=_SPAN_NAME_TRANSCRIPTION,
event_name="with_raw_response.audio.transcriptions.create",
Expand All @@ -102,25 +103,25 @@ def __init__(self, client: Union[OpenAI, AsyncOpenAI, None] = None):
class _TranslationPatchees:
def __init__(self, client: Union[OpenAI, AsyncOpenAI, None] = None):
audio_translations_create = Patchee(
obj=client.audio.translations if client else openai.audio.translations,
obj=get_optional_attr([client, openai], ["audio", "translations"]),
method_name="create",
span_name=_SPAN_NAME_TRANSLATION,
event_name="audio.translations.create",
)

audio_raw_translations_create = Patchee(
obj=client.audio.with_raw_response.translations
if client
else openai.audio.with_raw_response.translations,
obj=get_optional_attr(
[client, openai], ["audio", "with_raw_response", "translations"]
),
method_name="create",
span_name=_SPAN_NAME_TRANSLATION,
event_name="audio.with_raw_response.translations.create",
)

audio_translations_raw_create = Patchee(
obj=client.audio.translations.with_raw_response
if client
else openai.audio.translations.with_raw_response,
obj=get_optional_attr(
[client, openai], ["audio", "translations", "with_raw_response"]
),
method_name="create",
span_name=_SPAN_NAME_TRANSLATION,
event_name="audio.translations.with_raw_response.create",
Expand All @@ -134,7 +135,7 @@ def __init__(self, client: Union[OpenAI, AsyncOpenAI, None] = None):

if client:
raw_audio_translations_create = Patchee(
obj=client.with_raw_response.audio.translations,
obj=get_attr(client, ["with_raw_response", "audio", "translations"]),
method_name="create",
span_name=_SPAN_NAME_TRANSLATION,
event_name="with_raw_response.audio.translations.create",
Expand Down
17 changes: 9 additions & 8 deletions src/greptimeai/patchee/openai_patchee/chat_completion.py
Original file line number Diff line number Diff line change
Expand Up @@ -4,32 +4,33 @@
from openai import AsyncOpenAI, OpenAI

from greptimeai.patchee import Patchee
from greptimeai.utils.attr import get_attr, get_optional_attr

from . import _SPAN_NAME_COMPLETION, OpenaiPatchees


class ChatCompletionPatchees(OpenaiPatchees):
def __init__(self, client: Union[OpenAI, AsyncOpenAI, None] = None):
chat_completion_create = Patchee(
obj=client.chat.completions if client else openai.chat.completions,
obj=get_optional_attr([client, openai], ["chat", "completions"]),
method_name="create",
span_name=_SPAN_NAME_COMPLETION,
event_name="chat.completions.create",
)

chat_raw_completion_create = Patchee(
obj=client.chat.with_raw_response.completions
if client
else openai.chat.with_raw_response.completions,
obj=get_optional_attr(
[client, openai], ["chat", "with_raw_response", "completions"]
),
method_name="create",
span_name=_SPAN_NAME_COMPLETION,
event_name="chat.with_raw_response.completions.create",
)

chat_completion_raw_create = Patchee(
obj=client.chat.completions.with_raw_response
if client
else openai.chat.completions.with_raw_response,
obj=get_optional_attr(
[client, openai], ["chat", "completions", "with_raw_response"]
),
method_name="create",
span_name=_SPAN_NAME_COMPLETION,
event_name="chat.completions.with_raw_response.create",
Expand All @@ -43,7 +44,7 @@ def __init__(self, client: Union[OpenAI, AsyncOpenAI, None] = None):

if client:
raw_chat_completion_create = Patchee(
obj=client.with_raw_response.chat.completions,
obj=get_attr(client, ["with_raw_response", "chat", "completions"]),
method_name="create",
span_name=_SPAN_NAME_COMPLETION,
event_name="with_raw_response.chat.completions.create",
Expand Down
11 changes: 6 additions & 5 deletions src/greptimeai/patchee/openai_patchee/completion.py
Original file line number Diff line number Diff line change
Expand Up @@ -4,23 +4,24 @@
from openai import AsyncOpenAI, OpenAI

from greptimeai.patchee import Patchee
from greptimeai.utils.attr import get_attr, get_optional_attr

from . import _SPAN_NAME_COMPLETION, OpenaiPatchees


class CompletionPatchees(OpenaiPatchees):
def __init__(self, client: Union[OpenAI, AsyncOpenAI, None] = None):
completion_create = Patchee(
obj=client.completions if client else openai.completions,
obj=get_optional_attr([client, openai], ["completions"]),
method_name="create",
span_name=_SPAN_NAME_COMPLETION,
event_name="completions.create",
)

completion_raw_create = Patchee(
obj=client.completions.with_raw_response
if client
else openai.completions.with_raw_response,
obj=get_optional_attr(
[client, openai], ["completions", "with_raw_response"]
),
method_name="create",
span_name=_SPAN_NAME_COMPLETION,
event_name="completions.with_raw_response.create",
Expand All @@ -33,7 +34,7 @@ def __init__(self, client: Union[OpenAI, AsyncOpenAI, None] = None):

if client:
raw_completion_create = Patchee(
obj=client.with_raw_response.completions,
obj=get_attr(client, ["with_raw_response", "completions"]),
method_name="create",
span_name=_SPAN_NAME_COMPLETION,
event_name="with_raw_response.completions.create",
Expand Down
11 changes: 6 additions & 5 deletions src/greptimeai/patchee/openai_patchee/embedding.py
Original file line number Diff line number Diff line change
Expand Up @@ -4,23 +4,24 @@
from openai import AsyncOpenAI, OpenAI

from greptimeai.patchee import Patchee
from greptimeai.utils.attr import get_attr, get_optional_attr

from . import _SPAN_NAME_EMBEDDING, OpenaiPatchees


class EmbeddingPatchees(OpenaiPatchees):
def __init__(self, client: Union[OpenAI, AsyncOpenAI, None] = None):
embeddings_create = Patchee(
obj=client.embeddings if client else openai.embeddings,
obj=get_optional_attr([client, openai], ["embeddings"]),
method_name="create",
span_name=_SPAN_NAME_EMBEDDING,
event_name="embeddings.create",
)

embeddings_raw_create = Patchee(
obj=client.embeddings.with_raw_response
if client
else openai.embeddings.with_raw_response,
obj=get_optional_attr(
[client, openai], ["embeddings", "with_raw_response"]
),
method_name="create",
span_name=_SPAN_NAME_EMBEDDING,
event_name="embeddings.with_raw_response.create",
Expand All @@ -33,7 +34,7 @@ def __init__(self, client: Union[OpenAI, AsyncOpenAI, None] = None):

if client:
raw_embeddings_create = Patchee(
obj=client.with_raw_response.embeddings,
obj=get_attr(client, ["with_raw_response", "embeddings"]),
method_name="create",
span_name=_SPAN_NAME_EMBEDDING,
event_name="with_raw_response.embeddings.create",
Expand Down
9 changes: 4 additions & 5 deletions src/greptimeai/patchee/openai_patchee/file.py
Original file line number Diff line number Diff line change
Expand Up @@ -4,23 +4,22 @@
from openai import AsyncOpenAI, OpenAI

from greptimeai.patchee import Patchee
from greptimeai.utils.attr import get_attr, get_optional_attr

from . import _SPAN_NAME_FILE, OpenaiPatchees


class _FilePatchees:
def __init__(self, client: Union[OpenAI, AsyncOpenAI, None], method_name: str):
files = Patchee(
obj=client.files if client else openai.files,
obj=get_optional_attr([client, openai], ["files"]),
method_name=method_name,
span_name=_SPAN_NAME_FILE,
event_name=f"files.{method_name}",
)

files_raw = Patchee(
obj=client.files.with_raw_response
if client
else openai.files.with_raw_response,
obj=get_optional_attr([client, openai], ["files", "with_raw_response"]),
method_name=method_name,
span_name=_SPAN_NAME_FILE,
event_name=f"files.with_raw_response.{method_name}",
Expand All @@ -30,7 +29,7 @@ def __init__(self, client: Union[OpenAI, AsyncOpenAI, None], method_name: str):

if client:
raw_files = Patchee(
obj=client.with_raw_response.files,
obj=get_attr(client, ["with_raw_response", "files"]),
method_name=method_name,
span_name=_SPAN_NAME_FILE,
event_name=f"with_raw_response.files.{method_name}",
Expand Down
17 changes: 9 additions & 8 deletions src/greptimeai/patchee/openai_patchee/fine_tuning.py
Original file line number Diff line number Diff line change
Expand Up @@ -4,32 +4,33 @@
from openai import AsyncOpenAI, OpenAI

from greptimeai.patchee import Patchee
from greptimeai.utils.attr import get_attr, get_optional_attr

from . import _SPAN_NAME_FINE_TUNNING, OpenaiPatchees


class _FineTuningPatchees:
def __init__(self, client: Union[OpenAI, AsyncOpenAI, None], method_name: str):
fine_tuning_jobs = Patchee(
obj=client.fine_tuning.jobs if client else openai.fine_tuning.jobs,
obj=get_optional_attr([client, openai], ["fine_tuning", "jobs"]),
method_name=method_name,
span_name=_SPAN_NAME_FINE_TUNNING,
event_name=f"fine_tuning.jobs.{method_name}",
)

fine_tuning_raw_jobs = Patchee(
obj=client.fine_tuning.with_raw_response.jobs
if client
else openai.fine_tuning.with_raw_response.jobs,
obj=get_optional_attr(
[client, openai], ["fine_tuning", "with_raw_response", "jobs"]
),
method_name=method_name,
span_name=_SPAN_NAME_FINE_TUNNING,
event_name=f"fine_tuning.with_raw_response.jobs.{method_name}",
)

fine_tuning_jobs_raw = Patchee(
obj=client.fine_tuning.jobs.with_raw_response
if client
else openai.fine_tuning.jobs.with_raw_response,
obj=get_optional_attr(
[client, openai], ["fine_tuning", "jobs", "with_raw_response"]
),
method_name=method_name,
span_name=_SPAN_NAME_FINE_TUNNING,
event_name=f"fine_tuning.jobs.with_raw_response.{method_name}",
Expand All @@ -43,7 +44,7 @@ def __init__(self, client: Union[OpenAI, AsyncOpenAI, None], method_name: str):

if client:
raw_fine_tuning = Patchee(
obj=client.with_raw_response.fine_tuning.jobs,
obj=get_attr(client, ["with_raw_response", "fine_tuning", "jobs"]),
method_name=method_name,
span_name=_SPAN_NAME_FINE_TUNNING,
event_name=f"with_raw_response.fine_tuning.jobs.{method_name}",
Expand Down
9 changes: 4 additions & 5 deletions src/greptimeai/patchee/openai_patchee/image.py
Original file line number Diff line number Diff line change
Expand Up @@ -4,23 +4,22 @@
from openai import AsyncOpenAI, OpenAI

from greptimeai.patchee import Patchee
from greptimeai.utils.attr import get_attr, get_optional_attr

from . import _SPAN_NAME_IMAGE, OpenaiPatchees


class _ImagePatchees:
def __init__(self, client: Union[OpenAI, AsyncOpenAI, None], method_name: str):
images = Patchee(
obj=client.images if client else openai.images,
obj=get_optional_attr([client, openai], ["images"]),
method_name=method_name,
span_name=_SPAN_NAME_IMAGE,
event_name=f"images.{method_name}",
)

images_raw = Patchee(
obj=client.images.with_raw_response
if client
else openai.images.with_raw_response,
obj=get_optional_attr([client, openai], ["images", "with_raw_response"]),
method_name=method_name,
span_name=_SPAN_NAME_IMAGE,
event_name=f"images.with_raw_response.{method_name}",
Expand All @@ -30,7 +29,7 @@ def __init__(self, client: Union[OpenAI, AsyncOpenAI, None], method_name: str):

if client:
raw_images = Patchee(
obj=client.with_raw_response.images,
obj=get_attr(client, ["with_raw_response", "images"]),
method_name=method_name,
span_name=_SPAN_NAME_IMAGE,
event_name=f"with_raw_response.images.{method_name}",
Expand Down
Loading

0 comments on commit 47bebb8

Please sign in to comment.