cactus-react-native 0.1.1 → 0.1.3
This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
- package/README.md +872 -146
- package/android/src/main/CMakeLists.txt +1 -1
- package/android/src/main/jniLibs/arm64-v8a/libcactus.so +0 -0
- package/android/src/main/jniLibs/arm64-v8a/libcactus_v8.so +0 -0
- package/android/src/main/jniLibs/arm64-v8a/libcactus_v8_2.so +0 -0
- package/android/src/main/jniLibs/arm64-v8a/libcactus_v8_2_dotprod.so +0 -0
- package/android/src/main/jniLibs/arm64-v8a/libcactus_v8_2_dotprod_i8mm.so +0 -0
- package/android/src/main/jniLibs/arm64-v8a/libcactus_v8_2_i8mm.so +0 -0
- package/android/src/main/jniLibs/x86_64/libcactus.so +0 -0
- package/android/src/main/jniLibs/x86_64/libcactus_x86_64.so +0 -0
- package/ios/CMakeLists.txt +6 -6
- package/ios/cactus.xcframework/ios-arm64/cactus.framework/Headers/cactus.h +12 -0
- package/ios/cactus.xcframework/ios-arm64/cactus.framework/cactus +0 -0
- package/ios/cactus.xcframework/ios-arm64_x86_64-simulator/cactus.framework/Headers/cactus.h +12 -0
- package/ios/cactus.xcframework/ios-arm64_x86_64-simulator/cactus.framework/cactus +0 -0
- package/ios/cactus.xcframework/tvos-arm64/cactus.framework/Headers/cactus.h +12 -0
- package/ios/cactus.xcframework/tvos-arm64/cactus.framework/cactus +0 -0
- package/ios/cactus.xcframework/tvos-arm64_x86_64-simulator/cactus.framework/Headers/cactus.h +12 -0
- package/ios/cactus.xcframework/tvos-arm64_x86_64-simulator/cactus.framework/cactus +0 -0
- package/lib/commonjs/index.js.map +1 -1
- package/lib/commonjs/lm.js.map +1 -0
- package/lib/commonjs/tts.js.map +1 -0
- package/lib/commonjs/vlm.js.map +0 -0
- package/lib/module/index.js.map +1 -1
- package/lib/module/lm.js.map +0 -0
- package/lib/module/tts.js.map +1 -0
- package/lib/module/vlm.js.map +1 -0
- package/lib/typescript/index.d.ts +5 -1
- package/lib/typescript/index.d.ts.map +1 -1
- package/lib/typescript/lm.d.ts +41 -0
- package/lib/typescript/lm.d.ts.map +1 -0
- package/lib/typescript/tts.d.ts +10 -0
- package/lib/typescript/tts.d.ts.map +1 -0
- package/lib/typescript/vlm.d.ts +44 -0
- package/lib/typescript/vlm.d.ts.map +1 -0
- package/package.json +2 -1
- package/src/index.ts +11 -1
- package/src/lm.ts +49 -0
- package/src/tts.ts +45 -0
- package/src/vlm.ts +70 -0
- package/lib/commonjs/NativeCactus.js +0 -10
- package/lib/commonjs/chat.js +0 -37
- package/lib/commonjs/grammar.js +0 -560
- package/lib/commonjs/index.js +0 -412
- package/lib/commonjs/tools.js +0 -118
- package/lib/commonjs/tools.js.map +0 -1
- package/lib/module/NativeCactus.js +0 -8
- package/lib/module/chat.js +0 -33
- package/lib/module/grammar.js +0 -553
- package/lib/module/index.js +0 -363
- package/lib/module/tools.js +0 -110
- package/lib/module/tools.js.map +0 -1
package/lib/commonjs/index.js
DELETED
|
@@ -1,412 +0,0 @@
|
|
|
1
|
-
"use strict";
|
|
2
|
-
|
|
3
|
-
Object.defineProperty(exports, "__esModule", {
|
|
4
|
-
value: true
|
|
5
|
-
});
|
|
6
|
-
exports.LlamaContext = void 0;
|
|
7
|
-
Object.defineProperty(exports, "SchemaGrammarConverter", {
|
|
8
|
-
enumerable: true,
|
|
9
|
-
get: function () {
|
|
10
|
-
return _grammar.SchemaGrammarConverter;
|
|
11
|
-
}
|
|
12
|
-
});
|
|
13
|
-
Object.defineProperty(exports, "Tools", {
|
|
14
|
-
enumerable: true,
|
|
15
|
-
get: function () {
|
|
16
|
-
return _tools.Tools;
|
|
17
|
-
}
|
|
18
|
-
});
|
|
19
|
-
exports.addNativeLogListener = addNativeLogListener;
|
|
20
|
-
Object.defineProperty(exports, "convertJsonSchemaToGrammar", {
|
|
21
|
-
enumerable: true,
|
|
22
|
-
get: function () {
|
|
23
|
-
return _grammar.convertJsonSchemaToGrammar;
|
|
24
|
-
}
|
|
25
|
-
});
|
|
26
|
-
exports.initContext = exports.getTTSType = exports.getFormattedAudioCompletion = exports.getAudioCompletionGuideTokens = exports.decodeAudioTokens = void 0;
|
|
27
|
-
exports.initLlama = initLlama;
|
|
28
|
-
exports.isVocoderEnabled = exports.isMultimodalSupportVision = exports.isMultimodalSupportAudio = exports.isMultimodalEnabled = exports.initVocoder = exports.initMultimodal = void 0;
|
|
29
|
-
exports.loadLlamaModelInfo = loadLlamaModelInfo;
|
|
30
|
-
exports.multimodalCompletion = void 0;
|
|
31
|
-
exports.releaseAllLlama = releaseAllLlama;
|
|
32
|
-
exports.releaseVocoder = exports.releaseMultimodal = void 0;
|
|
33
|
-
exports.setContextLimit = setContextLimit;
|
|
34
|
-
exports.toggleNativeLog = toggleNativeLog;
|
|
35
|
-
exports.tokenize = void 0;
|
|
36
|
-
var _reactNative = require("react-native");
|
|
37
|
-
var _NativeCactus = _interopRequireDefault(require("./NativeCactus"));
|
|
38
|
-
var _grammar = require("./grammar");
|
|
39
|
-
var _chat = require("./chat");
|
|
40
|
-
var _tools = require("./tools");
|
|
41
|
-
function _interopRequireDefault(e) { return e && e.__esModule ? e : { default: e }; }
|
|
42
|
-
const EVENT_ON_INIT_CONTEXT_PROGRESS = '@Cactus_onInitContextProgress';
|
|
43
|
-
const EVENT_ON_TOKEN = '@Cactus_onToken';
|
|
44
|
-
const EVENT_ON_NATIVE_LOG = '@Cactus_onNativeLog';
|
|
45
|
-
let EventEmitter;
|
|
46
|
-
if (_reactNative.Platform.OS === 'ios') {
|
|
47
|
-
// @ts-ignore
|
|
48
|
-
EventEmitter = new _reactNative.NativeEventEmitter(_NativeCactus.default);
|
|
49
|
-
}
|
|
50
|
-
if (_reactNative.Platform.OS === 'android') {
|
|
51
|
-
EventEmitter = _reactNative.DeviceEventEmitter;
|
|
52
|
-
}
|
|
53
|
-
const logListeners = [];
|
|
54
|
-
|
|
55
|
-
// @ts-ignore
|
|
56
|
-
if (EventEmitter) {
|
|
57
|
-
EventEmitter.addListener(EVENT_ON_NATIVE_LOG, evt => {
|
|
58
|
-
logListeners.forEach(listener => listener(evt.level, evt.text));
|
|
59
|
-
});
|
|
60
|
-
// Trigger unset to use default log callback
|
|
61
|
-
_NativeCactus.default?.toggleNativeLog?.(false)?.catch?.(() => {});
|
|
62
|
-
}
|
|
63
|
-
const getJsonSchema = responseFormat => {
|
|
64
|
-
if (responseFormat?.type === 'json_schema') {
|
|
65
|
-
return responseFormat.json_schema?.schema;
|
|
66
|
-
}
|
|
67
|
-
if (responseFormat?.type === 'json_object') {
|
|
68
|
-
return responseFormat.schema || {};
|
|
69
|
-
}
|
|
70
|
-
return null;
|
|
71
|
-
};
|
|
72
|
-
class LlamaContext {
|
|
73
|
-
gpu = false;
|
|
74
|
-
reasonNoGPU = '';
|
|
75
|
-
constructor({
|
|
76
|
-
contextId,
|
|
77
|
-
gpu,
|
|
78
|
-
reasonNoGPU,
|
|
79
|
-
model
|
|
80
|
-
}) {
|
|
81
|
-
this.id = contextId;
|
|
82
|
-
this.gpu = gpu;
|
|
83
|
-
this.reasonNoGPU = reasonNoGPU;
|
|
84
|
-
this.model = model;
|
|
85
|
-
}
|
|
86
|
-
|
|
87
|
-
/**
|
|
88
|
-
* Load cached prompt & completion state from a file.
|
|
89
|
-
*/
|
|
90
|
-
async loadSession(filepath) {
|
|
91
|
-
let path = filepath;
|
|
92
|
-
if (path.startsWith('file://')) path = path.slice(7);
|
|
93
|
-
return _NativeCactus.default.loadSession(this.id, path);
|
|
94
|
-
}
|
|
95
|
-
|
|
96
|
-
/**
|
|
97
|
-
* Save current cached prompt & completion state to a file.
|
|
98
|
-
*/
|
|
99
|
-
async saveSession(filepath, options) {
|
|
100
|
-
return _NativeCactus.default.saveSession(this.id, filepath, options?.tokenSize || -1);
|
|
101
|
-
}
|
|
102
|
-
isLlamaChatSupported() {
|
|
103
|
-
return !!this.model.chatTemplates.llamaChat;
|
|
104
|
-
}
|
|
105
|
-
isJinjaSupported() {
|
|
106
|
-
const {
|
|
107
|
-
minja
|
|
108
|
-
} = this.model.chatTemplates;
|
|
109
|
-
return !!minja?.toolUse || !!minja?.default;
|
|
110
|
-
}
|
|
111
|
-
async getFormattedChat(messages, template, params) {
|
|
112
|
-
const chat = (0, _chat.formatChat)(messages);
|
|
113
|
-
const useJinja = this.isJinjaSupported() && params?.jinja;
|
|
114
|
-
let tmpl = this.isLlamaChatSupported() || useJinja ? undefined : 'chatml';
|
|
115
|
-
if (template) tmpl = template; // Force replace if provided
|
|
116
|
-
const jsonSchema = getJsonSchema(params?.response_format);
|
|
117
|
-
return _NativeCactus.default.getFormattedChat(this.id, JSON.stringify(chat), tmpl, {
|
|
118
|
-
jinja: useJinja,
|
|
119
|
-
json_schema: jsonSchema ? JSON.stringify(jsonSchema) : undefined,
|
|
120
|
-
tools: params?.tools ? JSON.stringify(params.tools) : undefined,
|
|
121
|
-
parallel_tool_calls: params?.parallel_tool_calls ? JSON.stringify(params.parallel_tool_calls) : undefined,
|
|
122
|
-
tool_choice: params?.tool_choice
|
|
123
|
-
});
|
|
124
|
-
}
|
|
125
|
-
async completionWithTools(params, callback, recursionCount = 0, recursionLimit = 3) {
|
|
126
|
-
if (!params.messages) {
|
|
127
|
-
// tool calling only works with messages
|
|
128
|
-
return this.completion(params, callback);
|
|
129
|
-
}
|
|
130
|
-
if (!params.tools) {
|
|
131
|
-
// no tools => default completion
|
|
132
|
-
return this.completion(params, callback);
|
|
133
|
-
}
|
|
134
|
-
if (recursionCount >= recursionLimit) {
|
|
135
|
-
return this.completion(params, callback);
|
|
136
|
-
}
|
|
137
|
-
let messages = [...(params?.messages || [])]; // avoid mutating the original
|
|
138
|
-
|
|
139
|
-
if (recursionCount === 0) {
|
|
140
|
-
messages = (0, _tools.injectToolsIntoMessages)(messages, params.tools);
|
|
141
|
-
}
|
|
142
|
-
const result = await this.completion({
|
|
143
|
-
...params,
|
|
144
|
-
messages
|
|
145
|
-
}, callback);
|
|
146
|
-
const {
|
|
147
|
-
toolCalled,
|
|
148
|
-
toolName,
|
|
149
|
-
toolInput,
|
|
150
|
-
toolOutput
|
|
151
|
-
} = await (0, _tools.parseAndExecuteTool)(result, params.tools);
|
|
152
|
-
if (toolCalled && toolName && toolInput) {
|
|
153
|
-
const newMessages = (0, _tools.updateMessagesWithToolCall)(messages, toolName, toolInput, toolOutput);
|
|
154
|
-
return await this.completionWithTools({
|
|
155
|
-
...params,
|
|
156
|
-
messages: newMessages
|
|
157
|
-
}, callback, recursionCount + 1, recursionLimit);
|
|
158
|
-
}
|
|
159
|
-
return result;
|
|
160
|
-
}
|
|
161
|
-
async completion(params, callback) {
|
|
162
|
-
const nativeParams = {
|
|
163
|
-
...params,
|
|
164
|
-
prompt: params.prompt || '',
|
|
165
|
-
emit_partial_completion: !!callback
|
|
166
|
-
};
|
|
167
|
-
if (params.messages) {
|
|
168
|
-
// messages always win
|
|
169
|
-
const formattedResult = await this.getFormattedChat(params.messages, params.chat_template || params.chatTemplate, {
|
|
170
|
-
jinja: params.jinja,
|
|
171
|
-
tools: params.tools,
|
|
172
|
-
parallel_tool_calls: params.parallel_tool_calls,
|
|
173
|
-
tool_choice: params.tool_choice
|
|
174
|
-
});
|
|
175
|
-
if (typeof formattedResult === 'string') {
|
|
176
|
-
nativeParams.prompt = formattedResult || '';
|
|
177
|
-
} else {
|
|
178
|
-
nativeParams.prompt = formattedResult.prompt || '';
|
|
179
|
-
if (typeof formattedResult.chat_format === 'number') nativeParams.chat_format = formattedResult.chat_format;
|
|
180
|
-
if (formattedResult.grammar) nativeParams.grammar = formattedResult.grammar;
|
|
181
|
-
if (typeof formattedResult.grammar_lazy === 'boolean') nativeParams.grammar_lazy = formattedResult.grammar_lazy;
|
|
182
|
-
if (formattedResult.grammar_triggers) nativeParams.grammar_triggers = formattedResult.grammar_triggers;
|
|
183
|
-
if (formattedResult.preserved_tokens) nativeParams.preserved_tokens = formattedResult.preserved_tokens;
|
|
184
|
-
if (formattedResult.additional_stops) {
|
|
185
|
-
if (!nativeParams.stop) nativeParams.stop = [];
|
|
186
|
-
nativeParams.stop.push(...formattedResult.additional_stops);
|
|
187
|
-
}
|
|
188
|
-
}
|
|
189
|
-
} else {
|
|
190
|
-
nativeParams.prompt = params.prompt || '';
|
|
191
|
-
}
|
|
192
|
-
if (nativeParams.response_format && !nativeParams.grammar) {
|
|
193
|
-
const jsonSchema = getJsonSchema(params.response_format);
|
|
194
|
-
if (jsonSchema) nativeParams.json_schema = JSON.stringify(jsonSchema);
|
|
195
|
-
}
|
|
196
|
-
let tokenListener = callback && EventEmitter.addListener(EVENT_ON_TOKEN, evt => {
|
|
197
|
-
const {
|
|
198
|
-
contextId,
|
|
199
|
-
tokenResult
|
|
200
|
-
} = evt;
|
|
201
|
-
if (contextId !== this.id) return;
|
|
202
|
-
callback(tokenResult);
|
|
203
|
-
});
|
|
204
|
-
if (!nativeParams.prompt) throw new Error('Prompt is required');
|
|
205
|
-
const promise = _NativeCactus.default.completion(this.id, nativeParams);
|
|
206
|
-
return promise.then(completionResult => {
|
|
207
|
-
tokenListener?.remove();
|
|
208
|
-
tokenListener = null;
|
|
209
|
-
return completionResult;
|
|
210
|
-
}).catch(err => {
|
|
211
|
-
tokenListener?.remove();
|
|
212
|
-
tokenListener = null;
|
|
213
|
-
throw err;
|
|
214
|
-
});
|
|
215
|
-
}
|
|
216
|
-
stopCompletion() {
|
|
217
|
-
return _NativeCactus.default.stopCompletion(this.id);
|
|
218
|
-
}
|
|
219
|
-
tokenize(text) {
|
|
220
|
-
return _NativeCactus.default.tokenize(this.id, text);
|
|
221
|
-
}
|
|
222
|
-
detokenize(tokens) {
|
|
223
|
-
return _NativeCactus.default.detokenize(this.id, tokens);
|
|
224
|
-
}
|
|
225
|
-
embedding(text, params) {
|
|
226
|
-
return _NativeCactus.default.embedding(this.id, text, params || {});
|
|
227
|
-
}
|
|
228
|
-
async bench(pp, tg, pl, nr) {
|
|
229
|
-
const result = await _NativeCactus.default.bench(this.id, pp, tg, pl, nr);
|
|
230
|
-
const [modelDesc, modelSize, modelNParams, ppAvg, ppStd, tgAvg, tgStd] = JSON.parse(result);
|
|
231
|
-
return {
|
|
232
|
-
modelDesc,
|
|
233
|
-
modelSize,
|
|
234
|
-
modelNParams,
|
|
235
|
-
ppAvg,
|
|
236
|
-
ppStd,
|
|
237
|
-
tgAvg,
|
|
238
|
-
tgStd
|
|
239
|
-
};
|
|
240
|
-
}
|
|
241
|
-
async applyLoraAdapters(loraList) {
|
|
242
|
-
let loraAdapters = [];
|
|
243
|
-
if (loraList) loraAdapters = loraList.map(l => ({
|
|
244
|
-
path: l.path.replace(/file:\/\//, ''),
|
|
245
|
-
scaled: l.scaled
|
|
246
|
-
}));
|
|
247
|
-
return _NativeCactus.default.applyLoraAdapters(this.id, loraAdapters);
|
|
248
|
-
}
|
|
249
|
-
async removeLoraAdapters() {
|
|
250
|
-
return _NativeCactus.default.removeLoraAdapters(this.id);
|
|
251
|
-
}
|
|
252
|
-
async getLoadedLoraAdapters() {
|
|
253
|
-
return _NativeCactus.default.getLoadedLoraAdapters(this.id);
|
|
254
|
-
}
|
|
255
|
-
async release() {
|
|
256
|
-
return _NativeCactus.default.releaseContext(this.id);
|
|
257
|
-
}
|
|
258
|
-
}
|
|
259
|
-
exports.LlamaContext = LlamaContext;
|
|
260
|
-
async function toggleNativeLog(enabled) {
|
|
261
|
-
return _NativeCactus.default.toggleNativeLog(enabled);
|
|
262
|
-
}
|
|
263
|
-
function addNativeLogListener(listener) {
|
|
264
|
-
logListeners.push(listener);
|
|
265
|
-
return {
|
|
266
|
-
remove: () => {
|
|
267
|
-
logListeners.splice(logListeners.indexOf(listener), 1);
|
|
268
|
-
}
|
|
269
|
-
};
|
|
270
|
-
}
|
|
271
|
-
async function setContextLimit(limit) {
|
|
272
|
-
return _NativeCactus.default.setContextLimit(limit);
|
|
273
|
-
}
|
|
274
|
-
let contextIdCounter = 0;
|
|
275
|
-
const contextIdRandom = () => process.env.NODE_ENV === 'test' ? 0 : Math.floor(Math.random() * 100000);
|
|
276
|
-
const modelInfoSkip = [
|
|
277
|
-
// Large fields
|
|
278
|
-
'tokenizer.ggml.tokens', 'tokenizer.ggml.token_type', 'tokenizer.ggml.merges', 'tokenizer.ggml.scores'];
|
|
279
|
-
async function loadLlamaModelInfo(model) {
|
|
280
|
-
let path = model;
|
|
281
|
-
if (path.startsWith('file://')) path = path.slice(7);
|
|
282
|
-
return _NativeCactus.default.modelInfo(path, modelInfoSkip);
|
|
283
|
-
}
|
|
284
|
-
const poolTypeMap = {
|
|
285
|
-
// -1 is unspecified as undefined
|
|
286
|
-
none: 0,
|
|
287
|
-
mean: 1,
|
|
288
|
-
cls: 2,
|
|
289
|
-
last: 3,
|
|
290
|
-
rank: 4
|
|
291
|
-
};
|
|
292
|
-
async function initLlama({
|
|
293
|
-
model,
|
|
294
|
-
is_model_asset: isModelAsset,
|
|
295
|
-
pooling_type: poolingType,
|
|
296
|
-
lora,
|
|
297
|
-
lora_list: loraList,
|
|
298
|
-
...rest
|
|
299
|
-
}, onProgress) {
|
|
300
|
-
let path = model;
|
|
301
|
-
if (path.startsWith('file://')) path = path.slice(7);
|
|
302
|
-
let loraPath = lora;
|
|
303
|
-
if (loraPath?.startsWith('file://')) loraPath = loraPath.slice(7);
|
|
304
|
-
let loraAdapters = [];
|
|
305
|
-
if (loraList) loraAdapters = loraList.map(l => ({
|
|
306
|
-
path: l.path.replace(/file:\/\//, ''),
|
|
307
|
-
scaled: l.scaled
|
|
308
|
-
}));
|
|
309
|
-
const contextId = contextIdCounter + contextIdRandom();
|
|
310
|
-
contextIdCounter += 1;
|
|
311
|
-
let removeProgressListener = null;
|
|
312
|
-
if (onProgress) {
|
|
313
|
-
removeProgressListener = EventEmitter.addListener(EVENT_ON_INIT_CONTEXT_PROGRESS, evt => {
|
|
314
|
-
if (evt.contextId !== contextId) return;
|
|
315
|
-
onProgress(evt.progress);
|
|
316
|
-
});
|
|
317
|
-
}
|
|
318
|
-
const poolType = poolTypeMap[poolingType];
|
|
319
|
-
const {
|
|
320
|
-
gpu,
|
|
321
|
-
reasonNoGPU,
|
|
322
|
-
model: modelDetails,
|
|
323
|
-
androidLib
|
|
324
|
-
} = await _NativeCactus.default.initContext(contextId, {
|
|
325
|
-
model: path,
|
|
326
|
-
is_model_asset: !!isModelAsset,
|
|
327
|
-
use_progress_callback: !!onProgress,
|
|
328
|
-
pooling_type: poolType,
|
|
329
|
-
lora: loraPath,
|
|
330
|
-
lora_list: loraAdapters,
|
|
331
|
-
...rest
|
|
332
|
-
}).catch(err => {
|
|
333
|
-
removeProgressListener?.remove();
|
|
334
|
-
throw err;
|
|
335
|
-
});
|
|
336
|
-
removeProgressListener?.remove();
|
|
337
|
-
return new LlamaContext({
|
|
338
|
-
contextId,
|
|
339
|
-
gpu,
|
|
340
|
-
reasonNoGPU,
|
|
341
|
-
model: modelDetails,
|
|
342
|
-
androidLib
|
|
343
|
-
});
|
|
344
|
-
}
|
|
345
|
-
async function releaseAllLlama() {
|
|
346
|
-
return _NativeCactus.default.releaseAllContexts();
|
|
347
|
-
}
|
|
348
|
-
const initContext = async params => {
|
|
349
|
-
return await _NativeCactus.default.initContext(contextIdCounter++, params);
|
|
350
|
-
};
|
|
351
|
-
exports.initContext = initContext;
|
|
352
|
-
const initMultimodal = async (contextId, mmprojPath, useGpu = false) => {
|
|
353
|
-
return await _NativeCactus.default.initMultimodal(contextId, mmprojPath, useGpu);
|
|
354
|
-
};
|
|
355
|
-
exports.initMultimodal = initMultimodal;
|
|
356
|
-
const isMultimodalEnabled = async contextId => {
|
|
357
|
-
return await _NativeCactus.default.isMultimodalEnabled(contextId);
|
|
358
|
-
};
|
|
359
|
-
exports.isMultimodalEnabled = isMultimodalEnabled;
|
|
360
|
-
const isMultimodalSupportVision = async contextId => {
|
|
361
|
-
return await _NativeCactus.default.isMultimodalSupportVision(contextId);
|
|
362
|
-
};
|
|
363
|
-
exports.isMultimodalSupportVision = isMultimodalSupportVision;
|
|
364
|
-
const isMultimodalSupportAudio = async contextId => {
|
|
365
|
-
return await _NativeCactus.default.isMultimodalSupportAudio(contextId);
|
|
366
|
-
};
|
|
367
|
-
exports.isMultimodalSupportAudio = isMultimodalSupportAudio;
|
|
368
|
-
const releaseMultimodal = async contextId => {
|
|
369
|
-
return await _NativeCactus.default.releaseMultimodal(contextId);
|
|
370
|
-
};
|
|
371
|
-
exports.releaseMultimodal = releaseMultimodal;
|
|
372
|
-
const multimodalCompletion = async (contextId, prompt, mediaPaths, params) => {
|
|
373
|
-
return await _NativeCactus.default.multimodalCompletion(contextId, prompt, mediaPaths, params);
|
|
374
|
-
};
|
|
375
|
-
exports.multimodalCompletion = multimodalCompletion;
|
|
376
|
-
const initVocoder = async (contextId, vocoderModelPath) => {
|
|
377
|
-
return await _NativeCactus.default.initVocoder(contextId, vocoderModelPath);
|
|
378
|
-
};
|
|
379
|
-
exports.initVocoder = initVocoder;
|
|
380
|
-
const isVocoderEnabled = async contextId => {
|
|
381
|
-
return await _NativeCactus.default.isVocoderEnabled(contextId);
|
|
382
|
-
};
|
|
383
|
-
exports.isVocoderEnabled = isVocoderEnabled;
|
|
384
|
-
const getTTSType = async contextId => {
|
|
385
|
-
return await _NativeCactus.default.getTTSType(contextId);
|
|
386
|
-
};
|
|
387
|
-
exports.getTTSType = getTTSType;
|
|
388
|
-
const getFormattedAudioCompletion = async (contextId, speakerJsonStr, textToSpeak) => {
|
|
389
|
-
return await _NativeCactus.default.getFormattedAudioCompletion(contextId, speakerJsonStr, textToSpeak);
|
|
390
|
-
};
|
|
391
|
-
exports.getFormattedAudioCompletion = getFormattedAudioCompletion;
|
|
392
|
-
const getAudioCompletionGuideTokens = async (contextId, textToSpeak) => {
|
|
393
|
-
return await _NativeCactus.default.getAudioCompletionGuideTokens(contextId, textToSpeak);
|
|
394
|
-
};
|
|
395
|
-
exports.getAudioCompletionGuideTokens = getAudioCompletionGuideTokens;
|
|
396
|
-
const decodeAudioTokens = async (contextId, tokens) => {
|
|
397
|
-
return await _NativeCactus.default.decodeAudioTokens(contextId, tokens);
|
|
398
|
-
};
|
|
399
|
-
exports.decodeAudioTokens = decodeAudioTokens;
|
|
400
|
-
const releaseVocoder = async contextId => {
|
|
401
|
-
return await _NativeCactus.default.releaseVocoder(contextId);
|
|
402
|
-
};
|
|
403
|
-
exports.releaseVocoder = releaseVocoder;
|
|
404
|
-
const tokenize = async (contextId, text, mediaPaths) => {
|
|
405
|
-
if (mediaPaths && mediaPaths.length > 0) {
|
|
406
|
-
return await _NativeCactus.default.tokenize(contextId, text, mediaPaths);
|
|
407
|
-
} else {
|
|
408
|
-
return await _NativeCactus.default.tokenize(contextId, text);
|
|
409
|
-
}
|
|
410
|
-
};
|
|
411
|
-
exports.tokenize = tokenize;
|
|
412
|
-
//# sourceMappingURL=index.js.map
|
package/lib/commonjs/tools.js
DELETED
|
@@ -1,118 +0,0 @@
|
|
|
1
|
-
"use strict";
|
|
2
|
-
|
|
3
|
-
Object.defineProperty(exports, "__esModule", {
|
|
4
|
-
value: true
|
|
5
|
-
});
|
|
6
|
-
exports.Tools = void 0;
|
|
7
|
-
exports.injectToolsIntoMessages = injectToolsIntoMessages;
|
|
8
|
-
exports.parseAndExecuteTool = parseAndExecuteTool;
|
|
9
|
-
exports.updateMessagesWithToolCall = updateMessagesWithToolCall;
|
|
10
|
-
class Tools {
|
|
11
|
-
tools = new Map();
|
|
12
|
-
add(func, description, parameters) {
|
|
13
|
-
this.tools.set(func.name, {
|
|
14
|
-
func,
|
|
15
|
-
description,
|
|
16
|
-
parameters,
|
|
17
|
-
required: Object.entries(parameters).filter(([_, param]) => param.required).map(([key, _]) => key)
|
|
18
|
-
});
|
|
19
|
-
return func;
|
|
20
|
-
}
|
|
21
|
-
getSchemas() {
|
|
22
|
-
return Array.from(this.tools.entries()).map(([name, {
|
|
23
|
-
description,
|
|
24
|
-
parameters,
|
|
25
|
-
required
|
|
26
|
-
}]) => ({
|
|
27
|
-
type: "function",
|
|
28
|
-
function: {
|
|
29
|
-
name,
|
|
30
|
-
description,
|
|
31
|
-
parameters: {
|
|
32
|
-
type: "object",
|
|
33
|
-
properties: parameters,
|
|
34
|
-
required
|
|
35
|
-
}
|
|
36
|
-
}
|
|
37
|
-
}));
|
|
38
|
-
}
|
|
39
|
-
async execute(name, args) {
|
|
40
|
-
const tool = this.tools.get(name);
|
|
41
|
-
if (!tool) throw new Error(`Tool ${name} not found`);
|
|
42
|
-
return await tool.func(...Object.values(args));
|
|
43
|
-
}
|
|
44
|
-
}
|
|
45
|
-
exports.Tools = Tools;
|
|
46
|
-
function injectToolsIntoMessages(messages, tools) {
|
|
47
|
-
const newMessages = [...messages];
|
|
48
|
-
const toolsSchemas = tools.getSchemas();
|
|
49
|
-
const promptToolInjection = `You have access to the following functions. Use them if required -
|
|
50
|
-
${JSON.stringify(toolsSchemas, null, 2)}
|
|
51
|
-
Only use an available tool if needed. If a tool is chosen, respond ONLY with a JSON object matching the following schema:
|
|
52
|
-
\`\`\`json
|
|
53
|
-
{
|
|
54
|
-
"tool_name": "<name of the tool>",
|
|
55
|
-
"tool_input": {
|
|
56
|
-
"<parameter_name>": "<parameter_value>",
|
|
57
|
-
...
|
|
58
|
-
}
|
|
59
|
-
}
|
|
60
|
-
\`\`\`
|
|
61
|
-
Remember, if you are calling a tool, you must respond with the JSON object and the JSON object ONLY!
|
|
62
|
-
If no tool is needed, respond normally.
|
|
63
|
-
`;
|
|
64
|
-
const systemMessage = newMessages.find(m => m.role === 'system');
|
|
65
|
-
if (!systemMessage) {
|
|
66
|
-
newMessages.unshift({
|
|
67
|
-
role: 'system',
|
|
68
|
-
content: promptToolInjection
|
|
69
|
-
});
|
|
70
|
-
} else {
|
|
71
|
-
systemMessage.content = `${systemMessage.content}\n\n${promptToolInjection}`;
|
|
72
|
-
}
|
|
73
|
-
return newMessages;
|
|
74
|
-
}
|
|
75
|
-
async function parseAndExecuteTool(result, tools) {
|
|
76
|
-
const match = result.content.match(/```json\s*([\s\S]*?)\s*```/);
|
|
77
|
-
if (!match || !match[1]) return {
|
|
78
|
-
toolCalled: false
|
|
79
|
-
};
|
|
80
|
-
try {
|
|
81
|
-
const jsonContent = JSON.parse(match[1]);
|
|
82
|
-
const {
|
|
83
|
-
tool_name,
|
|
84
|
-
tool_input
|
|
85
|
-
} = jsonContent;
|
|
86
|
-
// console.log('Calling tool:', tool_name, tool_input);
|
|
87
|
-
const toolOutput = (await tools.execute(tool_name, tool_input)) || true;
|
|
88
|
-
// console.log('Tool called result:', toolOutput);
|
|
89
|
-
|
|
90
|
-
return {
|
|
91
|
-
toolCalled: true,
|
|
92
|
-
toolName: tool_name,
|
|
93
|
-
toolInput: tool_input,
|
|
94
|
-
toolOutput
|
|
95
|
-
};
|
|
96
|
-
} catch (error) {
|
|
97
|
-
// console.error('Error parsing JSON:', match, error);
|
|
98
|
-
return {
|
|
99
|
-
toolCalled: false
|
|
100
|
-
};
|
|
101
|
-
}
|
|
102
|
-
}
|
|
103
|
-
function updateMessagesWithToolCall(messages, toolName, toolInput, toolOutput) {
|
|
104
|
-
const newMessages = [...messages];
|
|
105
|
-
newMessages.push({
|
|
106
|
-
role: 'function-call',
|
|
107
|
-
content: JSON.stringify({
|
|
108
|
-
name: toolName,
|
|
109
|
-
arguments: toolInput
|
|
110
|
-
}, null, 2)
|
|
111
|
-
});
|
|
112
|
-
newMessages.push({
|
|
113
|
-
role: 'function-response',
|
|
114
|
-
content: JSON.stringify(toolOutput, null, 2)
|
|
115
|
-
});
|
|
116
|
-
return newMessages;
|
|
117
|
-
}
|
|
118
|
-
//# sourceMappingURL=tools.js.map
|
|
@@ -1 +0,0 @@
|
|
|
1
|
-
{"version":3,"names":["Tools","tools","Map","add","func","description","parameters","set","name","required","Object","entries","filter","_","param","map","key","getSchemas","Array","from","type","function","properties","execute","args","tool","get","Error","values","exports","injectToolsIntoMessages","messages","newMessages","toolsSchemas","promptToolInjection","JSON","stringify","systemMessage","find","m","role","unshift","content","parseAndExecuteTool","result","match","toolCalled","jsonContent","parse","tool_name","tool_input","toolOutput","toolName","toolInput","error","updateMessagesWithToolCall","push","ar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C,UAAU,CAAC,KAAI,IAAI;IACrE;;IAEA,OAAO;MACHJ,UAAU,EAAE,IAAI;MAChBM,QAAQ,EAAEH,SAAS;MACnBI,SAAS,EAAEH,UAAU;MACrBC;IACJ,CAAC;EACL,CAAC,CAAC,OAAOG,KAAK,EAAE;IACZ;IACA,OAAO;MAACR,UAAU,EAAE;IAAK,CAAC;EAC9B;AACF;AAEO,SAASS,0BAA0BA,CAACxB,QAAsC,EAAEqB,QAAgB,EAAEC,SAAc,EAAEF,UAAe,EAAgC;EAClK,MAAMnB,WAAW,GAAG,CAAC,GAAGD,QAAQ,CAAC;EAEjCC,WAAW,CAACwB,IAAI,CAAC;IACbhB,IAAI,EAAE,eAAe;IACrBE,OAAO,EAAEP,IAAI,CAACC,SAAS,CAAC;MAAC5B,IAAI,EAAE4C,QAAQ;MAAEK,SAAS,EAAEJ;IAAS,CAAC,EAAE,IAAI,EAAE,CAAC;EAC3E,CAAC,CAAC;EACFrB,WAAW,CAACwB,IAAI,CAAC;IACbhB,IAAI,EAAE,mBAAmB;IACzBE,OAAO,EAAEP,IAAI,CAACC,SAAS,CAACe,UAAU,EAAE,IAAI,EAAE,CAAC;EAC/C,CAAC,CAAC;EAEF,OAAOnB,WAAW;AACpB","ignoreList":[]}
|
package/lib/module/chat.js
DELETED
|
@@ -1,33 +0,0 @@
|
|
|
1
|
-
"use strict";
|
|
2
|
-
|
|
3
|
-
export function formatChat(messages) {
|
|
4
|
-
const chat = [];
|
|
5
|
-
messages.forEach(currMsg => {
|
|
6
|
-
const role = currMsg.role || '';
|
|
7
|
-
let content = '';
|
|
8
|
-
if ('content' in currMsg) {
|
|
9
|
-
if (typeof currMsg.content === 'string') {
|
|
10
|
-
;
|
|
11
|
-
({
|
|
12
|
-
content
|
|
13
|
-
} = currMsg);
|
|
14
|
-
} else if (Array.isArray(currMsg.content)) {
|
|
15
|
-
currMsg.content.forEach(part => {
|
|
16
|
-
if ('text' in part) {
|
|
17
|
-
content += `${content ? '\n' : ''}${part.text}`;
|
|
18
|
-
}
|
|
19
|
-
});
|
|
20
|
-
} else {
|
|
21
|
-
throw new TypeError("Invalid 'content' type (ref: https://github.com/ggerganov/llama.cpp/issues/8367)");
|
|
22
|
-
}
|
|
23
|
-
} else {
|
|
24
|
-
throw new Error("Missing 'content' (ref: https://github.com/ggerganov/llama.cpp/issues/8367)");
|
|
25
|
-
}
|
|
26
|
-
chat.push({
|
|
27
|
-
role,
|
|
28
|
-
content
|
|
29
|
-
});
|
|
30
|
-
});
|
|
31
|
-
return chat;
|
|
32
|
-
}
|
|
33
|
-
//# sourceMappingURL=chat.js.map
|