From 87ecb4e519d552bd04bf28296bd6a445c31655b0 Mon Sep 17 00:00:00 2001 From: Junyan Qin Date: Thu, 21 Aug 2025 21:38:58 +0800 Subject: [PATCH] feat: add note for remove_think & remove dify remove cot code --- .../dbm005_pipeline_remove_cot_config.py | 2 +- pkg/provider/runners/difysvapi.py | 50 +++++-------------- templates/default-pipeline-config.json | 3 +- templates/metadata/pipeline/ai.yaml | 22 -------- templates/metadata/pipeline/output.yaml | 6 +-- 5 files changed, 17 insertions(+), 66 deletions(-) diff --git a/pkg/persistence/migrations/dbm005_pipeline_remove_cot_config.py b/pkg/persistence/migrations/dbm005_pipeline_remove_cot_config.py index 14f0beec..8e4d544d 100644 --- a/pkg/persistence/migrations/dbm005_pipeline_remove_cot_config.py +++ b/pkg/persistence/migrations/dbm005_pipeline_remove_cot_config.py @@ -20,7 +20,7 @@ class DBMigratePipelineRemoveCotConfig(migration.DBMigration): config = serialized_pipeline['config'] if 'remove-think' not in config['output']['misc']: - config['output']['misc']['remove-think'] = True + config['output']['misc']['remove-think'] = False await self.ap.persistence_mgr.execute_async( sqlalchemy.update(persistence_pipeline.LegacyPipeline) diff --git a/pkg/provider/runners/difysvapi.py b/pkg/provider/runners/difysvapi.py index 8b66751c..9eb14a6c 100644 --- a/pkg/provider/runners/difysvapi.py +++ b/pkg/provider/runners/difysvapi.py @@ -3,7 +3,6 @@ from __future__ import annotations import typing import json import uuid -import re import base64 @@ -38,33 +37,9 @@ class DifyServiceAPIRunner(runner.RequestRunner): base_url=self.pipeline_config['ai']['dify-service-api']['base-url'], ) - def _try_convert_thinking(self, resp_text: str) -> str: - """尝试转换 Dify 的思考提示""" - if not resp_text.startswith( - '
Thinking... ' - ): - return resp_text - - if self.pipeline_config['ai']['dify-service-api']['thinking-convert'] == 'original': - return resp_text - - if self.pipeline_config['ai']['dify-service-api']['thinking-convert'] == 'remove': - return re.sub( - r'
Thinking... .*?
', - '', - resp_text, - flags=re.DOTALL, - ) - - if self.pipeline_config['ai']['dify-service-api']['thinking-convert'] == 'plain': - pattern = r'
Thinking... (.*?)
' - thinking_text = re.search(pattern, resp_text, flags=re.DOTALL) - content_text = re.sub(pattern, '', resp_text, flags=re.DOTALL) - return f'{thinking_text.group(1)}\n{content_text}' - def _process_thinking_content( - self, - content: str, + self, + content: str, ) -> tuple[str, str]: """处理思维链内容 @@ -354,8 +329,9 @@ class DifyServiceAPIRunner(runner.RequestRunner): yield msg - - async def _chat_messages_chunk(self, query: core_entities.Query) -> typing.AsyncGenerator[llm_entities.MessageChunk, None]: + async def _chat_messages_chunk( + self, query: core_entities.Query + ) -> typing.AsyncGenerator[llm_entities.MessageChunk, None]: """调用聊天助手""" cov_id = query.session.using_conversation.uuid or '' query.variables['conversation_id'] = cov_id @@ -371,8 +347,6 @@ class DifyServiceAPIRunner(runner.RequestRunner): for image_id in image_ids ] - mode = 'basic' # 标记是基础编排还是工作流编排 - basic_mode_pending_chunk = '' inputs = {} @@ -411,6 +385,7 @@ class DifyServiceAPIRunner(runner.RequestRunner): continue if '' in chunk['answer'] and not think_end: import re + content = re.sub(r'^\n', '', chunk['answer']) basic_mode_pending_chunk += content think_end = True @@ -433,13 +408,11 @@ class DifyServiceAPIRunner(runner.RequestRunner): is_final=is_final, ) - if chunk is None: raise errors.DifyAPIError('Dify API 没有返回任何响应,请检查网络连接和API配置') query.session.using_conversation.uuid = chunk['conversation_id'] - async def _agent_chat_messages_chunk( self, query: core_entities.Query ) -> typing.AsyncGenerator[llm_entities.MessageChunk, None]: @@ -496,6 +469,7 @@ class DifyServiceAPIRunner(runner.RequestRunner): continue if '' in chunk['answer'] and not think_end: import re + content = re.sub(r'^\n', '', chunk['answer']) pending_agent_message += content think_end = True @@ -509,7 +483,6 @@ class DifyServiceAPIRunner(runner.RequestRunner): elif chunk['event'] == 'message_end': is_final = True else: - if chunk['event'] == 'agent_thought': if chunk['tool'] != '' and chunk['observation'] != '': # 工具调用结果,跳过 continue @@ -543,7 +516,6 @@ class DifyServiceAPIRunner(runner.RequestRunner): role='assistant', content=[llm_entities.ContentElement.from_image_url(image_url)], is_final=is_final, - ) if chunk['event'] == 'error': @@ -560,7 +532,9 @@ class DifyServiceAPIRunner(runner.RequestRunner): query.session.using_conversation.uuid = chunk['conversation_id'] - async def _workflow_messages_chunk(self, query: core_entities.Query) -> typing.AsyncGenerator[llm_entities.MessageChunk, None]: + async def _workflow_messages_chunk( + self, query: core_entities.Query + ) -> typing.AsyncGenerator[llm_entities.MessageChunk, None]: """调用工作流""" if not query.session.using_conversation.uuid: @@ -618,6 +592,7 @@ class DifyServiceAPIRunner(runner.RequestRunner): continue if '' in chunk['data']['text'] and not think_end: import re + content = re.sub(r'^\n', '', chunk['data']['text']) workflow_contents += content think_end = True @@ -650,7 +625,6 @@ class DifyServiceAPIRunner(runner.RequestRunner): yield msg - if messsage_idx % 8 == 0 or is_final: yield llm_entities.MessageChunk( role='assistant', @@ -694,4 +668,4 @@ class DifyServiceAPIRunner(runner.RequestRunner): else: raise errors.DifyAPIError( f'不支持的 Dify 应用类型: {self.pipeline_config["ai"]["dify-service-api"]["app-type"]}' - ) \ No newline at end of file + ) diff --git a/templates/default-pipeline-config.json b/templates/default-pipeline-config.json index 855e2ac6..2184f982 100644 --- a/templates/default-pipeline-config.json +++ b/templates/default-pipeline-config.json @@ -51,7 +51,6 @@ "base-url": "https://api.dify.ai/v1", "app-type": "chat", "api-key": "your-api-key", - "thinking-convert": "plain", "timeout": 30 }, "dashscope-app-api": { @@ -88,7 +87,7 @@ "at-sender": true, "quote-origin": true, "track-function-calls": false, - "remove-think": true + "remove-think": false } } } \ No newline at end of file diff --git a/templates/metadata/pipeline/ai.yaml b/templates/metadata/pipeline/ai.yaml index 63c56a8a..4564f097 100644 --- a/templates/metadata/pipeline/ai.yaml +++ b/templates/metadata/pipeline/ai.yaml @@ -118,28 +118,6 @@ stages: zh_Hans: API 密钥 type: string required: true - - name: thinking-convert - label: - en_US: CoT Convert - zh_Hans: 思维链转换策略 - type: select - required: true - default: plain - options: - - name: plain - label: - en_US: Convert to ... - zh_Hans: 转换成 ... - - name: original - label: - en_US: Original - zh_Hans: 原始 - - name: remove - label: - en_US: Remove - zh_Hans: 移除 - - - name: dashscope-app-api label: en_US: Aliyun Dashscope App API diff --git a/templates/metadata/pipeline/output.yaml b/templates/metadata/pipeline/output.yaml index 66bb312c..8d571e91 100644 --- a/templates/metadata/pipeline/output.yaml +++ b/templates/metadata/pipeline/output.yaml @@ -110,8 +110,8 @@ stages: en_US: Remove CoT zh_Hans: 删除思维链 description: - en_US: If enabled, LangBot will remove the LLM thought content in response - zh_Hans: 如果启用,将自动删除大模型回复中的模型思考内容 + en_US: 'If enabled, LangBot will remove the LLM thought content in response. Note: When using streaming response, removing CoT may cause the first token to wait for a long time.' + zh_Hans: '如果启用,将自动删除大模型回复中的模型思考内容。注意:当您使用流式响应时,删除思维链可能会导致首个 Token 的等待时间过长' type: boolean required: true - default: true + default: false