mirror of
https://github.com/langbot-app/LangBot.git
synced 2026-06-02 03:55:55 +00:00
feat: add note for remove_think & remove dify remove cot code
This commit is contained in:
@@ -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)
|
||||
|
||||
@@ -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(
|
||||
'<details style="color:gray;background-color: #f8f8f8;padding: 8px;border-radius: 4px;" open> <summary> Thinking... </summary>'
|
||||
):
|
||||
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'<details style="color:gray;background-color: #f8f8f8;padding: 8px;border-radius: 4px;" open> <summary> Thinking... </summary>.*?</details>',
|
||||
'',
|
||||
resp_text,
|
||||
flags=re.DOTALL,
|
||||
)
|
||||
|
||||
if self.pipeline_config['ai']['dify-service-api']['thinking-convert'] == 'plain':
|
||||
pattern = r'<details style="color:gray;background-color: #f8f8f8;padding: 8px;border-radius: 4px;" open> <summary> Thinking... </summary>(.*?)</details>'
|
||||
thinking_text = re.search(pattern, resp_text, flags=re.DOTALL)
|
||||
content_text = re.sub(pattern, '', resp_text, flags=re.DOTALL)
|
||||
return f'<think>{thinking_text.group(1)}</think>\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 '</think>' in chunk['answer'] and not think_end:
|
||||
import re
|
||||
|
||||
content = re.sub(r'^\n</think>', '', 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 '</think>' in chunk['answer'] and not think_end:
|
||||
import re
|
||||
|
||||
content = re.sub(r'^\n</think>', '', 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 '</think>' in chunk['data']['text'] and not think_end:
|
||||
import re
|
||||
|
||||
content = re.sub(r'^\n</think>', '', 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"]}'
|
||||
)
|
||||
)
|
||||
|
||||
@@ -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
|
||||
}
|
||||
}
|
||||
}
|
||||
@@ -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 <think>...</think>
|
||||
zh_Hans: 转换成 <think>...</think>
|
||||
- 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
|
||||
|
||||
@@ -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
|
||||
|
||||
Reference in New Issue
Block a user