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
5 changes: 5 additions & 0 deletions .changeset/fifty-beds-smell.md
Original file line number Diff line number Diff line change
@@ -0,0 +1,5 @@
---
'@ai-sdk/amazon-bedrock': patch
---

Add Nova Web Grounding support for Amazon Bedrock
53 changes: 53 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 @@ -600,6 +600,59 @@ Parameters:

These tools can be used in conjunction with the `anthropic.claude-3-5-sonnet-20240620-v1:0` model to enable more complex interactions and tasks.

## Nova Web Grounding

Amazon Nova models support Web Grounding, a built-in tool that provides real-time web search capabilities. Web Grounding is a turnkey Retrieval Augmented Generation (RAG) option that allows Nova models to intelligently decide when to retrieve and incorporate relevant up-to-date information based on the context of the prompt.

### When to Use Web Grounding

Web Grounding is particularly useful for:

- Applications requiring access to current, factual information
- Responses that need well-cited sources
- Knowledge-based chat assistants providing up-to-date information
- Content generation tools requiring fact-checking and source verification
- Research assistants synthesizing information from multiple current sources
- Customer support applications where accuracy and verifiability are crucial

### Using Web Grounding

Web Grounding is available via the `tools` property of the provider instance:

```ts
import { bedrock } from '@ai-sdk/amazon-bedrock';
import { generateText } from 'ai';

const result = await generateText({
model: bedrock('us.amazon.nova-premier-v1:0'),
prompt: 'What are the current AWS Regions and their locations?',
tools: {
nova_grounding: bedrock.tools.nova_grounding(),
},
});

console.log(result.text);
```

When Web Grounding is enabled, the model will automatically:

1. Determine when external information is needed based on the prompt
2. Search for relevant, up-to-date information from the web
3. Incorporate the retrieved information into its response
4. Provide source citations for the information used

### Availability

- **Regions**: US East (N. Virginia), with US East (Ohio) and US West (Oregon) coming soon
- **Models**: Currently available for Nova Premier, with support for other Nova models coming soon
- **Pricing**: Web Grounding incurs additional costs. See the [Amazon Bedrock pricing page](https://aws.amazon.com/bedrock/pricing/) for details

<Note>
Web Grounding represents a significant step forward in making AI applications
more reliable and current with minimum effort, eliminating the need to build
and maintain complex RAG pipelines.
</Note>

### Model Capabilities

| Model | Image Input | Object Generation | Tool Usage | Tool Streaming |
Expand Down
8 changes: 7 additions & 1 deletion packages/amazon-bedrock/src/bedrock-api-types.ts
Original file line number Diff line number Diff line change
Expand Up @@ -63,8 +63,14 @@ export interface BedrockTool {
};
}

export interface BedrockSystemTool {
systemTool: {
name: string;
};
}

export interface BedrockToolConfiguration {
tools?: Array<BedrockTool | BedrockCachePoint>;
tools?: Array<BedrockTool | BedrockSystemTool | BedrockCachePoint>;
toolChoice?:
| { tool: { name: string } }
| { auto: {} }
Expand Down
38 changes: 38 additions & 0 deletions packages/amazon-bedrock/src/bedrock-nova-tools.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,38 @@
import { LanguageModelV3ProviderDefinedTool } from '@ai-sdk/provider';

/**
* Nova-specific tools that can be used with Nova models on Amazon Bedrock.
* These are system tools that are executed by the Bedrock service.
*/
export const novaTools = {
/**
* Nova Web Grounding tool for real-time web search and information retrieval.
* This is a system tool that is executed by the Bedrock service.
*
* Web Grounding provides a turnkey RAG option that allows Nova models to
* intelligently decide when to retrieve and incorporate relevant up-to-date
* information based on the context of the prompt.
*
* @example
* ```typescript
* import { bedrock } from '@ai-sdk/amazon-bedrock';
* import { generateText } from 'ai';
*
* const result = await generateText({
* model: bedrock('us.amazon.nova-premier-v1:0'),
* prompt: 'What are the current AWS Regions and their locations?',
* tools: {
* nova_grounding: bedrock.tools.nova_grounding(),
* },
* });
* ```
*/
nova_grounding: (
args: Record<string, unknown> = {},
): LanguageModelV3ProviderDefinedTool => ({
type: 'provider-defined' as const,
id: 'nova.nova_grounding',
name: 'nova_grounding',
args,
}),
};
204 changes: 204 additions & 0 deletions packages/amazon-bedrock/src/bedrock-prepare-tools.test.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,204 @@
import { describe, it, expect } from 'vitest';
import { prepareTools } from './bedrock-prepare-tools';

describe('prepareTools', () => {
it('should return empty toolConfig when tools are null', async () => {
const result = await prepareTools({
tools: undefined,
modelId: 'us.amazon.nova-premier-v1:0',
});
expect(result).toEqual({
toolConfig: {},
additionalTools: undefined,
toolWarnings: [],
betas: new Set(),
});
});

it('should return empty toolConfig when tools are empty', async () => {
const result = await prepareTools({
tools: [],
modelId: 'us.amazon.nova-premier-v1:0',
});
expect(result).toEqual({
toolConfig: {},
additionalTools: undefined,
toolWarnings: [],
betas: new Set(),
});
});

it('should correctly prepare function tools', async () => {
const result = await prepareTools({
tools: [
{
type: 'function',
name: 'testFunction',
description: 'A test function',
inputSchema: { type: 'object', properties: {} },
},
],
modelId: 'us.amazon.nova-premier-v1:0',
});

expect(result.toolConfig.tools).toEqual([
{
toolSpec: {
name: 'testFunction',
description: 'A test function',
inputSchema: {
json: { type: 'object', properties: {} },
},
},
},
]);
expect(result.toolWarnings).toEqual([]);
});

describe('Nova provider-defined tools', () => {
it('should correctly prepare nova_grounding tool', async () => {
const result = await prepareTools({
tools: [
{
type: 'provider-defined',
id: 'nova.nova_grounding',
name: 'nova_grounding',
args: {},
},
],
modelId: 'us.amazon.nova-premier-v1:0',
});

expect(result.toolConfig.tools).toEqual([
{
systemTool: {
name: 'nova_grounding',
},
},
]);
expect(result.toolWarnings).toEqual([]);
expect(result.betas.size).toBe(0);
});

it('should warn when mixing Nova tools with function tools and exclude function tools', async () => {
const result = await prepareTools({
tools: [
{
type: 'provider-defined',
id: 'nova.nova_grounding',
name: 'nova_grounding',
args: {},
},
{
type: 'function',
name: 'testFunction',
inputSchema: { type: 'object', properties: {} },
},
],
modelId: 'us.amazon.nova-premier-v1:0',
});

expect(result.toolWarnings).toContainEqual({
type: 'unsupported-setting',
setting: 'tools',
details:
'Mixed Nova provider-defined tools and standard function tools are not supported in a single call to Bedrock. Only Nova tools will be used.',
});

// Verify that only Nova tools are included, function tools are excluded
expect(result.toolConfig.tools).toEqual([
{
systemTool: {
name: 'nova_grounding',
},
},
]);
expect(result.toolConfig.tools?.length).toBe(1);
});

it('should not use Nova tools with non-Nova models', async () => {
const result = await prepareTools({
tools: [
{
type: 'provider-defined',
id: 'nova.nova_grounding',
name: 'nova_grounding',
args: {},
},
],
modelId: 'anthropic.claude-3-5-sonnet-20241022-v2:0',
});

expect(result.toolConfig.tools).toBeUndefined();
expect(result.toolWarnings).toContainEqual({
type: 'unsupported-tool',
tool: {
type: 'provider-defined',
id: 'nova.nova_grounding',
name: 'nova_grounding',
args: {},
},
});
});
});

describe('Anthropic provider-defined tools', () => {
it('should warn when mixing Anthropic tools with function tools and exclude function tools', async () => {
const result = await prepareTools({
tools: [
{
type: 'provider-defined',
id: 'anthropic.bash_20241022',
name: 'bash',
args: {},
},
{
type: 'function',
name: 'testFunction',
inputSchema: { type: 'object', properties: {} },
},
],
modelId: 'anthropic.claude-3-5-sonnet-20241022-v2:0',
});

expect(result.toolWarnings).toContainEqual({
type: 'unsupported-setting',
setting: 'tools',
details:
'Mixed Anthropic provider-defined tools and standard function tools are not supported in a single call to Bedrock. Only Anthropic tools will be used.',
});

// Verify that only Anthropic tools are included, function tools are excluded
expect(result.toolConfig.tools).toHaveLength(1);
expect(result.toolConfig.tools?.[0]).toHaveProperty('toolSpec');
expect((result.toolConfig.tools?.[0] as any).toolSpec?.name).toBe('bash');
});

it('should filter out web_search_20250305 and add warning', async () => {
const result = await prepareTools({
tools: [
{
type: 'provider-defined',
id: 'anthropic.web_search_20250305',
name: 'web_search',
args: {},
},
],
modelId: 'anthropic.claude-3-5-sonnet-20241022-v2:0',
});

expect(result.toolConfig.tools).toBeUndefined();
expect(result.toolWarnings).toContainEqual({
type: 'unsupported-tool',
tool: {
type: 'provider-defined',
id: 'anthropic.web_search_20250305',
name: 'web_search',
args: {},
},
details:
'The web_search_20250305 tool is not supported on Amazon Bedrock.',
});
});
});
});
Loading
Loading