From 1da1721ec2ee0522a585958a0e3083d6e21f90f4 Mon Sep 17 00:00:00 2001 From: Phantom <59059173+EurFelux@users.noreply.github.com> Date: Wed, 20 Aug 2025 15:11:07 +0800 Subject: [PATCH] feat(openai): handle special tokens for zhipu api (#9323) MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit * feat(openai): 添加对智谱特殊token的过滤处理 在OpenAIAPIClient中添加对智谱AI特殊token的过滤逻辑,避免不需要的token被输出 * docs(OpenAIApiClient): 添加注释 * refactor(zhipu): 重命名并更新智谱特殊token处理逻辑 将 ZHIPU_SPECIAL_TOKENS_TO_FILTER 重命名为 ZHIPU_RESULT_TOKENS 以更准确描述用途 修改智谱API特殊token处理逻辑,不再过滤而是用**标记结果token --- .../aiCore/clients/openai/OpenAIApiClient.ts | 23 +++++++++++++++---- src/renderer/src/config/models.ts | 3 +++ 2 files changed, 21 insertions(+), 5 deletions(-) diff --git a/src/renderer/src/aiCore/clients/openai/OpenAIApiClient.ts b/src/renderer/src/aiCore/clients/openai/OpenAIApiClient.ts index 7fcae3823c..e9133400dc 100644 --- a/src/renderer/src/aiCore/clients/openai/OpenAIApiClient.ts +++ b/src/renderer/src/aiCore/clients/openai/OpenAIApiClient.ts @@ -26,7 +26,8 @@ import { isSupportedThinkingTokenQwenModel, isSupportedThinkingTokenZhipuModel, isVisionModel, - MODEL_SUPPORTED_REASONING_EFFORT + MODEL_SUPPORTED_REASONING_EFFORT, + ZHIPU_RESULT_TOKENS } from '@renderer/config/models' import { isSupportArrayContentProvider, @@ -896,10 +897,22 @@ export class OpenAIAPIClient extends OpenAIBaseClient< accumulatingText = true } // logger.silly('enqueue TEXT_DELTA') - controller.enqueue({ - type: ChunkType.TEXT_DELTA, - text: contentSource.content - }) + // 处理特殊token + // 智谱api的一个chunk中只会输出一个token,因而使用 ===,避免正常内容被误判 + if ( + context.provider.id === SystemProviderIds.zhipu && + ZHIPU_RESULT_TOKENS.some((pattern) => contentSource.content === pattern) + ) { + controller.enqueue({ + type: ChunkType.TEXT_DELTA, + text: '**' // strong + }) + } else { + controller.enqueue({ + type: ChunkType.TEXT_DELTA, + text: contentSource.content + }) + } } else { accumulatingText = false } diff --git a/src/renderer/src/config/models.ts b/src/renderer/src/config/models.ts index d424cf1402..a4fdcf0fb9 100644 --- a/src/renderer/src/config/models.ts +++ b/src/renderer/src/config/models.ts @@ -3238,3 +3238,6 @@ export const isGPT5SeriesModel = (model: Model) => { const modelId = getLowerBaseModelName(model.id) return modelId.includes('gpt-5') } + +// zhipu 视觉推理模型用这组 special token 标记推理结果 +export const ZHIPU_RESULT_TOKENS = ['<|begin_of_box|>', '<|end_of_box|>'] as const