continue-custom/core/llm/llms/Vllm.ts
Dallin Romney f7907fff08
fix: vLLM provider should respect user-configured contextLength and model (#11868)
* fix: respect user-configured maxTokens and contextLength for autocomplete

Two fixes for autocomplete's maxTokens not being respected (#6003):

1. vLLM provider: _setupCompletionOptions() unconditionally overwrote
   the user's contextLength and model with server-reported values. Now
   user-explicit settings take priority over auto-detected values.

2. Autocomplete pipeline: renderPromptWithTokenLimit() returned
   completionOptions without maxTokens, relying on an implicit merge
   deeper in the call stack. Now maxTokens is explicitly included in
   the returned completionOptions so the value flows through to the
   API request reliably.

* remove verbose comments from Vllm provider

* style: fix prettier formatting

* fix: only set maxTokens when llm provides a value

* remove defensive-only maxTokens pass-through from autocomplete pipeline
2026-03-25 22:21:11 -07:00

94 lines
2.4 KiB
TypeScript

import { Chunk, LLMOptions } from "../../index.js";
import OpenAI from "./OpenAI.js";
// vLLM-specific rerank response types
interface VllmRerankItem {
index: number;
document: {
text: string;
};
relevance_score: number;
}
interface VllmRerankResponse {
id: string;
model: string;
usage: {
total_tokens: number;
};
results: VllmRerankItem[];
}
class Vllm extends OpenAI {
static providerName = "vllm";
private _userExplicitContextLength: boolean;
private _userExplicitModel: boolean;
constructor(options: LLMOptions) {
super(options);
this._userExplicitContextLength = options.contextLength !== undefined;
this._userExplicitModel =
options.model !== undefined && options.model !== "";
if (options.isFromAutoDetect) {
this._setupCompletionOptions();
}
}
supportsFim(): boolean {
return false;
}
async rerank(query: string, chunks: Chunk[]): Promise<number[]> {
if (this.useOpenAIAdapterFor.includes("rerank") && this.openaiAdapter) {
const results = (await this.openaiAdapter.rerank({
model: this.model,
query,
documents: chunks.map((chunk) => chunk.content),
})) as unknown as VllmRerankResponse;
// vLLM uses 'results' array instead of 'data'
if (results.results && Array.isArray(results.results)) {
const sortedResults = results.results.sort((a, b) => a.index - b.index);
return sortedResults.map((result) => result.relevance_score);
}
throw new Error(
`vLLM rerank response missing 'results' array. Got: ${JSON.stringify(Object.keys(results))}`,
);
}
throw new Error("vLLM rerank requires OpenAI adapter");
}
private _setupCompletionOptions() {
this.fetch(this._getEndpoint("models"), {
method: "GET",
headers: this._getHeaders(),
})
.then(async (response) => {
if (response.status !== 200) {
console.warn(
"Error calling vLLM /models endpoint: ",
await response.text(),
);
return;
}
const json = await response.json();
const data = json.data[0];
if (!this._userExplicitModel) {
this.model = data.id;
}
if (!this._userExplicitContextLength) {
this._contextLength = Number.parseInt(data.max_model_len);
}
})
.catch((e) => {
console.log(`Failed to list models for vLLM: ${e.message}`);
});
}
}
export default Vllm;