-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathindex.js
More file actions
158 lines (128 loc) · 3.22 KB
/
index.js
File metadata and controls
158 lines (128 loc) · 3.22 KB
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
const binding = require('./binding')
class LlamaModel {
constructor (path, opts = {}) {
this._handle = binding.loadModel(path, opts)
}
tokenize (text, addBos = true) {
return binding.tokenize(this._handle, text, addBos)
}
detokenize (tokens) {
return binding.detokenize(this._handle, tokens)
}
isEogToken (token) {
return binding.isEogToken(this._handle, token)
}
get embeddingDimension () {
return binding.getEmbeddingDimension(this._handle)
}
get trainingContextSize () {
return binding.getTrainingContextSize(this._handle)
}
getMeta (key) {
return binding.getModelMeta(this._handle, key)
}
get name () {
return this.getMeta('general.name')
}
free () {
if (this._handle) {
binding.freeModel(this._handle)
this._handle = null
}
}
}
class LlamaContext {
constructor (model, opts = {}) {
if (!(model instanceof LlamaModel)) {
throw new Error('First argument must be a LlamaModel')
}
this._model = model
this._handle = binding.createContext(model._handle, opts)
}
get contextSize () {
return binding.getContextSize(this._handle)
}
decode (tokens) {
binding.decode(this._handle, tokens)
}
getEmbeddings (idx = -1) {
return binding.getEmbeddings(this._handle, idx)
}
clearMemory () {
binding.clearMemory(this._handle)
}
free () {
if (this._handle) {
binding.freeContext(this._handle)
this._handle = null
}
}
}
class LlamaSampler {
constructor (model, opts = {}) {
if (!(model instanceof LlamaModel)) {
throw new Error('First argument must be a LlamaModel')
}
this._handle = binding.createSampler(model._handle, opts)
}
sample (ctx, idx = -1) {
if (!(ctx instanceof LlamaContext)) {
throw new Error('First argument must be a LlamaContext')
}
return binding.sample(ctx._handle, this._handle, idx)
}
accept (token) {
binding.acceptToken(this._handle, token)
}
free () {
if (this._handle) {
binding.freeSampler(this._handle)
this._handle = null
}
}
}
function generate (model, ctx, sampler, prompt, maxTokens = 128) {
const tokens = model.tokenize(prompt, true)
ctx.decode(tokens)
const generated = []
for (let i = 0; i < maxTokens; i++) {
const token = sampler.sample(ctx, -1)
if (model.isEogToken(token)) break
sampler.accept(token)
generated.push(token)
// Decode single token for next iteration
ctx.decode(new Int32Array([token]))
}
return model.detokenize(new Int32Array(generated))
}
// Log level: 0=off, 1=errors only, 2=all (default)
function setLogLevel (level) {
binding.setLogLevel(level)
}
// Convenience function to suppress all llama.cpp output
function setQuiet (quiet = true) {
binding.setLogLevel(quiet ? 0 : 2)
}
// Read GGUF metadata without loading the full model
function readGgufMeta (path, key) {
return binding.readGgufMeta(path, key)
}
// Get model name from GGUF file
function getModelName (path) {
return readGgufMeta(path, 'general.name')
}
function systemInfo () {
return binding.systemInfo()
}
module.exports = {
LlamaModel,
LlamaContext,
LlamaSampler,
generate,
setLogLevel,
setQuiet,
readGgufMeta,
getModelName,
systemInfo,
binding
}