Skip to content

Commit eb20aaf

Browse files
author
Raphael Eidus
committed
feat(amazon-bedrock): add guardrail content support for text and image parts
1 parent 7943a4b commit eb20aaf

File tree

6 files changed

+416
-9
lines changed

6 files changed

+416
-9
lines changed

.changeset/wild-coins-fly.md

Lines changed: 7 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,7 @@
1+
---
2+
"@ai-sdk/amazon-bedrock": patch
3+
---
4+
5+
Adds support for specifying which parts of a message to guard using GuardrailConverseContentBlock
6+
7+
ref: https://docs.aws.amazon.com/bedrock/latest/userguide/guardrails-use-converse-api.html

content/providers/01-ai-sdk-providers/08-amazon-bedrock.mdx

Lines changed: 74 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -295,6 +295,80 @@ if (result.providerMetadata?.bedrock.trace) {
295295

296296
See the [Amazon Bedrock Guardrails documentation](https://docs.aws.amazon.com/bedrock/latest/userguide/guardrails.html) for more information.
297297

298+
#### Guard Content
299+
300+
You can mark individual text or image parts as guard content using `providerOptions` on the part itself. This wraps the part in a [`guardContent`](https://docs.aws.amazon.com/bedrock/latest/APIReference/API_runtime_GuardrailConverseContentBlock.html) block, which tells Bedrock Guardrails to evaluate that content specifically.
301+
302+
For text parts, you can also specify `guardContentQualifiers` to indicate how the content should be treated by the guardrail (e.g. as a grounding source, a query, or guard content):
303+
304+
```ts
305+
const result = await generateText({
306+
model: bedrock('anthropic.claude-3-sonnet-20240229-v1:0'),
307+
providerOptions: {
308+
bedrock: {
309+
guardrailConfig: {
310+
guardrailIdentifier: '1abcd2ef34gh',
311+
guardrailVersion: '1',
312+
},
313+
},
314+
},
315+
messages: [
316+
{
317+
role: 'user',
318+
content: [
319+
{
320+
type: 'text',
321+
text: 'London is the capital of UK. Tokyo is the capital of Japan.',
322+
providerOptions: {
323+
bedrock: {
324+
guardContent: true,
325+
guardContentQualifiers: ['grounding_source'],
326+
},
327+
},
328+
},
329+
{
330+
type: 'text',
331+
text: 'Some additional background information.',
332+
},
333+
{
334+
type: 'text',
335+
text: 'What is the capital of Japan?',
336+
providerOptions: {
337+
bedrock: {
338+
guardContent: true,
339+
guardContentQualifiers: ['query'],
340+
},
341+
},
342+
},
343+
],
344+
},
345+
],
346+
});
347+
```
348+
349+
Image parts can also be marked as guard content:
350+
351+
```ts
352+
{
353+
type: 'file',
354+
data: imageBase64,
355+
mediaType: 'image/png',
356+
providerOptions: {
357+
bedrock: {
358+
guardContent: true,
359+
},
360+
},
361+
}
362+
```
363+
364+
The available `guardContentQualifiers` are:
365+
366+
- `'grounding_source'` — content used as a grounding source for the guardrail
367+
- `'query'` — content treated as a query to evaluate
368+
- `'guard_content'` — content to be evaluated by the guardrail
369+
370+
See the [Amazon Bedrock Guardrails with Converse API documentation](https://docs.aws.amazon.com/bedrock/latest/userguide/guardrails-use-converse-api.html) for more information.
371+
298372
### Citations
299373

300374
Amazon Bedrock supports citations for document-based inputs across compatible models. When enabled:

packages/amazon-bedrock/src/bedrock-api-types.ts

Lines changed: 9 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -158,8 +158,16 @@ export interface BedrockDocumentBlock {
158158
};
159159
}
160160

161+
export interface BedrockGuardrailTextBlock extends BedrockTextBlock {
162+
qualifiers?: Array<'grounding_source' | 'query' | 'guard_content'>;
163+
}
164+
161165
export interface BedrockGuardrailConverseContentBlock {
162-
guardContent: unknown;
166+
guardContent:
167+
| {
168+
text: BedrockGuardrailTextBlock;
169+
}
170+
| BedrockImageBlock;
163171
}
164172

165173
export interface BedrockImageBlock {

packages/amazon-bedrock/src/bedrock-chat-options.ts

Lines changed: 28 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -98,6 +98,34 @@ export type BedrockFilePartProviderOptions = z.infer<
9898
typeof bedrockFilePartProviderOptions
9999
>;
100100

101+
/**
102+
* Bedrock text part provider options for guardrail content.
103+
* These options apply to individual text parts.
104+
*/
105+
export const bedrockTextPartProviderOptions = z.object({
106+
guardContent: z.boolean().optional(),
107+
guardContentQualifiers: z
108+
.array(z.enum(['grounding_source', 'query', 'guard_content']))
109+
.optional(),
110+
});
111+
112+
export type BedrockTextPartProviderOptions = z.infer<
113+
typeof bedrockTextPartProviderOptions
114+
>;
115+
116+
/**
117+
* Bedrock image part provider options for guardrail content.
118+
* These options apply to individual image parts.
119+
*/
120+
121+
export const bedrockImagePartProviderOptions = z.object({
122+
guardContent: z.boolean().optional(),
123+
});
124+
125+
export type BedrockImagePartProviderOptions = z.infer<
126+
typeof bedrockImagePartProviderOptions
127+
>;
128+
101129
export const amazonBedrockLanguageModelOptions = z.object({
102130
/**
103131
* Additional inference parameters that the model supports,

0 commit comments

Comments
 (0)