import {
BedrockRuntimeClient,
InvokeModelCommand,
} from "@aws-sdk/client-bedrock-runtime";
const chat: Chat = async ({ userMessage }) => {
const { userMessage } = content;
// Create request parameters
const payload = {
anthropic_version: "bedrock-2023-05-31",
max_tokens: 1000,
messages: [
{ role: "user", content: [{ type: "text", text: userMessage }] },
],
};
// Call the API
const client = new BedrockRuntimeClient({
region: process.env.AWS_REGION,
});
const apiResponse = await client.send(
new InvokeModelCommand({
contentType: "application/json",
body: JSON.stringify(payload),
modelId: "anthropic.claude-3-haiku-20240307-v1:0",
})
);
// Parse the response
const decodedResponseBody = new TextDecoder().decode(apiResponse.body);
const responseBody = JSON.parse(decodedResponseBody);
// Return the response
return {
message: responseBody.content[0].text,
};
};