This repository was archived by the owner on May 15, 2026. It is now read-only.
-
Notifications
You must be signed in to change notification settings - Fork 3.3k
fix: proper context window loading for LMStudio (fixes #5075) #5814
Closed
Closed
Changes from all commits
Commits
Show all changes
3 commits
Select commit
Hold shift + click to select a range
File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
There are no files selected for viewing
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -1,8 +1,7 @@ | ||
| import { Anthropic } from "@anthropic-ai/sdk" | ||
| import OpenAI from "openai" | ||
| import axios from "axios" | ||
|
|
||
| import { type ModelInfo, openAiModelInfoSaneDefaults, LMSTUDIO_DEFAULT_TEMPERATURE } from "@roo-code/types" | ||
| import { LMSTUDIO_DEFAULT_TEMPERATURE, type ModelInfo, openAiModelInfoSaneDefaults } from "@roo-code/types" | ||
|
|
||
| import type { ApiHandlerOptions } from "../../shared/api" | ||
|
|
||
|
|
@@ -11,22 +10,41 @@ import { XmlMatcher } from "../../utils/xml-matcher" | |
| import { convertToOpenAiMessages } from "../transform/openai-format" | ||
| import { ApiStream } from "../transform/stream" | ||
|
|
||
| import type { ApiHandlerCreateMessageMetadata, SingleCompletionHandler } from "../index" | ||
| import { BaseProvider } from "./base-provider" | ||
| import type { SingleCompletionHandler, ApiHandlerCreateMessageMetadata } from "../index" | ||
| import { flushModels, getModels } from "./fetchers/modelCache" | ||
|
|
||
| type ModelInfoCaching = { | ||
| modelInfo: ModelInfo | ||
| cached: boolean | ||
| } | ||
|
|
||
| export class LmStudioHandler extends BaseProvider implements SingleCompletionHandler { | ||
| protected options: ApiHandlerOptions | ||
| private client: OpenAI | ||
| private cachedModelInfo: ModelInfoCaching = { | ||
| modelInfo: openAiModelInfoSaneDefaults, | ||
| cached: false, | ||
| } | ||
| private lastRecacheTime: number = -1 | ||
|
|
||
| constructor(options: ApiHandlerOptions) { | ||
| super() | ||
| this.options = options | ||
| this.client = new OpenAI({ | ||
| baseURL: (this.options.lmStudioBaseUrl || "http://localhost:1234") + "/v1", | ||
| baseURL: this.getBaseUrl() + "/v1", | ||
| apiKey: "noop", | ||
| }) | ||
| } | ||
|
|
||
| private getBaseUrl(): string { | ||
| if (this.options.lmStudioBaseUrl && this.options.lmStudioBaseUrl.trim() !== "") { | ||
| return this.options.lmStudioBaseUrl.trim() | ||
| } else { | ||
| return "http://localhost:1234" | ||
| } | ||
| } | ||
|
|
||
| override async *createMessage( | ||
| systemPrompt: string, | ||
| messages: Anthropic.Messages.MessageParam[], | ||
|
|
@@ -118,6 +136,29 @@ export class LmStudioHandler extends BaseProvider implements SingleCompletionHan | |
| outputTokens = 0 | ||
| } | ||
|
|
||
| if ( | ||
| !this.cachedModelInfo.cached && | ||
| (this.lastRecacheTime < 0 || Date.now() - this.lastRecacheTime > 30 * 1000) | ||
| ) { | ||
| // assume that if we didn't get a response in 30 seconds | ||
| this.lastRecacheTime = Date.now() // Update last recache time to avoid race condition | ||
|
|
||
| // We need to fetch the model info every time we open a new session | ||
| // to ensure we have the latest context window and other details | ||
| // since LM Studio models can chance their context windows on reload | ||
| await flushModels("lmstudio") | ||
| const models = await getModels({ provider: "lmstudio", baseUrl: this.getBaseUrl() }) | ||
| if (models && models[this.getModel().id]) { | ||
| this.cachedModelInfo = { | ||
| modelInfo: models[this.getModel().id], | ||
| cached: true, | ||
| } | ||
| } else { | ||
| // if model info is not found, still mark the result as cached to avoid retries on every chunk | ||
| this.cachedModelInfo.cached = true | ||
| } | ||
| } | ||
|
pwilkin marked this conversation as resolved.
Member
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. I noticed that the model info fetching happens after the streaming has already started. This means the first message in a conversation will still use the default context window (128,000) from I know that you mentioned that LMStudio uses JIT model loading, the concern is that this initial request might overwhelm the context window if that initial request has a lot of tokens, any idea on how to handle this? |
||
|
|
||
| yield { | ||
| type: "usage", | ||
| inputTokens, | ||
|
|
@@ -133,7 +174,7 @@ export class LmStudioHandler extends BaseProvider implements SingleCompletionHan | |
| override getModel(): { id: string; info: ModelInfo } { | ||
| return { | ||
| id: this.options.lmStudioModelId || "", | ||
| info: openAiModelInfoSaneDefaults, | ||
| info: this.cachedModelInfo.modelInfo, | ||
| } | ||
| } | ||
|
|
||
|
|
@@ -161,17 +202,3 @@ export class LmStudioHandler extends BaseProvider implements SingleCompletionHan | |
| } | ||
| } | ||
| } | ||
|
|
||
| export async function getLmStudioModels(baseUrl = "http://localhost:1234") { | ||
| try { | ||
| if (!URL.canParse(baseUrl)) { | ||
| return [] | ||
| } | ||
|
|
||
| const response = await axios.get(`${baseUrl}/v1/models`) | ||
| const modelsArray = response.data?.data?.map((model: any) => model.id) || [] | ||
| return [...new Set<string>(modelsArray)] | ||
| } catch (error) { | ||
| return [] | ||
| } | ||
| } | ||
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Oops, something went wrong.
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
Uh oh!
There was an error while loading. Please reload this page.