Repository navigation
Expand file tree
/
Copy pathlambda_new.py
More file actions
261 lines (239 loc) · 9.6 KB
/
Copy pathlambda_new.py
File metadata and controls
261 lines (239 loc) · 9.6 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
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
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
import re
from time import time
from uuid import uuid4
from common import BedrockKnowledgeBase, FlorenceOpenAI, Storage
from const import (
BEDROCK_DATA_SOURCE_ID,
BEDROCK_KNOWLEDGE_BASE_ID,
FLRORENCE_URL,
PROMPT_TEMPLATE,
S3_BUCKET_CHAT_HISTORY,
S3_KEY_PREFIX_CHAT_HISTORY,
SECRET_ID,
WORDS_MAPPING
)
from logger import logger
from utils import generate_sequential_chains, retrieve_secrets, word_replacement
def lambda_handler(event, context):
# parse event body
if "feedback" in event and "message_id" in event:
conversation_id = event.get("conversation_id", "")
message_id = event["message_id"]
feedback = event["feedback"]
if not conversation_id or not message_id or feedback is None:
return {
"statusCode": 400,
"headers": {"Content-Type": "application/json"},
"body": {
"error": [{
"id": str(uuid4()),
"code": "FEEDBACK_PARAM_ERROR",
"detail": "feedback リクエストに必要なパラメータが足りません",
"raw_content": str(event)
}]
}
}
try:
secrets = retrieve_secrets(secret_id=SECRET_ID)
storage_client = Storage(S3_BUCKET_CHAT_HISTORY)
# 履歴取得
history = storage_client.retrieve_history_message(
key_prefix=S3_KEY_PREFIX_CHAT_HISTORY,
conversation_id=conversation_id,
)
# assistant メッセージに feedback を追加(sentence_idで特定)
for msg in reversed(history.get("messages", [])):
if msg.get("sentence_id") == message_id and msg.get("role") == "assistant":
msg["feedback"] = feedback
break
# 更新
_ = storage_client.update_conversation_history(
key_prefix=S3_KEY_PREFIX_CHAT_HISTORY,
conversation_id=conversation_id,
data=history,
)
return {
"statusCode": 200,
"headers": {"Content-Type": "application/json"},
"body": {"message": "Feedback saved"}
}
except Exception as e:
logger.error("フィードバック処理エラー: " + str(e))
return {
"statusCode": 500,
"headers": {"Content-Type": "application/json"},
"body": {
"error": [{
"id": str(uuid4()),
"code": "FEEDBACK_SAVE_ERROR",
"detail": "フィードバック保存時にエラーが発生しました",
"raw_content": str(e)
}]
}
}
messages = event.get("messages", [])
conversation_id = event.get("conversation_id", "")
if not messages:
return {
"statusCode": 400,
"headers": {"Content-Type": "application/json"},
"body": {"error": [{"id": str(uuid4()), "code": "", "detail": "ペイロードにmessagesが存在しない", "raw_content": ""}]}
}
elif not isinstance(messages, list):
return {
"statusCode": 400,
"headers": {"Content-Type": "application/json"},
"body": {"error": [{"id": str(uuid4()), "code": "", "detail": "messagesはarray型ではない", "raw_content": ""}]}
}
last_message = messages[-1]
if not isinstance(last_message, dict):
return {
"statusCode": 400,
"headers": {"Content-Type": "application/json"},
"body": {"error": [{"id": str(uuid4()), "code": "", "detail": "messageはobject型ではない", "raw_content": ""}]}
}
sentence = last_message.get("content", "")
sentence_id = last_message.get("id", "")
if not sentence_id:
return {
"statusCode": 400,
"headers": {"Content-Type": "application/json"},
"body": {"error": [{"id": str(uuid4()), "code": "", "detail": "messageオブジェクトにidが存在しない", "raw_content": ""}]}
}
create_time = last_message.get("create_time", "")
if not create_time:
create_time = int(time())
# initial
secrets = retrieve_secrets(secret_id=SECRET_ID)
cmdbid, apitoken = secrets["CMDBID"], secrets["APITOKEN"]
storage_client = Storage(S3_BUCKET_CHAT_HISTORY)
knowledge_base_client = BedrockKnowledgeBase(
knowledge_base_id=BEDROCK_KNOWLEDGE_BASE_ID
)
openai_client = FlorenceOpenAI(
url=FLRORENCE_URL, cmdbid=cmdbid, apitoken=apitoken
)
logger.info("初期化が正常に完了しました。")
# retrive chat history
if not conversation_id:
conversation_id = str(uuid4()).replace("-", "")
conversation_history = {
"conversation_id": conversation_id,
"create_time": create_time,
"messages": [],
}
else:
try:
conversation_history = storage_client.retrieve_history_message(
key_prefix=S3_KEY_PREFIX_CHAT_HISTORY,
conversation_id=conversation_id,
)
logger.info("会話履歴の取得が正常に完了しました。")
except Exception as e:
logger.warning("会話履歴の取得に失敗しました。\n" f"{e}")
conversation_history = {
"conversation_id": conversation_id,
"create_time": create_time,
"messages": [],
}
# FIXME: combine all user input content as next question
if len(conversation_history.get("messages", [])) > 1:
previous_questions = "\n".join(
[
msg["content"]
for msg in conversation_history.get("messages")
if msg["role"] == "user"
]
)
question = (
previous_questions + "\n" + sentence
if previous_questions
else sentence
)
else:
question = sentence
# query transformation
question = word_replacement(question, WORDS_MAPPING)
# retrieve context
try:
raw_chunks = knowledge_base_client.retrieve(
data_source_id=BEDROCK_DATA_SOURCE_ID,
text=question,
number_of_chunks=5,
search_type="HYBRID"
)
logger.info("ベクトル検索が正常に完了しました。")
except Exception as e:
logger.error("Bedrockにエラーが起きた\n" + str(e))
return {
"statusCode": 500,
"headers": {"Content-Type": "application/json"},
"body": {"error": [{"id": str(uuid4()), "code": "", "detail": "Bedrockにエラーが起きた", "raw_content": str(e)}]}
}
# generate sequential chains
messages = generate_sequential_chains(
prompt_template=PROMPT_TEMPLATE,
context="\n".join(
[str(raw_chunk["content"]).strip() for raw_chunk in raw_chunks]
),
question=question,
conversation_history=conversation_history,
)
logger.info("プロンプト生成が正常に完了しました。")
# request ChatGPT
status_code, response_message = openai_client.chat(
model="gpt-4o", messages=messages
)
if status_code != 200:
logger.error("Bedrockにエラーが起きた\n" + str(response_message))
return {
"statusCode": 500,
"headers": {"Content-Type": "application/json"},
"body": {"error": [{"id": str(uuid4()), "code": "", "detail": "Florenceにエラーが起きた", "raw_content": str(response_message)}]}
}
assistant_message = re.search(r"(?<=<markdown>)(.*?)(?=</markdown>)", str(response_message.get("content")), re.DOTALL).group().strip("\n").strip() \
if re.search(r"(?<=<markdown>)(.*)(?=</markdown>)", str(response_message.get("content")), re.DOTALL) \
else response_message.get("content")
logger.info("Florenceリクエストが正常に完了しました。")
# format reference metadata
reference = []
for idx, raw_chunk in enumerate(raw_chunks):
metadata = raw_chunk["metadata"]
reference.append({
"display_name": f"{metadata['x-display-name']} | ページ: {int(metadata['x-amz-bedrock-kb-document-page-number']) + 1}" if "x-amz-bedrock-kb-document-page-number" in metadata and "DCC手続フォーラム | " not in metadata['x-display-name'] else metadata['x-display-name'],
"display_url": metadata['x-display-url']
})
# update conversation history
conversation_history["messages"].extend(
[
{
"role": "user",
"content": sentence,
"sentence_id": sentence_id,
"create_time": create_time,
"prompt": messages[-1]["content"],
"raw_chunks": raw_chunks,
},
{
"role": "assitant",
"create_time": int(time()),
"content": assistant_message,
},
]
)
_ = storage_client.update_conversation_history(
key_prefix=S3_KEY_PREFIX_CHAT_HISTORY,
conversation_id=conversation_id,
data=conversation_history,
)
return {
"statusCode": 200,
"headers": {"Content-Type": "application/json"},
"body":
{
"message": assistant_message,
"reference": reference,
"raw_chunks": raw_chunks,
"question": question
},
}