-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathindex.py
More file actions
36 lines (30 loc) · 1010 Bytes
/
index.py
File metadata and controls
36 lines (30 loc) · 1010 Bytes
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
import json
from pydantic import ValidationError
from lf_toolkit.chat import ChatRequest
from src.module import chat_module
def handler(event, context):
"""
Lambda handler function
"""
if "body" in event:
try:
event = json.loads(event["body"])
except json.JSONDecodeError:
return {
"statusCode": 400,
"body": "Invalid JSON format in the body. Please check the input.",
}
try:
request = ChatRequest.model_validate(event)
except ValidationError as e:
return {"statusCode": 400, "body": e.json()}
try:
result = chat_module(request)
except Exception as e:
return {
"statusCode": 500,
"body": f"An error occurred within the chat_module(): {str(e)}",
}
response = {"statusCode": 200, "body": result.model_dump_json()}
print("Returning response:", " ".join(json.dumps(response, indent=2).splitlines()))
return response