| 1 | import { Chunk, LLMOptions } from "../../index.js"; |
| 2 | import { BaseLLM } from "../index.js"; |
| 3 | |
| 4 | class HuggingFaceTEIEmbeddingsProvider extends BaseLLM { |
| 5 | static providerName = "huggingface-tei"; |
| 6 | |
| 7 | static defaultOptions: Partial<LLMOptions> | undefined = { |
| 8 | apiBase: "http://localhost:8080", |
| 9 | model: "tei", |
| 10 | }; |
| 11 | |
| 12 | constructor(options: LLMOptions) { |
| 13 | super(options); |
| 14 | |
| 15 | this.doInfoRequest() |
| 16 | .then((response) => { |
| 17 | this.model = response.model_id; |
| 18 | this.maxEmbeddingBatchSize = response.max_client_batch_size; |
| 19 | }) |
| 20 | .catch((error) => { |
| 21 | console.error( |
| 22 | "Failed to fetch info from HuggingFace TEI Embeddings Provider:", |
| 23 | error, |
| 24 | ); |
| 25 | }); |
| 26 | } |
| 27 | |
| 28 | async _embed(batch: string[]): Promise<number[][]> { |
| 29 | const headers: Record<string, string> = { |
| 30 | "Content-Type": "application/json", |
| 31 | }; |
| 32 | |
| 33 | if (this.apiKey) { |
| 34 | headers["Authorization"] = `Bearer ${this.apiKey}`; |
| 35 | } |
| 36 | |
| 37 | const resp = await this.fetch(new URL("embed", this.apiBase), { |
| 38 | method: "POST", |
| 39 | body: JSON.stringify({ |
| 40 | inputs: batch, |
| 41 | }), |
| 42 | headers, |
| 43 | }); |
| 44 | if (!resp.ok) { |
| 45 | const text = await resp.text(); |
| 46 | let teiError: TEIEmbedErrorResponse | null = null; |
| 47 | try { |
| 48 | teiError = JSON.parse(text); |
| 49 | } catch (e) { |
| 50 | console.log(`Failed to parse TEI embed error response:\n${text}`, e); |
| 51 | } |
| 52 | if (teiError && (teiError.error_type || teiError.error)) { |
| 53 | throw new TEIEmbedError(teiError); |
| 54 | } |
| 55 | throw new Error(text); |
| 56 | } |
| 57 | return (await resp.json()) as number[][]; |
| 58 | } |
| 59 | |
| 60 | async doInfoRequest(): Promise<TEIInfoResponse> { |
| 61 | // TODO - need to use custom fetch for this request? |
| 62 | const resp = await this.fetch(new URL("info", this.apiBase), { |
| 63 | method: "GET", |
| 64 | }); |
| 65 | if (!resp.ok) { |
| 66 | throw new Error(await resp.text()); |
| 67 | } |
| 68 | return (await resp.json()) as TEIInfoResponse; |
| 69 | } |
| 70 | |
| 71 | async rerank(query: string, chunks: Chunk[]): Promise<number[]> { |
| 72 | const headers: Record<string, string> = { |
| 73 | "Content-Type": "application/json", |
| 74 | }; |
| 75 | |
| 76 | if (this.apiKey) { |
| 77 | headers["Authorization"] = `Bearer ${this.apiKey}`; |
| 78 | } |
| 79 | |
| 80 | const resp = await this.fetch(new URL("rerank", this.apiBase), { |
| 81 | method: "POST", |
| 82 | headers, |
| 83 | body: JSON.stringify({ |
| 84 | query: query, |
| 85 | return_text: false, |
| 86 | raw_scores: false, |
| 87 | texts: chunks.map((chunk) => chunk.content), |
| 88 | truncation_direction: "Right", |
| 89 | truncate: true, |
| 90 | }), |
| 91 | }); |
| 92 | |
| 93 | if (!resp.ok) { |
| 94 | throw new Error(await resp.text()); |
| 95 | } |
| 96 | |
| 97 | const data = (await resp.json()) as any; |
| 98 | // Resort into original order and extract scores |
| 99 | const results = data.sort((a: any, b: any) => a.index - b.index); |
| 100 | return results.map((result: any) => result.score); |
| 101 | } |
| 102 | } |
| 103 | |
| 104 | class TEIEmbedError extends Error { |
| 105 | constructor(teiResponse: TEIEmbedErrorResponse) { |
| 106 | super(JSON.stringify(teiResponse)); |
| 107 | } |
| 108 | } |
| 109 | |
| 110 | type TEIEmbedErrorResponse = { |
| 111 | error: string; |
| 112 | error_type: string; |
| 113 | }; |
| 114 | |
| 115 | type TEIInfoResponse = { |
| 116 | model_id: string; |
| 117 | model_sha: string; |
| 118 | model_dtype: string; |
| 119 | model_type: { |
| 120 | embedding: { |
| 121 | pooling: string; |
| 122 | }; |
| 123 | }; |
| 124 | max_concurrent_requests: number; |
| 125 | max_input_length: number; |
| 126 | max_batch_tokens: number; |
| 127 | max_batch_requests: number; |
| 128 | max_client_batch_size: number; |
| 129 | auto_truncate: boolean; |
| 130 | tokenization_workers: number; |
| 131 | version: string; |
| 132 | sha: string; |
| 133 | docker_label: string; |
| 134 | }; |
| 135 | |
| 136 | export default HuggingFaceTEIEmbeddingsProvider; |