Created
October 13, 2023 09:14
-
-
Save chgasparoto/fcbc4468281fa4adf0d2f85c6ee761e1 to your computer and use it in GitHub Desktop.
AWS Bedrock Typescript
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
import { | |
BedrockRuntimeClient, | |
InvokeModelCommand, | |
} from '@aws-sdk/client-bedrock-runtime'; | |
import { APIGatewayProxyEventV2, APIGatewayProxyResultV2 } from 'aws-lambda'; | |
const client = new BedrockRuntimeClient({}); | |
export const handler = async ( | |
event: APIGatewayProxyEventV2, | |
): Promise<APIGatewayProxyResultV2> => { | |
console.log({ | |
message: 'Event received', | |
data: JSON.stringify(event), | |
}); | |
if (event.requestContext.http.method !== 'POST') { | |
return 'Only POST method is allowed'; | |
} | |
try { | |
const { prompt, temperature } = JSON.parse(event.body); | |
if (!prompt || prompt.length < 10) { | |
return 'The `prompt` param must be set in the request body and must have at leat 10 chars'; | |
} | |
const temp = parseFloat(temperature.replace(',', '.')); | |
if (temperature && isNaN(temp)) { | |
return 'The `temperature` param must be a number'; | |
} | |
const maxTokens = process.env.MAX_TOKENS; | |
const command = new InvokeModelCommand({ | |
modelId: 'ai21.j2-ultra-v1', | |
body: JSON.stringify({ | |
prompt, | |
maxTokens: maxTokens ? parseInt(maxTokens, 10) : 1525, | |
temperature: temp || 0.7, | |
topP: 1, | |
}), | |
accept: 'application/json', | |
contentType: 'application/json', | |
}); | |
const response = await client.send(command); | |
const result = JSON.parse(Buffer.from(response.body).toString()); | |
console.log({ | |
message: 'Response from Bedrock', | |
data: JSON.stringify(result), | |
}); | |
return result.completions[0].data.text; | |
} catch (error) { | |
console.error({ | |
message: error.message, | |
data: error, | |
}); | |
return 'Something went wrong. Please, try again or contact the admin'; | |
} | |
}; |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment