pyrit.prompt_target.OpenAIResponseTarget#
- class OpenAIResponseTarget(*, api_version: str | None = '2025-03-01-preview', max_output_tokens: int | None = None, temperature: float | None = None, top_p: float | None = None, extra_body_parameters: dict[str, Any] | None = None, **kwargs)[source]#
Bases:
OpenAIChatTargetBase
This class enables communication with endpoints that support the OpenAI Response API.
This works with models such as o1, o3, and o4-mini. Depending on the endpoint this allows for a variety of inputs, outputs, and tool calls. For more information, see the OpenAI Response API documentation: https://platform.openai.com/docs/api-reference/responses/create
- __init__(*, api_version: str | None = '2025-03-01-preview', max_output_tokens: int | None = None, temperature: float | None = None, top_p: float | None = None, extra_body_parameters: dict[str, Any] | None = None, **kwargs)[source]#
Initializes the OpenAIResponseTarget with the provided parameters.
- Parameters:
model_name (str, Optional) – The name of the model.
endpoint (str, Optional) – The target URL for the OpenAI service.
api_key (str, Optional) – The API key for accessing the Azure OpenAI service. Defaults to the OPENAI_RESPONSES_KEY environment variable.
headers (str, Optional) – Headers of the endpoint (JSON).
api_version (str, Optional) – The version of the Azure OpenAI API. Defaults to “2025-03-01-preview”.
max_requests_per_minute (int, Optional) – Number of requests the target can handle per minute before hitting a rate limit. The number of requests sent to the target will be capped at the value provided.
max_output_tokens (int, Optional) – The maximum number of tokens that can be generated in the response. This value can be used to control costs for text generated via API.
temperature (float, Optional) – The temperature parameter for controlling the randomness of the response.
top_p (float, Optional) – The top-p parameter for controlling the diversity of the response.
is_json_supported (bool, Optional) – If True, the target will support formatting responses as JSON by setting the response_format header. Official OpenAI models all support this, but if you are using this target with different models, is_json_supported should be set correctly to avoid issues when using adversarial infrastructure (e.g. Crescendo scorers will set this flag).
extra_body_parameters (dict, Optional) – Additional parameters to be included in the request body.
httpx_client_kwargs (dict, Optional) – Additional kwargs to be passed to the httpx.AsyncClient() constructor. For example, to specify a 3 minute timeout: httpx_client_kwargs={“timeout”: 180}
- Raises:
PyritException – If the temperature or top_p values are out of bounds.
ValueError – If the temperature is not between 0 and 2 (inclusive).
ValueError – If the top_p is not between 0 and 1 (inclusive).
ValueError – If both max_output_tokens and max_tokens are provided.
RateLimitException – If the target is rate-limited.
httpx.HTTPStatusError – If the request fails with a 400 Bad Request or 429 Too Many Requests error.
json.JSONDecodeError – If the response from the target is not valid JSON.
Exception – If the request fails for any other reason.
Methods
__init__
(*[, api_version, ...])Initializes the OpenAIResponseTarget with the provided parameters.
dispose_db_engine
()Dispose DuckDB database engine to release database connections and resources.
get_identifier
()is_json_response_supported
()Indicates that this target supports JSON response format.
is_response_format_json
(request_piece)Checks if the response format is JSON and ensures the target supports it.
refresh_auth_headers
()Refresh the authentication headers.
send_prompt_async
(**kwargs)Sends a normalized prompt async to the prompt target.
set_system_prompt
(*, system_prompt, ...[, ...])Sets the system prompt for the prompt target.
Attributes
ADDITIONAL_REQUEST_HEADERS
supported_converters