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:
309
+
310
+
<divclass="alert alert-info">
311
+
**dd-trace-py v3.14.0-rc1** SDK version has now been replaced by the new standardized common message format.
312
+
</div>
300
313
301
314
```py
302
-
from ddtrace.appsec.ai_guard import new_ai_guard_client, Prompt, ToolCall
315
+
from ddtrace.appsec.ai_guard import new_ai_guard_client, Function, Message, Options, ToolCall
303
316
304
-
client = new_ai_guard_client(
305
-
api_key="<YOUR_API_KEY>",
306
-
app_key="<YOUR_APPLICATION_KEY>"
307
-
)
317
+
client = new_ai_guard_client()
308
318
```
309
319
310
320
#### Example: Evaluate a user prompt {#python-example-evaluate-user-prompt}
311
321
312
322
```py
313
323
# 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"),
324
+
result = client.evaluate(
325
+
messages=[
326
+
Message(role="system", content="You are an AI Assistant"),
327
+
Message(role="user", content="What is the weather like today?"),
317
328
],
318
-
role="user",
319
-
content="What is the weather like today?"
329
+
options=Options(block=False)
320
330
)
321
331
```
322
332
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.
333
+
The `evaluate` method accepts the following parameters:
334
+
-`messages` (required): list of messages (prompts or tool calls) for AI Guard to evaluate.
335
+
-`opts` (optional): dictionary with a block flag; if set to `true`, the SDK raises an `AIGuardAbortError` when the assessment is `DENY` or `ABORT` and the service is configured with blocking enabled.
327
336
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`.
337
+
The method returns an Evaluation object containing:
338
+
-`action`: `ALLOW`, `DENY`, or `ABORT`.
339
+
-`reason`: natural language summary of the decision.
329
340
330
341
#### Example: Evaluate a tool call {#python-example-evaluate-tool-call}
331
342
343
+
Like evaluating user prompts, the method can also be used to evaluate tool calls:
344
+
332
345
```py
333
346
# 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.
345
-
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
365
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
367
364
368
#### Example: Evaluate a user prompt {#javascript-example-evaluate-user-prompt}
@@ -376,7 +380,7 @@ const result = await tracer.aiguard.evaluate([
376
380
377
381
The evaluate method returns a promise and receives the following parameters:
378
382
-`messages` (required): list of messages (prompts or tool calls) for AI Guard to evaluate.
379
-
-`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`.
383
+
-`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` and the service is configured with blocking enabled.
380
384
381
385
The method returns a promise that resolves to an Evaluation object containing:
382
386
-`action`: `ALLOW`, `DENY`, or `ABORT`.
@@ -412,15 +416,6 @@ const result = await tracer.aiguard.evaluate([
412
416
{{% tab "Java" %}}
413
417
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
418
415
-
Before using the SDK, make sure the following environment variables are properly configured:
The following sections provide practical usage examples:
425
420
426
421
#### Example: Evaluate a user prompt {#java-example-evaluate-user-prompt}
@@ -439,7 +434,7 @@ final AIGuard.Evaluation evaluation = AIGuard.evaluate(
439
434
440
435
The evaluate method receives the following parameters:
441
436
-`messages` (required): list of messages (prompts or tool calls) for AI Guard to evaluate.
442
-
-`options` (optional): object with a block flag; if set to `true`, the SDK throws an `AIGuardAbortError` when the assessment is `DENY` or `ABORT`.
437
+
-`options` (optional): object with a block flag; if set to `true`, the SDK throws an `AIGuardAbortError` when the assessment is `DENY` or `ABORT` and the service is configured with blocking enabled.
443
438
444
439
The method returns an Evaluation object containing:
445
440
-`action`: `ALLOW`, `DENY`, or `ABORT`.
@@ -508,4 +503,4 @@ Follow the instructions to create a new [metric monitor][11].
0 commit comments