mirror of
https://github.com/1Panel-dev/MaxKB.git
synced 2025-12-26 01:33:05 +00:00
fix: 修复模型回答错误时,接口报错
This commit is contained in:
parent
9365b2864c
commit
ffb7dd63db
|
|
@ -65,10 +65,15 @@ def write_context(node_variable: Dict, workflow_variable: Dict, node: INode, wor
|
|||
|
||||
|
||||
def get_to_response_write_context(node_variable: Dict, node: INode):
|
||||
def _write_context(answer):
|
||||
def _write_context(answer, status=200):
|
||||
chat_model = node_variable.get('chat_model')
|
||||
message_tokens = chat_model.get_num_tokens_from_messages(node_variable.get('message_list'))
|
||||
answer_tokens = chat_model.get_num_tokens(answer)
|
||||
if status == 200:
|
||||
answer_tokens = chat_model.get_num_tokens(answer)
|
||||
else:
|
||||
answer_tokens = 0
|
||||
node.err_message = answer
|
||||
node.status = status
|
||||
node.context['message_tokens'] = message_tokens
|
||||
node.context['answer_tokens'] = answer_tokens
|
||||
node.context['answer'] = answer
|
||||
|
|
@ -170,7 +175,7 @@ class BaseChatNode(IChatNode):
|
|||
|
||||
def get_details(self, index: int, **kwargs):
|
||||
return {
|
||||
'name':self.node.properties.get('stepName'),
|
||||
'name': self.node.properties.get('stepName'),
|
||||
"index": index,
|
||||
'run_time': self.context.get('run_time'),
|
||||
'system': self.node_params.get('system'),
|
||||
|
|
|
|||
|
|
@ -29,14 +29,21 @@ def event_content(chat_id, chat_record_id, response, workflow,
|
|||
@param post_handler: 后置处理器
|
||||
"""
|
||||
answer = ''
|
||||
for chunk in response:
|
||||
answer += chunk.content
|
||||
try:
|
||||
for chunk in response:
|
||||
answer += chunk.content
|
||||
yield 'data: ' + json.dumps({'chat_id': str(chat_id), 'id': str(chat_record_id), 'operate': True,
|
||||
'content': chunk.content, 'is_end': False}, ensure_ascii=False) + "\n\n"
|
||||
write_context(answer, 200)
|
||||
post_handler.handler(chat_id, chat_record_id, answer, workflow)
|
||||
yield 'data: ' + json.dumps({'chat_id': str(chat_id), 'id': str(chat_record_id), 'operate': True,
|
||||
'content': chunk.content, 'is_end': False}, ensure_ascii=False) + "\n\n"
|
||||
write_context(answer)
|
||||
post_handler.handler(chat_id, chat_record_id, answer, workflow)
|
||||
yield 'data: ' + json.dumps({'chat_id': str(chat_id), 'id': str(chat_record_id), 'operate': True,
|
||||
'content': '', 'is_end': True}, ensure_ascii=False) + "\n\n"
|
||||
'content': '', 'is_end': True}, ensure_ascii=False) + "\n\n"
|
||||
except Exception as e:
|
||||
answer = str(e)
|
||||
write_context(answer, 500)
|
||||
post_handler.handler(chat_id, chat_record_id, answer, workflow)
|
||||
yield 'data: ' + json.dumps({'chat_id': str(chat_id), 'id': str(chat_record_id), 'operate': True,
|
||||
'content': answer, 'is_end': True}, ensure_ascii=False) + "\n\n"
|
||||
|
||||
|
||||
def to_stream_response(chat_id, chat_record_id, response: Iterator[BaseMessageChunk], workflow, write_context,
|
||||
|
|
|
|||
Loading…
Reference in New Issue