-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathlambda_function.py
More file actions
63 lines (55 loc) · 1.94 KB
/
Copy pathlambda_function.py
File metadata and controls
63 lines (55 loc) · 1.94 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
import boto3
import json
bedrock = boto3.client('bedrock-runtime', region_name='us-east-1')
def lambda_handler(event, context):
# Handle preflight (OPTIONS) request for CORS
if event['httpMethod'] == 'OPTIONS':
return {
'statusCode': 200,
'headers': {
'Access-Control-Allow-Origin': '*',
'Access-Control-Allow-Headers': '*',
'Access-Control-Allow-Methods': 'OPTIONS,POST,GET'
},
'body': json.dumps('Preflight OK')
}
# Parse incoming JSON
body = json.loads(event['body'])
user_message = body.get('message', '')
history = body.get('history', [])
# Optional: construct prompt with history
conversation = ""
for turn in history:
conversation += f"User: {turn['user']}\nAssistant: {turn['assistant']}\n"
conversation += f"User: {user_message}\nAssistant:"
# Create payload for Titan
request_body = {
"inputText": conversation,
"textGenerationConfig": {
"maxTokenCount": 300,
"temperature": 0.7,
"topP": 0.9,
"stopSequences": []
}
}
# Call Titan model
response = bedrock.invoke_model(
modelId='amazon.titan-text-express-v1',
body=json.dumps(request_body),
contentType='application/json',
accept='application/json'
)
# Extract model output
result = json.loads(response['body'].read())
reply = result.get('results', [{}])[0].get('outputText', '')
# Return response with CORS headers
return {
'statusCode': 200,
'headers': {
'Content-Type': 'application/json',
'Access-Control-Allow-Origin': '*',
'Access-Control-Allow-Headers': '*',
'Access-Control-Allow-Methods': 'OPTIONS,POST,GET'
},
'body': json.dumps({'response': reply})
}