Skip to content

Commit

Permalink
feat: Add transcription and translation endpoints
Browse files Browse the repository at this point in the history
  • Loading branch information
stainless-app[bot] authored and gradenr committed Mar 8, 2024
1 parent 220a2f1 commit d6be6e7
Show file tree
Hide file tree
Showing 31 changed files with 1,450 additions and 285 deletions.
2 changes: 1 addition & 1 deletion .stats.yml
Original file line number Diff line number Diff line change
@@ -1 +1 @@
configured_endpoints: 4
configured_endpoints: 6
4 changes: 2 additions & 2 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -39,7 +39,7 @@ chat_completion = client.chat.completions.create(
],
model="mixtral-8x7b-32768",
)
print(chat_completion.choices[0].message.content)
print(chat_completion.choices_0.message.content)
```

While you can provide an `api_key` keyword argument,
Expand Down Expand Up @@ -72,7 +72,7 @@ async def main() -> None:
],
model="mixtral-8x7b-32768",
)
print(chat_completion.choices[0].message.content)
print(chat_completion.choices_0.message.content)


asyncio.run(main())
Expand Down
26 changes: 26 additions & 0 deletions api.md
Original file line number Diff line number Diff line change
Expand Up @@ -12,6 +12,32 @@ Methods:

- <code title="post /openai/v1/chat/completions">client.chat.completions.<a href="./src/groq/resources/chat/completions.py">create</a>(\*\*<a href="src/groq/types/chat/completion_create_params.py">params</a>) -> <a href="./src/groq/types/chat/chat_completion.py">ChatCompletion</a></code>

# Audio

Types:

```python
from groq.types import Translation
```

## Transcriptions

Types:

```python
from groq.types.audio import Transcription
```

Methods:

- <code title="post /openai/v1/audio/transcriptions">client.audio.transcriptions.<a href="./src/groq/resources/audio/transcriptions.py">create</a>(\*\*<a href="src/groq/types/audio/transcription_create_params.py">params</a>) -> <a href="./src/groq/types/audio/transcription.py">Transcription</a></code>

## Translations

Methods:

- <code title="post /openai/v1/audio/translations">client.audio.translations.<a href="./src/groq/resources/audio/translations.py">create</a>(\*\*<a href="src/groq/types/audio/translation_create_params.py">params</a>) -> <a href="./src/groq/types/translation.py">Translation</a></code>

# Models

Types:
Expand Down
1 change: 0 additions & 1 deletion examples/chat_completion_streaming.py
Original file line number Diff line number Diff line change
Expand Up @@ -46,4 +46,3 @@
print(chunk.choices[0].delta.content, end="")
if chunk.choices[0].finish_reason:
print(f"\n\nUsage stats: {chunk.x_groq.usage}")

10 changes: 10 additions & 0 deletions src/groq/_base_client.py
Original file line number Diff line number Diff line change
Expand Up @@ -779,6 +779,11 @@ def __init__(
else:
timeout = DEFAULT_TIMEOUT

if http_client is not None and not isinstance(http_client, httpx.Client): # pyright: ignore[reportUnnecessaryIsInstance]
raise TypeError(
f"Invalid `http_client` argument; Expected an instance of `httpx.Client` but got {type(http_client)}"
)

super().__init__(
version=version,
limits=limits,
Expand Down Expand Up @@ -1308,6 +1313,11 @@ def __init__(
else:
timeout = DEFAULT_TIMEOUT

if http_client is not None and not isinstance(http_client, httpx.AsyncClient): # pyright: ignore[reportUnnecessaryIsInstance]
raise TypeError(
f"Invalid `http_client` argument; Expected an instance of `httpx.AsyncClient` but got {type(http_client)}"
)

super().__init__(
version=version,
base_url=base_url,
Expand Down
8 changes: 8 additions & 0 deletions src/groq/_client.py
Original file line number Diff line number Diff line change
Expand Up @@ -47,6 +47,7 @@

class Groq(SyncAPIClient):
chat: resources.Chat
audio: resources.Audio
models: resources.Models
with_raw_response: GroqWithRawResponse
with_streaming_response: GroqWithStreamedResponse
Expand Down Expand Up @@ -104,6 +105,7 @@ def __init__(
)

self.chat = resources.Chat(self)
self.audio = resources.Audio(self)
self.models = resources.Models(self)
self.with_raw_response = GroqWithRawResponse(self)
self.with_streaming_response = GroqWithStreamedResponse(self)
Expand Down Expand Up @@ -215,6 +217,7 @@ def _make_status_error(

class AsyncGroq(AsyncAPIClient):
chat: resources.AsyncChat
audio: resources.AsyncAudio
models: resources.AsyncModels
with_raw_response: AsyncGroqWithRawResponse
with_streaming_response: AsyncGroqWithStreamedResponse
Expand Down Expand Up @@ -272,6 +275,7 @@ def __init__(
)

self.chat = resources.AsyncChat(self)
self.audio = resources.AsyncAudio(self)
self.models = resources.AsyncModels(self)
self.with_raw_response = AsyncGroqWithRawResponse(self)
self.with_streaming_response = AsyncGroqWithStreamedResponse(self)
Expand Down Expand Up @@ -384,24 +388,28 @@ def _make_status_error(
class GroqWithRawResponse:
def __init__(self, client: Groq) -> None:
self.chat = resources.ChatWithRawResponse(client.chat)
self.audio = resources.AudioWithRawResponse(client.audio)
self.models = resources.ModelsWithRawResponse(client.models)


class AsyncGroqWithRawResponse:
def __init__(self, client: AsyncGroq) -> None:
self.chat = resources.AsyncChatWithRawResponse(client.chat)
self.audio = resources.AsyncAudioWithRawResponse(client.audio)
self.models = resources.AsyncModelsWithRawResponse(client.models)


class GroqWithStreamedResponse:
def __init__(self, client: Groq) -> None:
self.chat = resources.ChatWithStreamingResponse(client.chat)
self.audio = resources.AudioWithStreamingResponse(client.audio)
self.models = resources.ModelsWithStreamingResponse(client.models)


class AsyncGroqWithStreamedResponse:
def __init__(self, client: AsyncGroq) -> None:
self.chat = resources.AsyncChatWithStreamingResponse(client.chat)
self.audio = resources.AsyncAudioWithStreamingResponse(client.audio)
self.models = resources.AsyncModelsWithStreamingResponse(client.models)


Expand Down
5 changes: 5 additions & 0 deletions src/groq/_files.py
Original file line number Diff line number Diff line change
Expand Up @@ -13,12 +13,17 @@
FileContent,
RequestFiles,
HttpxFileTypes,
Base64FileInput,
HttpxFileContent,
HttpxRequestFiles,
)
from ._utils import is_tuple_t, is_mapping_t, is_sequence_t


def is_base64_file_input(obj: object) -> TypeGuard[Base64FileInput]:
return isinstance(obj, io.IOBase) or isinstance(obj, os.PathLike)


def is_file_content(obj: object) -> TypeGuard[FileContent]:
return (
isinstance(obj, bytes) or isinstance(obj, tuple) or isinstance(obj, io.IOBase) or isinstance(obj, os.PathLike)
Expand Down
8 changes: 8 additions & 0 deletions src/groq/_response.py
Original file line number Diff line number Diff line change
Expand Up @@ -172,6 +172,12 @@ def _parse(self, *, to: type[_T] | None = None) -> R | _T:
if cast_to == bytes:
return cast(R, response.content)

if cast_to == int:
return cast(R, int(response.text))

if cast_to == float:
return cast(R, float(response.text))

origin = get_origin(cast_to) or cast_to

if origin == APIResponse:
Expand Down Expand Up @@ -273,6 +279,8 @@ class MyModel(BaseModel):
- `list`
- `Union`
- `str`
- `int`
- `float`
- `httpx.Response`
"""
cache_key = to if to is not None else self._cast_to
Expand Down
6 changes: 0 additions & 6 deletions src/groq/_streaming.py
Original file line number Diff line number Diff line change
Expand Up @@ -58,8 +58,6 @@ def __stream__(self) -> Iterator[_T]:
iterator = self._iter_events()

for sse in iterator:
if sse.data.startswith("[DONE]"):
break
yield process_data(data=sse.json(), cast_to=cast_to, response=response)

# Ensure the entire stream is consumed
Expand Down Expand Up @@ -116,13 +114,9 @@ async def __aiter__(self) -> AsyncIterator[_T]:
async def _iter_events(self) -> AsyncIterator[ServerSentEvent]:
if isinstance(self._decoder, SSEBytesDecoder):
async for sse in self._decoder.aiter_bytes(self.response.aiter_bytes()):
if sse.data.startswith("[DONE]"):
break
yield sse
else:
async for sse in self._decoder.aiter(self.response.aiter_lines()):
if sse.data.startswith("[DONE]"):
break
yield sse

async def __stream__(self) -> AsyncIterator[_T]:
Expand Down
2 changes: 2 additions & 0 deletions src/groq/_types.py
Original file line number Diff line number Diff line change
Expand Up @@ -40,8 +40,10 @@
ProxiesDict = Dict["str | URL", Union[None, str, URL, Proxy]]
ProxiesTypes = Union[str, Proxy, ProxiesDict]
if TYPE_CHECKING:
Base64FileInput = Union[IO[bytes], PathLike[str]]
FileContent = Union[IO[bytes], bytes, PathLike[str]]
else:
Base64FileInput = Union[IO[bytes], PathLike]
FileContent = Union[IO[bytes], bytes, PathLike] # PathLike is not subscriptable in Python 3.8.
FileTypes = Union[
# file (or bytes)
Expand Down
2 changes: 2 additions & 0 deletions src/groq/_utils/__init__.py
Original file line number Diff line number Diff line change
Expand Up @@ -44,5 +44,7 @@
from ._transform import (
PropertyInfo as PropertyInfo,
transform as transform,
async_transform as async_transform,
maybe_transform as maybe_transform,
async_maybe_transform as async_maybe_transform,
)
Loading

0 comments on commit d6be6e7

Please sign in to comment.