File size: 904 Bytes
287a0bc
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
import { IEmbeddingFunction } from "./IEmbeddingFunction";

let CohereAiApi: any;

export class HuggingFaceEmbeddingServerFunction implements IEmbeddingFunction {
    private url: string;

    constructor({ url }: { url: string }) {
        // we used to construct the client here, but we need to async import the types
        // for the openai npm package, and the constructor can not be async
        this.url = url;
    }

    public async generate(texts: string[]) {
        const response = await fetch(this.url, {
            method: 'POST',
            headers: {
                'Content-Type': 'application/json'
            },
            body: JSON.stringify({ 'inputs': texts })
        });

        if (!response.ok) {
            throw new Error(`Failed to generate embeddings: ${response.statusText}`);
        }

        const data = await response.json();
        return data;
    }

}