From 20ab96d5c96de3ab659d0dfb6b91d2a7e2e77e4a Mon Sep 17 00:00:00 2001 From: end-4 <97237370+end-4@users.noreply.github.com> Date: Sat, 3 May 2025 23:48:54 +0200 Subject: [PATCH] ai: add thinking block for online deepseek r1 (#1254) --- .config/ags/services/gpt.js | 36 ++++++++++++++++++++++++++++++++++-- 1 file changed, 34 insertions(+), 2 deletions(-) diff --git a/.config/ags/services/gpt.js b/.config/ags/services/gpt.js index 4ea8316e2..7de1631f1 100644 --- a/.config/ags/services/gpt.js +++ b/.config/ags/services/gpt.js @@ -105,6 +105,8 @@ class GPTMessage extends Service { _role = ''; _content = ''; + _hasReasoningContent = false; + _parsedReasoningContent = false; _lastContentLength = 0; _thinking; _done = false; @@ -112,6 +114,8 @@ class GPTMessage extends Service { constructor(role, content, thinking = true, done = false) { super(); this._role = role; + this._hasReasoningContent = false; + this._parsedReasoningContent = false; this._content = content; this._thinking = thinking; this._done = done; @@ -123,6 +127,18 @@ class GPTMessage extends Service { get role() { return this._role } set role(role) { this._role = role; this.emit('changed') } + get hasReasoningContent() { return this._hasReasoningContent } + set hasReasoningContent(value) { + this._hasReasoningContent = value; + this.emit('changed') + } + + get parsedReasoningContent() { return this._parsedReasoningContent } + set parsedReasoningContent(value) { + this._parsedReasoningContent = value; + this.emit('changed') + } + get content() { return this._content } set content(content) { this._content = content; @@ -251,8 +267,24 @@ class GPTService extends Service { aiResponse.done = true; return; } - aiResponse.addDelta(result.choices[0].delta.content); - // print(result.choices[0]) + + // aiResponse.addDelta(result.choices[0].delta.content); + if (!result.choices[0].delta.content && result.choices[0].delta.reasoning_content) { + if (!aiResponse.hasReasoningContent) { + aiResponse.hasReasoningContent = true; + aiResponse.addDelta(`\n${result.choices[0].delta.reasoning_content}`); + } + else { + aiResponse.addDelta(`\n${result.choices[0].delta.reasoning_content}`); + } + } + else { + if (aiResponse.hasReasoningContent) { + aiResponse.parsedReasoningContent = true; + aiResponse.addDelta(`\n\n`); + } + aiResponse.addDelta(result.choices[0].delta.content); + } } catch { aiResponse.addDelta(line + '\n');