pyrit.prompt_target.OpenAIChatTarget#

class OpenAIChatTarget(*, max_completion_tokens: int | None = None, max_tokens: int | None = None, temperature: float | None = None, top_p: float | None = None, frequency_penalty: float | None = None, presence_penalty: float | None = None, seed: int | None = None, n: int | None = None, is_json_supported: bool = True, extra_body_parameters: dict[str, Any] | None = None, **kwargs)[source]#

Bases: OpenAIChatTargetBase

This class facilitates multimodal (image and text) input and text output generation.

This works with GPT3.5, GPT4, GPT4o, GPT-V, and other compatible models

Parameters:
  • api_key (str) – The api key for the OpenAI API

  • endpoint (str) – The endpoint for the OpenAI API

  • model_name (str) – The model name for the OpenAI API

  • deployment_name (str) – For Azure, the deployment name

  • temperature (float) – The temperature for the completion

  • max_completion_tokens (int) – The maximum number of tokens to be returned by the model. The total length of input tokens and generated tokens is limited by the model’s context length.

  • max_tokens (int) – Deprecated. Use max_completion_tokens instead

  • top_p (float) – The nucleus sampling probability.

  • frequency_penalty (float) – Number between -2.0 and 2.0. Positive values penalize new tokens based on their existing frequency in the text so far, decreasing the model’s likelihood to repeat the same line verbatim.

  • presence_penalty (float) – Number between -2.0 and 2.0. Positive values penalize new tokens based on whether they appear in the text so far, increasing the model’s likelihood to talk about new topics.

  • seed (int) – This feature is in Beta. If specified, our system will make a best effort to sample deterministically, such that repeated requests with the same seed and parameters should return the same result.

  • n (int) – How many chat completion choices to generate for each input message. Note that you will be charged based on the number of generated tokens across all of the choices. Keep n as 1 to minimize costs.

  • extra_body_parameters (dict) – Additional parameters to send in the request body

__init__(*, max_completion_tokens: int | None = None, max_tokens: int | None = None, temperature: float | None = None, top_p: float | None = None, frequency_penalty: float | None = None, presence_penalty: float | None = None, seed: int | None = None, n: int | None = None, is_json_supported: bool = True, extra_body_parameters: dict[str, Any] | None = None, **kwargs)[source]#
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_CHAT_KEY environment variable.

  • headers (str, Optional) – Headers of the endpoint (JSON).

  • use_entra_auth (bool, Optional) – When set to True, user authentication is used instead of API Key. DefaultAzureCredential is taken for https://cognitiveservices.azure.com/.default . Please run az login locally to leverage user AuthN.

  • 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_completion_tokens (int, Optional) –

    An upper bound for the number of tokens that can be generated for a completion, including visible output tokens and reasoning tokens.

    NOTE: Specify this value when using an o1 series model.

  • max_tokens (int, Optional) –

    The maximum number of tokens that can be generated in the chat completion. This value can be used to control costs for text generated via API.

    This value is now deprecated in favor of max_completion_tokens, and IS NOT COMPATIBLE with o1 series models.

  • 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.

  • frequency_penalty (float, Optional) – The frequency penalty parameter for penalizing frequently generated tokens.

  • presence_penalty (float, Optional) – The presence penalty parameter for penalizing tokens that are already present in the conversation history.

  • seed (int, Optional) – If specified, openAI will make a best effort to sample deterministically, such that repeated requests with the same seed and parameters should return the same result.

  • n (int, Optional) – The number of completions to generate for each prompt.

  • 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_completion_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__(*[, max_completion_tokens, ...])

dispose_db_engine()

Dispose 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(message_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_model_name(*, model_name)

Set the model name for this target.

set_system_prompt(*, system_prompt, ...[, ...])

Sets the system prompt for the prompt target.

Attributes

api_key_environment_variable: str#
endpoint_environment_variable: str#
model_name_environment_variable: str#