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

[prototype] bedrock anthropic with invokemodel #2613

Draft
wants to merge 5 commits into
base: main
Choose a base branch
from
Draft
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
34 changes: 34 additions & 0 deletions examples/ai-core/src/generate-object/amazon-bedrock-anthropic.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,34 @@
import { bedrock } from '@ai-sdk/amazon-bedrock';
import { generateObject } from 'ai';
import dotenv from 'dotenv';
import { z } from 'zod';

dotenv.config();

async function main() {
const result = await generateObject({
model: bedrock.anthropicLanguageModel(
'anthropic.claude-3-5-sonnet-20240620-v1:0',
),
schema: z.object({
recipe: z.object({
name: z.string(),
ingredients: z.array(
z.object({
name: z.string(),
amount: z.string(),
}),
),
steps: z.array(z.string()),
}),
}),
prompt: 'Generate a lasagna recipe.',
});

console.log(JSON.stringify(result.object, null, 2));
console.log();
console.log('Token usage:', result.usage);
console.log('Finish reason:', result.finishReason);
}

main().catch(console.error);
21 changes: 21 additions & 0 deletions examples/ai-core/src/generate-text/amazon-bedrock-anthropic.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,21 @@
import { bedrock } from '@ai-sdk/amazon-bedrock';
import { generateText } from 'ai';
import dotenv from 'dotenv';

dotenv.config();

async function main() {
const result = await generateText({
model: bedrock.anthropicLanguageModel(
'anthropic.claude-3-haiku-20240307-v1:0',
),
prompt: 'Invent a new holiday and describe its traditions.',
});

console.log(result.text);
console.log();
console.log('Token usage:', result.usage);
console.log('Finish reason:', result.finishReason);
}

main().catch(console.error);
34 changes: 34 additions & 0 deletions examples/ai-core/src/stream-object/amazon-bedrock-anthropic.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,34 @@
import { bedrock } from '@ai-sdk/amazon-bedrock';
import { streamObject } from 'ai';
import dotenv from 'dotenv';
import { z } from 'zod';

dotenv.config();

async function main() {
const result = await streamObject({
model: bedrock.anthropicLanguageModel(
'anthropic.claude-3-5-sonnet-20240620-v1:0',
),
schema: z.object({
characters: z.array(
z.object({
name: z.string(),
class: z
.string()
.describe('Character class, e.g. warrior, mage, or thief.'),
description: z.string(),
}),
),
}),
prompt:
'Generate 3 character descriptions for a fantasy role playing game.',
});

for await (const partialObject of result.partialObjectStream) {
console.clear();
console.log(partialObject);
}
}

main().catch(console.error);
24 changes: 24 additions & 0 deletions examples/ai-core/src/stream-text/amazon-bedrock-anthropic.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,24 @@
import { bedrock } from '@ai-sdk/amazon-bedrock';
import { streamText } from 'ai';
import dotenv from 'dotenv';

dotenv.config();

async function main() {
const result = await streamText({
model: bedrock.anthropicLanguageModel(
'anthropic.claude-3-haiku-20240307-v1:0',
),
prompt: 'Invent a new holiday and describe its traditions.',
});

for await (const textPart of result.textStream) {
process.stdout.write(textPart);
}

console.log();
console.log('Token usage:', await result.usage);
console.log('Finish reason:', await result.finishReason);
}

main().catch(console.error);
46 changes: 46 additions & 0 deletions packages/amazon-bedrock/src/anthropic/anthropic-messages-prompt.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,46 @@
export type AnthropicMessagesPrompt = {
system?: string;
messages: AnthropicMessage[];
};

export type AnthropicMessage = AnthropicUserMessage | AnthropicAssistantMessage;

export interface AnthropicUserMessage {
role: 'user';
content: Array<
AnthropicTextContent | AnthropicImageContent | AnthropicToolResultContent
>;
}

export interface AnthropicAssistantMessage {
role: 'assistant';
content: Array<AnthropicTextContent | AnthropicToolCallContent>;
}

export interface AnthropicTextContent {
type: 'text';
text: string;
}

export interface AnthropicImageContent {
type: 'image';
source: {
type: 'base64';
media_type: string;
data: string;
};
}

export interface AnthropicToolCallContent {
type: 'tool_use';
id: string;
name: string;
input: unknown;
}

export interface AnthropicToolResultContent {
type: 'tool_result';
tool_use_id: string;
content: unknown;
is_error?: boolean;
}
Loading
Loading