Skip to content

Commit

Permalink
feat: Update the lambda to accept json as input; parametrize the mode…
Browse files Browse the repository at this point in the history
…l invocation
  • Loading branch information
krzysiekb committed Dec 29, 2023
1 parent d132281 commit 2115faf
Showing 1 changed file with 24 additions and 5 deletions.
29 changes: 24 additions & 5 deletions backend/lambda/backend_lambda.py
Original file line number Diff line number Diff line change
Expand Up @@ -2,22 +2,41 @@

import boto3

bedrock = boto3.client('bedrock-runtime')


def handler(event, context):
body = event['body']
print('request: {}'.format(json.dumps(body)))
request = json.loads(event['body'])
print('request: {}'.format(json.dumps(request)))

# Invoke the model
input_message = request.get('inputMessage')
body = json.dumps({
'inputText': input_message,
'textGenerationConfig': {
'temperature': 0.0,
'topP': 0.9,
'maxTokenCount': 300,
}
})

bedrock = boto3.client('bedrock-runtime')
response = bedrock.invoke_model(
modelId='amazon.titan-text-lite-v1',
contentType='application/json',
body=body
)

print('response: {}'.format(json.dumps(response)))

# Return the response
results = json.loads(response.get('body')).get('results')
output_message = results[0].get('outputText')

return {
'statusCode': 200,
'headers': {
'Content-Type': response['contentType']
'Content-Type': 'application/json'
},
'body': json.loads(response['body'])
'inputMessage': input_message,
'outputMessage': output_message
}

0 comments on commit 2115faf

Please sign in to comment.