Skip to content

Add edit, audio, and moderation APIs to client abstraction #5

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Merged
merged 7 commits into from
Jun 22, 2023
Merged
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
147 changes: 146 additions & 1 deletion openai/client.py
Original file line number Diff line number Diff line change
Expand Up @@ -371,8 +371,153 @@ async def aimage_edit(
response_format=response_format,
user=user,
)
return typing.cast(openai.Image, await openai.Image.acreate_edit(**kwargs))
return cast(openai.Image, await openai.Image.acreate_edit(**kwargs))

def edit(
self,
instruction: str,
*,
input: str = ...,
n: int = ...,
temperature: float = ...,
top_p: float = ...,
**kwargs,
):
self._populate_args(
Copy link
Owner

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

It is a bit of an annoying pattern to have to do this dance. But any "magic" (e.g. decorator) would not be significantly simpler. It might be possible to inspect the decorated function and use annotations to avoid the duplication of signatures across, but that does feel pri3:ish.

kwargs,
instruction=instruction,
input=input,
n=n,
temperature=temperature,
top_p=top_p,
)
self._normalize_model(kwargs)
return cast(openai.Edit, openai.Edit.create(**kwargs))

async def aedit(
self,
instruction: str,
*,
input: str = ...,
n: int = ...,
temperature: float = ...,
top_p: float = ...,
**kwargs,
):
self._populate_args(
kwargs,
instruction=instruction,
input=input,
n=n,
temperature=temperature,
top_p=top_p,
)
self._normalize_model(kwargs)
return cast(openai.Edit, await openai.Edit.acreate(**kwargs))

def moderation(
self,
input: Union[str, Iterable[str]],
**kwargs,
):
self._populate_args(
kwargs,
input=input,
)
self._normalize_model(kwargs)
return cast(openai.Moderation, openai.Moderation.create(**kwargs))

async def amoderation(
self,
input: Union[str, Iterable[str]],
**kwargs,
):
self._populate_args(
kwargs,
input=input,
)
self._normalize_model(kwargs)
return cast(openai.Moderation, await openai.Moderation.acreate(**kwargs))

def transcribe_audio(
self,
file: Union[bytes, BinaryIO],
*,
prompt: str = ...,
response_format: str = ...,
temperature: float = ...,
language: str = ...,
**kwargs,
):
self._populate_args(
kwargs,
file=file,
prompt=prompt,
response_format=response_format,
temperature=temperature,
language=language
)
self._normalize_model(kwargs)
return cast(openai.Audio, openai.Audio.transcribe(**kwargs))

async def atranscribe_audio(
self,
file: Union[bytes, BinaryIO],
*,
prompt: str = ...,
response_format: str = ...,
temperature: float = ...,
language: str = ...,
**kwargs,
):
self._populate_args(
kwargs,
file=file,
prompt=prompt,
response_format=response_format,
temperature=temperature,
language=language
)
self._normalize_model(kwargs)
return cast(openai.Audio, await openai.Audio.atranscribe(**kwargs))

def translate_audio(
self,
file: Union[bytes, BinaryIO],
*,
prompt: str = ...,
response_format: str = ...,
temperature: float = ...,
**kwargs,
):
self._populate_args(
kwargs,
file=file,
prompt=prompt,
response_format=response_format,
temperature=temperature,
)
self._normalize_model(kwargs)
return cast(openai.Audio, openai.Audio.translate(**kwargs))

async def atranslate_audio(
self,
file: Union[bytes, BinaryIO],
*,
prompt: str = ...,
response_format: str = ...,
temperature: float = ...,
**kwargs,
):
self._populate_args(
kwargs,
file=file,
prompt=prompt,
response_format=response_format,
temperature=temperature,
)
self._normalize_model(kwargs)
return cast(openai.Audio, await openai.Audio.atranslate(**kwargs))

if __name__ == "__main__":
client = OpenAIClient(
Expand Down