1
- import { LLMOptions } from "../../index.js" ;
1
+ import { Chunk , LLMOptions } from "../../index.js" ;
2
2
3
3
import OpenAI from "./OpenAI.js" ;
4
4
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
+
5
23
class Vllm extends OpenAI {
6
24
static providerName = "vllm" ;
7
25
constructor ( options : LLMOptions ) {
@@ -12,6 +30,28 @@ class Vllm extends OpenAI {
12
30
}
13
31
}
14
32
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
+
15
55
private _setupCompletionOptions ( ) {
16
56
this . fetch ( this . _getEndpoint ( "models" ) , {
17
57
method : "GET" ,
0 commit comments