-
Notifications
You must be signed in to change notification settings - Fork 2.5k
/
Copy pathembeddings.ts
152 lines (132 loc) Β· 4.32 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
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
import { Embeddings, EmbeddingsParams } from "@langchain/core/embeddings";
import { Ollama } from "ollama/browser";
import type { Options as OllamaOptions } from "ollama";
import { OllamaCamelCaseOptions } from "./types.js";
/**
* Interface for OllamaEmbeddings parameters. Extends EmbeddingsParams and
* defines additional parameters specific to the OllamaEmbeddings class.
*/
export interface OllamaEmbeddingsParams extends EmbeddingsParams {
/**
* The Ollama model to use for embeddings.
* @default "mxbai-embed-large"
*/
model?: string;
/**
* Base URL of the Ollama server
* @default "http://localhost:11434"
*/
baseUrl?: string;
/**
* Defaults to "5m"
*/
keepAlive?: string | number;
/**
* Whether or not to truncate the input text to fit inside the model's
* context window.
* @default false
*/
truncate?: boolean;
/**
* Optional HTTP Headers to include in the request.
*/
headers?: Headers | Record<string, string>;
/**
* Advanced Ollama API request parameters in camelCase, see
* https://github.com/ollama/ollama/blob/main/docs/modelfile.md#valid-parameters-and-values
* for details of the available parameters.
*/
requestOptions?: OllamaCamelCaseOptions & Partial<OllamaOptions>;
}
export class OllamaEmbeddings extends Embeddings {
model = "mxbai-embed-large";
baseUrl = "http://localhost:11434";
keepAlive?: string | number;
requestOptions?: Partial<OllamaOptions>;
client: Ollama;
truncate = false;
constructor(fields?: OllamaEmbeddingsParams) {
super({ maxConcurrency: 1, ...fields });
this.client = new Ollama({
host: fields?.baseUrl,
headers: fields?.headers ? new Headers(fields.headers) : undefined,
});
this.baseUrl = fields?.baseUrl ?? this.baseUrl;
this.model = fields?.model ?? this.model;
this.keepAlive = fields?.keepAlive;
this.truncate = fields?.truncate ?? this.truncate;
this.requestOptions = fields?.requestOptions
? this._convertOptions(fields?.requestOptions)
: undefined;
}
/** convert camelCased Ollama request options like "useMMap" to
* the snake_cased equivalent which the ollama API actually uses.
* Used only for consistency with the llms/Ollama and chatModels/Ollama classes
*/
_convertOptions(
requestOptions: OllamaCamelCaseOptions
): Partial<OllamaOptions> {
const snakeCasedOptions: Partial<OllamaOptions> = {};
const mapping: Record<keyof OllamaCamelCaseOptions, string> = {
embeddingOnly: "embedding_only",
frequencyPenalty: "frequency_penalty",
keepAlive: "keep_alive",
logitsAll: "logits_all",
lowVram: "low_vram",
mainGpu: "main_gpu",
mirostat: "mirostat",
mirostatEta: "mirostat_eta",
mirostatTau: "mirostat_tau",
numBatch: "num_batch",
numCtx: "num_ctx",
numGpu: "num_gpu",
numKeep: "num_keep",
numPredict: "num_predict",
numThread: "num_thread",
penalizeNewline: "penalize_newline",
presencePenalty: "presence_penalty",
repeatLastN: "repeat_last_n",
repeatPenalty: "repeat_penalty",
temperature: "temperature",
stop: "stop",
tfsZ: "tfs_z",
topK: "top_k",
topP: "top_p",
typicalP: "typical_p",
useMlock: "use_mlock",
useMmap: "use_mmap",
vocabOnly: "vocab_only",
f16Kv: "f16_kv",
numa: "numa",
seed: "seed",
};
for (const [key, value] of Object.entries(requestOptions)) {
const snakeCasedOption = mapping[key as keyof OllamaCamelCaseOptions];
if (snakeCasedOption) {
snakeCasedOptions[snakeCasedOption as keyof OllamaOptions] = value;
} else {
// Just pass unknown options through
snakeCasedOptions[key as keyof OllamaOptions] = value;
}
}
return snakeCasedOptions;
}
async embedDocuments(texts: string[]): Promise<number[][]> {
return this.embeddingWithRetry(texts);
}
async embedQuery(text: string) {
return (await this.embeddingWithRetry([text]))[0];
}
private async embeddingWithRetry(texts: string[]): Promise<number[][]> {
const res = await this.caller.call(() =>
this.client.embed({
model: this.model,
input: texts,
keep_alive: this.keepAlive,
options: this.requestOptions,
truncate: this.truncate,
})
);
return res.embeddings;
}
}