pyrit.prompt_target.OpenAITarget#
- class OpenAITarget(*, model_name: str | None = None, endpoint: str | None = None, api_key: str | Callable[[], str | Awaitable[str]] | None = None, headers: str | None = None, max_requests_per_minute: int | None = None, httpx_client_kwargs: dict | None = None, underlying_model: str | None = None)[source]#
Bases:
PromptChatTargetAbstract base class for OpenAI-based prompt targets.
This class provides common functionality for interacting with OpenAI API endpoints, handling authentication, rate limiting, and request/response processing.
Read more about the various models here: https://learn.microsoft.com/en-us/azure/ai-services/openai/concepts/models.
- __init__(*, model_name: str | None = None, endpoint: str | None = None, api_key: str | Callable[[], str | Awaitable[str]] | None = None, headers: str | None = None, max_requests_per_minute: int | None = None, httpx_client_kwargs: dict | None = None, underlying_model: str | None = None) None[source]#
Initialize an instance of OpenAITarget.
- Parameters:
model_name (str, Optional) – The name of the model (or name of deployment in Azure). If no value is provided, the environment variable will be used (set by subclass).
endpoint (str, Optional) – The target URL for the OpenAI service.
api_key (str | Callable[[], str], Optional) – The API key for accessing the OpenAI service, or a callable that returns an access token. For Azure endpoints with Entra authentication, pass a token provider from pyrit.auth (e.g., get_azure_openai_auth(endpoint)). Defaults to the target-specific API key environment variable.
headers (str, Optional) – Extra headers of the endpoint (JSON).
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.
httpx_client_kwargs (dict, Optional) – Additional kwargs to be passed to the httpx.AsyncClient() constructor.
underlying_model (str, Optional) – The underlying model name (e.g., “gpt-4o”) used solely for target identifier purposes. This is useful when the deployment name in Azure differs from the actual model. If not provided, will attempt to fetch from environment variable. If it is not there either, the identifier “model_name” attribute will use the model_name. Defaults to None.
Methods
__init__(*[, model_name, endpoint, api_key, ...])Initialize an instance of OpenAITarget.
dispose_db_engine()Dispose database engine to release database connections and resources.
get_identifier()Get an identifier dictionary for this prompt target.
Abstract method to determine if JSON response format is supported by the target.
is_response_format_json(message_piece)Check if the response format is JSON and ensure the target supports it.
send_prompt_async(*, message)Send 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, ...[, ...])Set the system prompt for the prompt target.
Attributes
supported_convertersA list of PromptConverters that are supported by the prompt target.