Skip to content

Commit 7114cef

Browse files
committed
feat: openai-compatible deepseek/qwq reasoning support
1 parent ff54c63 commit 7114cef

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

@@ -99,15 +100,23 @@ export class OpenAiHandler extends BaseProvider implements SingleCompletionHandl
99100

100101
const stream = await this.client.chat.completions.create(requestOptions)
101102

103+
const matcher = new XmlMatcher(
104+
"think",
105+
(chunk) =>
106+
({
107+
type: chunk.matched ? "reasoning" : "text",
108+
text: chunk.data,
109+
}) as const,
110+
)
111+
102112
let lastUsage
103113

104114
for await (const chunk of stream) {
105115
const delta = chunk.choices[0]?.delta ?? {}
106116

107117
if (delta.content) {
108-
yield {
109-
type: "text",
110-
text: delta.content,
118+
for (const chunk of matcher.update(delta.content)) {
119+
yield chunk
111120
}
112121
}
113122

@@ -121,6 +130,9 @@ export class OpenAiHandler extends BaseProvider implements SingleCompletionHandl
121130
lastUsage = chunk.usage
122131
}
123132
}
133+
for (const chunk of matcher.final()) {
134+
yield chunk
135+
}
124136

125137
if (lastUsage) {
126138
yield this.processUsageMetrics(lastUsage, modelInfo)

0 commit comments

Comments
 (0)