From a3c42005ca662eca2cccb2a5d7ffac29b9b2f7d9 Mon Sep 17 00:00:00 2001 From: Lukas Kreussel <65088241+LLukas22@users.noreply.github.com> Date: Fri, 19 Jul 2024 20:22:26 +0200 Subject: [PATCH] Add `HuggingFaceTEIReranker` (#1711) * Add `HuggingFaceTEIReranker` * Add `huggingface-tei` to `config_schema.json` --- core/context/rerankers/index.ts | 2 + core/context/rerankers/tei.ts | 49 +++++++++++++++++++ core/index.d.ts | 2 +- docs/docs/walkthroughs/codebase-embeddings.md | 19 ++++++- extensions/vscode/config_schema.json | 46 ++++++++++++++++- 5 files changed, 115 insertions(+), 3 deletions(-) create mode 100644 core/context/rerankers/tei.ts diff --git a/core/context/rerankers/index.ts b/core/context/rerankers/index.ts index 0a7cc685a..af3f91556 100644 --- a/core/context/rerankers/index.ts +++ b/core/context/rerankers/index.ts @@ -3,10 +3,12 @@ import { CohereReranker } from "./cohere.js"; import { FreeTrialReranker } from "./freeTrial.js"; import { LLMReranker } from "./llm.js"; import { VoyageReranker } from "./voyage.js"; +import {HuggingFaceTEIReranker} from "./tei.js" export const AllRerankers: { [key in RerankerName]: any } = { cohere: CohereReranker, llm: LLMReranker, voyage: VoyageReranker, "free-trial": FreeTrialReranker, + "huggingface-tei": HuggingFaceTEIReranker }; diff --git a/core/context/rerankers/tei.ts b/core/context/rerankers/tei.ts new file mode 100644 index 000000000..667181ae3 --- /dev/null +++ b/core/context/rerankers/tei.ts @@ -0,0 +1,49 @@ +import fetch from "node-fetch"; +import { Chunk, Reranker } from "../../index.js"; + +export class HuggingFaceTEIReranker implements Reranker { + name = "huggingface-tei"; + + static defaultOptions = { + apiBase: "http://localhost:8080", + truncate: true, + truncation_direction: "Right" + }; + + constructor( + private readonly params: { + apiBase?: string; + truncate?: boolean; + truncation_direction?: string; + }, + ) {} + + async rerank(query: string, chunks: Chunk[]): Promise { + let apiBase = this.params.apiBase ?? HuggingFaceTEIReranker.defaultOptions.apiBase; + if (!apiBase.endsWith("/")) { + apiBase += "/"; + } + + const resp = await fetch(new URL("rerank", apiBase), { + method: "POST", + headers: { 'Content-Type': 'application/json' }, + body: JSON.stringify({ + query: query, + return_text: false, + raw_scores: false, + texts: chunks.map((chunk) => chunk.content), + truncation_direction: this.params.truncation_direction ?? HuggingFaceTEIReranker.defaultOptions.truncation_direction, + truncate: this.params.truncate ?? HuggingFaceTEIReranker.defaultOptions.truncate + }), + }); + + if (!resp.ok) { + throw new Error(await resp.text()); + } + + const data = (await resp.json()) as any; + // Resort into original order and extract scores + const results = data.sort((a: any, b: any) => a.index - b.index); + return results.map((result: any) => result.score); + } +} diff --git a/core/index.d.ts b/core/index.d.ts index 89b4f0656..55969eed8 100644 --- a/core/index.d.ts +++ b/core/index.d.ts @@ -759,7 +759,7 @@ export interface EmbeddingsProvider { embed(chunks: string[]): Promise; } -export type RerankerName = "cohere" | "voyage" | "llm" | "free-trial"; +export type RerankerName = "cohere" | "voyage" | "llm" | "free-trial" | "huggingface-tei"; export interface RerankerDescription { name: RerankerName; diff --git a/docs/docs/walkthroughs/codebase-embeddings.md b/docs/docs/walkthroughs/codebase-embeddings.md index 8b2764c1c..21945734b 100644 --- a/docs/docs/walkthroughs/codebase-embeddings.md +++ b/docs/docs/walkthroughs/codebase-embeddings.md @@ -231,7 +231,7 @@ export function modifyConfig(config: Config): Config { The reranker plays a crucial role in refining the results retrieved from your codebase. It processes the initial set of results obtained through embeddings-based retrieval, improving their relevance and accuracy for your queries. -Continue offers several reranking options: `cohere`, `voyage`, `llm`, and `free-trial`, which can be configured in `config.json`. +Continue offers several reranking options: `cohere`, `voyage`, `llm`, `hugginface-tei`, and `free-trial`, which can be configured in `config.json`. ### Voyage AI @@ -282,6 +282,23 @@ If you only have access to a single LLM, then you can use it as a reranker. This The `"modelTitle"` field must match one of the models in your "models" array in config.json. +### Text Embeddings Inference + +[Hugging Face Text Embeddings Inference](https://huggingface.co/docs/text-embeddings-inference/en/index) enables you to host your own [reranker endpoint](https://huggingface.github.io/text-embeddings-inference/#/Text%20Embeddings%20Inference/rerank). You can configure your reranker as follows: + +```json title="~/.continue/config.json" +{ + "reranker": { + "name": "huggingface-tei", + "params": { + "apiBase": "http://localhost:8080", + "truncate": true, + "truncation_direction": "Right" + } + }, +} +``` + ### Free Trial (Voyage AI) Continue offers a free trial of Voyage AI's reranking model. diff --git a/extensions/vscode/config_schema.json b/extensions/vscode/config_schema.json index 92d70609b..19813eb6a 100644 --- a/extensions/vscode/config_schema.json +++ b/extensions/vscode/config_schema.json @@ -1955,7 +1955,7 @@ "type": "object", "properties": { "name": { - "enum": ["cohere", "voyage", "llm", "free-trial"] + "enum": ["cohere", "voyage", "llm", "free-trial", "huggingface-tei"] }, "params": { "type": "object" @@ -2045,6 +2045,50 @@ } } } + }, + { + "if": { + "properties": { + "name": { + "enum": ["huggingface-tei"] + } + }, + "required": ["name"] + }, + "then": { + "properties": { + "params": { + "type": "object", + "properties": { + "apiBase": { + "type": "string", + "default": "http://localhost:8080" + }, + "truncate": { + "type": "boolean", + "description": "Wether to truncate long sequences to the maximum allowed context length.", + "default": false + }, + "truncation_direction": { + "enum": ["Right","Left"], + "markdownDescription": "Wether to truncate sequences from the `left` or `right`.", + "default": "Right" + } + }, + "required": ["apiBase"] + } + }, + "if": { + "properties": { + "truncate": { + "const": true + } + } + }, + "then": { + "required": ["truncation_direction"] + } + } } ] },