sessions.py 12 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301
  1. # File generated from our OpenAPI spec by Stainless. See CONTRIBUTING.md for details.
  2. from __future__ import annotations
  3. import httpx
  4. from .... import _legacy_response
  5. from ...._types import Body, Omit, Query, Headers, NotGiven, omit, not_given
  6. from ...._utils import maybe_transform, async_maybe_transform
  7. from ...._compat import cached_property
  8. from ...._resource import SyncAPIResource, AsyncAPIResource
  9. from ...._response import to_streamed_response_wrapper, async_to_streamed_response_wrapper
  10. from ...._base_client import make_request_options
  11. from ....types.beta.chatkit import (
  12. ChatSessionWorkflowParam,
  13. ChatSessionRateLimitsParam,
  14. ChatSessionExpiresAfterParam,
  15. ChatSessionChatKitConfigurationParam,
  16. session_create_params,
  17. )
  18. from ....types.beta.chatkit.chat_session import ChatSession
  19. from ....types.beta.chatkit.chat_session_workflow_param import ChatSessionWorkflowParam
  20. from ....types.beta.chatkit.chat_session_rate_limits_param import ChatSessionRateLimitsParam
  21. from ....types.beta.chatkit.chat_session_expires_after_param import ChatSessionExpiresAfterParam
  22. from ....types.beta.chatkit.chat_session_chatkit_configuration_param import ChatSessionChatKitConfigurationParam
  23. __all__ = ["Sessions", "AsyncSessions"]
  24. class Sessions(SyncAPIResource):
  25. @cached_property
  26. def with_raw_response(self) -> SessionsWithRawResponse:
  27. """
  28. This property can be used as a prefix for any HTTP method call to return
  29. the raw response object instead of the parsed content.
  30. For more information, see https://www.github.com/openai/openai-python#accessing-raw-response-data-eg-headers
  31. """
  32. return SessionsWithRawResponse(self)
  33. @cached_property
  34. def with_streaming_response(self) -> SessionsWithStreamingResponse:
  35. """
  36. An alternative to `.with_raw_response` that doesn't eagerly read the response body.
  37. For more information, see https://www.github.com/openai/openai-python#with_streaming_response
  38. """
  39. return SessionsWithStreamingResponse(self)
  40. def create(
  41. self,
  42. *,
  43. user: str,
  44. workflow: ChatSessionWorkflowParam,
  45. chatkit_configuration: ChatSessionChatKitConfigurationParam | Omit = omit,
  46. expires_after: ChatSessionExpiresAfterParam | Omit = omit,
  47. rate_limits: ChatSessionRateLimitsParam | Omit = omit,
  48. # Use the following arguments if you need to pass additional parameters to the API that aren't available via kwargs.
  49. # The extra values given here take precedence over values defined on the client or passed to this method.
  50. extra_headers: Headers | None = None,
  51. extra_query: Query | None = None,
  52. extra_body: Body | None = None,
  53. timeout: float | httpx.Timeout | None | NotGiven = not_given,
  54. ) -> ChatSession:
  55. """
  56. Create a ChatKit session
  57. Args:
  58. user: A free-form string that identifies your end user; ensures this Session can
  59. access other objects that have the same `user` scope.
  60. workflow: Workflow that powers the session.
  61. chatkit_configuration: Optional overrides for ChatKit runtime configuration features
  62. expires_after: Optional override for session expiration timing in seconds from creation.
  63. Defaults to 10 minutes.
  64. rate_limits: Optional override for per-minute request limits. When omitted, defaults to 10.
  65. extra_headers: Send extra headers
  66. extra_query: Add additional query parameters to the request
  67. extra_body: Add additional JSON properties to the request
  68. timeout: Override the client-level default timeout for this request, in seconds
  69. """
  70. extra_headers = {"OpenAI-Beta": "chatkit_beta=v1", **(extra_headers or {})}
  71. return self._post(
  72. "/chatkit/sessions",
  73. body=maybe_transform(
  74. {
  75. "user": user,
  76. "workflow": workflow,
  77. "chatkit_configuration": chatkit_configuration,
  78. "expires_after": expires_after,
  79. "rate_limits": rate_limits,
  80. },
  81. session_create_params.SessionCreateParams,
  82. ),
  83. options=make_request_options(
  84. extra_headers=extra_headers, extra_query=extra_query, extra_body=extra_body, timeout=timeout
  85. ),
  86. cast_to=ChatSession,
  87. )
  88. def cancel(
  89. self,
  90. session_id: str,
  91. *,
  92. # Use the following arguments if you need to pass additional parameters to the API that aren't available via kwargs.
  93. # The extra values given here take precedence over values defined on the client or passed to this method.
  94. extra_headers: Headers | None = None,
  95. extra_query: Query | None = None,
  96. extra_body: Body | None = None,
  97. timeout: float | httpx.Timeout | None | NotGiven = not_given,
  98. ) -> ChatSession:
  99. """
  100. Cancel a ChatKit session
  101. Args:
  102. extra_headers: Send extra headers
  103. extra_query: Add additional query parameters to the request
  104. extra_body: Add additional JSON properties to the request
  105. timeout: Override the client-level default timeout for this request, in seconds
  106. """
  107. if not session_id:
  108. raise ValueError(f"Expected a non-empty value for `session_id` but received {session_id!r}")
  109. extra_headers = {"OpenAI-Beta": "chatkit_beta=v1", **(extra_headers or {})}
  110. return self._post(
  111. f"/chatkit/sessions/{session_id}/cancel",
  112. options=make_request_options(
  113. extra_headers=extra_headers, extra_query=extra_query, extra_body=extra_body, timeout=timeout
  114. ),
  115. cast_to=ChatSession,
  116. )
  117. class AsyncSessions(AsyncAPIResource):
  118. @cached_property
  119. def with_raw_response(self) -> AsyncSessionsWithRawResponse:
  120. """
  121. This property can be used as a prefix for any HTTP method call to return
  122. the raw response object instead of the parsed content.
  123. For more information, see https://www.github.com/openai/openai-python#accessing-raw-response-data-eg-headers
  124. """
  125. return AsyncSessionsWithRawResponse(self)
  126. @cached_property
  127. def with_streaming_response(self) -> AsyncSessionsWithStreamingResponse:
  128. """
  129. An alternative to `.with_raw_response` that doesn't eagerly read the response body.
  130. For more information, see https://www.github.com/openai/openai-python#with_streaming_response
  131. """
  132. return AsyncSessionsWithStreamingResponse(self)
  133. async def create(
  134. self,
  135. *,
  136. user: str,
  137. workflow: ChatSessionWorkflowParam,
  138. chatkit_configuration: ChatSessionChatKitConfigurationParam | Omit = omit,
  139. expires_after: ChatSessionExpiresAfterParam | Omit = omit,
  140. rate_limits: ChatSessionRateLimitsParam | Omit = omit,
  141. # Use the following arguments if you need to pass additional parameters to the API that aren't available via kwargs.
  142. # The extra values given here take precedence over values defined on the client or passed to this method.
  143. extra_headers: Headers | None = None,
  144. extra_query: Query | None = None,
  145. extra_body: Body | None = None,
  146. timeout: float | httpx.Timeout | None | NotGiven = not_given,
  147. ) -> ChatSession:
  148. """
  149. Create a ChatKit session
  150. Args:
  151. user: A free-form string that identifies your end user; ensures this Session can
  152. access other objects that have the same `user` scope.
  153. workflow: Workflow that powers the session.
  154. chatkit_configuration: Optional overrides for ChatKit runtime configuration features
  155. expires_after: Optional override for session expiration timing in seconds from creation.
  156. Defaults to 10 minutes.
  157. rate_limits: Optional override for per-minute request limits. When omitted, defaults to 10.
  158. extra_headers: Send extra headers
  159. extra_query: Add additional query parameters to the request
  160. extra_body: Add additional JSON properties to the request
  161. timeout: Override the client-level default timeout for this request, in seconds
  162. """
  163. extra_headers = {"OpenAI-Beta": "chatkit_beta=v1", **(extra_headers or {})}
  164. return await self._post(
  165. "/chatkit/sessions",
  166. body=await async_maybe_transform(
  167. {
  168. "user": user,
  169. "workflow": workflow,
  170. "chatkit_configuration": chatkit_configuration,
  171. "expires_after": expires_after,
  172. "rate_limits": rate_limits,
  173. },
  174. session_create_params.SessionCreateParams,
  175. ),
  176. options=make_request_options(
  177. extra_headers=extra_headers, extra_query=extra_query, extra_body=extra_body, timeout=timeout
  178. ),
  179. cast_to=ChatSession,
  180. )
  181. async def cancel(
  182. self,
  183. session_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. ) -> ChatSession:
  192. """
  193. Cancel a ChatKit session
  194. Args:
  195. extra_headers: Send extra headers
  196. extra_query: Add additional query parameters to the request
  197. extra_body: Add additional JSON properties to the request
  198. timeout: Override the client-level default timeout for this request, in seconds
  199. """
  200. if not session_id:
  201. raise ValueError(f"Expected a non-empty value for `session_id` but received {session_id!r}")
  202. extra_headers = {"OpenAI-Beta": "chatkit_beta=v1", **(extra_headers or {})}
  203. return await self._post(
  204. f"/chatkit/sessions/{session_id}/cancel",
  205. options=make_request_options(
  206. extra_headers=extra_headers, extra_query=extra_query, extra_body=extra_body, timeout=timeout
  207. ),
  208. cast_to=ChatSession,
  209. )
  210. class SessionsWithRawResponse:
  211. def __init__(self, sessions: Sessions) -> None:
  212. self._sessions = sessions
  213. self.create = _legacy_response.to_raw_response_wrapper(
  214. sessions.create,
  215. )
  216. self.cancel = _legacy_response.to_raw_response_wrapper(
  217. sessions.cancel,
  218. )
  219. class AsyncSessionsWithRawResponse:
  220. def __init__(self, sessions: AsyncSessions) -> None:
  221. self._sessions = sessions
  222. self.create = _legacy_response.async_to_raw_response_wrapper(
  223. sessions.create,
  224. )
  225. self.cancel = _legacy_response.async_to_raw_response_wrapper(
  226. sessions.cancel,
  227. )
  228. class SessionsWithStreamingResponse:
  229. def __init__(self, sessions: Sessions) -> None:
  230. self._sessions = sessions
  231. self.create = to_streamed_response_wrapper(
  232. sessions.create,
  233. )
  234. self.cancel = to_streamed_response_wrapper(
  235. sessions.cancel,
  236. )
  237. class AsyncSessionsWithStreamingResponse:
  238. def __init__(self, sessions: AsyncSessions) -> None:
  239. self._sessions = sessions
  240. self.create = async_to_streamed_response_wrapper(
  241. sessions.create,
  242. )
  243. self.cancel = async_to_streamed_response_wrapper(
  244. sessions.cancel,
  245. )