input_tokens.py 14 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309
  1. # File generated from our OpenAPI spec by Stainless. See CONTRIBUTING.md for details.
  2. from __future__ import annotations
  3. from typing import Union, Iterable, Optional
  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, async_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 ..._base_client import make_request_options
  13. from ...types.responses import input_token_count_params
  14. from ...types.responses.tool_param import ToolParam
  15. from ...types.shared_params.reasoning import Reasoning
  16. from ...types.responses.response_input_item_param import ResponseInputItemParam
  17. from ...types.responses.input_token_count_response import InputTokenCountResponse
  18. __all__ = ["InputTokens", "AsyncInputTokens"]
  19. class InputTokens(SyncAPIResource):
  20. @cached_property
  21. def with_raw_response(self) -> InputTokensWithRawResponse:
  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 InputTokensWithRawResponse(self)
  28. @cached_property
  29. def with_streaming_response(self) -> InputTokensWithStreamingResponse:
  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 InputTokensWithStreamingResponse(self)
  35. def count(
  36. self,
  37. *,
  38. conversation: Optional[input_token_count_params.Conversation] | Omit = omit,
  39. input: Union[str, Iterable[ResponseInputItemParam], None] | Omit = omit,
  40. instructions: Optional[str] | Omit = omit,
  41. model: Optional[str] | Omit = omit,
  42. parallel_tool_calls: Optional[bool] | Omit = omit,
  43. previous_response_id: Optional[str] | Omit = omit,
  44. reasoning: Optional[Reasoning] | Omit = omit,
  45. text: Optional[input_token_count_params.Text] | Omit = omit,
  46. tool_choice: Optional[input_token_count_params.ToolChoice] | Omit = omit,
  47. tools: Optional[Iterable[ToolParam]] | Omit = omit,
  48. truncation: Literal["auto", "disabled"] | Omit = omit,
  49. # Use the following arguments if you need to pass additional parameters to the API that aren't available via kwargs.
  50. # The extra values given here take precedence over values defined on the client or passed to this method.
  51. extra_headers: Headers | None = None,
  52. extra_query: Query | None = None,
  53. extra_body: Body | None = None,
  54. timeout: float | httpx.Timeout | None | NotGiven = not_given,
  55. ) -> InputTokenCountResponse:
  56. """
  57. Get input token counts
  58. Args:
  59. conversation: The conversation that this response belongs to. Items from this conversation are
  60. prepended to `input_items` for this response request. Input items and output
  61. items from this response are automatically added to this conversation after this
  62. response completes.
  63. input: Text, image, or file inputs to the model, used to generate a response
  64. instructions: A system (or developer) message inserted into the model's context. When used
  65. along with `previous_response_id`, the instructions from a previous response
  66. will not be carried over to the next response. This makes it simple to swap out
  67. system (or developer) messages in new responses.
  68. model: Model ID used to generate the response, like `gpt-4o` or `o3`. OpenAI offers a
  69. wide range of models with different capabilities, performance characteristics,
  70. and price points. Refer to the
  71. [model guide](https://platform.openai.com/docs/models) to browse and compare
  72. available models.
  73. parallel_tool_calls: Whether to allow the model to run tool calls in parallel.
  74. previous_response_id: The unique ID of the previous response to the model. Use this to create
  75. multi-turn conversations. Learn more about
  76. [conversation state](https://platform.openai.com/docs/guides/conversation-state).
  77. Cannot be used in conjunction with `conversation`.
  78. reasoning: **gpt-5 and o-series models only** Configuration options for
  79. [reasoning models](https://platform.openai.com/docs/guides/reasoning).
  80. text: Configuration options for a text response from the model. Can be plain text or
  81. structured JSON data. Learn more:
  82. - [Text inputs and outputs](https://platform.openai.com/docs/guides/text)
  83. - [Structured Outputs](https://platform.openai.com/docs/guides/structured-outputs)
  84. tool_choice: How the model should select which tool (or tools) to use when generating a
  85. response. See the `tools` parameter to see how to specify which tools the model
  86. can call.
  87. tools: An array of tools the model may call while generating a response. You can
  88. specify which tool to use by setting the `tool_choice` parameter.
  89. truncation: The truncation strategy to use for the model response. - `auto`: If the input to
  90. this Response exceeds the model's context window size, the model will truncate
  91. the response to fit the context window by dropping items from the beginning of
  92. the conversation. - `disabled` (default): If the input size will exceed the
  93. context window size for a model, the request will fail with a 400 error.
  94. extra_headers: Send extra headers
  95. extra_query: Add additional query parameters to the request
  96. extra_body: Add additional JSON properties to the request
  97. timeout: Override the client-level default timeout for this request, in seconds
  98. """
  99. return self._post(
  100. "/responses/input_tokens",
  101. body=maybe_transform(
  102. {
  103. "conversation": conversation,
  104. "input": input,
  105. "instructions": instructions,
  106. "model": model,
  107. "parallel_tool_calls": parallel_tool_calls,
  108. "previous_response_id": previous_response_id,
  109. "reasoning": reasoning,
  110. "text": text,
  111. "tool_choice": tool_choice,
  112. "tools": tools,
  113. "truncation": truncation,
  114. },
  115. input_token_count_params.InputTokenCountParams,
  116. ),
  117. options=make_request_options(
  118. extra_headers=extra_headers, extra_query=extra_query, extra_body=extra_body, timeout=timeout
  119. ),
  120. cast_to=InputTokenCountResponse,
  121. )
  122. class AsyncInputTokens(AsyncAPIResource):
  123. @cached_property
  124. def with_raw_response(self) -> AsyncInputTokensWithRawResponse:
  125. """
  126. This property can be used as a prefix for any HTTP method call to return
  127. the raw response object instead of the parsed content.
  128. For more information, see https://www.github.com/openai/openai-python#accessing-raw-response-data-eg-headers
  129. """
  130. return AsyncInputTokensWithRawResponse(self)
  131. @cached_property
  132. def with_streaming_response(self) -> AsyncInputTokensWithStreamingResponse:
  133. """
  134. An alternative to `.with_raw_response` that doesn't eagerly read the response body.
  135. For more information, see https://www.github.com/openai/openai-python#with_streaming_response
  136. """
  137. return AsyncInputTokensWithStreamingResponse(self)
  138. async def count(
  139. self,
  140. *,
  141. conversation: Optional[input_token_count_params.Conversation] | Omit = omit,
  142. input: Union[str, Iterable[ResponseInputItemParam], None] | Omit = omit,
  143. instructions: Optional[str] | Omit = omit,
  144. model: Optional[str] | Omit = omit,
  145. parallel_tool_calls: Optional[bool] | Omit = omit,
  146. previous_response_id: Optional[str] | Omit = omit,
  147. reasoning: Optional[Reasoning] | Omit = omit,
  148. text: Optional[input_token_count_params.Text] | Omit = omit,
  149. tool_choice: Optional[input_token_count_params.ToolChoice] | Omit = omit,
  150. tools: Optional[Iterable[ToolParam]] | Omit = omit,
  151. truncation: Literal["auto", "disabled"] | Omit = omit,
  152. # Use the following arguments if you need to pass additional parameters to the API that aren't available via kwargs.
  153. # The extra values given here take precedence over values defined on the client or passed to this method.
  154. extra_headers: Headers | None = None,
  155. extra_query: Query | None = None,
  156. extra_body: Body | None = None,
  157. timeout: float | httpx.Timeout | None | NotGiven = not_given,
  158. ) -> InputTokenCountResponse:
  159. """
  160. Get input token counts
  161. Args:
  162. conversation: The conversation that this response belongs to. Items from this conversation are
  163. prepended to `input_items` for this response request. Input items and output
  164. items from this response are automatically added to this conversation after this
  165. response completes.
  166. input: Text, image, or file inputs to the model, used to generate a response
  167. instructions: A system (or developer) message inserted into the model's context. When used
  168. along with `previous_response_id`, the instructions from a previous response
  169. will not be carried over to the next response. This makes it simple to swap out
  170. system (or developer) messages in new responses.
  171. model: Model ID used to generate the response, like `gpt-4o` or `o3`. OpenAI offers a
  172. wide range of models with different capabilities, performance characteristics,
  173. and price points. Refer to the
  174. [model guide](https://platform.openai.com/docs/models) to browse and compare
  175. available models.
  176. parallel_tool_calls: Whether to allow the model to run tool calls in parallel.
  177. previous_response_id: The unique ID of the previous response to the model. Use this to create
  178. multi-turn conversations. Learn more about
  179. [conversation state](https://platform.openai.com/docs/guides/conversation-state).
  180. Cannot be used in conjunction with `conversation`.
  181. reasoning: **gpt-5 and o-series models only** Configuration options for
  182. [reasoning models](https://platform.openai.com/docs/guides/reasoning).
  183. text: Configuration options for a text response from the model. Can be plain text or
  184. structured JSON data. Learn more:
  185. - [Text inputs and outputs](https://platform.openai.com/docs/guides/text)
  186. - [Structured Outputs](https://platform.openai.com/docs/guides/structured-outputs)
  187. tool_choice: How the model should select which tool (or tools) to use when generating a
  188. response. See the `tools` parameter to see how to specify which tools the model
  189. can call.
  190. tools: An array of tools the model may call while generating a response. You can
  191. specify which tool to use by setting the `tool_choice` parameter.
  192. truncation: The truncation strategy to use for the model response. - `auto`: If the input to
  193. this Response exceeds the model's context window size, the model will truncate
  194. the response to fit the context window by dropping items from the beginning of
  195. the conversation. - `disabled` (default): If the input size will exceed the
  196. context window size for a model, the request will fail with a 400 error.
  197. extra_headers: Send extra headers
  198. extra_query: Add additional query parameters to the request
  199. extra_body: Add additional JSON properties to the request
  200. timeout: Override the client-level default timeout for this request, in seconds
  201. """
  202. return await self._post(
  203. "/responses/input_tokens",
  204. body=await async_maybe_transform(
  205. {
  206. "conversation": conversation,
  207. "input": input,
  208. "instructions": instructions,
  209. "model": model,
  210. "parallel_tool_calls": parallel_tool_calls,
  211. "previous_response_id": previous_response_id,
  212. "reasoning": reasoning,
  213. "text": text,
  214. "tool_choice": tool_choice,
  215. "tools": tools,
  216. "truncation": truncation,
  217. },
  218. input_token_count_params.InputTokenCountParams,
  219. ),
  220. options=make_request_options(
  221. extra_headers=extra_headers, extra_query=extra_query, extra_body=extra_body, timeout=timeout
  222. ),
  223. cast_to=InputTokenCountResponse,
  224. )
  225. class InputTokensWithRawResponse:
  226. def __init__(self, input_tokens: InputTokens) -> None:
  227. self._input_tokens = input_tokens
  228. self.count = _legacy_response.to_raw_response_wrapper(
  229. input_tokens.count,
  230. )
  231. class AsyncInputTokensWithRawResponse:
  232. def __init__(self, input_tokens: AsyncInputTokens) -> None:
  233. self._input_tokens = input_tokens
  234. self.count = _legacy_response.async_to_raw_response_wrapper(
  235. input_tokens.count,
  236. )
  237. class InputTokensWithStreamingResponse:
  238. def __init__(self, input_tokens: InputTokens) -> None:
  239. self._input_tokens = input_tokens
  240. self.count = to_streamed_response_wrapper(
  241. input_tokens.count,
  242. )
  243. class AsyncInputTokensWithStreamingResponse:
  244. def __init__(self, input_tokens: AsyncInputTokens) -> None:
  245. self._input_tokens = input_tokens
  246. self.count = async_to_streamed_response_wrapper(
  247. input_tokens.count,
  248. )