Skip to content

Commit c8b3095

Browse files
authored
Merge pull request #1566 from lightrabbit/feature/openai-compatible-deepseek-reasoning-support
feat: openai-compatible deepseek/qwq reasoning support
2 parents 0d81c0d + 7114cef commit c8b3095

File tree

1 file changed

+15
-3
lines changed

1 file changed

+15
-3
lines changed

src/api/providers/openai.ts

Lines changed: 15 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -14,6 +14,7 @@ import { convertToR1Format } from "../transform/r1-format"
1414
import { convertToSimpleMessages } from "../transform/simple-format"
1515
import { ApiStream, ApiStreamUsageChunk } from "../transform/stream"
1616
import { BaseProvider } from "./base-provider"
17+
import { XmlMatcher } from "../../utils/xml-matcher"
1718

1819
const DEEP_SEEK_DEFAULT_TEMPERATURE = 0.6
1920

@@ -134,15 +135,23 @@ export class OpenAiHandler extends BaseProvider implements SingleCompletionHandl
134135

135136
const stream = await this.client.chat.completions.create(requestOptions)
136137

138+
const matcher = new XmlMatcher(
139+
"think",
140+
(chunk) =>
141+
({
142+
type: chunk.matched ? "reasoning" : "text",
143+
text: chunk.data,
144+
}) as const,
145+
)
146+
137147
let lastUsage
138148

139149
for await (const chunk of stream) {
140150
const delta = chunk.choices[0]?.delta ?? {}
141151

142152
if (delta.content) {
143-
yield {
144-
type: "text",
145-
text: delta.content,
153+
for (const chunk of matcher.update(delta.content)) {
154+
yield chunk
146155
}
147156
}
148157

@@ -156,6 +165,9 @@ export class OpenAiHandler extends BaseProvider implements SingleCompletionHandl
156165
lastUsage = chunk.usage
157166
}
158167
}
168+
for (const chunk of matcher.final()) {
169+
yield chunk
170+
}
159171

160172
if (lastUsage) {
161173
yield this.processUsageMetrics(lastUsage, modelInfo)

0 commit comments

Comments
 (0)