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

Amazon Bedrock: adding stream support for Cohere #1301

Merged
merged 3 commits into from
Apr 9, 2024
Merged
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
Original file line number Diff line number Diff line change
Expand Up @@ -175,6 +175,7 @@ export async function POST(req: Request) {
accept: 'application/json',
body: JSON.stringify({
prompt: buildPrompt(messages),
stream: true,
max_tokens: 300,
}),
}),
Expand Down
1 change: 1 addition & 0 deletions examples/next-aws-bedrock/app/api/chat-cohere/route.ts
Original file line number Diff line number Diff line change
Expand Up @@ -41,6 +41,7 @@ export async function POST(req: Request) {
accept: 'application/json',
body: JSON.stringify({
prompt: buildPrompt(messages),
stream: true,
max_tokens: 300,
}),
}),
Expand Down
30 changes: 27 additions & 3 deletions packages/core/streams/aws-bedrock-stream.test.ts
Original file line number Diff line number Diff line change
Expand Up @@ -122,7 +122,15 @@ describe('AWS Bedrock', () => {
const response = new StreamingTextResponse(stream);

expect(await readAllChunks(response)).toEqual([
' Hi! How can I help you today?',
' Hello',
'!',
' How',
' can',
' I',
' help',
' you',
' today',
'?',
]);
});

Expand All @@ -141,7 +149,15 @@ describe('AWS Bedrock', () => {
const response = new StreamingTextResponse(stream, {}, data);

expect(await readAllChunks(response)).toEqual([
'0:" Hi! How can I help you today?"\n',
'0:" Hello"\n',
'0:"!"\n',
'0:" How"\n',
'0:" can"\n',
'0:" I"\n',
'0:" help"\n',
'0:" you"\n',
'0:" today"\n',
'0:"?"\n',
]);
});

Expand All @@ -162,7 +178,15 @@ describe('AWS Bedrock', () => {

expect(await readAllChunks(response)).toEqual([
'2:[{"t1":"v1"}]\n',
'0:" Hi! How can I help you today?"\n',
'0:" Hello"\n',
'0:"!"\n',
'0:" How"\n',
'0:" can"\n',
'0:" I"\n',
'0:" help"\n',
'0:" you"\n',
'0:" today"\n',
'0:"?"\n',
]);
});
});
Expand Down
8 changes: 1 addition & 7 deletions packages/core/streams/aws-bedrock-stream.ts
Original file line number Diff line number Diff line change
Expand Up @@ -49,13 +49,7 @@ export function AWSBedrockCohereStream(
response: AWSBedrockResponse,
callbacks?: AIStreamCallbacksAndOptions,
): ReadableStream {
return AWSBedrockStream(
response,
callbacks,
// As of 2023-11-17, Bedrock does not support streaming for Cohere,
// so we take the full generation:
chunk => chunk.generations?.[0]?.text,
);
return AWSBedrockStream(response, callbacks, chunk => chunk?.text);
}

export function AWSBedrockLlama2Stream(
Expand Down
18 changes: 9 additions & 9 deletions packages/core/tests/snapshots/aws-bedrock.ts
Original file line number Diff line number Diff line change
Expand Up @@ -29,15 +29,15 @@ export const bedrockAnthropicV3Chunks = [
];

export const bedrockCohereChunks = [
{
generations: [
{
finish_reason: 'COMPLETE',
id: '0c72c609-c8ac-4601-a323-653db56c8d80',
text: ' Hi! How can I help you today?',
},
],
},
{ is_finished: false, text: ' Hello' },
{ is_finished: false, text: '!' },
{ is_finished: false, text: ' How' },
{ is_finished: false, text: ' can' },
{ is_finished: false, text: ' I' },
{ is_finished: false, text: ' help' },
{ is_finished: false, text: ' you' },
{ is_finished: false, text: ' today' },
{ is_finished: false, text: '?' },
];

export const bedrockLlama2Chunks = [
Expand Down
Loading