Skip to content

Commit 3cb4a47

Browse files
committed
fix: Loop nodes cannot be used in the knowledge base workflow
1 parent 4ea9c55 commit 3cb4a47

File tree

10 files changed

+50
-28
lines changed

10 files changed

+50
-28
lines changed

apps/application/flow/i_step_node.py

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -185,7 +185,8 @@ def get_answer_list(self) -> List[Answer] | None:
185185
return None
186186
reasoning_content_enable = self.context.get('model_setting', {}).get('reasoning_content_enable', False)
187187
return [
188-
Answer(self.answer_text, self.view_type, self.runtime_node_id, self.workflow_params['chat_record_id'], {},
188+
Answer(self.answer_text, self.view_type, self.runtime_node_id, self.workflow_params.get('chat_record_id'),
189+
{},
189190
self.runtime_node_id, self.context.get('reasoning_content', '') if reasoning_content_enable else '')]
190191

191192
def __init__(self, node, workflow_params, workflow_manage, up_node_id_list=None,
Lines changed: 15 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,15 @@
1+
# coding=utf-8
2+
"""
3+
@project: maxkb
4+
@Author:虎
5+
@file: workflow_manage.py
6+
@date:2024/1/9 17:40
7+
@desc:
8+
"""
9+
from application.flow.i_step_node import KnowledgeFlowParamsSerializer
10+
from application.flow.loop_workflow_manage import LoopWorkflowManage
11+
12+
13+
class KnowledgeLoopWorkflowManage(LoopWorkflowManage):
14+
def get_params_serializer_class(self):
15+
return KnowledgeFlowParamsSerializer

apps/application/flow/knowledge_workflow_manage.py

Lines changed: 1 addition & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -8,13 +8,12 @@
88
"""
99
import traceback
1010
from concurrent.futures import ThreadPoolExecutor
11-
from typing import List
1211

1312
from django.db.models import QuerySet
1413
from django.utils.translation import get_language
1514

1615
from application.flow.common import Workflow
17-
from application.flow.i_step_node import WorkFlowPostHandler, KnowledgeFlowParamsSerializer, INode
16+
from application.flow.i_step_node import WorkFlowPostHandler, KnowledgeFlowParamsSerializer
1817
from application.flow.workflow_manage import WorkflowManage
1918
from common.handle.base_to_response import BaseToResponse
2019
from common.handle.impl.response.system_to_response import SystemToResponse

apps/application/flow/loop_workflow_manage.py

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -116,7 +116,7 @@ def stream(self):
116116
close_old_connections()
117117
language = get_language()
118118
self.run_chain_async(self.start_node, None, language)
119-
return self.await_result()
119+
return self.await_result(is_cleanup=False)
120120

121121
def get_index(self):
122122
return self.loop_params.get('index')

apps/application/flow/step_node/loop_node/i_loop_node.py

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -54,5 +54,5 @@ def _run(self):
5454
array[1:])
5555
return self.execute(**{**self.node_params_serializer.data, "array": array}, **self.flow_params_serializer.data)
5656

57-
def execute(self, loop_type, array, number, loop_body, stream, **kwargs) -> NodeResult:
57+
def execute(self, loop_type, array, number, loop_body, **kwargs) -> NodeResult:
5858
pass

apps/application/flow/step_node/loop_node/impl/base_loop_node.py

Lines changed: 21 additions & 16 deletions
Original file line numberDiff line numberDiff line change
@@ -11,7 +11,7 @@
1111

1212
from django.utils.translation import gettext as _
1313

14-
from application.flow.common import Answer
14+
from application.flow.common import Answer, WorkflowMode
1515
from application.flow.i_step_node import NodeResult, WorkFlowPostHandler, INode
1616
from application.flow.step_node.loop_node.i_loop_node import ILoopNode
1717
from application.flow.tools import Reasoning
@@ -197,6 +197,7 @@ def loop(workflow_manage_new_instance, node: INode, generate_loop):
197197
insert_or_replace(loop_node_data, index, instance.get_runtime_details())
198198
insert_or_replace(loop_answer_data, index,
199199
get_answer_list(instance, child_node_node_dict, node.runtime_node_id))
200+
instance._cleanup()
200201
if break_outer:
201202
break
202203
node.context['is_interrupt_exec'] = is_interrupt_exec
@@ -206,7 +207,7 @@ def loop(workflow_manage_new_instance, node: INode, generate_loop):
206207
node.context["item"] = current_index
207208

208209

209-
def get_write_context(loop_type, array, number, loop_body, stream):
210+
def get_write_context(loop_type, array, number, loop_body):
210211
def inner_write_context(node_variable: Dict, workflow_variable: Dict, node: INode, workflow):
211212
if loop_type == 'ARRAY':
212213
return loop(node_variable['workflow_manage_new_instance'], node, generate_loop_array(array))
@@ -248,27 +249,31 @@ def get_answer_list(self) -> List[Answer] | None:
248249
def get_loop_context(self):
249250
return self.context
250251

251-
def execute(self, loop_type, array, number, loop_body, stream, **kwargs) -> NodeResult:
252+
def execute(self, loop_type, array, number, loop_body, **kwargs) -> NodeResult:
252253
from application.flow.loop_workflow_manage import LoopWorkflowManage, Workflow
254+
from application.flow.knowledge_loop_workflow_manage import KnowledgeLoopWorkflowManage
253255
def workflow_manage_new_instance(loop_data, global_data, start_node_id=None,
254256
start_node_data=None, chat_record=None, child_node=None):
255-
workflow_manage = LoopWorkflowManage(Workflow.new_instance(loop_body), self.workflow_manage.params,
256-
LoopWorkFlowPostHandler(
257-
self.workflow_manage.work_flow_post_handler.chat_info),
258-
self.workflow_manage,
259-
loop_data,
260-
self.get_loop_context,
261-
base_to_response=LoopToResponse(),
262-
start_node_id=start_node_id,
263-
start_node_data=start_node_data,
264-
chat_record=chat_record,
265-
child_node=child_node
266-
)
257+
workflow_mode = WorkflowMode.KNOWLEDGE_LOOP if WorkflowMode.KNOWLEDGE == self.workflow_manage.flow.workflow_mode else WorkflowMode.APPLICATION_LOOP
258+
c = KnowledgeLoopWorkflowManage if workflow_mode == WorkflowMode.KNOWLEDGE_LOOP else LoopWorkflowManage
259+
workflow_manage = c(Workflow.new_instance(loop_body, workflow_mode),
260+
self.workflow_manage.params,
261+
LoopWorkFlowPostHandler(
262+
self.workflow_manage.work_flow_post_handler.chat_info),
263+
self.workflow_manage,
264+
loop_data,
265+
self.get_loop_context,
266+
base_to_response=LoopToResponse(),
267+
start_node_id=start_node_id,
268+
start_node_data=start_node_data,
269+
chat_record=chat_record,
270+
child_node=child_node
271+
)
267272

268273
return workflow_manage
269274

270275
return NodeResult({'workflow_manage_new_instance': workflow_manage_new_instance}, {},
271-
_write_context=get_write_context(loop_type, array, number, loop_body, stream),
276+
_write_context=get_write_context(loop_type, array, number, loop_body),
272277
_is_interrupt=_is_interrupt_exec)
273278

274279
def get_loop_context_data(self):

apps/application/flow/step_node/loop_start_node/impl/base_start_node.py

Lines changed: 3 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -10,6 +10,7 @@
1010

1111
from rest_framework import serializers
1212

13+
from application.flow.common import WorkflowMode
1314
from application.flow.i_step_node import NodeResult
1415
from application.flow.step_node.loop_start_node.i_loop_start_node import ILoopStarNode
1516

@@ -31,7 +32,8 @@ def execute(self, **kwargs) -> NodeResult:
3132
'index': loop_params.get("index"),
3233
'item': loop_params.get("item")
3334
}
34-
self.workflow_manage.chat_context = self.workflow_manage.get_chat_info().get_chat_variable()
35+
if WorkflowMode.APPLICATION_LOOP == self.workflow_manage.flow.workflow_mode:
36+
self.workflow_manage.chat_context = self.workflow_manage.get_chat_info().get_chat_variable()
3537
return NodeResult(node_variable, {})
3638

3739
def get_details(self, index: int, **kwargs):

apps/application/flow/workflow_manage.py

Lines changed: 3 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -316,7 +316,7 @@ def is_run(self, timeout=0.5):
316316
except Exception as e:
317317
return True
318318

319-
def await_result(self):
319+
def await_result(self, is_cleanup=True):
320320
try:
321321
while self.is_run():
322322
while True:
@@ -344,7 +344,8 @@ def await_result(self):
344344
'',
345345
[],
346346
'', True, message_tokens, answer_tokens, {})
347-
self._cleanup()
347+
if is_cleanup:
348+
self._cleanup()
348349

349350
def run_chain_async(self, current_node, node_result_future, language='zh'):
350351
future = executor.submit(self.run_chain_manage, current_node, node_result_future, language)

ui/src/components/workflow-dropdown-menu/index.vue

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -20,7 +20,9 @@ defineProps({
2020
})
2121
const kw: any = {
2222
[WorkflowMode.Application]: ApplicationDropdownMenu,
23+
[WorkflowMode.ApplicationLoop]: ApplicationDropdownMenu,
2324
[WorkflowMode.Knowledge]: KnowledgeDropdownMenu,
25+
[WorkflowMode.KnowledgeLoop]: KnowledgeDropdownMenu,
2426
}
2527
</script>
2628
<style lang="scss">

ui/src/workflow/common/NodeContainer.vue

Lines changed: 1 addition & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -46,10 +46,7 @@
4646
<p class="mt-8 lighter">
4747
<span>{{ $t('views.workflow.condition.front') }}</span>
4848
<el-select v-model="condition" size="small" style="width: 60px; margin: 0 8px">
49-
<el-option
50-
:label="$t('views.workflow.condition.AND')"
51-
value="AND"
52-
/>
49+
<el-option :label="$t('views.workflow.condition.AND')" value="AND" />
5350
<el-option :label="$t('views.workflow.condition.OR')" value="OR" />
5451
</el-select>
5552
<span>{{ $t('views.workflow.condition.text') }}</span>

0 commit comments

Comments
 (0)