Skip to content
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
7 changes: 7 additions & 0 deletions .changeset/wild-coins-fly.md
Original file line number Diff line number Diff line change
@@ -0,0 +1,7 @@
---
"@ai-sdk/amazon-bedrock": patch
---

Adds support for specifying which parts of a message to guard using GuardrailConverseContentBlock

ref: https://docs.aws.amazon.com/bedrock/latest/userguide/guardrails-use-converse-api.html
74 changes: 74 additions & 0 deletions content/providers/01-ai-sdk-providers/08-amazon-bedrock.mdx
Original file line number Diff line number Diff line change
Expand Up @@ -295,6 +295,80 @@ if (result.providerMetadata?.bedrock.trace) {

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

#### Guard Content

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.

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):

```ts
const result = await generateText({
model: bedrock('anthropic.claude-3-sonnet-20240229-v1:0'),
providerOptions: {
bedrock: {
guardrailConfig: {
guardrailIdentifier: '1abcd2ef34gh',
guardrailVersion: '1',
},
},
},
messages: [
{
role: 'user',
content: [
{
type: 'text',
text: 'London is the capital of UK. Tokyo is the capital of Japan.',
providerOptions: {
bedrock: {
guardContent: true,
guardContentQualifiers: ['grounding_source'],
},
},
},
{
type: 'text',
text: 'Some additional background information.',
},
{
type: 'text',
text: 'What is the capital of Japan?',
providerOptions: {
bedrock: {
guardContent: true,
guardContentQualifiers: ['query'],
},
},
},
],
},
],
});
```

Image parts can also be marked as guard content:

```ts
{
type: 'file',
data: imageBase64,
mediaType: 'image/png',
providerOptions: {
bedrock: {
guardContent: true,
},
},
}
```

The available `guardContentQualifiers` are:

- `'grounding_source'` — content used as a grounding source for the guardrail
- `'query'` — content treated as a query to evaluate
- `'guard_content'` — content to be evaluated by the guardrail

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.

### Citations

Amazon Bedrock supports citations for document-based inputs across compatible models. When enabled:
Expand Down
10 changes: 9 additions & 1 deletion packages/amazon-bedrock/src/bedrock-api-types.ts
Original file line number Diff line number Diff line change
Expand Up @@ -158,8 +158,16 @@ export interface BedrockDocumentBlock {
};
}

export interface BedrockGuardrailTextBlock extends BedrockTextBlock {
qualifiers?: Array<'grounding_source' | 'query' | 'guard_content'>;
}

export interface BedrockGuardrailConverseContentBlock {
guardContent: unknown;
guardContent:
| {
text: BedrockGuardrailTextBlock;
}
| BedrockImageBlock;
}

export interface BedrockImageBlock {
Expand Down
28 changes: 28 additions & 0 deletions packages/amazon-bedrock/src/bedrock-chat-options.ts
Original file line number Diff line number Diff line change
Expand Up @@ -98,6 +98,34 @@ export type BedrockFilePartProviderOptions = z.infer<
typeof bedrockFilePartProviderOptions
>;

/**
* Bedrock text part provider options for guardrail content.
* These options apply to individual text parts.
*/
export const bedrockTextPartProviderOptions = z.object({
guardContent: z.boolean().optional(),
guardContentQualifiers: z
.array(z.enum(['grounding_source', 'query', 'guard_content']))
.optional(),
});

export type BedrockTextPartProviderOptions = z.infer<
typeof bedrockTextPartProviderOptions
>;

/**
* Bedrock image part provider options for guardrail content.
* These options apply to individual image parts.
*/

export const bedrockImagePartProviderOptions = z.object({
guardContent: z.boolean().optional(),
});

export type BedrockImagePartProviderOptions = z.infer<
typeof bedrockImagePartProviderOptions
>;

export const amazonBedrockLanguageModelOptions = z.object({
/**
* Additional inference parameters that the model supports,
Expand Down
Loading
Loading