1# File generated from our OpenAPI spec by Stainless. See CONTRIBUTING.md for details.
  2
  3from __future__ import annotations
  4
  5from typing_extensions import Literal
  6
  7import httpx
  8
  9from .... import _legacy_response
 10from ...._types import Body, Omit, Query, Headers, NotGiven, omit, not_given
 11from ...._utils import maybe_transform
 12from ...._compat import cached_property
 13from ...._resource import SyncAPIResource, AsyncAPIResource
 14from ...._response import to_streamed_response_wrapper, async_to_streamed_response_wrapper
 15from ....pagination import SyncCursorPage, AsyncCursorPage
 16from ...._base_client import AsyncPaginator, make_request_options
 17from ....types.chat.completions import message_list_params
 18from ....types.chat.chat_completion_store_message import ChatCompletionStoreMessage
 19
 20__all__ = ["Messages", "AsyncMessages"]
 21
 22
 23class Messages(SyncAPIResource):
 24    @cached_property
 25    def with_raw_response(self) -> MessagesWithRawResponse:
 26        """
 27        This property can be used as a prefix for any HTTP method call to return
 28        the raw response object instead of the parsed content.
 29
 30        For more information, see https://www.github.com/openai/openai-python#accessing-raw-response-data-eg-headers
 31        """
 32        return MessagesWithRawResponse(self)
 33
 34    @cached_property
 35    def with_streaming_response(self) -> MessagesWithStreamingResponse:
 36        """
 37        An alternative to `.with_raw_response` that doesn't eagerly read the response body.
 38
 39        For more information, see https://www.github.com/openai/openai-python#with_streaming_response
 40        """
 41        return MessagesWithStreamingResponse(self)
 42
 43    def list(
 44        self,
 45        completion_id: str,
 46        *,
 47        after: str | Omit = omit,
 48        limit: int | Omit = omit,
 49        order: Literal["asc", "desc"] | Omit = omit,
 50        # Use the following arguments if you need to pass additional parameters to the API that aren't available via kwargs.
 51        # The extra values given here take precedence over values defined on the client or passed to this method.
 52        extra_headers: Headers | None = None,
 53        extra_query: Query | None = None,
 54        extra_body: Body | None = None,
 55        timeout: float | httpx.Timeout | None | NotGiven = not_given,
 56    ) -> SyncCursorPage[ChatCompletionStoreMessage]:
 57        """Get the messages in a stored chat completion.
 58
 59        Only Chat Completions that have
 60        been created with the `store` parameter set to `true` will be returned.
 61
 62        Args:
 63          after: Identifier for the last message from the previous pagination request.
 64
 65          limit: Number of messages to retrieve.
 66
 67          order: Sort order for messages by timestamp. Use `asc` for ascending order or `desc`
 68              for descending order. Defaults to `asc`.
 69
 70          extra_headers: Send extra headers
 71
 72          extra_query: Add additional query parameters to the request
 73
 74          extra_body: Add additional JSON properties to the request
 75
 76          timeout: Override the client-level default timeout for this request, in seconds
 77        """
 78        if not completion_id:
 79            raise ValueError(f"Expected a non-empty value for `completion_id` but received {completion_id!r}")
 80        return self._get_api_list(
 81            f"/chat/completions/{completion_id}/messages",
 82            page=SyncCursorPage[ChatCompletionStoreMessage],
 83            options=make_request_options(
 84                extra_headers=extra_headers,
 85                extra_query=extra_query,
 86                extra_body=extra_body,
 87                timeout=timeout,
 88                query=maybe_transform(
 89                    {
 90                        "after": after,
 91                        "limit": limit,
 92                        "order": order,
 93                    },
 94                    message_list_params.MessageListParams,
 95                ),
 96            ),
 97            model=ChatCompletionStoreMessage,
 98        )
 99
100
101class AsyncMessages(AsyncAPIResource):
102    @cached_property
103    def with_raw_response(self) -> AsyncMessagesWithRawResponse:
104        """
105        This property can be used as a prefix for any HTTP method call to return
106        the raw response object instead of the parsed content.
107
108        For more information, see https://www.github.com/openai/openai-python#accessing-raw-response-data-eg-headers
109        """
110        return AsyncMessagesWithRawResponse(self)
111
112    @cached_property
113    def with_streaming_response(self) -> AsyncMessagesWithStreamingResponse:
114        """
115        An alternative to `.with_raw_response` that doesn't eagerly read the response body.
116
117        For more information, see https://www.github.com/openai/openai-python#with_streaming_response
118        """
119        return AsyncMessagesWithStreamingResponse(self)
120
121    def list(
122        self,
123        completion_id: str,
124        *,
125        after: str | Omit = omit,
126        limit: int | Omit = omit,
127        order: Literal["asc", "desc"] | Omit = omit,
128        # Use the following arguments if you need to pass additional parameters to the API that aren't available via kwargs.
129        # The extra values given here take precedence over values defined on the client or passed to this method.
130        extra_headers: Headers | None = None,
131        extra_query: Query | None = None,
132        extra_body: Body | None = None,
133        timeout: float | httpx.Timeout | None | NotGiven = not_given,
134    ) -> AsyncPaginator[ChatCompletionStoreMessage, AsyncCursorPage[ChatCompletionStoreMessage]]:
135        """Get the messages in a stored chat completion.
136
137        Only Chat Completions that have
138        been created with the `store` parameter set to `true` will be returned.
139
140        Args:
141          after: Identifier for the last message from the previous pagination request.
142
143          limit: Number of messages to retrieve.
144
145          order: Sort order for messages by timestamp. Use `asc` for ascending order or `desc`
146              for descending order. Defaults to `asc`.
147
148          extra_headers: Send extra headers
149
150          extra_query: Add additional query parameters to the request
151
152          extra_body: Add additional JSON properties to the request
153
154          timeout: Override the client-level default timeout for this request, in seconds
155        """
156        if not completion_id:
157            raise ValueError(f"Expected a non-empty value for `completion_id` but received {completion_id!r}")
158        return self._get_api_list(
159            f"/chat/completions/{completion_id}/messages",
160            page=AsyncCursorPage[ChatCompletionStoreMessage],
161            options=make_request_options(
162                extra_headers=extra_headers,
163                extra_query=extra_query,
164                extra_body=extra_body,
165                timeout=timeout,
166                query=maybe_transform(
167                    {
168                        "after": after,
169                        "limit": limit,
170                        "order": order,
171                    },
172                    message_list_params.MessageListParams,
173                ),
174            ),
175            model=ChatCompletionStoreMessage,
176        )
177
178
179class MessagesWithRawResponse:
180    def __init__(self, messages: Messages) -> None:
181        self._messages = messages
182
183        self.list = _legacy_response.to_raw_response_wrapper(
184            messages.list,
185        )
186
187
188class AsyncMessagesWithRawResponse:
189    def __init__(self, messages: AsyncMessages) -> None:
190        self._messages = messages
191
192        self.list = _legacy_response.async_to_raw_response_wrapper(
193            messages.list,
194        )
195
196
197class MessagesWithStreamingResponse:
198    def __init__(self, messages: Messages) -> None:
199        self._messages = messages
200
201        self.list = to_streamed_response_wrapper(
202            messages.list,
203        )
204
205
206class AsyncMessagesWithStreamingResponse:
207    def __init__(self, messages: AsyncMessages) -> None:
208        self._messages = messages
209
210        self.list = async_to_streamed_response_wrapper(
211            messages.list,
212        )