From b87b5bfd348b07c4651f010a83099a9efba3f150 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?=E7=8E=8B=E8=89=AF?= <841369634@qq.com> Date: Wed, 6 May 2026 15:41:29 +0800 Subject: [PATCH] feat: Configurable output of `reason` for `Intent recognition` --- .../flow/step_node/intent_node/i_intent_node.py | 1 + .../intent_node/impl/base_intent_node.py | 15 ++++++++++----- .../intent_node/impl/prompt_template.py | 5 +---- .../components/execution-detail-card/index.vue | 2 +- ui/src/locales/lang/en-US/workflow.ts | 7 ++++--- ui/src/locales/lang/zh-CN/workflow.ts | 7 ++++--- ui/src/locales/lang/zh-Hant/workflow.ts | 7 ++++--- .../nodes/intent-classify-node/index.vue | 17 +++++++++++++++++ 8 files changed, 42 insertions(+), 19 deletions(-) diff --git a/apps/application/flow/step_node/intent_node/i_intent_node.py b/apps/application/flow/step_node/intent_node/i_intent_node.py index d22d321c842..30c15001bc4 100644 --- a/apps/application/flow/step_node/intent_node/i_intent_node.py +++ b/apps/application/flow/step_node/intent_node/i_intent_node.py @@ -26,6 +26,7 @@ class IntentNodeSerializer(serializers.Serializer): model_params_setting = serializers.DictField(required=False, label=_("Model parameter settings")) branch = IntentBranchSerializer(many=True) + output_reason = serializers.BooleanField(required=False, default=True, label=_("Output reason")) class IIntentNode(INode): diff --git a/apps/application/flow/step_node/intent_node/impl/base_intent_node.py b/apps/application/flow/step_node/intent_node/impl/base_intent_node.py index b3f1608acc2..cf7ac4cabe7 100644 --- a/apps/application/flow/step_node/intent_node/impl/base_intent_node.py +++ b/apps/application/flow/step_node/intent_node/impl/base_intent_node.py @@ -52,7 +52,7 @@ def save_context(self, details, workflow_manage): self.context['branch_id'] = details.get('branch_id') self.context['category'] = details.get('category') - def execute(self, model_id, dialogue_number, history_chat_record, user_input, branch, + def execute(self, model_id, dialogue_number, history_chat_record, user_input, branch, output_reason, model_params_setting=None, model_id_type=None, model_id_reference=None, **kwargs) -> NodeResult: # 处理引用类型 if model_id_type == 'reference' and model_id_reference: @@ -84,7 +84,7 @@ def execute(self, model_id, dialogue_number, history_chat_record, user_input, br self.context['user_input'] = user_input # 构建分类提示词 - prompt = self.build_classification_prompt(user_input, branch) + prompt = self.build_classification_prompt(user_input, branch, output_reason) # 生成消息列表 system = self.build_system_prompt() @@ -106,7 +106,7 @@ def execute(self, model_id, dialogue_number, history_chat_record, user_input, br 'history_message': history_message, 'user_input': user_input, 'branch_id': matched_branch['id'], - 'reason': self.parse_result_reason(r.content), + 'reason': self.parse_result_reason(r.content) if output_reason is not False else '', 'category': matched_branch.get('content', matched_branch['id']) }, {}, _write_context=write_context) @@ -140,7 +140,7 @@ def build_system_prompt(self) -> str: """构建系统提示词""" return "你是一个专业的意图识别助手,请根据用户输入和意图选项,准确识别用户的真实意图。" - def build_classification_prompt(self, user_input: str, branch: List[Dict]) -> str: + def build_classification_prompt(self, user_input: str, branch: List[Dict], output_reason) -> str: """构建分类提示词""" classification_list = [] @@ -162,9 +162,14 @@ def build_classification_prompt(self, user_input: str, branch: List[Dict]) -> st }) classification_id += 1 + # 构建输出JSON结构, + reason_field = ',\n"reason": ""' if output_reason is not False else '' + output_json = f'{{\n"classificationId": 0{reason_field}\n}}' + return PROMPT_TEMPLATE.format( classification_list=classification_list, - user_input=user_input + user_input=user_input, + output_json=output_json ) def generate_message_list(self, system: str, prompt: str, history_message): diff --git a/apps/application/flow/step_node/intent_node/impl/prompt_template.py b/apps/application/flow/step_node/intent_node/impl/prompt_template.py index 1bcfd61743e..831eef61e36 100644 --- a/apps/application/flow/step_node/intent_node/impl/prompt_template.py +++ b/apps/application/flow/step_node/intent_node/impl/prompt_template.py @@ -20,10 +20,7 @@ - Strictly ensure that the output is in a valid JSON format. - Do not add prefix ```json or suffix ``` - The answer needs to include the following fields such as: -{{ -"classificationId": 0, -"reason": "" -}} +{output_json} ## Limit - Please do not reply in text.""" diff --git a/ui/src/components/execution-detail-card/index.vue b/ui/src/components/execution-detail-card/index.vue index 255ec043837..61622f780ef 100644 --- a/ui/src/components/execution-detail-card/index.vue +++ b/ui/src/components/execution-detail-card/index.vue @@ -281,7 +281,7 @@ {{ $t('chat.executionDetails.currentChat') }}