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