Skip to content

Commit 2357a8f

Browse files
feat(api): add support for storing chat completions (#2117)
1 parent 3f8d820 commit 2357a8f

22 files changed

+1350
-85
lines changed

.stats.yml

+2-2
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

+13-1
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

+18-2
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
"""
@@ -50,6 +65,7 @@ def blocking_func(arg1, arg2, kwarg1=None):
5065
# blocking code
5166
return result
5267
68+
5369
result = asyncify(blocking_function)(arg1, arg2, kwarg1=value1)
5470
```
5571

src/openai/cli/_api/chat/completions.py

+4-4
Original file line numberDiff line numberDiff line change
@@ -104,13 +104,13 @@ def create(args: CLIChatCompletionCreateArgs) -> None:
104104
"stream": False,
105105
}
106106
if args.temperature is not None:
107-
params['temperature'] = args.temperature
107+
params["temperature"] = args.temperature
108108
if args.stop is not None:
109-
params['stop'] = args.stop
109+
params["stop"] = args.stop
110110
if args.top_p is not None:
111-
params['top_p'] = args.top_p
111+
params["top_p"] = args.top_p
112112
if args.n is not None:
113-
params['n'] = args.n
113+
params["n"] = args.n
114114
if args.stream:
115115
params["stream"] = args.stream # type: ignore
116116
if args.max_tokens is not None:

src/openai/lib/_parsing/_completions.py

+2-2
Original file line numberDiff line numberDiff line change
@@ -45,13 +45,13 @@ def validate_input_tools(
4545
for tool in tools:
4646
if tool["type"] != "function":
4747
raise ValueError(
48-
f'Currently only `function` tool types support auto-parsing; Received `{tool["type"]}`',
48+
f"Currently only `function` tool types support auto-parsing; Received `{tool['type']}`",
4949
)
5050

5151
strict = tool["function"].get("strict")
5252
if strict is not True:
5353
raise ValueError(
54-
f'`{tool["function"]["name"]}` is not strict. Only `strict` function tools can be auto-parsed'
54+
f"`{tool['function']['name']}` is not strict. Only `strict` function tools can be auto-parsed"
5555
)
5656

5757

src/openai/resources/chat/chat.py

+1-1
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,
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)