-
Notifications
You must be signed in to change notification settings - Fork 1.6k
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Support Bedrock Anthropic Stream for Messages API (#1226)
- Loading branch information
1 parent
adccb26
commit fe72ac4
Showing
6 changed files
with
163 additions
and
1 deletion.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
42 changes: 42 additions & 0 deletions
42
examples/next-aws-bedrock/app/api/chat-anthropic-v3/route.ts
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,42 @@ | ||
import { | ||
BedrockRuntimeClient, | ||
InvokeModelWithResponseStreamCommand, | ||
} from '@aws-sdk/client-bedrock-runtime'; | ||
import { AWSBedrockAnthropicMessagesStream, StreamingTextResponse } from 'ai'; | ||
import { experimental_buildAnthropicMessages } from 'ai/prompts'; | ||
|
||
// IMPORTANT! Set the runtime to edge | ||
export const runtime = 'edge'; | ||
|
||
const bedrockClient = new BedrockRuntimeClient({ | ||
region: process.env.AWS_REGION ?? 'us-east-1', | ||
credentials: { | ||
accessKeyId: process.env.AWS_ACCESS_KEY_ID ?? '', | ||
secretAccessKey: process.env.AWS_SECRET_ACCESS_KEY ?? '', | ||
}, | ||
}); | ||
|
||
export async function POST(req: Request) { | ||
// Extract the `prompt` from the body of the request | ||
const { messages } = await req.json(); | ||
|
||
// Ask Claude for a streaming chat completion given the prompt | ||
const bedrockResponse = await bedrockClient.send( | ||
new InvokeModelWithResponseStreamCommand({ | ||
modelId: 'anthropic.claude-3-haiku-20240307-v1:0', | ||
contentType: 'application/json', | ||
accept: 'application/json', | ||
body: JSON.stringify({ | ||
anthropic_version: 'bedrock-2023-05-31', | ||
message: experimental_buildAnthropicMessages(messages), | ||
max_tokens: 300, | ||
}), | ||
}), | ||
); | ||
|
||
// Convert the response into a friendly text-stream | ||
const stream = AWSBedrockAnthropicMessagesStream(bedrockResponse); | ||
|
||
// Respond with the stream | ||
return new StreamingTextResponse(stream); | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters