threads.py 20 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521
  1. # File generated from our OpenAPI spec by Stainless. See CONTRIBUTING.md for details.
  2. from __future__ import annotations
  3. from typing import Any, cast
  4. from typing_extensions import Literal
  5. import httpx
  6. from .... import _legacy_response
  7. from ...._types import Body, Omit, Query, Headers, NotGiven, omit, not_given
  8. from ...._utils import maybe_transform
  9. from ...._compat import cached_property
  10. from ...._resource import SyncAPIResource, AsyncAPIResource
  11. from ...._response import to_streamed_response_wrapper, async_to_streamed_response_wrapper
  12. from ....pagination import SyncConversationCursorPage, AsyncConversationCursorPage
  13. from ...._base_client import AsyncPaginator, make_request_options
  14. from ....types.beta.chatkit import thread_list_params, thread_list_items_params
  15. from ....types.beta.chatkit.chatkit_thread import ChatKitThread
  16. from ....types.beta.chatkit.thread_delete_response import ThreadDeleteResponse
  17. from ....types.beta.chatkit.chatkit_thread_item_list import Data
  18. __all__ = ["Threads", "AsyncThreads"]
  19. class Threads(SyncAPIResource):
  20. @cached_property
  21. def with_raw_response(self) -> ThreadsWithRawResponse:
  22. """
  23. This property can be used as a prefix for any HTTP method call to return
  24. the raw response object instead of the parsed content.
  25. For more information, see https://www.github.com/openai/openai-python#accessing-raw-response-data-eg-headers
  26. """
  27. return ThreadsWithRawResponse(self)
  28. @cached_property
  29. def with_streaming_response(self) -> ThreadsWithStreamingResponse:
  30. """
  31. An alternative to `.with_raw_response` that doesn't eagerly read the response body.
  32. For more information, see https://www.github.com/openai/openai-python#with_streaming_response
  33. """
  34. return ThreadsWithStreamingResponse(self)
  35. def retrieve(
  36. self,
  37. thread_id: str,
  38. *,
  39. # Use the following arguments if you need to pass additional parameters to the API that aren't available via kwargs.
  40. # The extra values given here take precedence over values defined on the client or passed to this method.
  41. extra_headers: Headers | None = None,
  42. extra_query: Query | None = None,
  43. extra_body: Body | None = None,
  44. timeout: float | httpx.Timeout | None | NotGiven = not_given,
  45. ) -> ChatKitThread:
  46. """
  47. Retrieve a ChatKit thread
  48. Args:
  49. extra_headers: Send extra headers
  50. extra_query: Add additional query parameters to the request
  51. extra_body: Add additional JSON properties to the request
  52. timeout: Override the client-level default timeout for this request, in seconds
  53. """
  54. if not thread_id:
  55. raise ValueError(f"Expected a non-empty value for `thread_id` but received {thread_id!r}")
  56. extra_headers = {"OpenAI-Beta": "chatkit_beta=v1", **(extra_headers or {})}
  57. return self._get(
  58. f"/chatkit/threads/{thread_id}",
  59. options=make_request_options(
  60. extra_headers=extra_headers, extra_query=extra_query, extra_body=extra_body, timeout=timeout
  61. ),
  62. cast_to=ChatKitThread,
  63. )
  64. def list(
  65. self,
  66. *,
  67. after: str | Omit = omit,
  68. before: str | Omit = omit,
  69. limit: int | Omit = omit,
  70. order: Literal["asc", "desc"] | Omit = omit,
  71. user: str | Omit = omit,
  72. # Use the following arguments if you need to pass additional parameters to the API that aren't available via kwargs.
  73. # The extra values given here take precedence over values defined on the client or passed to this method.
  74. extra_headers: Headers | None = None,
  75. extra_query: Query | None = None,
  76. extra_body: Body | None = None,
  77. timeout: float | httpx.Timeout | None | NotGiven = not_given,
  78. ) -> SyncConversationCursorPage[ChatKitThread]:
  79. """
  80. List ChatKit threads
  81. Args:
  82. after: List items created after this thread item ID. Defaults to null for the first
  83. page.
  84. before: List items created before this thread item ID. Defaults to null for the newest
  85. results.
  86. limit: Maximum number of thread items to return. Defaults to 20.
  87. order: Sort order for results by creation time. Defaults to `desc`.
  88. user: Filter threads that belong to this user identifier. Defaults to null to return
  89. all users.
  90. extra_headers: Send extra headers
  91. extra_query: Add additional query parameters to the request
  92. extra_body: Add additional JSON properties to the request
  93. timeout: Override the client-level default timeout for this request, in seconds
  94. """
  95. extra_headers = {"OpenAI-Beta": "chatkit_beta=v1", **(extra_headers or {})}
  96. return self._get_api_list(
  97. "/chatkit/threads",
  98. page=SyncConversationCursorPage[ChatKitThread],
  99. options=make_request_options(
  100. extra_headers=extra_headers,
  101. extra_query=extra_query,
  102. extra_body=extra_body,
  103. timeout=timeout,
  104. query=maybe_transform(
  105. {
  106. "after": after,
  107. "before": before,
  108. "limit": limit,
  109. "order": order,
  110. "user": user,
  111. },
  112. thread_list_params.ThreadListParams,
  113. ),
  114. ),
  115. model=ChatKitThread,
  116. )
  117. def delete(
  118. self,
  119. thread_id: str,
  120. *,
  121. # Use the following arguments if you need to pass additional parameters to the API that aren't available via kwargs.
  122. # The extra values given here take precedence over values defined on the client or passed to this method.
  123. extra_headers: Headers | None = None,
  124. extra_query: Query | None = None,
  125. extra_body: Body | None = None,
  126. timeout: float | httpx.Timeout | None | NotGiven = not_given,
  127. ) -> ThreadDeleteResponse:
  128. """
  129. Delete a ChatKit thread
  130. Args:
  131. extra_headers: Send extra headers
  132. extra_query: Add additional query parameters to the request
  133. extra_body: Add additional JSON properties to the request
  134. timeout: Override the client-level default timeout for this request, in seconds
  135. """
  136. if not thread_id:
  137. raise ValueError(f"Expected a non-empty value for `thread_id` but received {thread_id!r}")
  138. extra_headers = {"OpenAI-Beta": "chatkit_beta=v1", **(extra_headers or {})}
  139. return self._delete(
  140. f"/chatkit/threads/{thread_id}",
  141. options=make_request_options(
  142. extra_headers=extra_headers, extra_query=extra_query, extra_body=extra_body, timeout=timeout
  143. ),
  144. cast_to=ThreadDeleteResponse,
  145. )
  146. def list_items(
  147. self,
  148. thread_id: str,
  149. *,
  150. after: str | Omit = omit,
  151. before: str | Omit = omit,
  152. limit: int | Omit = omit,
  153. order: Literal["asc", "desc"] | Omit = omit,
  154. # Use the following arguments if you need to pass additional parameters to the API that aren't available via kwargs.
  155. # The extra values given here take precedence over values defined on the client or passed to this method.
  156. extra_headers: Headers | None = None,
  157. extra_query: Query | None = None,
  158. extra_body: Body | None = None,
  159. timeout: float | httpx.Timeout | None | NotGiven = not_given,
  160. ) -> SyncConversationCursorPage[Data]:
  161. """
  162. List ChatKit thread items
  163. Args:
  164. after: List items created after this thread item ID. Defaults to null for the first
  165. page.
  166. before: List items created before this thread item ID. Defaults to null for the newest
  167. results.
  168. limit: Maximum number of thread items to return. Defaults to 20.
  169. order: Sort order for results by creation time. Defaults to `desc`.
  170. extra_headers: Send extra headers
  171. extra_query: Add additional query parameters to the request
  172. extra_body: Add additional JSON properties to the request
  173. timeout: Override the client-level default timeout for this request, in seconds
  174. """
  175. if not thread_id:
  176. raise ValueError(f"Expected a non-empty value for `thread_id` but received {thread_id!r}")
  177. extra_headers = {"OpenAI-Beta": "chatkit_beta=v1", **(extra_headers or {})}
  178. return self._get_api_list(
  179. f"/chatkit/threads/{thread_id}/items",
  180. page=SyncConversationCursorPage[Data],
  181. options=make_request_options(
  182. extra_headers=extra_headers,
  183. extra_query=extra_query,
  184. extra_body=extra_body,
  185. timeout=timeout,
  186. query=maybe_transform(
  187. {
  188. "after": after,
  189. "before": before,
  190. "limit": limit,
  191. "order": order,
  192. },
  193. thread_list_items_params.ThreadListItemsParams,
  194. ),
  195. ),
  196. model=cast(Any, Data), # Union types cannot be passed in as arguments in the type system
  197. )
  198. class AsyncThreads(AsyncAPIResource):
  199. @cached_property
  200. def with_raw_response(self) -> AsyncThreadsWithRawResponse:
  201. """
  202. This property can be used as a prefix for any HTTP method call to return
  203. the raw response object instead of the parsed content.
  204. For more information, see https://www.github.com/openai/openai-python#accessing-raw-response-data-eg-headers
  205. """
  206. return AsyncThreadsWithRawResponse(self)
  207. @cached_property
  208. def with_streaming_response(self) -> AsyncThreadsWithStreamingResponse:
  209. """
  210. An alternative to `.with_raw_response` that doesn't eagerly read the response body.
  211. For more information, see https://www.github.com/openai/openai-python#with_streaming_response
  212. """
  213. return AsyncThreadsWithStreamingResponse(self)
  214. async def retrieve(
  215. self,
  216. thread_id: str,
  217. *,
  218. # Use the following arguments if you need to pass additional parameters to the API that aren't available via kwargs.
  219. # The extra values given here take precedence over values defined on the client or passed to this method.
  220. extra_headers: Headers | None = None,
  221. extra_query: Query | None = None,
  222. extra_body: Body | None = None,
  223. timeout: float | httpx.Timeout | None | NotGiven = not_given,
  224. ) -> ChatKitThread:
  225. """
  226. Retrieve a ChatKit thread
  227. Args:
  228. extra_headers: Send extra headers
  229. extra_query: Add additional query parameters to the request
  230. extra_body: Add additional JSON properties to the request
  231. timeout: Override the client-level default timeout for this request, in seconds
  232. """
  233. if not thread_id:
  234. raise ValueError(f"Expected a non-empty value for `thread_id` but received {thread_id!r}")
  235. extra_headers = {"OpenAI-Beta": "chatkit_beta=v1", **(extra_headers or {})}
  236. return await self._get(
  237. f"/chatkit/threads/{thread_id}",
  238. options=make_request_options(
  239. extra_headers=extra_headers, extra_query=extra_query, extra_body=extra_body, timeout=timeout
  240. ),
  241. cast_to=ChatKitThread,
  242. )
  243. def list(
  244. self,
  245. *,
  246. after: str | Omit = omit,
  247. before: str | Omit = omit,
  248. limit: int | Omit = omit,
  249. order: Literal["asc", "desc"] | Omit = omit,
  250. user: str | Omit = omit,
  251. # Use the following arguments if you need to pass additional parameters to the API that aren't available via kwargs.
  252. # The extra values given here take precedence over values defined on the client or passed to this method.
  253. extra_headers: Headers | None = None,
  254. extra_query: Query | None = None,
  255. extra_body: Body | None = None,
  256. timeout: float | httpx.Timeout | None | NotGiven = not_given,
  257. ) -> AsyncPaginator[ChatKitThread, AsyncConversationCursorPage[ChatKitThread]]:
  258. """
  259. List ChatKit threads
  260. Args:
  261. after: List items created after this thread item ID. Defaults to null for the first
  262. page.
  263. before: List items created before this thread item ID. Defaults to null for the newest
  264. results.
  265. limit: Maximum number of thread items to return. Defaults to 20.
  266. order: Sort order for results by creation time. Defaults to `desc`.
  267. user: Filter threads that belong to this user identifier. Defaults to null to return
  268. all users.
  269. extra_headers: Send extra headers
  270. extra_query: Add additional query parameters to the request
  271. extra_body: Add additional JSON properties to the request
  272. timeout: Override the client-level default timeout for this request, in seconds
  273. """
  274. extra_headers = {"OpenAI-Beta": "chatkit_beta=v1", **(extra_headers or {})}
  275. return self._get_api_list(
  276. "/chatkit/threads",
  277. page=AsyncConversationCursorPage[ChatKitThread],
  278. options=make_request_options(
  279. extra_headers=extra_headers,
  280. extra_query=extra_query,
  281. extra_body=extra_body,
  282. timeout=timeout,
  283. query=maybe_transform(
  284. {
  285. "after": after,
  286. "before": before,
  287. "limit": limit,
  288. "order": order,
  289. "user": user,
  290. },
  291. thread_list_params.ThreadListParams,
  292. ),
  293. ),
  294. model=ChatKitThread,
  295. )
  296. async def delete(
  297. self,
  298. thread_id: str,
  299. *,
  300. # Use the following arguments if you need to pass additional parameters to the API that aren't available via kwargs.
  301. # The extra values given here take precedence over values defined on the client or passed to this method.
  302. extra_headers: Headers | None = None,
  303. extra_query: Query | None = None,
  304. extra_body: Body | None = None,
  305. timeout: float | httpx.Timeout | None | NotGiven = not_given,
  306. ) -> ThreadDeleteResponse:
  307. """
  308. Delete a ChatKit thread
  309. Args:
  310. extra_headers: Send extra headers
  311. extra_query: Add additional query parameters to the request
  312. extra_body: Add additional JSON properties to the request
  313. timeout: Override the client-level default timeout for this request, in seconds
  314. """
  315. if not thread_id:
  316. raise ValueError(f"Expected a non-empty value for `thread_id` but received {thread_id!r}")
  317. extra_headers = {"OpenAI-Beta": "chatkit_beta=v1", **(extra_headers or {})}
  318. return await self._delete(
  319. f"/chatkit/threads/{thread_id}",
  320. options=make_request_options(
  321. extra_headers=extra_headers, extra_query=extra_query, extra_body=extra_body, timeout=timeout
  322. ),
  323. cast_to=ThreadDeleteResponse,
  324. )
  325. def list_items(
  326. self,
  327. thread_id: str,
  328. *,
  329. after: str | Omit = omit,
  330. before: str | Omit = omit,
  331. limit: int | Omit = omit,
  332. order: Literal["asc", "desc"] | Omit = omit,
  333. # Use the following arguments if you need to pass additional parameters to the API that aren't available via kwargs.
  334. # The extra values given here take precedence over values defined on the client or passed to this method.
  335. extra_headers: Headers | None = None,
  336. extra_query: Query | None = None,
  337. extra_body: Body | None = None,
  338. timeout: float | httpx.Timeout | None | NotGiven = not_given,
  339. ) -> AsyncPaginator[Data, AsyncConversationCursorPage[Data]]:
  340. """
  341. List ChatKit thread items
  342. Args:
  343. after: List items created after this thread item ID. Defaults to null for the first
  344. page.
  345. before: List items created before this thread item ID. Defaults to null for the newest
  346. results.
  347. limit: Maximum number of thread items to return. Defaults to 20.
  348. order: Sort order for results by creation time. Defaults to `desc`.
  349. extra_headers: Send extra headers
  350. extra_query: Add additional query parameters to the request
  351. extra_body: Add additional JSON properties to the request
  352. timeout: Override the client-level default timeout for this request, in seconds
  353. """
  354. if not thread_id:
  355. raise ValueError(f"Expected a non-empty value for `thread_id` but received {thread_id!r}")
  356. extra_headers = {"OpenAI-Beta": "chatkit_beta=v1", **(extra_headers or {})}
  357. return self._get_api_list(
  358. f"/chatkit/threads/{thread_id}/items",
  359. page=AsyncConversationCursorPage[Data],
  360. options=make_request_options(
  361. extra_headers=extra_headers,
  362. extra_query=extra_query,
  363. extra_body=extra_body,
  364. timeout=timeout,
  365. query=maybe_transform(
  366. {
  367. "after": after,
  368. "before": before,
  369. "limit": limit,
  370. "order": order,
  371. },
  372. thread_list_items_params.ThreadListItemsParams,
  373. ),
  374. ),
  375. model=cast(Any, Data), # Union types cannot be passed in as arguments in the type system
  376. )
  377. class ThreadsWithRawResponse:
  378. def __init__(self, threads: Threads) -> None:
  379. self._threads = threads
  380. self.retrieve = _legacy_response.to_raw_response_wrapper(
  381. threads.retrieve,
  382. )
  383. self.list = _legacy_response.to_raw_response_wrapper(
  384. threads.list,
  385. )
  386. self.delete = _legacy_response.to_raw_response_wrapper(
  387. threads.delete,
  388. )
  389. self.list_items = _legacy_response.to_raw_response_wrapper(
  390. threads.list_items,
  391. )
  392. class AsyncThreadsWithRawResponse:
  393. def __init__(self, threads: AsyncThreads) -> None:
  394. self._threads = threads
  395. self.retrieve = _legacy_response.async_to_raw_response_wrapper(
  396. threads.retrieve,
  397. )
  398. self.list = _legacy_response.async_to_raw_response_wrapper(
  399. threads.list,
  400. )
  401. self.delete = _legacy_response.async_to_raw_response_wrapper(
  402. threads.delete,
  403. )
  404. self.list_items = _legacy_response.async_to_raw_response_wrapper(
  405. threads.list_items,
  406. )
  407. class ThreadsWithStreamingResponse:
  408. def __init__(self, threads: Threads) -> None:
  409. self._threads = threads
  410. self.retrieve = to_streamed_response_wrapper(
  411. threads.retrieve,
  412. )
  413. self.list = to_streamed_response_wrapper(
  414. threads.list,
  415. )
  416. self.delete = to_streamed_response_wrapper(
  417. threads.delete,
  418. )
  419. self.list_items = to_streamed_response_wrapper(
  420. threads.list_items,
  421. )
  422. class AsyncThreadsWithStreamingResponse:
  423. def __init__(self, threads: AsyncThreads) -> None:
  424. self._threads = threads
  425. self.retrieve = async_to_streamed_response_wrapper(
  426. threads.retrieve,
  427. )
  428. self.list = async_to_streamed_response_wrapper(
  429. threads.list,
  430. )
  431. self.delete = async_to_streamed_response_wrapper(
  432. threads.delete,
  433. )
  434. self.list_items = async_to_streamed_response_wrapper(
  435. threads.list_items,
  436. )