diff --git a/.env.example b/.env.example index 0df009a..abd8b6c 100644 --- a/.env.example +++ b/.env.example @@ -22,6 +22,7 @@ CLOUDFLARE_IMGBED_URL=https://xxxxxxx.pages.dev/upload CLOUDFLARE_IMGBED_AUTH_CODE=xxxxxxxxx ########################################################################## #########################stream_optimizer 相关配置######################## +STREAM_OPTIMIZER_ENABLED=false STREAM_MIN_DELAY=0.016 STREAM_MAX_DELAY=0.024 STREAM_SHORT_TEXT_THRESHOLD=10 diff --git a/app/config/config.py b/app/config/config.py index 78cf94a..c6650e7 100644 --- a/app/config/config.py +++ b/app/config/config.py @@ -36,6 +36,7 @@ class Settings(BaseSettings): CLOUDFLARE_IMGBED_AUTH_CODE: str = "" # 流式输出优化器配置 + STREAM_OPTIMIZER_ENABLED: bool = False STREAM_MIN_DELAY: float = DEFAULT_STREAM_MIN_DELAY STREAM_MAX_DELAY: float = DEFAULT_STREAM_MAX_DELAY STREAM_SHORT_TEXT_THRESHOLD: int = DEFAULT_STREAM_SHORT_TEXT_THRESHOLD diff --git a/app/service/chat/gemini_chat_service.py b/app/service/chat/gemini_chat_service.py index bda8484..8bec12b 100644 --- a/app/service/chat/gemini_chat_service.py +++ b/app/service/chat/gemini_chat_service.py @@ -167,9 +167,8 @@ class GeminiChatService: json.loads(line), model, stream=True ) text = self._extract_text_from_response(response_data) - - # 如果有文本内容,使用流式输出优化器处理 - if text: + # 如果有文本内容,且开启了流式输出优化器,则使用流式输出优化器处理 + if text and settings.STREAM_OPTIMIZER_ENABLED: # 使用流式输出优化器处理文本输出 async for ( optimized_chunk diff --git a/app/service/chat/openai_chat_service.py b/app/service/chat/openai_chat_service.py index 4b94816..4f3cf95 100644 --- a/app/service/chat/openai_chat_service.py +++ b/app/service/chat/openai_chat_service.py @@ -215,7 +215,7 @@ class OpenAIChatService: if openai_chunk: # 提取文本内容 text = self._extract_text_from_openai_chunk(openai_chunk) - if text: + if text and settings.STREAM_OPTIMIZER_ENABLED: # 使用流式输出优化器处理文本输出 async for ( optimized_chunk