Skip to content

Commit

Permalink
feat(api): Define OpenAI-compatible models
Browse files Browse the repository at this point in the history
  • Loading branch information
Stainless Bot authored and gradenr committed May 22, 2024
1 parent d48e365 commit bc1bb7b
Show file tree
Hide file tree
Showing 48 changed files with 929 additions and 827 deletions.
2 changes: 1 addition & 1 deletion .stats.yml
Original file line number Diff line number Diff line change
@@ -1,2 +1,2 @@
configured_endpoints: 7
openapi_spec_url: https://storage.googleapis.com/stainless-sdk-openapi-specs/groqcloud%2Fgroqcloud-45aff5d28bf67a962a928a38a6cf90ae3034bc824d2e65036d8784643e4995db.yml
openapi_spec_url: https://storage.googleapis.com/stainless-sdk-openapi-specs/groqcloud%2Fgroqcloud-36fcf453a77cbc8279361577a1e785a3a86ef7bcbde2195270a83e93cbc4b8b3.yml
37 changes: 0 additions & 37 deletions CHANGELOG.md
Original file line number Diff line number Diff line change
@@ -1,42 +1,5 @@
# Changelog

## 0.7.0 (2024-05-22)

Full Changelog: [v0.6.0...v0.7.0](https://github.com/groq/groq-python/compare/v0.6.0...v0.7.0)

### Features

* **api:** Improve types ([e46d3ed](https://github.com/groq/groq-python/commit/e46d3ed8591ef543a6f50c4443825fd547b74ab4))


### Chores

* fix array indices in README examples ([bacc106](https://github.com/groq/groq-python/commit/bacc10611d74ae9369093bbf44a451182b82e253))
* patch streaming ([9287ee7](https://github.com/groq/groq-python/commit/9287ee77e505187bb504729fad95edde41663ad0))

## 0.6.0 (2024-05-16)

Full Changelog: [v0.5.0...v0.6.0](https://github.com/groq/groq-python/compare/v0.5.0...v0.6.0)

### Features

* **api:** OpenAPI spec update via Stainless API ([#39](https://github.com/groq/groq-python/issues/39)) ([53a8b27](https://github.com/groq/groq-python/commit/53a8b27365c085227e449812779c5bafadada9fb))
* **api:** OpenAPI spec update via Stainless API ([#45](https://github.com/groq/groq-python/issues/45)) ([18bce01](https://github.com/groq/groq-python/commit/18bce01c835fbc0485ef78f12b28caead37d15c4))
* **api:** update via SDK Studio ([#35](https://github.com/groq/groq-python/issues/35)) ([5ca3b78](https://github.com/groq/groq-python/commit/5ca3b785626145a87a27d64a4a4c3eae1b1fa8de))
* **api:** update via SDK Studio ([#38](https://github.com/groq/groq-python/issues/38)) ([dc73eeb](https://github.com/groq/groq-python/commit/dc73eeb92a441123596b3f3420a757074ee62345))
* **api:** update via SDK Studio ([#40](https://github.com/groq/groq-python/issues/40)) ([7227816](https://github.com/groq/groq-python/commit/7227816b92397d913f8aeceda64127e2db99f720))
* **api:** update via SDK Studio ([#41](https://github.com/groq/groq-python/issues/41)) ([3fdb317](https://github.com/groq/groq-python/commit/3fdb31786699d3298d5064faac77e26dbc7852d8))
* **api:** update via SDK Studio ([#42](https://github.com/groq/groq-python/issues/42)) ([d270e51](https://github.com/groq/groq-python/commit/d270e51d63edc138b0f0afa0b53c2f84a69b1f73))
* **api:** update via SDK Studio ([#43](https://github.com/groq/groq-python/issues/43)) ([0e04381](https://github.com/groq/groq-python/commit/0e043816e5cbcdf5def7583d20312c6f5e29808f))
* **api:** update via SDK Studio ([#44](https://github.com/groq/groq-python/issues/44)) ([3807b4c](https://github.com/groq/groq-python/commit/3807b4c67981aec711d3e60e57ec0e5e9493f8a0))
* **api:** update via SDK Studio ([#46](https://github.com/groq/groq-python/issues/46)) ([ed1ea2a](https://github.com/groq/groq-python/commit/ed1ea2a99f95d4d353c3dd9baf7fc1c5d37cc20a))


### Chores

* **examples:** embedding example ([961159c](https://github.com/groq/groq-python/commit/961159c9c83a21cd9631af4802e4b538ebbbb186))
* patch streaming ([c8eaad3](https://github.com/groq/groq-python/commit/c8eaad35abe33b692e029495157948db3a09af3c))

## 0.5.0 (2024-04-11)

Full Changelog: [v0.4.2...v0.5.0](https://github.com/groq/groq-python/compare/v0.4.2...v0.5.0)
Expand Down
30 changes: 19 additions & 11 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -24,9 +24,13 @@ pip install groq
The full API of this library can be found in [api.md](api.md).

```python
import os
from groq import Groq

client = Groq()
client = Groq(
# This is the default and can be omitted
api_key=os.environ.get("GROQ_API_KEY"),
)

chat_completion = client.chat.completions.create(
messages=[
Expand All @@ -35,9 +39,9 @@ chat_completion = client.chat.completions.create(
"content": "Explain the importance of low latency LLMs",
}
],
model="mixtral-8x7b-32768",
model="llama3-8b-8192",
)
print(chat_completion.choices[0].message.content)
print(chat_completion.choices_0.message.content)
```

While you can provide an `api_key` keyword argument,
Expand All @@ -50,10 +54,14 @@ so that your API Key is not stored in source control.
Simply import `AsyncGroq` instead of `Groq` and use `await` with each API call:

```python
import os
import asyncio
from groq import AsyncGroq

client = AsyncGroq()
client = AsyncGroq(
# This is the default and can be omitted
api_key=os.environ.get("GROQ_API_KEY"),
)


async def main() -> None:
Expand All @@ -64,9 +72,9 @@ async def main() -> None:
"content": "Explain the importance of low latency LLMs",
}
],
model="mixtral-8x7b-32768",
model="llama3-8b-8192",
)
print(chat_completion.choices[0].message.content)
print(chat_completion.choices_0.message.content)


asyncio.run(main())
Expand Down Expand Up @@ -110,7 +118,7 @@ try:
"content": "Explain the importance of low latency LLMs",
},
],
model="mixtral-8x7b-32768",
model="llama3-8b-8192",
)
except groq.APIConnectionError as e:
print("The server could not be reached")
Expand Down Expand Up @@ -165,7 +173,7 @@ client.with_options(max_retries=5).chat.completions.create(
"content": "Explain the importance of low latency LLMs",
},
],
model="mixtral-8x7b-32768",
model="llama3-8b-8192",
)
```

Expand Down Expand Up @@ -200,7 +208,7 @@ client.with_options(timeout=5.0).chat.completions.create(
"content": "Explain the importance of low latency LLMs",
},
],
model="mixtral-8x7b-32768",
model="llama3-8b-8192",
)
```

Expand Down Expand Up @@ -248,7 +256,7 @@ response = client.chat.completions.with_raw_response.create(
"role": "user",
"content": "Explain the importance of low latency LLMs",
}],
model="mixtral-8x7b-32768",
model="llama3-8b-8192",
)
print(response.headers.get('X-My-Header'))

Expand Down Expand Up @@ -278,7 +286,7 @@ with client.chat.completions.with_streaming_response.create(
"content": "Explain the importance of low latency LLMs",
},
],
model="mixtral-8x7b-32768",
model="llama3-8b-8192",
) as response:
print(response.headers.get("X-My-Header"))

Expand Down
54 changes: 44 additions & 10 deletions api.md
Original file line number Diff line number Diff line change
@@ -1,14 +1,16 @@
# Embeddings

Types:
# Shared Types

```python
from groq.types import CreateEmbeddingResponse, Embedding
from groq.types import ErrorObject, FunctionDefinition, FunctionParameters
```

Methods:
# Completions

- <code title="post /openai/v1/embeddings">client.embeddings.<a href="./src/groq/resources/embeddings.py">create</a>(\*\*<a href="src/groq/types/embedding_create_params.py">params</a>) -> <a href="./src/groq/types/create_embedding_response.py">CreateEmbeddingResponse</a></code>
Types:

```python
from groq.types import CompletionUsage
```

# Chat

Expand All @@ -17,13 +19,45 @@ Methods:
Types:

```python
from groq.types.chat import ChatCompletion
from groq.types.chat import (
ChatCompletion,
ChatCompletionAssistantMessageParam,
ChatCompletionChunk,
ChatCompletionContentPart,
ChatCompletionContentPartImage,
ChatCompletionContentPartText,
ChatCompletionFunctionCallOption,
ChatCompletionFunctionMessageParam,
ChatCompletionMessage,
ChatCompletionMessageParam,
ChatCompletionMessageToolCall,
ChatCompletionNamedToolChoice,
ChatCompletionRole,
ChatCompletionSystemMessageParam,
ChatCompletionTokenLogprob,
ChatCompletionTool,
ChatCompletionToolChoiceOption,
ChatCompletionToolMessageParam,
ChatCompletionUserMessageParam,
)
```

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>

# Embeddings

Types:

```python
from groq.types import CreateEmbeddingResponse, Embedding
```

Methods:

- <code title="post /openai/v1/embeddings">client.embeddings.<a href="./src/groq/resources/embeddings.py">create</a>(\*\*<a href="src/groq/types/embedding_create_params.py">params</a>) -> <a href="./src/groq/types/create_embedding_response.py">CreateEmbeddingResponse</a></code>

# Audio

## Transcriptions
Expand Down Expand Up @@ -55,11 +89,11 @@ Methods:
Types:

```python
from groq.types import Model, ModelList
from groq.types import Model, ModelDeleted, ModelListResponse
```

Methods:

- <code title="get /openai/v1/models/{model}">client.models.<a href="./src/groq/resources/models.py">retrieve</a>(model) -> <a href="./src/groq/types/model.py">Model</a></code>
- <code title="get /openai/v1/models">client.models.<a href="./src/groq/resources/models.py">list</a>() -> <a href="./src/groq/types/model_list.py">ModelList</a></code>
- <code title="delete /openai/v1/models/{model}">client.models.<a href="./src/groq/resources/models.py">delete</a>(model) -> None</code>
- <code title="get /openai/v1/models">client.models.<a href="./src/groq/resources/models.py">list</a>() -> <a href="./src/groq/types/model_list_response.py">ModelListResponse</a></code>
- <code title="delete /openai/v1/models/{model}">client.models.<a href="./src/groq/resources/models.py">delete</a>(model) -> <a href="./src/groq/types/model_deleted.py">ModelDeleted</a></code>
16 changes: 8 additions & 8 deletions src/groq/_client.py
Original file line number Diff line number Diff line change
Expand Up @@ -46,8 +46,8 @@


class Groq(SyncAPIClient):
embeddings: resources.Embeddings
chat: resources.Chat
embeddings: resources.Embeddings
audio: resources.Audio
models: resources.Models
with_raw_response: GroqWithRawResponse
Expand Down Expand Up @@ -107,8 +107,8 @@ def __init__(
_strict_response_validation=_strict_response_validation,
)

self.embeddings = resources.Embeddings(self)
self.chat = resources.Chat(self)
self.embeddings = resources.Embeddings(self)
self.audio = resources.Audio(self)
self.models = resources.Models(self)
self.with_raw_response = GroqWithRawResponse(self)
Expand Down Expand Up @@ -220,8 +220,8 @@ def _make_status_error(


class AsyncGroq(AsyncAPIClient):
embeddings: resources.AsyncEmbeddings
chat: resources.AsyncChat
embeddings: resources.AsyncEmbeddings
audio: resources.AsyncAudio
models: resources.AsyncModels
with_raw_response: AsyncGroqWithRawResponse
Expand Down Expand Up @@ -281,8 +281,8 @@ def __init__(
_strict_response_validation=_strict_response_validation,
)

self.embeddings = resources.AsyncEmbeddings(self)
self.chat = resources.AsyncChat(self)
self.embeddings = resources.AsyncEmbeddings(self)
self.audio = resources.AsyncAudio(self)
self.models = resources.AsyncModels(self)
self.with_raw_response = AsyncGroqWithRawResponse(self)
Expand Down Expand Up @@ -395,32 +395,32 @@ def _make_status_error(

class GroqWithRawResponse:
def __init__(self, client: Groq) -> None:
self.embeddings = resources.EmbeddingsWithRawResponse(client.embeddings)
self.chat = resources.ChatWithRawResponse(client.chat)
self.embeddings = resources.EmbeddingsWithRawResponse(client.embeddings)
self.audio = resources.AudioWithRawResponse(client.audio)
self.models = resources.ModelsWithRawResponse(client.models)


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


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


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

Expand Down
4 changes: 0 additions & 4 deletions src/groq/_streaming.py
Original file line number Diff line number Diff line change
Expand Up @@ -55,8 +55,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 @@ -121,8 +119,6 @@ async def __stream__(self) -> AsyncIterator[_T]:
iterator = self._iter_events()

async 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
12 changes: 6 additions & 6 deletions src/groq/resources/__init__.py
Original file line number Diff line number Diff line change
Expand Up @@ -34,18 +34,18 @@
)

__all__ = [
"Embeddings",
"AsyncEmbeddings",
"EmbeddingsWithRawResponse",
"AsyncEmbeddingsWithRawResponse",
"EmbeddingsWithStreamingResponse",
"AsyncEmbeddingsWithStreamingResponse",
"Chat",
"AsyncChat",
"ChatWithRawResponse",
"AsyncChatWithRawResponse",
"ChatWithStreamingResponse",
"AsyncChatWithStreamingResponse",
"Embeddings",
"AsyncEmbeddings",
"EmbeddingsWithRawResponse",
"AsyncEmbeddingsWithRawResponse",
"EmbeddingsWithStreamingResponse",
"AsyncEmbeddingsWithStreamingResponse",
"Audio",
"AsyncAudio",
"AudioWithRawResponse",
Expand Down
Loading

0 comments on commit bc1bb7b

Please sign in to comment.