-
Notifications
You must be signed in to change notification settings - Fork 3.2k
feat(provider/amazon-bedrock): Support citations in amazon-bedrock-provider #8861
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
Changes from all commits
8fcd870
fa7c366
4668992
47e5cc0
42723a6
3f42708
b3bf98a
File filter
Filter by extension
Conversations
Jump to
Diff view
Diff view
There are no files selected for viewing
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,5 @@ | ||
| --- | ||
| '@ai-sdk/amazon-bedrock': patch | ||
| --- | ||
|
|
||
| Support citations in amazon-bedrock-provider |
| Original file line number | Diff line number | Diff line change |
|---|---|---|
|
|
@@ -272,6 +272,51 @@ if (result.providerMetadata?.bedrock.trace) { | |
|
|
||
| See the [Amazon Bedrock Guardrails documentation](https://docs.aws.amazon.com/bedrock/latest/userguide/guardrails.html) for more information. | ||
|
|
||
| ### Citations | ||
|
|
||
| Amazon Bedrock supports citations for document-based inputs across compatible models. When enabled: | ||
|
|
||
| - Some models can read documents with visual understanding, not just extracting text | ||
| - Models can cite specific parts of documents you provide, making it easier to trace information back to its source (Not Supported Yet) | ||
|
|
||
| ```ts | ||
| import { bedrock } from '@ai-sdk/amazon-bedrock'; | ||
| import { generateObject } from 'ai'; | ||
| import { z } from 'zod'; | ||
| import fs from 'fs'; | ||
|
|
||
| const result = await generateObject({ | ||
| model: bedrock('apac.anthropic.claude-sonnet-4-20250514-v1:0'), | ||
|
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. |
||
| schema: z.object({ | ||
| summary: z.string().describe('Summary of the PDF document'), | ||
| keyPoints: z.array(z.string()).describe('Key points from the PDF'), | ||
| }), | ||
| messages: [ | ||
| { | ||
| role: 'user', | ||
| content: [ | ||
| { | ||
| type: 'text', | ||
| text: 'Summarize this PDF and provide key points.', | ||
| }, | ||
| { | ||
| type: 'file', | ||
| data: fs.readFileSync('./document.pdf'), | ||
| mediaType: 'application/pdf', | ||
| providerOptions: { | ||
| bedrock: { | ||
| citations: { enabled: true }, | ||
| }, | ||
| }, | ||
| }, | ||
| ], | ||
| }, | ||
| ], | ||
| }); | ||
|
|
||
| console.log('Response:', result.object); | ||
| ``` | ||
|
|
||
| ### Cache Points | ||
|
|
||
| <Note> | ||
|
|
||
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,40 @@ | ||
| import { bedrock, BedrockProviderOptions } from '@ai-sdk/amazon-bedrock'; | ||
| import { generateObject } from 'ai'; | ||
| import { z } from 'zod'; | ||
| import fs from 'fs'; | ||
| import 'dotenv/config'; | ||
|
|
||
| async function main() { | ||
| const result = await generateObject({ | ||
| model: bedrock('us.anthropic.claude-sonnet-4-20250514-v1:0'), | ||
| schema: z.object({ | ||
| summary: z.string().describe('Summary of the PDF document'), | ||
| keyPoints: z.array(z.string()).describe('Key points from the PDF'), | ||
| }), | ||
| messages: [ | ||
| { | ||
| role: 'user', | ||
| content: [ | ||
| { | ||
| type: 'text', | ||
| text: 'Summarize this PDF and provide key points.', | ||
| }, | ||
| { | ||
| type: 'file', | ||
| data: fs.readFileSync('./data/ai.pdf'), | ||
| mediaType: 'application/pdf', | ||
| providerOptions: { | ||
| bedrock: { | ||
| citations: { enabled: true }, | ||
| }, | ||
| }, | ||
|
Comment on lines
+26
to
+30
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. the example behaves the same to me when I remove the providerOptions. What effect should it have? Does it need a PDF with certain content for it to work? There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Yes. There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Do you have such a pdf that we could use for this example? There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. @gr2m |
||
| }, | ||
| ], | ||
| }, | ||
| ], | ||
| }); | ||
|
|
||
| console.log('Response:', JSON.stringify(result, null, 2)); | ||
| } | ||
|
|
||
| main().catch(console.error); | ||
| Original file line number | Diff line number | Diff line change |
|---|---|---|
|
|
@@ -21,13 +21,26 @@ import { | |
| BedrockUserMessage, | ||
| } from './bedrock-api-types'; | ||
| import { bedrockReasoningMetadataSchema } from './bedrock-chat-language-model'; | ||
| import { bedrockFilePartProviderOptions } from './bedrock-chat-options'; | ||
|
|
||
| function getCachePoint( | ||
| providerMetadata: SharedV2ProviderMetadata | undefined, | ||
| ): BedrockCachePoint | undefined { | ||
| return providerMetadata?.bedrock?.cachePoint as BedrockCachePoint | undefined; | ||
| } | ||
|
|
||
| async function shouldEnableCitations( | ||
| providerMetadata: SharedV2ProviderMetadata | undefined, | ||
| ): Promise<boolean> { | ||
| const bedrockOptions = await parseProviderOptions({ | ||
|
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. I followed anthropic's implementation. |
||
| provider: 'bedrock', | ||
| providerOptions: providerMetadata, | ||
| schema: bedrockFilePartProviderOptions, | ||
| }); | ||
|
|
||
| return bedrockOptions?.citations?.enabled ?? false; | ||
| } | ||
|
|
||
| export async function convertToBedrockChatMessages( | ||
| prompt: LanguageModelV3Prompt, | ||
| ): Promise<{ | ||
|
|
@@ -108,11 +121,18 @@ export async function convertToBedrockChatMessages( | |
| }); | ||
| } | ||
|
|
||
| const enableCitations = await shouldEnableCitations( | ||
| part.providerOptions, | ||
| ); | ||
|
|
||
| bedrockContent.push({ | ||
| document: { | ||
| format: getBedrockDocumentFormat(part.mediaType), | ||
| name: generateDocumentName(), | ||
| source: { bytes: convertToBase64(part.data) }, | ||
| ...(enableCitations && { | ||
| citations: { enabled: true }, | ||
| }), | ||
| }, | ||
| }); | ||
| } | ||
|
|
||


Uh oh!
There was an error while loading. Please reload this page.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Accessing citation parameters in response is not supported yet. May be another PR.
visual understanding reference:
https://docs.claude.com/en/docs/build-with-claude/pdf-support#amazon-bedrock-pdf-support