You signed in with another tab or window. Reload to refresh your session.You signed out in another tab or window. Reload to refresh your session.You switched accounts on another tab or window. Reload to refresh your session.Dismiss alert
Beginning with [dd-trace-py v3.14.0rc1][1], a new Python SDK has been introduced. This SDK provides a streamlined interface for invoking the REST API directly from Python code. The following examples demonstrate its usage:
308
+
Beginning with [dd-trace-py v3.18.0][1], a new Python SDK has been introduced. This SDK provides a streamlined interface for invoking the REST API directly from Python code. The following examples demonstrate its usage:
300
309
301
310
```py
302
-
from ddtrace.appsec.ai_guard import new_ai_guard_client, Prompt, ToolCall
311
+
from ddtrace.appsec.ai_guard import new_ai_guard_client, Function, Message, Options, ToolCall
303
312
304
-
client = new_ai_guard_client(
305
-
api_key="<YOUR_API_KEY>",
306
-
app_key="<YOUR_APPLICATION_KEY>"
307
-
)
313
+
client = new_ai_guard_client()
308
314
```
309
315
310
316
#### Example: Evaluate a user prompt {#python-example-evaluate-user-prompt}
311
317
312
318
```py
313
319
# Check if processing the user prompt is considered safe
314
-
prompt_evaluation = client.evaluate_prompt(
315
-
history=[
316
-
Prompt(role="system", content="You are an AI Assistant"),
320
+
result = client.evaluate(
321
+
messages=[
322
+
Message(role="system", content="You are an AI Assistant"),
323
+
Message(role="user", content="What is the weather like today?"),
317
324
],
318
-
role="user",
319
-
content="What is the weather like today?"
325
+
options=Options(block=False)
320
326
)
321
327
```
322
328
323
-
The `evaluate_prompt` method accepts the following parameters:
324
-
-`history` (optional): A list of `Prompt` or `ToolCall` objects representing previous prompts or tool evaluations.
325
-
-`role` (required): A string specifying the role associated with the prompt.
326
-
-`content` (required): The content of the prompt.
329
+
The `evaluate` method accepts the following parameters:
330
+
-`messages` (required): list of messages (prompts or tool calls) for AI Guard to evaluate.
331
+
-`opts` (optional): dictionary with a block flag; if set to `true`, the SDK rejects the promise with `AIGuardAbortError` when the assessment is `DENY` or `ABORT`.
327
332
328
-
The method returns a Boolean value: `True` if the prompt is considered safe to execute, or `False` otherwise. If the REST API detects potentially dangerous content, it raises an `AIGuardAbortError`.
333
+
The method returns an Evaluation object containing:
334
+
-`action`: `ALLOW`, `DENY`, or `ABORT`.
335
+
-`reason`: natural language summary of the decision.
329
336
330
337
#### Example: Evaluate a tool call {#python-example-evaluate-tool-call}
331
338
339
+
Like evaluating user prompts, the method can also be used to evaluate tool calls:
340
+
332
341
```py
333
342
# Check if executing the shell tool is considered safe
In this case, the `evaluate_tool` method accepts the following parameters:
341
-
342
-
-`history` (optional): A list of `Prompt` or `ToolCall` objects representing previous prompts or tool evaluations.
343
-
-`tool_name` (required): A string specifying the name of the tool to invoke.
344
-
-`tool_args` (required): A dictionary containing the required tool arguments.
357
+
<divclass="alert alert-info">
358
+
[dd-trace-py v3.14.0-rc1][2] introduced an SDK version that has now been removed in favor of the standardized common message format.
359
+
</div>
345
360
346
-
The method returns a Boolean value: `True` if the tool invocation is considered safe, or `False` otherwise. If the REST API identifies potentially dangerous content, it raises an `AIGuardAbortError`.
Starting with [dd-trace-js v5.69.0][1], a new JavaScript SDK is available. This SDK offers a simplified interface for interacting with the REST API directly from JavaScript applications.
352
366
353
-
To use the SDK, ensure the following environment variables are configured:
The SDK is described in a dedicated [TypeScript][2] definition file. For convenience, the following sections provide practical usage examples:
363
368
364
369
#### Example: Evaluate a user prompt {#javascript-example-evaluate-user-prompt}
@@ -412,15 +417,6 @@ const result = await tracer.aiguard.evaluate([
412
417
{{% tab "Java" %}}
413
418
Beginning with [dd-trace-java v1.54.0][1], a new Java SDK is available. This SDK provides a streamlined interface for directly interacting with the REST API from Java applications.
414
419
415
-
Before using the SDK, make sure the following environment variables are properly configured:
0 commit comments