Elffuss BraidHub

kikocis/continue public

e8ee99375f3e · 1.2 KiBRawPermalink
1import { Chunk, LLMOptions } from "../../index.js";
2
3import OpenAI from "./OpenAI.js";
4
5class Voyage extends OpenAI {
6 static providerName = "voyage";
7 static defaultOptions: Partial<LLMOptions> | undefined = {
8 apiBase: "https://api.voyageai.com/v1/",
9 maxEmbeddingBatchSize: 128,
10 };
11
12 async rerank(query: string, chunks: Chunk[]): Promise<number[]> {
13 if (!query || chunks.length === 0) {
14 return [];
15 }
16 const url = new URL("rerank", this.apiBase);
17 const resp = await this.fetch(url, {
18 method: "POST",
19 headers: {
20 "Content-Type": "application/json",
21 Authorization: `Bearer ${this.apiKey}`,
22 },
23 body: JSON.stringify({
24 query,
25 documents: chunks.map((chunk) => chunk.content),
26 model: this.model ?? "rerank-2",
27 }),
28 });
29
30 if (resp.status !== 200) {
31 throw new Error(
32 `VoyageReranker API error ${resp.status}: ${await resp.text()}`,
33 );
34 }
35
36 const data = (await resp.json()) as {
37 data: Array<{ index: number; relevance_score: number }>;
38 };
39 const results = data.data.sort((a, b) => a.index - b.index);
40 return results.map((result) => result.relevance_score);
41 }
42}
43
44export default Voyage;