Skip to content

Commit 6e8aaf5

Browse files
feat(api): add support for storing chat completions (openai#2117)
1 parent 1063234 commit 6e8aaf5

19 files changed

+1336
-62
lines changed

.stats.yml

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1,2 +1,2 @@
1-
configured_endpoints: 69
2-
openapi_spec_url: https://storage.googleapis.com/stainless-sdk-openapi-specs/openai-dfb00c627f58e5180af7a9b29ed2f2aa0764a3b9daa6a32a1cc45bc8e48dfe15.yml
1+
configured_endpoints: 74
2+
openapi_spec_url: https://storage.googleapis.com/stainless-sdk-openapi-specs/openai-4aa6ee65ba9efc789e05e6a5ef0883b2cadf06def8efd863dbf75e9e233067e1.yml

api.md

Lines changed: 13 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -48,6 +48,7 @@ from openai.types.chat import (
4848
ChatCompletionContentPartInputAudio,
4949
ChatCompletionContentPartRefusal,
5050
ChatCompletionContentPartText,
51+
ChatCompletionDeleted,
5152
ChatCompletionDeveloperMessageParam,
5253
ChatCompletionFunctionCallOption,
5354
ChatCompletionFunctionMessageParam,
@@ -59,6 +60,7 @@ from openai.types.chat import (
5960
ChatCompletionPredictionContent,
6061
ChatCompletionReasoningEffort,
6162
ChatCompletionRole,
63+
ChatCompletionStoreMessage,
6264
ChatCompletionStreamOptions,
6365
ChatCompletionSystemMessageParam,
6466
ChatCompletionTokenLogprob,
@@ -71,7 +73,17 @@ from openai.types.chat import (
7173

7274
Methods:
7375

74-
- <code title="post /chat/completions">client.chat.completions.<a href="./src/openai/resources/chat/completions.py">create</a>(\*\*<a href="src/openai/types/chat/completion_create_params.py">params</a>) -> <a href="./src/openai/types/chat/chat_completion.py">ChatCompletion</a></code>
76+
- <code title="post /chat/completions">client.chat.completions.<a href="./src/openai/resources/chat/completions/completions.py">create</a>(\*\*<a href="src/openai/types/chat/completion_create_params.py">params</a>) -> <a href="./src/openai/types/chat/chat_completion.py">ChatCompletion</a></code>
77+
- <code title="get /chat/completions/{completion_id}">client.chat.completions.<a href="./src/openai/resources/chat/completions/completions.py">retrieve</a>(completion_id) -> <a href="./src/openai/types/chat/chat_completion.py">ChatCompletion</a></code>
78+
- <code title="post /chat/completions/{completion_id}">client.chat.completions.<a href="./src/openai/resources/chat/completions/completions.py">update</a>(completion_id, \*\*<a href="src/openai/types/chat/completion_update_params.py">params</a>) -> <a href="./src/openai/types/chat/chat_completion.py">ChatCompletion</a></code>
79+
- <code title="get /chat/completions">client.chat.completions.<a href="./src/openai/resources/chat/completions/completions.py">list</a>(\*\*<a href="src/openai/types/chat/completion_list_params.py">params</a>) -> <a href="./src/openai/types/chat/chat_completion.py">SyncCursorPage[ChatCompletion]</a></code>
80+
- <code title="delete /chat/completions/{completion_id}">client.chat.completions.<a href="./src/openai/resources/chat/completions/completions.py">delete</a>(completion_id) -> <a href="./src/openai/types/chat/chat_completion_deleted.py">ChatCompletionDeleted</a></code>
81+
82+
### Messages
83+
84+
Methods:
85+
86+
- <code title="get /chat/completions/{completion_id}/messages">client.chat.completions.messages.<a href="./src/openai/resources/chat/completions/messages.py">list</a>(completion_id, \*\*<a href="src/openai/types/chat/completions/message_list_params.py">params</a>) -> <a href="./src/openai/types/chat/chat_completion_store_message.py">SyncCursorPage[ChatCompletionStoreMessage]</a></code>
7587

7688
# Embeddings
7789

src/openai/_utils/_sync.py

Lines changed: 17 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -7,16 +7,20 @@
77
from typing import Any, TypeVar, Callable, Awaitable
88
from typing_extensions import ParamSpec
99

10+
import anyio
11+
import sniffio
12+
import anyio.to_thread
13+
1014
T_Retval = TypeVar("T_Retval")
1115
T_ParamSpec = ParamSpec("T_ParamSpec")
1216

1317

1418
if sys.version_info >= (3, 9):
15-
to_thread = asyncio.to_thread
19+
_asyncio_to_thread = asyncio.to_thread
1620
else:
1721
# backport of https://docs.python.org/3/library/asyncio-task.html#asyncio.to_thread
1822
# for Python 3.8 support
19-
async def to_thread(
23+
async def _asyncio_to_thread(
2024
func: Callable[T_ParamSpec, T_Retval], /, *args: T_ParamSpec.args, **kwargs: T_ParamSpec.kwargs
2125
) -> Any:
2226
"""Asynchronously run function *func* in a separate thread.
@@ -34,6 +38,17 @@ async def to_thread(
3438
return await loop.run_in_executor(None, func_call)
3539

3640

41+
async def to_thread(
42+
func: Callable[T_ParamSpec, T_Retval], /, *args: T_ParamSpec.args, **kwargs: T_ParamSpec.kwargs
43+
) -> T_Retval:
44+
if sniffio.current_async_library() == "asyncio":
45+
return await _asyncio_to_thread(func, *args, **kwargs)
46+
47+
return await anyio.to_thread.run_sync(
48+
functools.partial(func, *args, **kwargs),
49+
)
50+
51+
3752
# inspired by `asyncer`, https://github.com/tiangolo/asyncer
3853
def asyncify(function: Callable[T_ParamSpec, T_Retval]) -> Callable[T_ParamSpec, Awaitable[T_Retval]]:
3954
"""

src/openai/resources/chat/chat.py

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -4,7 +4,7 @@
44

55
from ..._compat import cached_property
66
from ..._resource import SyncAPIResource, AsyncAPIResource
7-
from .completions import (
7+
from .completions.completions import (
88
Completions,
99
AsyncCompletions,
1010
CompletionsWithRawResponse,
Lines changed: 33 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,33 @@
1+
# File generated from our OpenAPI spec by Stainless. See CONTRIBUTING.md for details.
2+
3+
from .messages import (
4+
Messages,
5+
AsyncMessages,
6+
MessagesWithRawResponse,
7+
AsyncMessagesWithRawResponse,
8+
MessagesWithStreamingResponse,
9+
AsyncMessagesWithStreamingResponse,
10+
)
11+
from .completions import (
12+
Completions,
13+
AsyncCompletions,
14+
CompletionsWithRawResponse,
15+
AsyncCompletionsWithRawResponse,
16+
CompletionsWithStreamingResponse,
17+
AsyncCompletionsWithStreamingResponse,
18+
)
19+
20+
__all__ = [
21+
"Messages",
22+
"AsyncMessages",
23+
"MessagesWithRawResponse",
24+
"AsyncMessagesWithRawResponse",
25+
"MessagesWithStreamingResponse",
26+
"AsyncMessagesWithStreamingResponse",
27+
"Completions",
28+
"AsyncCompletions",
29+
"CompletionsWithRawResponse",
30+
"AsyncCompletionsWithRawResponse",
31+
"CompletionsWithStreamingResponse",
32+
"AsyncCompletionsWithStreamingResponse",
33+
]

0 commit comments

Comments
 (0)