From bb0cb1cecc0c2b802a71f12f6dbc5021d5a336a5 Mon Sep 17 00:00:00 2001 From: shniubobo Date: Thu, 13 Feb 2025 22:22:51 +0000 Subject: [PATCH] fix: Regression on reasoning time PR #1253 fixed reasoning time calculation for APIs that return reasoning content in `delta.content`, but introduced a regression for those returning it in `delta.reasoning_content`. This commit fixes the regression. Fixes #1593 --- src/renderer/src/providers/OpenAIProvider.ts | 16 ++++++++++++---- 1 file changed, 12 insertions(+), 4 deletions(-) diff --git a/src/renderer/src/providers/OpenAIProvider.ts b/src/renderer/src/providers/OpenAIProvider.ts index ecfebc02..210b73e1 100644 --- a/src/renderer/src/providers/OpenAIProvider.ts +++ b/src/renderer/src/providers/OpenAIProvider.ts @@ -206,6 +206,10 @@ export default class OpenAIProvider extends BaseProvider { return streamOutput } + let hasReasoningContent = false + const isReasoningJustDone = (delta: OpenAI.Chat.Completions.ChatCompletionChunk.Choice.Delta) => + hasReasoningContent ? !!delta?.content : delta?.content === '' + let time_first_token_millsec = 0 let time_first_content_millsec = 0 const start_time_millsec = new Date().getTime() @@ -243,20 +247,24 @@ export default class OpenAIProvider extends BaseProvider { break } + const delta = chunk.choices[0]?.delta + + // @ts-expect-error `reasoning_content` not supported by OpenAI for now + if (delta?.reasoning_content) { + hasReasoningContent = true + } + if (time_first_token_millsec == 0) { time_first_token_millsec = new Date().getTime() - start_time_millsec } - //修复逻辑判断,当content为时,time_first_content_millsec才会被赋值,原有代码无意义. - if (time_first_content_millsec == 0 && chunk.choices[0]?.delta?.content == '') { + if (time_first_content_millsec == 0 && isReasoningJustDone(delta)) { time_first_content_millsec = new Date().getTime() } const time_completion_millsec = new Date().getTime() - start_time_millsec const time_thinking_millsec = time_first_content_millsec ? time_first_content_millsec - start_time_millsec : 0 - const delta = chunk.choices[0]?.delta - // Extract citations from the raw response if available const citations = (chunk as OpenAI.Chat.Completions.ChatCompletionChunk & { citations?: string[] })?.citations