moderations.py 7.6 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197
  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
  4. import httpx
  5. from .. import _legacy_response
  6. from ..types import moderation_create_params
  7. from .._types import Body, Omit, Query, Headers, NotGiven, SequenceNotStr, 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.moderation_model import ModerationModel
  14. from ..types.moderation_create_response import ModerationCreateResponse
  15. from ..types.moderation_multi_modal_input_param import ModerationMultiModalInputParam
  16. __all__ = ["Moderations", "AsyncModerations"]
  17. class Moderations(SyncAPIResource):
  18. @cached_property
  19. def with_raw_response(self) -> ModerationsWithRawResponse:
  20. """
  21. This property can be used as a prefix for any HTTP method call to return
  22. the raw response object instead of the parsed content.
  23. For more information, see https://www.github.com/openai/openai-python#accessing-raw-response-data-eg-headers
  24. """
  25. return ModerationsWithRawResponse(self)
  26. @cached_property
  27. def with_streaming_response(self) -> ModerationsWithStreamingResponse:
  28. """
  29. An alternative to `.with_raw_response` that doesn't eagerly read the response body.
  30. For more information, see https://www.github.com/openai/openai-python#with_streaming_response
  31. """
  32. return ModerationsWithStreamingResponse(self)
  33. def create(
  34. self,
  35. *,
  36. input: Union[str, SequenceNotStr[str], Iterable[ModerationMultiModalInputParam]],
  37. model: Union[str, ModerationModel] | Omit = omit,
  38. # Use the following arguments if you need to pass additional parameters to the API that aren't available via kwargs.
  39. # The extra values given here take precedence over values defined on the client or passed to this method.
  40. extra_headers: Headers | None = None,
  41. extra_query: Query | None = None,
  42. extra_body: Body | None = None,
  43. timeout: float | httpx.Timeout | None | NotGiven = not_given,
  44. ) -> ModerationCreateResponse:
  45. """Classifies if text and/or image inputs are potentially harmful.
  46. Learn more in
  47. the [moderation guide](https://platform.openai.com/docs/guides/moderation).
  48. Args:
  49. input: Input (or inputs) to classify. Can be a single string, an array of strings, or
  50. an array of multi-modal input objects similar to other models.
  51. model: The content moderation model you would like to use. Learn more in
  52. [the moderation guide](https://platform.openai.com/docs/guides/moderation), and
  53. learn about available models
  54. [here](https://platform.openai.com/docs/models#moderation).
  55. extra_headers: Send extra headers
  56. extra_query: Add additional query parameters to the request
  57. extra_body: Add additional JSON properties to the request
  58. timeout: Override the client-level default timeout for this request, in seconds
  59. """
  60. return self._post(
  61. "/moderations",
  62. body=maybe_transform(
  63. {
  64. "input": input,
  65. "model": model,
  66. },
  67. moderation_create_params.ModerationCreateParams,
  68. ),
  69. options=make_request_options(
  70. extra_headers=extra_headers, extra_query=extra_query, extra_body=extra_body, timeout=timeout
  71. ),
  72. cast_to=ModerationCreateResponse,
  73. )
  74. class AsyncModerations(AsyncAPIResource):
  75. @cached_property
  76. def with_raw_response(self) -> AsyncModerationsWithRawResponse:
  77. """
  78. This property can be used as a prefix for any HTTP method call to return
  79. the raw response object instead of the parsed content.
  80. For more information, see https://www.github.com/openai/openai-python#accessing-raw-response-data-eg-headers
  81. """
  82. return AsyncModerationsWithRawResponse(self)
  83. @cached_property
  84. def with_streaming_response(self) -> AsyncModerationsWithStreamingResponse:
  85. """
  86. An alternative to `.with_raw_response` that doesn't eagerly read the response body.
  87. For more information, see https://www.github.com/openai/openai-python#with_streaming_response
  88. """
  89. return AsyncModerationsWithStreamingResponse(self)
  90. async def create(
  91. self,
  92. *,
  93. input: Union[str, SequenceNotStr[str], Iterable[ModerationMultiModalInputParam]],
  94. model: Union[str, ModerationModel] | Omit = omit,
  95. # Use the following arguments if you need to pass additional parameters to the API that aren't available via kwargs.
  96. # The extra values given here take precedence over values defined on the client or passed to this method.
  97. extra_headers: Headers | None = None,
  98. extra_query: Query | None = None,
  99. extra_body: Body | None = None,
  100. timeout: float | httpx.Timeout | None | NotGiven = not_given,
  101. ) -> ModerationCreateResponse:
  102. """Classifies if text and/or image inputs are potentially harmful.
  103. Learn more in
  104. the [moderation guide](https://platform.openai.com/docs/guides/moderation).
  105. Args:
  106. input: Input (or inputs) to classify. Can be a single string, an array of strings, or
  107. an array of multi-modal input objects similar to other models.
  108. model: The content moderation model you would like to use. Learn more in
  109. [the moderation guide](https://platform.openai.com/docs/guides/moderation), and
  110. learn about available models
  111. [here](https://platform.openai.com/docs/models#moderation).
  112. extra_headers: Send extra headers
  113. extra_query: Add additional query parameters to the request
  114. extra_body: Add additional JSON properties to the request
  115. timeout: Override the client-level default timeout for this request, in seconds
  116. """
  117. return await self._post(
  118. "/moderations",
  119. body=await async_maybe_transform(
  120. {
  121. "input": input,
  122. "model": model,
  123. },
  124. moderation_create_params.ModerationCreateParams,
  125. ),
  126. options=make_request_options(
  127. extra_headers=extra_headers, extra_query=extra_query, extra_body=extra_body, timeout=timeout
  128. ),
  129. cast_to=ModerationCreateResponse,
  130. )
  131. class ModerationsWithRawResponse:
  132. def __init__(self, moderations: Moderations) -> None:
  133. self._moderations = moderations
  134. self.create = _legacy_response.to_raw_response_wrapper(
  135. moderations.create,
  136. )
  137. class AsyncModerationsWithRawResponse:
  138. def __init__(self, moderations: AsyncModerations) -> None:
  139. self._moderations = moderations
  140. self.create = _legacy_response.async_to_raw_response_wrapper(
  141. moderations.create,
  142. )
  143. class ModerationsWithStreamingResponse:
  144. def __init__(self, moderations: Moderations) -> None:
  145. self._moderations = moderations
  146. self.create = to_streamed_response_wrapper(
  147. moderations.create,
  148. )
  149. class AsyncModerationsWithStreamingResponse:
  150. def __init__(self, moderations: AsyncModerations) -> None:
  151. self._moderations = moderations
  152. self.create = async_to_streamed_response_wrapper(
  153. moderations.create,
  154. )