-
Notifications
You must be signed in to change notification settings - Fork 1
Expand file tree
/
Copy pathlambda_function.py
More file actions
65 lines (54 loc) · 2.1 KB
/
lambda_function.py
File metadata and controls
65 lines (54 loc) · 2.1 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
64
65
import json
import asyncio
import os
from google import genai
client = genai.Client(api_key=os.getenv('GEMINI_API_KEY'), http_options={'api_version': 'v1alpha'})
async def process_chat(message: str) -> str:
chat = client.aio.chats.create(model='gemini-2.0-flash-thinking-exp')
response = await chat.send_message(message)
return response.text
def lambda_handler(event, context):
print(event)
try:
# Parse the body as JSON
body = json.loads(event.get('body', '{}'))
message = body.get('message', '')
if not message:
raise ValueError("Message not found in the request body.")
print(message)
# Process the chat message
response_text = asyncio.run(process_chat(message))
return {
'statusCode': 200,
'body': json.dumps({'response': response_text}),
'headers': {
'Content-Type': 'application/json',
'Access-Control-Allow-Origin': '*',
'Access-Control-Allow-Methods': 'GET, POST, OPTIONS',
'Access-Control-Allow-Headers': 'Content-Type'
}
}
except (json.JSONDecodeError, ValueError) as e:
# Handle JSON parsing errors or missing 'message' key
return {
'statusCode': 400,
'body': json.dumps({'error': str(e)}),
'headers': {
'Content-Type': 'application/json',
'Access-Control-Allow-Origin': '*',
'Access-Control-Allow-Methods': 'GET, POST, OPTIONS',
'Access-Control-Allow-Headers': 'Content-Type'
}
}
except Exception as e:
# Handle other unexpected errors
return {
'statusCode': 500,
'body': json.dumps({'error': 'Internal server error', 'details': str(e)}),
'headers': {
'Content-Type': 'application/json',
'Access-Control-Allow-Origin': '*',
'Access-Control-Allow-Methods': 'GET, POST, OPTIONS',
'Access-Control-Allow-Headers': 'Content-Type'
}
}