Skip to content

Commit 1e5783d

Browse files
authored
LM studio reasoning support (thinking block) (#3719)
lmstudio reasoning support (thinking block) Similar to ollama implementation in #1080
1 parent 8721ab5 commit 1e5783d

File tree

1 file changed

+17
-4
lines changed

1 file changed

+17
-4
lines changed

src/api/providers/lmstudio.ts

Lines changed: 17 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -7,6 +7,7 @@ import { ApiHandlerOptions, ModelInfo, openAiModelInfoSaneDefaults } from "../..
77
import { convertToOpenAiMessages } from "../transform/openai-format"
88
import { ApiStream } from "../transform/stream"
99
import { BaseProvider } from "./base-provider"
10+
import { XmlMatcher } from "../../utils/xml-matcher"
1011

1112
const LMSTUDIO_DEFAULT_TEMPERATURE = 0
1213

@@ -44,18 +45,30 @@ export class LmStudioHandler extends BaseProvider implements SingleCompletionHan
4445
}
4546

4647
const results = await this.client.chat.completions.create(params)
47-
48+
49+
const matcher = new XmlMatcher(
50+
"think",
51+
(chunk) =>
52+
({
53+
type: chunk.matched ? "reasoning" : "text",
54+
text: chunk.data,
55+
}) as const,
56+
)
57+
4858
// Stream handling
4959
// @ts-ignore
5060
for await (const chunk of results) {
5161
const delta = chunk.choices[0]?.delta
62+
5263
if (delta?.content) {
53-
yield {
54-
type: "text",
55-
text: delta.content,
64+
for (const chunk of matcher.update(delta.content)) {
65+
yield chunk
5666
}
5767
}
5868
}
69+
for (const chunk of matcher.final()) {
70+
yield chunk
71+
}
5972
} catch (error) {
6073
// LM Studio doesn't return an error code/body for now
6174
throw new Error(

0 commit comments

Comments
 (0)