-
Notifications
You must be signed in to change notification settings - Fork 0
/
index.py
176 lines (145 loc) · 4.95 KB
/
index.py
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
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
import os
import json
import uuid
from datetime import datetime
from adapters.registry import registry
from aws_lambda_powertools import Logger, Tracer
from aws_lambda_powertools.utilities import parameters
from aws_lambda_powertools.utilities.batch import BatchProcessor, EventType
from aws_lambda_powertools.utilities.batch.exceptions import BatchProcessingError
from aws_lambda_powertools.utilities.data_classes.sqs_event import SQSRecord
from aws_lambda_powertools.utilities.typing import LambdaContext
from genai_core.utils.websocket import send_to_client
from genai_core.types import ChatbotAction
processor = BatchProcessor(event_type=EventType.SQS)
tracer = Tracer()
logger = Logger()
AWS_REGION = os.environ["AWS_REGION"]
API_KEYS_SECRETS_ARN = os.environ["API_KEYS_SECRETS_ARN"]
sequence_number = 0
def on_llm_new_token(
connection_id, user_id, session_id, self, token, run_id, *args, **kwargs
):
global sequence_number
sequence_number += 1
run_id = str(run_id)
send_to_client(
{
"type": "text",
"action": ChatbotAction.LLM_NEW_TOKEN.value,
"connectionId": connection_id,
"userId": user_id,
"timestamp": str(int(round(datetime.now().timestamp()))),
"data": {
"sessionId": session_id,
"token": {
"runId": run_id,
"sequenceNumber": sequence_number,
"value": token,
},
},
}
)
def handle_heartbeat(record):
connection_id = record["connectionId"]
user_id = record["userId"]
send_to_client(
{
"type": "text",
"action": ChatbotAction.HEARTBEAT.value,
"connectionId": connection_id,
"timestamp": str(int(round(datetime.now().timestamp()))),
"userId": user_id,
}
)
def handle_run(record):
connection_id = record["connectionId"]
user_id = record["userId"]
data = record["data"]
provider = data["provider"]
model_id = data["modelName"]
mode = data["mode"]
prompt = data["text"]
workspace_id = data.get("workspaceId", None)
session_id = data.get("sessionId")
if not session_id:
session_id = str(uuid.uuid4())
adapter = registry.get_adapter(f"{provider}.{model_id}")
adapter.on_llm_new_token = lambda *args, **kwargs: on_llm_new_token(
connection_id, user_id, session_id, *args, **kwargs
)
model = adapter(
model_id=model_id,
mode=mode,
session_id=session_id,
user_id=user_id,
model_kwargs=data.get("modelKwargs", {}),
)
response = model.run(
prompt=prompt,
workspace_id=workspace_id,
)
logger.info(response)
send_to_client(
{
"type": "text",
"action": ChatbotAction.FINAL_RESPONSE.value,
"connectionId": connection_id,
"timestamp": str(int(round(datetime.now().timestamp()))),
"userId": user_id,
"data": response,
}
)
@tracer.capture_method
def record_handler(record: SQSRecord):
payload: str = record.body
message: dict = json.loads(payload)
detail: dict = json.loads(message["Message"])
logger.info(detail)
if detail["action"] == ChatbotAction.RUN.value:
handle_run(detail)
elif detail["action"] == ChatbotAction.HEARTBEAT.value:
handle_heartbeat(detail)
def handle_failed_records(records):
for triplet in records:
status, error, record = triplet
payload: str = record.body
message: dict = json.loads(payload)
detail: dict = json.loads(message["Message"])
logger.info(detail)
connection_id = detail["connectionId"]
user_id = detail["userId"]
data = detail.get("data", {})
session_id = data.get("sessionId", "")
send_to_client(
{
"type": "text",
"action": "error",
"direction": "OUT",
"connectionId": connection_id,
"userId": user_id,
"timestamp": str(int(round(datetime.now().timestamp()))),
"data": {
"sessionId": session_id,
"content": str(error),
"type": "text",
},
}
)
@logger.inject_lambda_context(log_event=True)
@tracer.capture_lambda_handler
def handler(event, context: LambdaContext):
batch = event["Records"]
api_keys = parameters.get_secret(API_KEYS_SECRETS_ARN, transform="json")
for key in api_keys:
os.environ[key] = api_keys[key]
try:
with processor(records=batch, handler=record_handler):
processed_messages = processor.process()
except BatchProcessingError as e:
logger.error(e)
logger.info(processed_messages)
handle_failed_records(
message for message in processed_messages if message[0] == "fail"
)
return processor.response()