main
1# File generated from our OpenAPI spec by Stainless. See CONTRIBUTING.md for details.
2
3from __future__ import annotations
4
5from typing import Iterable, Optional
6
7import httpx
8
9from ... import _legacy_response
10from .items import (
11 Items,
12 AsyncItems,
13 ItemsWithRawResponse,
14 AsyncItemsWithRawResponse,
15 ItemsWithStreamingResponse,
16 AsyncItemsWithStreamingResponse,
17)
18from ..._types import Body, Omit, Query, Headers, NotGiven, omit, not_given
19from ..._utils import maybe_transform, async_maybe_transform
20from ..._compat import cached_property
21from ..._resource import SyncAPIResource, AsyncAPIResource
22from ..._response import to_streamed_response_wrapper, async_to_streamed_response_wrapper
23from ..._base_client import make_request_options
24from ...types.conversations import conversation_create_params, conversation_update_params
25from ...types.shared_params.metadata import Metadata
26from ...types.conversations.conversation import Conversation
27from ...types.responses.response_input_item_param import ResponseInputItemParam
28from ...types.conversations.conversation_deleted_resource import ConversationDeletedResource
29
30__all__ = ["Conversations", "AsyncConversations"]
31
32
33class Conversations(SyncAPIResource):
34 @cached_property
35 def items(self) -> Items:
36 return Items(self._client)
37
38 @cached_property
39 def with_raw_response(self) -> ConversationsWithRawResponse:
40 """
41 This property can be used as a prefix for any HTTP method call to return
42 the raw response object instead of the parsed content.
43
44 For more information, see https://www.github.com/openai/openai-python#accessing-raw-response-data-eg-headers
45 """
46 return ConversationsWithRawResponse(self)
47
48 @cached_property
49 def with_streaming_response(self) -> ConversationsWithStreamingResponse:
50 """
51 An alternative to `.with_raw_response` that doesn't eagerly read the response body.
52
53 For more information, see https://www.github.com/openai/openai-python#with_streaming_response
54 """
55 return ConversationsWithStreamingResponse(self)
56
57 def create(
58 self,
59 *,
60 items: Optional[Iterable[ResponseInputItemParam]] | Omit = omit,
61 metadata: Optional[Metadata] | Omit = omit,
62 # Use the following arguments if you need to pass additional parameters to the API that aren't available via kwargs.
63 # The extra values given here take precedence over values defined on the client or passed to this method.
64 extra_headers: Headers | None = None,
65 extra_query: Query | None = None,
66 extra_body: Body | None = None,
67 timeout: float | httpx.Timeout | None | NotGiven = not_given,
68 ) -> Conversation:
69 """
70 Create a conversation.
71
72 Args:
73 items: Initial items to include in the conversation context. You may add up to 20 items
74 at a time.
75
76 metadata: Set of 16 key-value pairs that can be attached to an object. This can be useful
77 for storing additional information about the object in a structured format, and
78 querying for objects via API or the dashboard.
79
80 Keys are strings with a maximum length of 64 characters. Values are strings with
81 a maximum length of 512 characters.
82
83 extra_headers: Send extra headers
84
85 extra_query: Add additional query parameters to the request
86
87 extra_body: Add additional JSON properties to the request
88
89 timeout: Override the client-level default timeout for this request, in seconds
90 """
91 return self._post(
92 "/conversations",
93 body=maybe_transform(
94 {
95 "items": items,
96 "metadata": metadata,
97 },
98 conversation_create_params.ConversationCreateParams,
99 ),
100 options=make_request_options(
101 extra_headers=extra_headers, extra_query=extra_query, extra_body=extra_body, timeout=timeout
102 ),
103 cast_to=Conversation,
104 )
105
106 def retrieve(
107 self,
108 conversation_id: str,
109 *,
110 # Use the following arguments if you need to pass additional parameters to the API that aren't available via kwargs.
111 # The extra values given here take precedence over values defined on the client or passed to this method.
112 extra_headers: Headers | None = None,
113 extra_query: Query | None = None,
114 extra_body: Body | None = None,
115 timeout: float | httpx.Timeout | None | NotGiven = not_given,
116 ) -> Conversation:
117 """
118 Get a conversation
119
120 Args:
121 extra_headers: Send extra headers
122
123 extra_query: Add additional query parameters to the request
124
125 extra_body: Add additional JSON properties to the request
126
127 timeout: Override the client-level default timeout for this request, in seconds
128 """
129 if not conversation_id:
130 raise ValueError(f"Expected a non-empty value for `conversation_id` but received {conversation_id!r}")
131 return self._get(
132 f"/conversations/{conversation_id}",
133 options=make_request_options(
134 extra_headers=extra_headers, extra_query=extra_query, extra_body=extra_body, timeout=timeout
135 ),
136 cast_to=Conversation,
137 )
138
139 def update(
140 self,
141 conversation_id: str,
142 *,
143 metadata: Optional[Metadata],
144 # Use the following arguments if you need to pass additional parameters to the API that aren't available via kwargs.
145 # The extra values given here take precedence over values defined on the client or passed to this method.
146 extra_headers: Headers | None = None,
147 extra_query: Query | None = None,
148 extra_body: Body | None = None,
149 timeout: float | httpx.Timeout | None | NotGiven = not_given,
150 ) -> Conversation:
151 """
152 Update a conversation
153
154 Args:
155 metadata: Set of 16 key-value pairs that can be attached to an object. This can be useful
156 for storing additional information about the object in a structured format, and
157 querying for objects via API or the dashboard.
158
159 Keys are strings with a maximum length of 64 characters. Values are strings with
160 a maximum length of 512 characters.
161
162 extra_headers: Send extra headers
163
164 extra_query: Add additional query parameters to the request
165
166 extra_body: Add additional JSON properties to the request
167
168 timeout: Override the client-level default timeout for this request, in seconds
169 """
170 if not conversation_id:
171 raise ValueError(f"Expected a non-empty value for `conversation_id` but received {conversation_id!r}")
172 return self._post(
173 f"/conversations/{conversation_id}",
174 body=maybe_transform({"metadata": metadata}, conversation_update_params.ConversationUpdateParams),
175 options=make_request_options(
176 extra_headers=extra_headers, extra_query=extra_query, extra_body=extra_body, timeout=timeout
177 ),
178 cast_to=Conversation,
179 )
180
181 def delete(
182 self,
183 conversation_id: str,
184 *,
185 # Use the following arguments if you need to pass additional parameters to the API that aren't available via kwargs.
186 # The extra values given here take precedence over values defined on the client or passed to this method.
187 extra_headers: Headers | None = None,
188 extra_query: Query | None = None,
189 extra_body: Body | None = None,
190 timeout: float | httpx.Timeout | None | NotGiven = not_given,
191 ) -> ConversationDeletedResource:
192 """Delete a conversation.
193
194 Items in the conversation will not be deleted.
195
196 Args:
197 extra_headers: Send extra headers
198
199 extra_query: Add additional query parameters to the request
200
201 extra_body: Add additional JSON properties to the request
202
203 timeout: Override the client-level default timeout for this request, in seconds
204 """
205 if not conversation_id:
206 raise ValueError(f"Expected a non-empty value for `conversation_id` but received {conversation_id!r}")
207 return self._delete(
208 f"/conversations/{conversation_id}",
209 options=make_request_options(
210 extra_headers=extra_headers, extra_query=extra_query, extra_body=extra_body, timeout=timeout
211 ),
212 cast_to=ConversationDeletedResource,
213 )
214
215
216class AsyncConversations(AsyncAPIResource):
217 @cached_property
218 def items(self) -> AsyncItems:
219 return AsyncItems(self._client)
220
221 @cached_property
222 def with_raw_response(self) -> AsyncConversationsWithRawResponse:
223 """
224 This property can be used as a prefix for any HTTP method call to return
225 the raw response object instead of the parsed content.
226
227 For more information, see https://www.github.com/openai/openai-python#accessing-raw-response-data-eg-headers
228 """
229 return AsyncConversationsWithRawResponse(self)
230
231 @cached_property
232 def with_streaming_response(self) -> AsyncConversationsWithStreamingResponse:
233 """
234 An alternative to `.with_raw_response` that doesn't eagerly read the response body.
235
236 For more information, see https://www.github.com/openai/openai-python#with_streaming_response
237 """
238 return AsyncConversationsWithStreamingResponse(self)
239
240 async def create(
241 self,
242 *,
243 items: Optional[Iterable[ResponseInputItemParam]] | Omit = omit,
244 metadata: Optional[Metadata] | Omit = omit,
245 # Use the following arguments if you need to pass additional parameters to the API that aren't available via kwargs.
246 # The extra values given here take precedence over values defined on the client or passed to this method.
247 extra_headers: Headers | None = None,
248 extra_query: Query | None = None,
249 extra_body: Body | None = None,
250 timeout: float | httpx.Timeout | None | NotGiven = not_given,
251 ) -> Conversation:
252 """
253 Create a conversation.
254
255 Args:
256 items: Initial items to include in the conversation context. You may add up to 20 items
257 at a time.
258
259 metadata: Set of 16 key-value pairs that can be attached to an object. This can be useful
260 for storing additional information about the object in a structured format, and
261 querying for objects via API or the dashboard.
262
263 Keys are strings with a maximum length of 64 characters. Values are strings with
264 a maximum length of 512 characters.
265
266 extra_headers: Send extra headers
267
268 extra_query: Add additional query parameters to the request
269
270 extra_body: Add additional JSON properties to the request
271
272 timeout: Override the client-level default timeout for this request, in seconds
273 """
274 return await self._post(
275 "/conversations",
276 body=await async_maybe_transform(
277 {
278 "items": items,
279 "metadata": metadata,
280 },
281 conversation_create_params.ConversationCreateParams,
282 ),
283 options=make_request_options(
284 extra_headers=extra_headers, extra_query=extra_query, extra_body=extra_body, timeout=timeout
285 ),
286 cast_to=Conversation,
287 )
288
289 async def retrieve(
290 self,
291 conversation_id: str,
292 *,
293 # Use the following arguments if you need to pass additional parameters to the API that aren't available via kwargs.
294 # The extra values given here take precedence over values defined on the client or passed to this method.
295 extra_headers: Headers | None = None,
296 extra_query: Query | None = None,
297 extra_body: Body | None = None,
298 timeout: float | httpx.Timeout | None | NotGiven = not_given,
299 ) -> Conversation:
300 """
301 Get a conversation
302
303 Args:
304 extra_headers: Send extra headers
305
306 extra_query: Add additional query parameters to the request
307
308 extra_body: Add additional JSON properties to the request
309
310 timeout: Override the client-level default timeout for this request, in seconds
311 """
312 if not conversation_id:
313 raise ValueError(f"Expected a non-empty value for `conversation_id` but received {conversation_id!r}")
314 return await self._get(
315 f"/conversations/{conversation_id}",
316 options=make_request_options(
317 extra_headers=extra_headers, extra_query=extra_query, extra_body=extra_body, timeout=timeout
318 ),
319 cast_to=Conversation,
320 )
321
322 async def update(
323 self,
324 conversation_id: str,
325 *,
326 metadata: Optional[Metadata],
327 # Use the following arguments if you need to pass additional parameters to the API that aren't available via kwargs.
328 # The extra values given here take precedence over values defined on the client or passed to this method.
329 extra_headers: Headers | None = None,
330 extra_query: Query | None = None,
331 extra_body: Body | None = None,
332 timeout: float | httpx.Timeout | None | NotGiven = not_given,
333 ) -> Conversation:
334 """
335 Update a conversation
336
337 Args:
338 metadata: Set of 16 key-value pairs that can be attached to an object. This can be useful
339 for storing additional information about the object in a structured format, and
340 querying for objects via API or the dashboard.
341
342 Keys are strings with a maximum length of 64 characters. Values are strings with
343 a maximum length of 512 characters.
344
345 extra_headers: Send extra headers
346
347 extra_query: Add additional query parameters to the request
348
349 extra_body: Add additional JSON properties to the request
350
351 timeout: Override the client-level default timeout for this request, in seconds
352 """
353 if not conversation_id:
354 raise ValueError(f"Expected a non-empty value for `conversation_id` but received {conversation_id!r}")
355 return await self._post(
356 f"/conversations/{conversation_id}",
357 body=await async_maybe_transform(
358 {"metadata": metadata}, conversation_update_params.ConversationUpdateParams
359 ),
360 options=make_request_options(
361 extra_headers=extra_headers, extra_query=extra_query, extra_body=extra_body, timeout=timeout
362 ),
363 cast_to=Conversation,
364 )
365
366 async def delete(
367 self,
368 conversation_id: str,
369 *,
370 # Use the following arguments if you need to pass additional parameters to the API that aren't available via kwargs.
371 # The extra values given here take precedence over values defined on the client or passed to this method.
372 extra_headers: Headers | None = None,
373 extra_query: Query | None = None,
374 extra_body: Body | None = None,
375 timeout: float | httpx.Timeout | None | NotGiven = not_given,
376 ) -> ConversationDeletedResource:
377 """Delete a conversation.
378
379 Items in the conversation will not be deleted.
380
381 Args:
382 extra_headers: Send extra headers
383
384 extra_query: Add additional query parameters to the request
385
386 extra_body: Add additional JSON properties to the request
387
388 timeout: Override the client-level default timeout for this request, in seconds
389 """
390 if not conversation_id:
391 raise ValueError(f"Expected a non-empty value for `conversation_id` but received {conversation_id!r}")
392 return await self._delete(
393 f"/conversations/{conversation_id}",
394 options=make_request_options(
395 extra_headers=extra_headers, extra_query=extra_query, extra_body=extra_body, timeout=timeout
396 ),
397 cast_to=ConversationDeletedResource,
398 )
399
400
401class ConversationsWithRawResponse:
402 def __init__(self, conversations: Conversations) -> None:
403 self._conversations = conversations
404
405 self.create = _legacy_response.to_raw_response_wrapper(
406 conversations.create,
407 )
408 self.retrieve = _legacy_response.to_raw_response_wrapper(
409 conversations.retrieve,
410 )
411 self.update = _legacy_response.to_raw_response_wrapper(
412 conversations.update,
413 )
414 self.delete = _legacy_response.to_raw_response_wrapper(
415 conversations.delete,
416 )
417
418 @cached_property
419 def items(self) -> ItemsWithRawResponse:
420 return ItemsWithRawResponse(self._conversations.items)
421
422
423class AsyncConversationsWithRawResponse:
424 def __init__(self, conversations: AsyncConversations) -> None:
425 self._conversations = conversations
426
427 self.create = _legacy_response.async_to_raw_response_wrapper(
428 conversations.create,
429 )
430 self.retrieve = _legacy_response.async_to_raw_response_wrapper(
431 conversations.retrieve,
432 )
433 self.update = _legacy_response.async_to_raw_response_wrapper(
434 conversations.update,
435 )
436 self.delete = _legacy_response.async_to_raw_response_wrapper(
437 conversations.delete,
438 )
439
440 @cached_property
441 def items(self) -> AsyncItemsWithRawResponse:
442 return AsyncItemsWithRawResponse(self._conversations.items)
443
444
445class ConversationsWithStreamingResponse:
446 def __init__(self, conversations: Conversations) -> None:
447 self._conversations = conversations
448
449 self.create = to_streamed_response_wrapper(
450 conversations.create,
451 )
452 self.retrieve = to_streamed_response_wrapper(
453 conversations.retrieve,
454 )
455 self.update = to_streamed_response_wrapper(
456 conversations.update,
457 )
458 self.delete = to_streamed_response_wrapper(
459 conversations.delete,
460 )
461
462 @cached_property
463 def items(self) -> ItemsWithStreamingResponse:
464 return ItemsWithStreamingResponse(self._conversations.items)
465
466
467class AsyncConversationsWithStreamingResponse:
468 def __init__(self, conversations: AsyncConversations) -> None:
469 self._conversations = conversations
470
471 self.create = async_to_streamed_response_wrapper(
472 conversations.create,
473 )
474 self.retrieve = async_to_streamed_response_wrapper(
475 conversations.retrieve,
476 )
477 self.update = async_to_streamed_response_wrapper(
478 conversations.update,
479 )
480 self.delete = async_to_streamed_response_wrapper(
481 conversations.delete,
482 )
483
484 @cached_property
485 def items(self) -> AsyncItemsWithStreamingResponse:
486 return AsyncItemsWithStreamingResponse(self._conversations.items)