conversations.py 19 KB

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