Skip to content
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

feat (provider/amazon-bedrock): remove dependence on AWS SDK Bedrock library #4582

Draft
wants to merge 10 commits into
base: main
Choose a base branch
from
Draft
5 changes: 5 additions & 0 deletions .changeset/soft-ladybugs-repeat.md
Original file line number Diff line number Diff line change
@@ -0,0 +1,5 @@
---
'@ai-sdk/amazon-bedrock': patch
---

feat (provider/amazon-bedrock): remove dependence on AWS SDK Bedrock library
6 changes: 4 additions & 2 deletions examples/ai-core/src/generate-text/amazon-bedrock.ts
Original file line number Diff line number Diff line change
Expand Up @@ -4,8 +4,10 @@ import 'dotenv/config';

async function main() {
const result = await generateText({
model: bedrock('anthropic.claude-3-haiku-20240307-v1:0'),
prompt: 'Invent a new holiday and describe its traditions.',
model: bedrock(
'arn:aws:bedrock:us-east-2:474668406012:inference-profile/us.anthropic.claude-3-5-sonnet-20240620-v1:0',
Copy link
Collaborator

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

we need to ideally figure out how to make it backwards compatible, or we need to announce breaking changes somehow

),
prompt: 'Give me an overview of the New Zealand Fiordland National Park.',
});

console.log(result.text);
Expand Down
4 changes: 3 additions & 1 deletion examples/ai-core/src/stream-text/amazon-bedrock-image.ts
Original file line number Diff line number Diff line change
Expand Up @@ -5,7 +5,9 @@ import fs from 'node:fs';

async function main() {
const result = streamText({
model: bedrock('anthropic.claude-3-haiku-20240307-v1:0'),
model: bedrock(
'arn:aws:bedrock:us-east-2:474668406012:inference-profile/us.anthropic.claude-3-5-sonnet-20240620-v1:0',
),
maxTokens: 512,
messages: [
{
Expand Down
6 changes: 4 additions & 2 deletions examples/ai-core/src/stream-text/amazon-bedrock.ts
Original file line number Diff line number Diff line change
Expand Up @@ -4,8 +4,10 @@ import 'dotenv/config';

async function main() {
const result = streamText({
model: bedrock('anthropic.claude-3-haiku-20240307-v1:0'),
prompt: 'Invent a new holiday and describe its traditions.',
model: bedrock(
'arn:aws:bedrock:us-east-2:474668406012:inference-profile/us.anthropic.claude-3-5-sonnet-20240620-v1:0',
),
prompt: 'Give me an overview of the New Zealand Fiordland National Park.',
});

for await (const textPart of result.textStream) {
Expand Down
6 changes: 3 additions & 3 deletions packages/amazon-bedrock/package.json
Original file line number Diff line number Diff line change
Expand Up @@ -32,13 +32,13 @@
"dependencies": {
"@ai-sdk/provider": "1.0.7",
"@ai-sdk/provider-utils": "2.1.6",
"@aws-sdk/client-bedrock-runtime": "^3.663.0"
"@smithy/eventstream-codec": "^4.0.1",
"@smithy/util-utf8": "^4.0.0",
"aws4fetch": "^1.0.20"
},
"devDependencies": {
"@smithy/types": "^3.5.0",
"@types/node": "^18.19.54",
"@vercel/ai-tsconfig": "workspace:*",
"aws-sdk-client-mock": "^4.0.2",
"tsup": "^8.3.0",
"typescript": "5.6.3",
"zod": "3.23.8"
Expand Down
120 changes: 120 additions & 0 deletions packages/amazon-bedrock/src/bedrock-api-types.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,120 @@
import { Resolvable } from '@ai-sdk/provider-utils';

export type BedrockHeadersFunction = (args: {
url: string;
headers: Record<string, string | undefined>;
body: unknown;
}) => Resolvable<Record<string, string | undefined>>;

export interface BedrockConverseInput {
system?: Array<{ text: string }>;
messages: Array<{
role: string;
content: Array<BedrockContentBlock>;
}>;
toolConfig?: BedrockToolConfiguration;
inferenceConfig?: {
maxTokens?: number;
temperature?: number;
topP?: number;
stopSequences?: string[];
};
additionalModelRequestFields?: Record<string, any>;
guardrailConfig?: any;
}

export interface BedrockGuardrailConfiguration {
guardrails?: Array<{
name: string;
description?: string;
parameters?: Record<string, any>;
}>;
}

export type BedrockGuardrailStreamConfiguration = BedrockGuardrailConfiguration;

export interface BedrockToolInputSchema {
json: Record<string, any>;
}

export interface BedrockTool {
toolSpec: {
name: string;
description?: string;
inputSchema: { json: any };
};
}

export interface BedrockToolConfiguration {
tools?: BedrockTool[];
toolChoice?:
| { tool: { name: string } }
| { auto: {} }
| { any: {} }
| undefined;
}

export type BedrockStopReason =
| 'stop'
| 'stop_sequence'
| 'end_turn'
| 'length'
| 'max_tokens'
| 'content-filter'
| 'content_filtered'
| 'guardrail_intervened'
| 'tool-calls'
| 'tool_use';

export type BedrockImageFormat = 'jpeg' | 'png' | 'gif';
export type BedrockDocumentFormat = 'pdf' | 'txt' | 'md';

export interface BedrockDocumentBlock {
document: {
format: BedrockDocumentFormat;
name: string;
source: {
bytes: string;
};
};
}

export interface BedrockGuardrailConverseContentBlock {
guardContent: any;
}

export interface BedrockImageBlock {
image: {
format: BedrockImageFormat;
source: {
bytes: string;
};
};
}

export interface BedrockToolResultBlock {
toolResult: {
toolUseId: string;
content: Array<{ text: string }>;
};
}

export interface BedrockToolUseBlock {
toolUse: {
toolUseId: string;
name: string;
input: Record<string, any>;
};
}

export interface BedrockTextBlock {
text: string;
}

export type BedrockContentBlock =
| BedrockDocumentBlock
| BedrockGuardrailConverseContentBlock
| BedrockImageBlock
| BedrockTextBlock
| BedrockToolResultBlock
| BedrockToolUseBlock;
Loading
Loading