Skip to content

Commit 309c56c

Browse files
Update AI Guard python SDK information
1 parent 63f4976 commit 309c56c

File tree

1 file changed

+42
-46
lines changed

1 file changed

+42
-46
lines changed

content/en/security/ai_guard/onboarding.md

Lines changed: 42 additions & 46 deletions
Original file line numberDiff line numberDiff line change
@@ -294,71 +294,76 @@ Example:
294294

295295
SDK instrumentation allows you to set up and monitor AI Guard activity in real time.
296296

297+
To use the SDK, ensure the following environment variables are configured:
298+
299+
| Variable | Value |
300+
|:-----------------------|:--------------------------------------------------------------|
301+
| `DD_AI_GUARD_ENABLED` | `true` |
302+
| `DD_API_KEY` | `<YOUR_API_KEY>` |
303+
| `DD_APP_KEY` | `<YOUR_APPLICATION_KEY>` |
304+
| `DD_TRACE_ENABLED` | `true` |
305+
297306
{{< tabs >}}
298307
{{% tab "Python" %}}
299-
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:
300309

301310
```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
303312

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()
308314
```
309315

310316
#### Example: Evaluate a user prompt {#python-example-evaluate-user-prompt}
311317

312318
```py
313319
# 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?"),
317324
],
318-
role="user",
319-
content="What is the weather like today?"
325+
options=Options(block=False)
320326
)
321327
```
322328

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`.
327332

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.
329336

330337
#### Example: Evaluate a tool call {#python-example-evaluate-tool-call}
331338

339+
Like evaluating user prompts, the method can also be used to evaluate tool calls:
340+
332341
```py
333342
# Check if executing the shell tool is considered safe
334-
tool_evaluation = client.evaluate_tool(
335-
tool_name="shell",
336-
tool_args={"command": "shutdown"}
343+
result = client.evaluate(
344+
messages=[
345+
Message(
346+
role="assistant",
347+
tool_calls=[
348+
ToolCall(
349+
id="call_1",
350+
function=Function(name="shell", arguments='{ "command": "shutdown" }'))
351+
],
352+
)
353+
]
337354
)
338355
```
339356

340-
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+
<div class="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>
345360

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`.
347-
348-
[1]: https://github.com/DataDog/dd-trace-py/releases/tag/v3.14.0rc1
361+
[1]: https://github.com/DataDog/dd-trace-py/releases/tag/v3.18.0
362+
[2]: https://github.com/DataDog/dd-trace-py/releases/tag/v3.14.0rc1
349363
{{% /tab %}}
350364
{{% tab "Javascript" %}}
351365
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.
352366

353-
To use the SDK, ensure the following environment variables are configured:
354-
355-
| Variable | Value |
356-
|:-----------------------|:--------------------------------------------------------------|
357-
| `DD_AI_GUARD_ENABLED` | `true` |
358-
| `DD_API_KEY` | `<YOUR_API_KEY>` |
359-
| `DD_APP_KEY` | `<YOUR_APPLICATION_KEY>` |
360-
| `DD_TRACE_ENABLED` | `true` |
361-
362367
The SDK is described in a dedicated [TypeScript][2] definition file. For convenience, the following sections provide practical usage examples:
363368

364369
#### Example: Evaluate a user prompt {#javascript-example-evaluate-user-prompt}
@@ -412,15 +417,6 @@ const result = await tracer.aiguard.evaluate([
412417
{{% tab "Java" %}}
413418
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.
414419

415-
Before using the SDK, make sure the following environment variables are properly configured:
416-
417-
| Variable | Value |
418-
|:-----------------------|:--------------------------------------------------------------|
419-
| `DD_AI_GUARD_ENABLED` | `true` |
420-
| `DD_API_KEY` | `<YOUR_API_KEY>` |
421-
| `DD_APP_KEY` | `<YOUR_APPLICATION_KEY>` |
422-
| `DD_TRACE_ENABLED` | `true` |
423-
424420
The following sections provide practical usage examples:
425421

426422
#### Example: Evaluate a user prompt {#java-example-evaluate-user-prompt}
@@ -508,4 +504,4 @@ Follow the instructions to create a new [metric monitor][11].
508504
[9]: /monitors/
509505
[10]: /monitors/types/apm/?tab=traceanalytics
510506
[11]: /monitors/types/metric/
511-
[12]: https://platform.openai.com/docs/api-reference/chat/object
507+
[12]: https://platform.openai.com/docs/api-reference/chat/object

0 commit comments

Comments
 (0)