Skip to content

Add reasoning content support (for DeepSeek format API) #840

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Open
wants to merge 1 commit into
base: master
Choose a base branch
from
Open
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
34 changes: 32 additions & 2 deletions src/services/apis/custom-api.mjs
Original file line number Diff line number Diff line change
Expand Up @@ -38,6 +38,14 @@ export async function generateAnswersWithCustomApi(

let answer = ''
let finished = false

let with_reasoning = false
let has_reasoning_start = false
let has_reasoning_end = false

const REASONING_START_SIGN = '[Think]\n\n'
Copy link
Preview

Copilot AI May 31, 2025

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

[nitpick] Magic strings for reasoning markers are declared inline; extracting these markers into a shared constants module or configuration will improve maintainability and consistency.

Copilot uses AI. Check for mistakes.

const REASONING_END_SIGN = '\n\n[Response]\n\n'

const finish = () => {
finished = true
pushRecord(session, question, answer)
Expand Down Expand Up @@ -76,12 +84,34 @@ export async function generateAnswersWithCustomApi(
if (data.response) answer = data.response
else {
const delta = data.choices[0]?.delta?.content
const delta_reasoning = data.choices[0]?.delta?.reasoning_content
const content = data.choices[0]?.message?.content
const content_reasoning = data.choices[0]?.message?.reasoning_content
const text = data.choices[0]?.text
if (delta !== undefined) {
answer += delta
// streaming handling
if (delta_reasoning) {
with_reasoning = true
if (!has_reasoning_start) {
answer += REASONING_START_SIGN
has_reasoning_start = true
}
answer += delta_reasoning
}
if (delta) {
if (with_reasoning && !has_reasoning_end) {
answer += REASONING_END_SIGN
has_reasoning_end = true
}
answer += delta
}
} else if (content) {
answer = content
// single response handling
if (content_reasoning) {
answer = REASONING_START_SIGN + content_reasoning + REASONING_END_SIGN + content
} else {
answer = content
}
} else if (text) {
answer += text
}
Expand Down