|
| 1 | +# coding=utf-8 |
| 2 | +""" |
| 3 | + @project: MaxKB |
| 4 | + @Author:虎 |
| 5 | + @file: base_loop_node.py |
| 6 | + @date:2025/3/11 18:24 |
| 7 | + @desc: |
| 8 | +""" |
| 9 | +import time |
| 10 | +from typing import Dict |
| 11 | + |
| 12 | +from application.flow.i_step_node import NodeResult, WorkFlowPostHandler, INode |
| 13 | +from application.flow.step_node.loop_node.i_loop_node import ILoopNode |
| 14 | +from application.flow.tools import Reasoning |
| 15 | +from common.handle.impl.response.loop_to_response import LoopToResponse |
| 16 | + |
| 17 | + |
| 18 | +def _write_context(node_variable: Dict, workflow_variable: Dict, node: INode, workflow, answer: str, |
| 19 | + reasoning_content: str): |
| 20 | + node.context['answer'] = answer |
| 21 | + node.context['run_time'] = time.time() - node.context['start_time'] |
| 22 | + node.context['reasoning_content'] = reasoning_content |
| 23 | + if workflow.is_result(node, NodeResult(node_variable, workflow_variable)): |
| 24 | + node.answer_text = answer |
| 25 | + |
| 26 | + |
| 27 | +def write_context_stream(node_variable: Dict, workflow_variable: Dict, node: INode, workflow): |
| 28 | + """ |
| 29 | + 写入上下文数据 (流式) |
| 30 | + @param node_variable: 节点数据 |
| 31 | + @param workflow_variable: 全局数据 |
| 32 | + @param node: 节点 |
| 33 | + @param workflow: 工作流管理器 |
| 34 | + """ |
| 35 | + response = node_variable.get('result') |
| 36 | + answer = '' |
| 37 | + reasoning_content = '' |
| 38 | + for chunk in response: |
| 39 | + content_chunk = chunk.get('content', '') |
| 40 | + reasoning_content_chunk = chunk.get('reasoning_content', '') |
| 41 | + reasoning_content += reasoning_content_chunk |
| 42 | + answer += content_chunk |
| 43 | + yield {'content': content_chunk, |
| 44 | + 'reasoning_content': reasoning_content_chunk} |
| 45 | + |
| 46 | + _write_context(node_variable, workflow_variable, node, workflow, answer, reasoning_content) |
| 47 | + |
| 48 | + |
| 49 | +def write_context(node_variable: Dict, workflow_variable: Dict, node: INode, workflow): |
| 50 | + """ |
| 51 | + 写入上下文数据 |
| 52 | + @param node_variable: 节点数据 |
| 53 | + @param workflow_variable: 全局数据 |
| 54 | + @param node: 节点实例对象 |
| 55 | + @param workflow: 工作流管理器 |
| 56 | + """ |
| 57 | + response = node_variable.get('result') |
| 58 | + model_setting = node.context.get('model_setting', |
| 59 | + {'reasoning_content_enable': False, 'reasoning_content_end': '</think>', |
| 60 | + 'reasoning_content_start': '<think>'}) |
| 61 | + reasoning = Reasoning(model_setting.get('reasoning_content_start'), model_setting.get('reasoning_content_end')) |
| 62 | + reasoning_result = reasoning.get_reasoning_content(response) |
| 63 | + reasoning_result_end = reasoning.get_end_reasoning_content() |
| 64 | + content = reasoning_result.get('content') + reasoning_result_end.get('content') |
| 65 | + if 'reasoning_content' in response.response_metadata: |
| 66 | + reasoning_content = response.response_metadata.get('reasoning_content', '') |
| 67 | + else: |
| 68 | + reasoning_content = reasoning_result.get('reasoning_content') + reasoning_result_end.get('reasoning_content') |
| 69 | + _write_context(node_variable, workflow_variable, node, workflow, content, reasoning_content) |
| 70 | + |
| 71 | + |
| 72 | +def loop_number(number, loop_body): |
| 73 | + """ |
| 74 | + 指定次数循环 |
| 75 | + @return: |
| 76 | + """ |
| 77 | + pass |
| 78 | + |
| 79 | + |
| 80 | +def loop_array(array, loop_body): |
| 81 | + """ |
| 82 | + 循环数组 |
| 83 | + @return: |
| 84 | + """ |
| 85 | + pass |
| 86 | + |
| 87 | + |
| 88 | +def loop_loop(loop_body): |
| 89 | + """ |
| 90 | + 无线循环 |
| 91 | + @return: |
| 92 | + """ |
| 93 | + pass |
| 94 | + |
| 95 | + |
| 96 | +class LoopWorkFlowPostHandler(WorkFlowPostHandler): |
| 97 | + def handler(self, chat_id, |
| 98 | + chat_record_id, |
| 99 | + answer, |
| 100 | + workflow): |
| 101 | + pass |
| 102 | + |
| 103 | + |
| 104 | +class BaseLoopNode(ILoopNode): |
| 105 | + def save_context(self, details, workflow_manage): |
| 106 | + self.context['result'] = details.get('result') |
| 107 | + self.answer_text = str(details.get('result')) |
| 108 | + |
| 109 | + def execute(self, loop_type, array, number, loop_body, stream, **kwargs) -> NodeResult: |
| 110 | + from application.flow.workflow_manage import WorkflowManage, Flow |
| 111 | + workflow_manage = WorkflowManage(Flow.new_instance(loop_body), self.workflow_manage.params, |
| 112 | + LoopWorkFlowPostHandler(self.workflow_manage.work_flow_post_handler.chat_info |
| 113 | + , |
| 114 | + self.workflow_manage.work_flow_post_handler.client_id, |
| 115 | + self.workflow_manage.work_flow_post_handler.client_type) |
| 116 | + , base_to_response=LoopToResponse()) |
| 117 | + result = workflow_manage.stream() |
| 118 | + return NodeResult({"result": result}, {}, _write_context=write_context_stream) |
| 119 | + |
| 120 | + def get_details(self, index: int, **kwargs): |
| 121 | + return { |
| 122 | + 'name': self.node.properties.get('stepName'), |
| 123 | + "index": index, |
| 124 | + "result": self.context.get('result'), |
| 125 | + "params": self.context.get('params'), |
| 126 | + 'run_time': self.context.get('run_time'), |
| 127 | + 'type': self.node.type, |
| 128 | + 'status': self.status, |
| 129 | + 'err_message': self.err_message |
| 130 | + } |
0 commit comments