Skip to content

Commit fded7c9

Browse files
author
yaojin
committed
prompt 完整消息存储
1 parent 939dc38 commit fded7c9

2 files changed

Lines changed: 13 additions & 6 deletions

File tree

src/backend/bisheng/api/services/workstation.py

Lines changed: 4 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -124,10 +124,12 @@ def get_chat_history(cls, chat_id: str, size: int = 4):
124124
for one in messages:
125125
# bug fix When constructing multi-turn dialogues, the input and response of
126126
# the user and the assistant were reversed, leading to incorrect question-and-answer sequences.
127+
extra = json.loads(one.extra) or {}
128+
content = extra['prompt'] if 'prompt' in extra else one.message
127129
if one.category == MsgCategory.Question:
128-
chat_history.append(HumanMessage(content=one.message))
130+
chat_history.append(HumanMessage(content=content))
129131
elif one.category == MsgCategory.Answer:
130-
chat_history.append(AIMessage(content=one.message))
132+
chat_history.append(AIMessage(content=content))
131133
logger.info(f'loaded {len(chat_history)} chat history for chat_id {chat_id}')
132134
return chat_history
133135

src/backend/bisheng/api/v1/workstation.py

Lines changed: 9 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -168,7 +168,7 @@ async def upload_file(
168168
"""
169169
# 读取文件内容
170170
# 保存文件
171-
file_path = save_uploaded_file(file.file, 'bisheng', file.filename)
171+
file_path = save_uploaded_file(file.file, 'bisheng', unquote(file.filename))
172172

173173
# 返回文件路径
174174
return resp_200(
@@ -207,7 +207,7 @@ async def gen_title(conversationId: str = Body(..., description='', embed=True),
207207
else:
208208
# 如果标题不存在,则返回空值
209209
return resp_500(
210-
"Title not found or method not implemented for the conversation\'s endpoint")
210+
data="Title not found or method not implemented for the conversation\'s endpoint")
211211

212212

213213
@router.get('/messages/{conversationId}')
@@ -370,13 +370,18 @@ async def event_stream():
370370
chunks = WorkStationService.queryChunksFromDB(data.text, login_user)
371371
prompt = wsConfig.knowledgeBase.prompt.format(
372372
retrieved_file_content='\n'.join(chunks)[:max_token], question=data.text)
373-
374-
if data.files:
373+
elif data.files:
375374
# 获取文件全文
376375
filecontent = '\n'.join(
377376
[getFileContent(file.get('filepath')) for file in data.files])
378377
prompt = wsConfig.fileUpload.prompt.format(file_content=filecontent[:max_token],
379378
question=data.text)
379+
if prompt != data.text:
380+
# 需要将原始消息存储
381+
extra = json.loads(message.extra)
382+
extra['prompt'] = prompt
383+
message.extra = json.dumps(extra)
384+
ChatMessageDao.insert_one(message)
380385
except Exception as e:
381386
logger.error(f'Error in processing the prompt: {e}')
382387
error = True

0 commit comments

Comments
 (0)