|
| 1 | +# Unless explicitly stated otherwise all files in this repository are licensed under the Apache-2.0 License. |
| 2 | +# This product includes software developed at Datadog (https://www.datadoghq.com/). |
| 3 | +# Copyright 2019-Present Datadog, Inc. |
| 4 | +from __future__ import annotations |
| 5 | + |
| 6 | +from typing import Any, Dict |
| 7 | + |
| 8 | +from datadog_api_client.api_client import ApiClient, Endpoint as _Endpoint |
| 9 | +from datadog_api_client.configuration import Configuration |
| 10 | +from datadog_api_client.v2.model.ai_guard_evaluate_response import AIGuardEvaluateResponse |
| 11 | +from datadog_api_client.v2.model.ai_guard_evaluate_request import AIGuardEvaluateRequest |
| 12 | + |
| 13 | + |
| 14 | +class AIGuardApi: |
| 15 | + """ |
| 16 | + Evaluate LLM interactions for security threats such as prompt injection and jailbreak attempts. |
| 17 | + """ |
| 18 | + |
| 19 | + def __init__(self, api_client=None): |
| 20 | + if api_client is None: |
| 21 | + api_client = ApiClient(Configuration()) |
| 22 | + self.api_client = api_client |
| 23 | + |
| 24 | + self._evaluate_ai_guard_request_endpoint = _Endpoint( |
| 25 | + settings={ |
| 26 | + "response_type": (AIGuardEvaluateResponse,), |
| 27 | + "auth": ["apiKeyAuth", "appKeyAuth", "AuthZ"], |
| 28 | + "endpoint_path": "/api/v2/ai-guard/evaluate", |
| 29 | + "operation_id": "evaluate_ai_guard_request", |
| 30 | + "http_method": "POST", |
| 31 | + "version": "v2", |
| 32 | + }, |
| 33 | + params_map={ |
| 34 | + "body": { |
| 35 | + "required": True, |
| 36 | + "openapi_types": (AIGuardEvaluateRequest,), |
| 37 | + "location": "body", |
| 38 | + }, |
| 39 | + }, |
| 40 | + headers_map={"accept": ["application/json"], "content_type": ["application/json"]}, |
| 41 | + api_client=api_client, |
| 42 | + ) |
| 43 | + |
| 44 | + def evaluate_ai_guard_request( |
| 45 | + self, |
| 46 | + body: AIGuardEvaluateRequest, |
| 47 | + ) -> AIGuardEvaluateResponse: |
| 48 | + """Evaluate an AI Guard request. |
| 49 | +
|
| 50 | + Analyzes a conversation for security threats such as prompt injection, |
| 51 | + jailbreak attempts, and other AI-specific attacks. Returns an action |
| 52 | + recommendation ( ``ALLOW`` , ``DENY`` , or ``ABORT`` ) along with the detected threat |
| 53 | + tags and their confidence scores. |
| 54 | + AI Guard must be activated for the organization, and the calling user or |
| 55 | + application key requires the ``ai_guard_evaluate`` permission. The action is a |
| 56 | + recommendation for the caller to enforce: ALLOW permits the interaction, |
| 57 | + DENY blocks the interaction, and ABORT terminates the workflow. |
| 58 | + This endpoint is rate limited to ``700`` requests per minute per organization. |
| 59 | + Organization token limits can also result in a 429 response. |
| 60 | + Malformed messages return a 400 response. Requests exceeding the raw-input |
| 61 | + limit of 1,000,000 tokens return a 413 response; this limit applies to the |
| 62 | + input context, not only the size of the HTTP request body. |
| 63 | +
|
| 64 | + :param body: The conversation to evaluate for security threats. |
| 65 | + :type body: AIGuardEvaluateRequest |
| 66 | + :rtype: AIGuardEvaluateResponse |
| 67 | + """ |
| 68 | + kwargs: Dict[str, Any] = {} |
| 69 | + kwargs["body"] = body |
| 70 | + |
| 71 | + return self._evaluate_ai_guard_request_endpoint.call_with_http_info(**kwargs) |
0 commit comments