pyrit.prompt_target.OpenAITarget#
- class OpenAITarget(*, model_name: str | None = None, endpoint: str | None = None, api_key: str | None = None, headers: str | None = None, use_entra_auth: bool = False, api_version: str | None = '2024-10-21', max_requests_per_minute: int | None = None, httpx_client_kwargs: dict | None = None)[source]#
Bases:
PromptChatTarget- __init__(*, model_name: str | None = None, endpoint: str | None = None, api_key: str | None = None, headers: str | None = None, use_entra_auth: bool = False, api_version: str | None = '2024-10-21', max_requests_per_minute: int | None = None, httpx_client_kwargs: dict | None = None) None[source]#
Abstract class that initializes an Azure or non-Azure OpenAI chat target.
Read more about the various models here: https://learn.microsoft.com/en-us/azure/ai-services/openai/concepts/models.
- 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 (only if you’re not using Entra authentication). Defaults to the OPENAI_CHAT_KEY environment variable.
headers (str, Optional) – Extra headers of the endpoint (JSON).
use_entra_auth (bool) – 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.
api_version (str, Optional) – The version of the Azure OpenAI API. Defaults to “2024-06-01”. If set to None, this will not be added as a query parameter to requests.
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.
Methods
__init__(*[, model_name, endpoint, api_key, ...])Abstract class that initializes an Azure or non-Azure OpenAI chat target.
dispose_db_engine()Dispose database engine to release database connections and resources.
get_identifier()Abstract method to determine if JSON response format is supported by the target.
is_response_format_json(message_piece)Checks if the response format is JSON and ensures the target supports it.
Refresh the authentication headers.
send_prompt_async(*, prompt_request)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
supported_converters- CHAT_URL_REGEX = ['/v1/chat/completions$', '/openai/deployments/[^/]+/chat/completions$', '/openai/chat/completions$']#
- DALLE_URL_REGEX = ['/images/generations$']#
- RESPONSE_URL_REGEX = ['/openai/responses$', 'v1/responses$']#
- SORA_URL_REGEX = ['/videos/v1/video/generations$', '/videos/v1/videos$', '/v1/videos$', '/openai/v1/video/generations$']#
- TTS_URL_REGEX = ['/audio/speech$']#