-
-
Notifications
You must be signed in to change notification settings - Fork 120
/
Copy pathLlamaEmbeddingContext.ts
165 lines (141 loc) · 5.66 KB
/
LlamaEmbeddingContext.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
153
154
155
156
157
158
159
160
161
162
163
164
165
import {AsyncDisposeAggregator, EventRelay, withLock} from "lifecycle-utils";
import {Token} from "../types.js";
import {LlamaText} from "../utils/LlamaText.js";
import {tokenizeInput} from "../utils/tokenizeInput.js";
import {resolveBeginningTokenToPrepend, resolveEndTokenToAppend} from "../utils/tokenizerUtils.js";
import {LlamaEmbedding} from "./LlamaEmbedding.js";
import type {LlamaModel} from "./LlamaModel/LlamaModel.js";
import type {LlamaContext, LlamaContextSequence} from "./LlamaContext/LlamaContext.js";
export type LlamaEmbeddingContextOptions = {
/**
* The number of tokens the model can see at once.
* - **`"auto"`** - adapt to the current VRAM state and attemp to set the context size as high as possible up to the size
* the model was trained on.
* - **`number`** - set the context size to a specific number of tokens.
* If there's not enough VRAM, an error will be thrown.
* Use with caution.
* - **`{min?: number, max?: number}`** - adapt to the current VRAM state and attemp to set the context size as high as possible
* up to the size the model was trained on, but at least `min` and at most `max`.
*
* Defaults to `"auto"`.
*/
contextSize?: "auto" | number | {
min?: number,
max?: number
},
/** prompt processing batch size */
batchSize?: number,
/**
* number of threads to use to evaluate tokens.
* set to 0 to use the maximum threads supported by the current machine hardware
*/
threads?: number,
/** An abort signal to abort the context creation */
createSignal?: AbortSignal,
/**
* Ignore insufficient memory errors and continue with the context creation.
* Can cause the process to crash if there's not enough VRAM for the new context.
*
* Defaults to `false`.
*/
ignoreMemorySafetyChecks?: boolean
};
/**
* @see [Using Embedding](https://node-llama-cpp.withcat.ai/guide/embedding) tutorial
*/
export class LlamaEmbeddingContext {
/** @internal */ private readonly _llamaContext: LlamaContext;
/** @internal */ private readonly _sequence: LlamaContextSequence;
/** @internal */ private readonly _disposeAggregator = new AsyncDisposeAggregator();
public readonly onDispose = new EventRelay<void>();
private constructor({
_llamaContext
}: {
_llamaContext: LlamaContext
}) {
this._llamaContext = _llamaContext;
this._sequence = this._llamaContext.getSequence();
this._disposeAggregator.add(
this._llamaContext.onDispose.createListener(() => {
void this._disposeAggregator.dispose();
})
);
this._disposeAggregator.add(this.onDispose.dispatchEvent);
this._disposeAggregator.add(async () => {
await this._llamaContext.dispose();
});
}
public async getEmbeddingFor(input: Token[] | string | LlamaText) {
const resolvedInput = tokenizeInput(input, this._llamaContext.model.tokenizer, undefined, true);
if (resolvedInput.length > this._llamaContext.contextSize)
throw new Error(
"Input is longer than the context size. " +
"Try to increase the context size or use another model that supports longer contexts."
);
else if (resolvedInput.length === 0)
return new LlamaEmbedding({
vector: []
});
const beginningToken = resolveBeginningTokenToPrepend(this.model.vocabularyType, this.model.tokens);
if (beginningToken != null && resolvedInput[0] !== beginningToken)
resolvedInput.unshift(beginningToken);
const endToken = resolveEndTokenToAppend(this.model.vocabularyType, this.model.tokens);
if (endToken != null && resolvedInput.at(-1) !== endToken)
resolvedInput.push(endToken);
return await withLock(this, "evaluate", async () => {
await this._sequence.eraseContextTokenRanges([{
start: 0,
end: this._sequence.nextTokenIndex
}]);
const iterator = this._sequence.evaluate(resolvedInput, {_noSampling: true});
// eslint-disable-next-line @typescript-eslint/no-unused-vars
for await (const token of iterator) {
break; // only generate one token to get embeddings
}
const embedding = this._llamaContext._ctx.getEmbedding(resolvedInput.length);
const embeddingVector = Array.from(embedding);
return new LlamaEmbedding({
vector: embeddingVector
});
});
}
public async dispose() {
await this._disposeAggregator.dispose();
}
/** @hidden */
public [Symbol.asyncDispose]() {
return this.dispose();
}
public get disposed() {
return this._llamaContext.disposed;
}
public get model() {
return this._llamaContext.model;
}
/** @internal */
public static async _create({
_model
}: {
_model: LlamaModel
}, {
contextSize,
batchSize,
threads = 6,
createSignal,
ignoreMemorySafetyChecks
}: LlamaEmbeddingContextOptions) {
if (_model.fileInsights.hasEncoder && _model.fileInsights.hasDecoder)
throw new Error("Computing embeddings is not supported for encoder-decoder models.");
const llamaContext = await _model.createContext({
contextSize,
batchSize,
threads,
createSignal,
ignoreMemorySafetyChecks,
_embeddings: true
});
return new LlamaEmbeddingContext({
_llamaContext: llamaContext
});
}
}