feat: move vLLM rerank response in the VLLM.ts and remove unused types

This commit is contained in:
Shankhadeep Dey 2025-06-05 11:12:20 +05:30
parent 1d000bec68
commit c8d9d959f1
4 changed files with 63 additions and 49 deletions

View file

@ -4,8 +4,7 @@ import { findLlmInfo } from "@continuedev/llm-info";
import {
BaseLlmApi,
ChatCompletionCreateParams,
constructLlmApi,
VllmRerankResponse
constructLlmApi
} from "@continuedev/openai-adapters";
import Handlebars from "handlebars";
@ -1069,23 +1068,17 @@ export abstract class BaseLLM implements ILLM {
documents: chunks.map((chunk) => chunk.content),
});
// Handle different response formats: OpenAI (data), vLLM (results)
let dataArray: Array<{ index: number; relevance_score: number }>;
// Standard OpenAI format
if (results.data && Array.isArray(results.data)) {
dataArray = results.data;
} else if ((results as VllmRerankResponse).results && Array.isArray((results as VllmRerankResponse).results)) {
dataArray = (results as VllmRerankResponse).results;
}else {
throw new Error(
`Unexpected rerank response format from ${this.providerName}. ` +
`Expected 'data' or 'results' array but got: ${JSON.stringify(results)}`
);
return results.data
.sort((a, b) => a.index - b.index)
.map((result) => result.relevance_score);
}
return dataArray
.sort((a, b) => a.index - b.index)
.map((result) => result.relevance_score);
throw new Error(
`Unexpected rerank response format from ${this.providerName}. ` +
`Expected 'data' array but got: ${JSON.stringify(Object.keys(results))}`
);
}
throw new Error(

View file

@ -1,7 +1,25 @@
import { LLMOptions } from "../../index.js";
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";
constructor(options: LLMOptions) {
@ -16,6 +34,28 @@ class Vllm extends OpenAI {
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.index);
}
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",

View file

@ -1,14 +1,14 @@
import {
ChatCompletion,
ChatCompletionChunk,
ChatCompletionCreateParamsNonStreaming,
ChatCompletionCreateParamsStreaming,
Completion,
CompletionCreateParamsNonStreaming,
CompletionCreateParamsStreaming,
CreateEmbeddingResponse,
EmbeddingCreateParams,
Model,
ChatCompletion,
ChatCompletionChunk,
ChatCompletionCreateParamsNonStreaming,
ChatCompletionCreateParamsStreaming,
Completion,
CompletionCreateParamsNonStreaming,
CompletionCreateParamsStreaming,
CreateEmbeddingResponse,
EmbeddingCreateParams,
Model,
} from "openai/resources/index";
export interface FimCreateParamsStreaming
@ -37,23 +37,7 @@ export interface CreateRerankResponse {
};
}
// vLLM-specific rerank response format
export interface VllmRerankItem {
index: number;
document: {
text: string;
};
relevance_score: number;
}
export interface VllmRerankResponse {
id: string;
model: string;
usage: {
total_tokens: number;
};
results: VllmRerankItem[];
}
export interface BaseLlmApi {
// Chat, no stream

View file

@ -117,13 +117,10 @@ export {
type Completion,
type CompletionCreateParams,
type CompletionCreateParamsNonStreaming,
type CompletionCreateParamsStreaming,
type CompletionCreateParamsStreaming
} from "openai/resources/index";
// export
export type {
BaseLlmApi,
VllmRerankItem,
VllmRerankResponse,
} from "./apis/base.js";
export type { BaseLlmApi } from "./apis/base.js";
export type { LLMConfig } from "./types.js";