From 1b1a25e68d6ae3c4a4fcabeb12faf6519c07ef18 Mon Sep 17 00:00:00 2001 From: 1e0n Date: Thu, 9 Oct 2025 11:50:49 +0800 Subject: [PATCH] =?UTF-8?q?=E4=BF=AE=E5=A4=8D=E6=B5=81=E5=BC=8F=E5=8F=82?= =?UTF-8?q?=E6=95=B0=E5=A4=84=E7=90=86=EF=BC=9A=E5=B0=8A=E9=87=8D=E5=AE=A2?= =?UTF-8?q?=E6=88=B7=E7=AB=AF=E6=98=8E=E7=A1=AE=E6=8C=87=E5=AE=9A=E7=9A=84?= =?UTF-8?q?stream=E5=8F=82=E6=95=B0?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit - 修正transformers中强制添加stream=true的错误逻辑 - 只有客户端明确指定stream参数时才转发该参数 - 客户端未指定stream时不强制添加,保持原有意图 - 更新routes.js中相应的流式判断逻辑 - 确保非流式请求得到正确处理 --- routes.js | 8 ++++---- transformers/request-anthropic.js | 8 ++++++-- transformers/request-openai.js | 8 ++++++-- 3 files changed, 16 insertions(+), 8 deletions(-) diff --git a/routes.js b/routes.js index 4c1f1a2..3d968c1 100644 --- a/routes.js +++ b/routes.js @@ -89,7 +89,7 @@ async function handleChatCompletions(req, res) { if (model.type === 'anthropic') { transformedRequest = transformToAnthropic(openaiRequest); - const isStreaming = openaiRequest.stream !== false; + const isStreaming = openaiRequest.stream === true; headers = getAnthropicHeaders(authHeader, clientHeaders, isStreaming, modelId); } else if (model.type === 'openai') { transformedRequest = transformToOpenAI(openaiRequest); @@ -120,7 +120,7 @@ async function handleChatCompletions(req, res) { }); } - const isStreaming = transformedRequest.stream !== false; + const isStreaming = transformedRequest.stream === true; if (isStreaming) { res.setHeader('Content-Type', 'text/event-stream'); @@ -268,7 +268,7 @@ async function handleDirectResponses(req, res) { }); } - const isStreaming = openaiRequest.stream !== false; + const isStreaming = openaiRequest.stream === true; if (isStreaming) { // 直接转发流式响应,不做任何转换 @@ -350,7 +350,7 @@ async function handleDirectMessages(req, res) { const clientHeaders = req.headers; // 获取 headers - const isStreaming = anthropicRequest.stream !== false; + const isStreaming = anthropicRequest.stream === true; const headers = getAnthropicHeaders(authHeader, clientHeaders, isStreaming, modelId); // 注入系统提示到 system 字段 diff --git a/transformers/request-anthropic.js b/transformers/request-anthropic.js index 7f533ca..1fea716 100644 --- a/transformers/request-anthropic.js +++ b/transformers/request-anthropic.js @@ -6,10 +6,14 @@ export function transformToAnthropic(openaiRequest) { const anthropicRequest = { model: openaiRequest.model, - messages: [], - stream: openaiRequest.stream !== false + messages: [] }; + // Only add stream parameter if explicitly provided by client + if (openaiRequest.stream !== undefined) { + anthropicRequest.stream = openaiRequest.stream; + } + // Handle max_tokens if (openaiRequest.max_tokens) { anthropicRequest.max_tokens = openaiRequest.max_tokens; diff --git a/transformers/request-openai.js b/transformers/request-openai.js index fb95ea1..2d6a44c 100644 --- a/transformers/request-openai.js +++ b/transformers/request-openai.js @@ -7,10 +7,14 @@ export function transformToOpenAI(openaiRequest) { const targetRequest = { model: openaiRequest.model, input: [], - store: false, - stream: openaiRequest.stream !== false + store: false }; + // Only add stream parameter if explicitly provided by client + if (openaiRequest.stream !== undefined) { + targetRequest.stream = openaiRequest.stream; + } + // Transform max_tokens to max_output_tokens if (openaiRequest.max_tokens) { targetRequest.max_output_tokens = openaiRequest.max_tokens;