Skip to content

Commit 6b4cee1

Browse files
authored
fix: 修复对话使用api调用无法响应数据 (#1755)
1 parent c4c4b6e commit 6b4cee1

File tree

3 files changed

+16
-8
lines changed

3 files changed

+16
-8
lines changed

apps/application/flow/step_node/application_node/impl/base_application_node.py

Lines changed: 5 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -19,8 +19,8 @@ def _is_interrupt_exec(node, node_variable: Dict, workflow_variable: Dict):
1919

2020
def _write_context(node_variable: Dict, workflow_variable: Dict, node: INode, workflow, answer: str):
2121
result = node_variable.get('result')
22-
node.context['child_node'] = node_variable['child_node']
23-
node.context['is_interrupt_exec'] = node_variable['is_interrupt_exec']
22+
node.context['child_node'] = node_variable.get('child_node')
23+
node.context['is_interrupt_exec'] = node_variable.get('is_interrupt_exec')
2424
node.context['message_tokens'] = result.get('usage', {}).get('prompt_tokens', 0)
2525
node.context['answer_tokens'] = result.get('usage', {}).get('completion_tokens', 0)
2626
node.context['answer'] = answer
@@ -81,7 +81,9 @@ def write_context(node_variable: Dict, workflow_variable: Dict, node: INode, wor
8181
@param node: 节点实例对象
8282
@param workflow: 工作流管理器
8383
"""
84-
response = node_variable.get('result')['choices'][0]['message']
84+
response = node_variable.get('result', {}).get('data', {})
85+
node_variable['result'] = {'usage': {'completion_tokens': response.get('completion_tokens'),
86+
'prompt_tokens': response.get('prompt_tokens')}}
8587
answer = response.get('content', '') or "抱歉,没有查找到相关内容,请重新描述您的问题或提供更多信息。"
8688
_write_context(node_variable, workflow_variable, node, workflow, answer)
8789

apps/application/flow/workflow_manage.py

Lines changed: 4 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -328,11 +328,13 @@ def run_block(self):
328328
'message_tokens' in row and row.get('message_tokens') is not None])
329329
answer_tokens = sum([row.get('answer_tokens') for row in details.values() if
330330
'answer_tokens' in row and row.get('answer_tokens') is not None])
331+
answer_text_list = self.get_answer_text_list()
332+
answer_text = '\n\n'.join(answer['content'] for answer in answer_text_list)
331333
self.work_flow_post_handler.handler(self.params['chat_id'], self.params['chat_record_id'],
332-
self.answer,
334+
answer_text,
333335
self)
334336
return self.base_to_response.to_block_response(self.params['chat_id'],
335-
self.params['chat_record_id'], self.answer, True
337+
self.params['chat_record_id'], answer_text, True
336338
, message_tokens, answer_tokens,
337339
_status=status.HTTP_200_OK if self.status == 200 else status.HTTP_500_INTERNAL_SERVER_ERROR)
338340

apps/common/handle/impl/response/system_to_response.py

Lines changed: 7 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -21,15 +21,19 @@ def to_block_response(self, chat_id, chat_record_id, content, is_end, completion
2121
if other_params is None:
2222
other_params = {}
2323
return result.success({'chat_id': str(chat_id), 'id': str(chat_record_id), 'operate': True,
24-
'content': content, 'is_end': is_end, **other_params}, response_status=_status,
24+
'content': content, 'is_end': is_end, **other_params,
25+
'completion_tokens': completion_tokens, 'prompt_tokens': prompt_tokens},
26+
response_status=_status,
2527
code=_status)
2628

27-
def to_stream_chunk_response(self, chat_id, chat_record_id, node_id, up_node_id_list, content, is_end, completion_tokens,
29+
def to_stream_chunk_response(self, chat_id, chat_record_id, node_id, up_node_id_list, content, is_end,
30+
completion_tokens,
2831
prompt_tokens, other_params: dict = None):
2932
if other_params is None:
3033
other_params = {}
3134
chunk = json.dumps({'chat_id': str(chat_id), 'chat_record_id': str(chat_record_id), 'operate': True,
32-
'content': content, 'node_id': node_id, 'up_node_id_list': up_node_id_list, 'is_end': is_end,
35+
'content': content, 'node_id': node_id, 'up_node_id_list': up_node_id_list,
36+
'is_end': is_end,
3337
'usage': {'completion_tokens': completion_tokens,
3438
'prompt_tokens': prompt_tokens,
3539
'total_tokens': completion_tokens + prompt_tokens},

0 commit comments

Comments
 (0)