-
Notifications
You must be signed in to change notification settings - Fork 2.2k
/
embeddings.ts
59 lines (52 loc) · 2.07 KB
/
embeddings.ts
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
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
import { AsyncCaller, AsyncCallerParams } from "./utils/async_caller.js";
/**
* The parameters required to initialize an instance of the Embeddings
* class.
*/
export type EmbeddingsParams = AsyncCallerParams;
export interface EmbeddingsInterface {
/**
* An abstract method that takes an array of documents as input and
* returns a promise that resolves to an array of vectors for each
* document.
* @param documents An array of documents to be embedded.
* @returns A promise that resolves to an array of vectors for each document.
*/
embedDocuments(documents: string[]): Promise<number[][]>;
/**
* An abstract method that takes a single document as input and returns a
* promise that resolves to a vector for the query document.
* @param document A single document to be embedded.
* @returns A promise that resolves to a vector for the query document.
*/
embedQuery(document: string): Promise<number[]>;
}
/**
* An abstract class that provides methods for embedding documents and
* queries using LangChain.
*/
export abstract class Embeddings implements EmbeddingsInterface {
/**
* The async caller should be used by subclasses to make any async calls,
* which will thus benefit from the concurrency and retry logic.
*/
caller: AsyncCaller;
constructor(params: EmbeddingsParams) {
this.caller = new AsyncCaller(params ?? {});
}
/**
* An abstract method that takes an array of documents as input and
* returns a promise that resolves to an array of vectors for each
* document.
* @param documents An array of documents to be embedded.
* @returns A promise that resolves to an array of vectors for each document.
*/
abstract embedDocuments(documents: string[]): Promise<number[][]>;
/**
* An abstract method that takes a single document as input and returns a
* promise that resolves to a vector for the query document.
* @param document A single document to be embedded.
* @returns A promise that resolves to a vector for the query document.
*/
abstract embedQuery(document: string): Promise<number[]>;
}