Skip to content

Commit 2f655fa

Browse files
feat: move vLLM rerank response in the VLLM.ts and remove unused types
1 parent 482fbf5 commit 2f655fa

File tree

4 files changed

+63
-49
lines changed

4 files changed

+63
-49
lines changed

core/llm/index.ts

Lines changed: 9 additions & 16 deletions
Original file line numberDiff line numberDiff line change
@@ -4,8 +4,7 @@ import { findLlmInfo } from "@continuedev/llm-info";
44
import {
55
BaseLlmApi,
66
ChatCompletionCreateParams,
7-
constructLlmApi,
8-
VllmRerankResponse
7+
constructLlmApi
98
} from "@continuedev/openai-adapters";
109
import Handlebars from "handlebars";
1110

@@ -1053,23 +1052,17 @@ export abstract class BaseLLM implements ILLM {
10531052
documents: chunks.map((chunk) => chunk.content),
10541053
});
10551054

1056-
// Handle different response formats: OpenAI (data), vLLM (results)
1057-
let dataArray: Array<{ index: number; relevance_score: number }>;
1058-
1055+
// Standard OpenAI format
10591056
if (results.data && Array.isArray(results.data)) {
1060-
dataArray = results.data;
1061-
} else if ((results as VllmRerankResponse).results && Array.isArray((results as VllmRerankResponse).results)) {
1062-
dataArray = (results as VllmRerankResponse).results;
1063-
}else {
1064-
throw new Error(
1065-
`Unexpected rerank response format from ${this.providerName}. ` +
1066-
`Expected 'data' or 'results' array but got: ${JSON.stringify(results)}`
1067-
);
1057+
return results.data
1058+
.sort((a, b) => a.index - b.index)
1059+
.map((result) => result.relevance_score);
10681060
}
10691061

1070-
return dataArray
1071-
.sort((a, b) => a.index - b.index)
1072-
.map((result) => result.relevance_score);
1062+
throw new Error(
1063+
`Unexpected rerank response format from ${this.providerName}. ` +
1064+
`Expected 'data' array but got: ${JSON.stringify(Object.keys(results))}`
1065+
);
10731066
}
10741067

10751068
throw new Error(

core/llm/llms/Vllm.ts

Lines changed: 41 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,7 +1,25 @@
1-
import { LLMOptions } from "../../index.js";
1+
import { Chunk, LLMOptions } from "../../index.js";
22

33
import OpenAI from "./OpenAI.js";
44

5+
// vLLM-specific rerank response types
6+
interface VllmRerankItem {
7+
index: number;
8+
document: {
9+
text: string;
10+
};
11+
relevance_score: number;
12+
}
13+
14+
interface VllmRerankResponse {
15+
id: string;
16+
model: string;
17+
usage: {
18+
total_tokens: number;
19+
};
20+
results: VllmRerankItem[];
21+
}
22+
523
class Vllm extends OpenAI {
624
static providerName = "vllm";
725
constructor(options: LLMOptions) {
@@ -12,6 +30,28 @@ class Vllm extends OpenAI {
1230
}
1331
}
1432

33+
async rerank(query: string, chunks: Chunk[]): Promise<number[]> {
34+
if (this.useOpenAIAdapterFor.includes("rerank") && this.openaiAdapter) {
35+
const results = await this.openaiAdapter.rerank({
36+
model: this.model,
37+
query,
38+
documents: chunks.map((chunk) => chunk.content),
39+
}) as unknown as VllmRerankResponse;
40+
41+
// vLLM uses 'results' array instead of 'data'
42+
if (results.results && Array.isArray(results.results)) {
43+
const sortedResults = results.results.sort((a, b) => a.index - b.index);
44+
return sortedResults.map((result) => result.index);
45+
}
46+
47+
throw new Error(
48+
`vLLM rerank response missing 'results' array. Got: ${JSON.stringify(Object.keys(results))}`
49+
);
50+
}
51+
52+
throw new Error("vLLM rerank requires OpenAI adapter");
53+
}
54+
1555
private _setupCompletionOptions() {
1656
this.fetch(this._getEndpoint("models"), {
1757
method: "GET",

packages/openai-adapters/src/apis/base.ts

Lines changed: 10 additions & 26 deletions
Original file line numberDiff line numberDiff line change
@@ -1,14 +1,14 @@
11
import {
2-
ChatCompletion,
3-
ChatCompletionChunk,
4-
ChatCompletionCreateParamsNonStreaming,
5-
ChatCompletionCreateParamsStreaming,
6-
Completion,
7-
CompletionCreateParamsNonStreaming,
8-
CompletionCreateParamsStreaming,
9-
CreateEmbeddingResponse,
10-
EmbeddingCreateParams,
11-
Model,
2+
ChatCompletion,
3+
ChatCompletionChunk,
4+
ChatCompletionCreateParamsNonStreaming,
5+
ChatCompletionCreateParamsStreaming,
6+
Completion,
7+
CompletionCreateParamsNonStreaming,
8+
CompletionCreateParamsStreaming,
9+
CreateEmbeddingResponse,
10+
EmbeddingCreateParams,
11+
Model,
1212
} from "openai/resources/index";
1313

1414
export interface FimCreateParamsStreaming
@@ -37,23 +37,7 @@ export interface CreateRerankResponse {
3737
};
3838
}
3939

40-
// vLLM-specific rerank response format
41-
export interface VllmRerankItem {
42-
index: number;
43-
document: {
44-
text: string;
45-
};
46-
relevance_score: number;
47-
}
4840

49-
export interface VllmRerankResponse {
50-
id: string;
51-
model: string;
52-
usage: {
53-
total_tokens: number;
54-
};
55-
results: VllmRerankItem[];
56-
}
5741

5842
export interface BaseLlmApi {
5943
// Chat, no stream

packages/openai-adapters/src/index.ts

Lines changed: 3 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -114,13 +114,10 @@ export {
114114
type Completion,
115115
type CompletionCreateParams,
116116
type CompletionCreateParamsNonStreaming,
117-
type CompletionCreateParamsStreaming,
117+
type CompletionCreateParamsStreaming
118118
} from "openai/resources/index";
119119

120120
// export
121-
export type {
122-
BaseLlmApi,
123-
VllmRerankItem,
124-
VllmRerankResponse,
125-
} from "./apis/base.js";
121+
export type { BaseLlmApi } from "./apis/base.js";
126122
export type { LLMConfig } from "./types.js";
123+

0 commit comments

Comments
 (0)