@agorapete/wllama 3.5.1-q2.0
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/.gitmodules +3 -0
- package/.prettierignore +38 -0
- package/AGENTS.md +1 -0
- package/CMakeLists.txt +131 -0
- package/LICENCE +21 -0
- package/README-dev.md +178 -0
- package/README.md +225 -0
- package/README_banner.png +0 -0
- package/assets/screenshot_0.png +0 -0
- package/cpp/generate_glue_prototype.js +115 -0
- package/cpp/glue.hpp +664 -0
- package/cpp/test_glue.cpp +80 -0
- package/cpp/wllama-context.h +1172 -0
- package/cpp/wllama-fs.h +148 -0
- package/cpp/wllama.cpp +187 -0
- package/cpp/wllama.h +6 -0
- package/esm/cache-manager.d.ts +130 -0
- package/esm/debug.d.ts +28 -0
- package/esm/glue/glue.d.ts +22 -0
- package/esm/glue/messages.d.ts +146 -0
- package/esm/huggingface.d.ts +31 -0
- package/esm/index.cjs +3406 -0
- package/esm/index.d.ts +8 -0
- package/esm/index.js +3387 -0
- package/esm/index.min.js +1 -0
- package/esm/index.min.js.map +1 -0
- package/esm/model-manager.d.ts +136 -0
- package/esm/storage/cos.d.ts +36 -0
- package/esm/storage/index.d.ts +33 -0
- package/esm/storage/opfs.d.ts +12 -0
- package/esm/types/oai-compat.d.ts +278 -0
- package/esm/types/types.d.ts +112 -0
- package/esm/utils.d.ts +119 -0
- package/esm/wasm/source-map.d.ts +1 -0
- package/esm/wasm/wllama.wasm +0 -0
- package/esm/wasm-from-cdn.d.ts +8 -0
- package/esm/wllama.d.ts +397 -0
- package/esm/worker.d.ts +92 -0
- package/esm/workers-code/generated.d.ts +4 -0
- package/guides/intro-v2.md +132 -0
- package/guides/intro-v3.1.md +40 -0
- package/guides/intro-v3.md +230 -0
- package/index.ts +1 -0
- package/package.json +71 -0
- package/scripts/bisect_test.sh +33 -0
- package/scripts/build_hf_space.sh +26 -0
- package/scripts/build_source_map.js +269 -0
- package/scripts/build_wasm.sh +19 -0
- package/scripts/build_worker.sh +38 -0
- package/scripts/check_debug_build.js +30 -0
- package/scripts/check_package_size.js +25 -0
- package/scripts/docker-compose.yml +76 -0
- package/scripts/generate_wasm_from_cdn.js +24 -0
- package/scripts/http_server.js +44 -0
- package/scripts/post_build.sh +32 -0
- package/src/cache-manager.ts +358 -0
- package/src/debug.ts +111 -0
- package/src/glue/glue.ts +291 -0
- package/src/glue/messages.ts +773 -0
- package/src/huggingface.ts +151 -0
- package/src/index.ts +8 -0
- package/src/mjs.test.ts +44 -0
- package/src/model-manager.test.ts +200 -0
- package/src/model-manager.ts +359 -0
- package/src/storage/cos.test.ts +83 -0
- package/src/storage/cos.ts +171 -0
- package/src/storage/index.ts +40 -0
- package/src/storage/opfs.ts +119 -0
- package/src/types/oai-compat.ts +342 -0
- package/src/types/types.ts +133 -0
- package/src/utils.test.ts +231 -0
- package/src/utils.ts +403 -0
- package/src/wasm/source-map.ts +7 -0
- package/src/wasm/wllama.js +1 -0
- package/src/wasm/wllama.wasm +0 -0
- package/src/wasm-from-cdn.ts +13 -0
- package/src/wllama.test.ts +392 -0
- package/src/wllama.ts +1138 -0
- package/src/wllama.wgpu.test.ts +62 -0
- package/src/worker.ts +443 -0
- package/src/workers-code/generated.ts +11 -0
- package/src/workers-code/llama-cpp.js +511 -0
- package/src/workers-code/opfs-utils.js +150 -0
- package/tsconfig.build.json +34 -0
- package/tsup.config.ts +23 -0
- package/vitest.config.ts +61 -0
|
@@ -0,0 +1,112 @@
|
|
|
1
|
+
export interface LoadModelParams {
|
|
2
|
+
log_level?: LogLevel;
|
|
3
|
+
seed?: number;
|
|
4
|
+
n_ctx?: number;
|
|
5
|
+
n_batch?: number;
|
|
6
|
+
n_gpu_layers?: number;
|
|
7
|
+
n_threads?: number;
|
|
8
|
+
embeddings?: boolean;
|
|
9
|
+
offload_kqv?: boolean;
|
|
10
|
+
pooling_type?: 'LLAMA_POOLING_TYPE_UNSPECIFIED' | 'LLAMA_POOLING_TYPE_NONE' | 'LLAMA_POOLING_TYPE_MEAN' | 'LLAMA_POOLING_TYPE_CLS' | 'unspecified' | 'none' | 'mean' | 'cls' | 'last' | 'rank';
|
|
11
|
+
rope_scaling_type?: 'LLAMA_ROPE_SCALING_TYPE_UNSPECIFIED' | 'LLAMA_ROPE_SCALING_TYPE_NONE' | 'LLAMA_ROPE_SCALING_TYPE_LINEAR' | 'LLAMA_ROPE_SCALING_TYPE_YARN';
|
|
12
|
+
rope_freq_base?: number;
|
|
13
|
+
rope_freq_scale?: number;
|
|
14
|
+
yarn_ext_factor?: number;
|
|
15
|
+
yarn_attn_factor?: number;
|
|
16
|
+
yarn_beta_fast?: number;
|
|
17
|
+
yarn_beta_slow?: number;
|
|
18
|
+
yarn_orig_ctx?: number;
|
|
19
|
+
cache_type_k?: 'f32' | 'f16' | 'q8_0' | 'q5_1' | 'q5_0' | 'q4_1' | 'q4_0';
|
|
20
|
+
cache_type_v?: 'f32' | 'f16' | 'q8_0' | 'q5_1' | 'q5_0' | 'q4_1' | 'q4_0';
|
|
21
|
+
flash_attn?: boolean;
|
|
22
|
+
swa_full?: boolean;
|
|
23
|
+
chat_template?: string;
|
|
24
|
+
jinja?: boolean;
|
|
25
|
+
reasoning?: boolean;
|
|
26
|
+
image_min_tokens?: number;
|
|
27
|
+
image_max_tokens?: number;
|
|
28
|
+
warmup?: boolean;
|
|
29
|
+
no_kv_offload?: boolean;
|
|
30
|
+
mmproj_offload?: boolean;
|
|
31
|
+
cont_batching?: boolean;
|
|
32
|
+
n_keep?: number;
|
|
33
|
+
ctx_shift?: boolean;
|
|
34
|
+
cache_idle_slots?: boolean;
|
|
35
|
+
n_cache_reuse?: number;
|
|
36
|
+
lora_adapters?: {
|
|
37
|
+
path: string;
|
|
38
|
+
scale?: number;
|
|
39
|
+
}[];
|
|
40
|
+
lora_init_without_apply?: boolean;
|
|
41
|
+
spec_draft_model?: string;
|
|
42
|
+
spec_draft_ngl?: number;
|
|
43
|
+
spec_draft_n_max?: number;
|
|
44
|
+
spec_draft_n_min?: number;
|
|
45
|
+
spec_draft_p_min?: number;
|
|
46
|
+
spec_draft_threads?: number;
|
|
47
|
+
spec_draft_threads_batch?: number;
|
|
48
|
+
kv_overrides?: Record<string, string>;
|
|
49
|
+
reasoning_budget_tokens?: number;
|
|
50
|
+
reasoning_budget_message?: string;
|
|
51
|
+
reasoning_format?: 'none' | 'deepseek-legacy' | 'deepseek';
|
|
52
|
+
skip_chat_parsing?: boolean;
|
|
53
|
+
prefill_assistant?: boolean;
|
|
54
|
+
default_template_kwargs?: Record<string, any>;
|
|
55
|
+
}
|
|
56
|
+
export interface LoadedContextInfo {
|
|
57
|
+
n_vocab: number;
|
|
58
|
+
n_ctx: number;
|
|
59
|
+
n_batch: number;
|
|
60
|
+
n_ubatch: number;
|
|
61
|
+
n_ctx_train: number;
|
|
62
|
+
n_embd: number;
|
|
63
|
+
n_layer: number;
|
|
64
|
+
metadata: Record<string, string>;
|
|
65
|
+
token_bos: number;
|
|
66
|
+
token_eos: number;
|
|
67
|
+
token_eot: number;
|
|
68
|
+
list_tokens_eog: number[];
|
|
69
|
+
has_encoder: boolean;
|
|
70
|
+
token_decoder_start: number;
|
|
71
|
+
add_bos_token: boolean;
|
|
72
|
+
add_eos_token: boolean;
|
|
73
|
+
has_image_input: boolean;
|
|
74
|
+
has_audio_input: boolean;
|
|
75
|
+
}
|
|
76
|
+
export interface SamplingParams {
|
|
77
|
+
seed?: number;
|
|
78
|
+
mirostat?: number | undefined;
|
|
79
|
+
mirostat_eta?: number | undefined;
|
|
80
|
+
mirostat_tau?: number | undefined;
|
|
81
|
+
samplers_sequence?: string[] | undefined;
|
|
82
|
+
temp?: number | undefined;
|
|
83
|
+
top_p?: number | undefined;
|
|
84
|
+
top_k?: number | undefined;
|
|
85
|
+
penalty_last_n?: number | undefined;
|
|
86
|
+
penalty_repeat?: number | undefined;
|
|
87
|
+
penalty_freq?: number | undefined;
|
|
88
|
+
penalty_present?: number | undefined;
|
|
89
|
+
dynatemp_range?: number | undefined;
|
|
90
|
+
dynatemp_exponent?: number | undefined;
|
|
91
|
+
grammar?: string;
|
|
92
|
+
n_prev?: number | undefined;
|
|
93
|
+
n_probs?: number | undefined;
|
|
94
|
+
min_p?: number | undefined;
|
|
95
|
+
typ_p?: number | undefined;
|
|
96
|
+
typical_p?: number | undefined;
|
|
97
|
+
logit_bias?: {
|
|
98
|
+
token: number;
|
|
99
|
+
bias: number;
|
|
100
|
+
}[] | undefined;
|
|
101
|
+
ignore_eos?: boolean | undefined;
|
|
102
|
+
}
|
|
103
|
+
export interface StreamParams<T> {
|
|
104
|
+
stream: true;
|
|
105
|
+
onData: (data: T) => void;
|
|
106
|
+
}
|
|
107
|
+
export declare enum LogLevel {
|
|
108
|
+
DEBUG = 1,
|
|
109
|
+
INFO = 2,
|
|
110
|
+
WARN = 3,
|
|
111
|
+
ERROR = 4
|
|
112
|
+
}
|
package/esm/utils.d.ts
ADDED
|
@@ -0,0 +1,119 @@
|
|
|
1
|
+
export declare const joinBuffers: (buffers: Uint8Array[]) => Uint8Array;
|
|
2
|
+
/**
|
|
3
|
+
* Convert list of bytes (number) to text
|
|
4
|
+
* @param buffer
|
|
5
|
+
* @returns a string
|
|
6
|
+
*/
|
|
7
|
+
export declare const bufToText: (buffer: ArrayBuffer | Uint8Array) => string;
|
|
8
|
+
/**
|
|
9
|
+
* Get default stdout/stderr config for wasm module
|
|
10
|
+
*/
|
|
11
|
+
export declare const getWModuleConfig: (pathConfig: {
|
|
12
|
+
[filename: string]: string;
|
|
13
|
+
}) => {
|
|
14
|
+
noInitialRun: boolean;
|
|
15
|
+
print: (text: any) => void;
|
|
16
|
+
printErr: (text: any) => void;
|
|
17
|
+
locateFile: (filename: string, basePath: string) => string;
|
|
18
|
+
};
|
|
19
|
+
export interface ShardInfo {
|
|
20
|
+
baseURL: string;
|
|
21
|
+
current: number;
|
|
22
|
+
total: number;
|
|
23
|
+
}
|
|
24
|
+
/**
|
|
25
|
+
* Parse shard number and total from a file name or URL
|
|
26
|
+
*/
|
|
27
|
+
export declare const parseShardNumber: (fnameOrUrl: string) => ShardInfo;
|
|
28
|
+
/**
|
|
29
|
+
* Parses a model URL and returns an array of URLs based on the following patterns:
|
|
30
|
+
* - If the input URL is an array, it returns the array itself.
|
|
31
|
+
* - If the input URL is a string in the `gguf-split` format, it returns an array containing the URL of each shard in ascending order.
|
|
32
|
+
* - Otherwise, it returns an array containing the input URL as a single element array.
|
|
33
|
+
* @param modelUrl URL or list of URLs
|
|
34
|
+
*/
|
|
35
|
+
export declare const parseModelUrl: (modelUrl: string) => string[];
|
|
36
|
+
/**
|
|
37
|
+
* Check if the given blobs are files or not, then sort them by shard number
|
|
38
|
+
*/
|
|
39
|
+
export declare const sortFileByShard: (blobs: Blob[]) => void;
|
|
40
|
+
export declare const isMmproj: (blob: Blob) => Promise<boolean>;
|
|
41
|
+
export declare const delay: (ms: number) => Promise<unknown>;
|
|
42
|
+
export declare const absoluteUrl: (relativePath: string) => string;
|
|
43
|
+
export declare const padDigits: (number: number, digits: number) => string;
|
|
44
|
+
export declare const sumArr: (arr: number[]) => number;
|
|
45
|
+
export declare const isString: (value: any) => boolean;
|
|
46
|
+
export declare const MMPROJ_FILE_NAME = "mmproj.gguf";
|
|
47
|
+
type ModelShard = {
|
|
48
|
+
blob: Blob;
|
|
49
|
+
name: string;
|
|
50
|
+
};
|
|
51
|
+
export declare const prepareBlobs: (blobsInp: Blob[]) => Promise<{
|
|
52
|
+
llm: ModelShard[];
|
|
53
|
+
mmproj: ModelShard | null;
|
|
54
|
+
all: ModelShard[];
|
|
55
|
+
}>;
|
|
56
|
+
/**
|
|
57
|
+
* Browser feature detection
|
|
58
|
+
* Copied from https://unpkg.com/wasm-feature-detect?module (Apache License)
|
|
59
|
+
*/
|
|
60
|
+
/**
|
|
61
|
+
* @returns true if browser support multi-threads
|
|
62
|
+
*/
|
|
63
|
+
export declare const isSupportMultiThread: () => Promise<boolean>;
|
|
64
|
+
/**
|
|
65
|
+
* @returns true if browser support JSPI
|
|
66
|
+
*/
|
|
67
|
+
export declare const isSupportJSPI: () => boolean;
|
|
68
|
+
/**
|
|
69
|
+
* @returns true if brower support WebGPU. Note: for browser without JSPI support, compat mode will be used.
|
|
70
|
+
*/
|
|
71
|
+
export declare const isSupportWebGPU: () => boolean;
|
|
72
|
+
/**
|
|
73
|
+
* @returns true if browser support WASM Memory64
|
|
74
|
+
*/
|
|
75
|
+
export declare const isSupportMem64: () => boolean;
|
|
76
|
+
/**
|
|
77
|
+
* Throws an error if the environment is not compatible
|
|
78
|
+
*/
|
|
79
|
+
export declare const checkEnvironmentCompatible: () => Promise<void>;
|
|
80
|
+
/**
|
|
81
|
+
* Check if browser is Safari
|
|
82
|
+
* Source: https://github.com/DamonOehlman/detect-browser/blob/master/src/index.ts
|
|
83
|
+
*/
|
|
84
|
+
export declare const isSafari: () => boolean;
|
|
85
|
+
/**
|
|
86
|
+
* Check if browser is Firefox
|
|
87
|
+
*/
|
|
88
|
+
export declare const isFirefox: () => boolean;
|
|
89
|
+
/**
|
|
90
|
+
* Regular expression to validate GGUF file paths/URLs
|
|
91
|
+
* Matches paths ending with .gguf and optional query parameters
|
|
92
|
+
*/
|
|
93
|
+
export declare const GGUF_FILE_REGEX: RegExp;
|
|
94
|
+
/**
|
|
95
|
+
* Validates if a given string is a valid GGUF file path/URL
|
|
96
|
+
* @param path The file path or URL to validate
|
|
97
|
+
* @returns true if the path is a valid GGUF file path/URL
|
|
98
|
+
*/
|
|
99
|
+
export declare const isValidGgufFile: (path: string) => boolean;
|
|
100
|
+
/**
|
|
101
|
+
* Check if browser is Safari iOS / iPad / iPhone
|
|
102
|
+
* Source: https://github.com/DamonOehlman/detect-browser/blob/master/src/index.ts
|
|
103
|
+
*/
|
|
104
|
+
export declare const isSafariMobile: () => boolean;
|
|
105
|
+
/**
|
|
106
|
+
* Create a worker from a string
|
|
107
|
+
*/
|
|
108
|
+
export declare const createWorker: (workerCode: string | Blob) => Worker;
|
|
109
|
+
/**
|
|
110
|
+
* Convert callback to async iterator
|
|
111
|
+
*/
|
|
112
|
+
export declare const cbToAsyncIter: <A extends any[], T>(fn: (...args: [...args: A, callback: (val?: T, done?: boolean, err?: Error) => void]) => void) => (...args: A) => AsyncIterable<T>;
|
|
113
|
+
/**
|
|
114
|
+
* Check if we can use async file read, where the wasm env can asynchronously read a Blob.
|
|
115
|
+
* Please refer to README-dev.md for more details.
|
|
116
|
+
*/
|
|
117
|
+
export declare const canUseAsyncFileRead: (compat: boolean) => boolean;
|
|
118
|
+
export declare const needCompat: () => boolean;
|
|
119
|
+
export {};
|
|
@@ -0,0 +1 @@
|
|
|
1
|
+
export declare const WASM_SOURCE_MAP: Record<string, string>;
|
|
Binary file
|
package/esm/wllama.d.ts
ADDED
|
@@ -0,0 +1,397 @@
|
|
|
1
|
+
/// <reference types="node" />
|
|
2
|
+
/// <reference types="node" />
|
|
3
|
+
import { type WllamaWorkerResources } from './worker';
|
|
4
|
+
import CacheManager, { type DownloadOptions } from './cache-manager';
|
|
5
|
+
import { ModelManager, Model, type ModelSource } from './model-manager';
|
|
6
|
+
import type { LoadedContextInfo, LoadModelParams, StreamParams } from './types/types';
|
|
7
|
+
import type { ChatCompletionChunk, ChatCompletionParams, ChatCompletionResponse, CreateEmbeddingResponse, EmbeddingCreateParams, RawCompletionChunk, RawCompletionParams, RawCompletionResponse, RerankParams, RerankResponse } from './types/oai-compat';
|
|
8
|
+
import { type HuggingFaceParams } from './huggingface';
|
|
9
|
+
export interface WllamaLogger {
|
|
10
|
+
debug: typeof console.debug;
|
|
11
|
+
log: typeof console.log;
|
|
12
|
+
warn: typeof console.warn;
|
|
13
|
+
error: typeof console.error;
|
|
14
|
+
}
|
|
15
|
+
export interface WllamaConfig {
|
|
16
|
+
/**
|
|
17
|
+
* If true, suppress all log messages from native CPP code
|
|
18
|
+
*/
|
|
19
|
+
suppressNativeLog?: boolean;
|
|
20
|
+
/**
|
|
21
|
+
* Custom logger functions
|
|
22
|
+
*/
|
|
23
|
+
logger?: WllamaLogger;
|
|
24
|
+
/**
|
|
25
|
+
* Maximum number of parallel files to be downloaded
|
|
26
|
+
*
|
|
27
|
+
* Default: parallelDownloads = 3
|
|
28
|
+
*/
|
|
29
|
+
parallelDownloads?: number;
|
|
30
|
+
/**
|
|
31
|
+
* Allow offline mode. If true, the model will be loaded from cache if it's available.
|
|
32
|
+
*
|
|
33
|
+
* Default: allowOffline = false
|
|
34
|
+
*/
|
|
35
|
+
allowOffline?: boolean;
|
|
36
|
+
/**
|
|
37
|
+
* Custom cache manager (only for advanced usage)
|
|
38
|
+
*/
|
|
39
|
+
cacheManager?: CacheManager;
|
|
40
|
+
/**
|
|
41
|
+
* Custom model manager (only for advanced usage)
|
|
42
|
+
*/
|
|
43
|
+
modelManager?: ModelManager;
|
|
44
|
+
}
|
|
45
|
+
export interface WllamaChatMessage {
|
|
46
|
+
role: 'system' | 'user' | 'assistant';
|
|
47
|
+
content: string;
|
|
48
|
+
}
|
|
49
|
+
export interface AssetsPathConfig {
|
|
50
|
+
default: string;
|
|
51
|
+
'single-thread/wllama.wasm'?: string;
|
|
52
|
+
'multi-thread/wllama.wasm'?: string;
|
|
53
|
+
}
|
|
54
|
+
export interface ModelMetadata {
|
|
55
|
+
hparams: {
|
|
56
|
+
nVocab: number;
|
|
57
|
+
nCtxTrain: number;
|
|
58
|
+
nEmbd: number;
|
|
59
|
+
nLayer: number;
|
|
60
|
+
};
|
|
61
|
+
meta: Record<string, string>;
|
|
62
|
+
}
|
|
63
|
+
/**
|
|
64
|
+
* Logger preset with debug messages suppressed
|
|
65
|
+
*/
|
|
66
|
+
export declare const LoggerWithoutDebug: {
|
|
67
|
+
debug: () => void;
|
|
68
|
+
assert(condition?: boolean | undefined, ...data: any[]): void;
|
|
69
|
+
assert(value: any, message?: string | undefined, ...optionalParams: any[]): void;
|
|
70
|
+
clear(): void;
|
|
71
|
+
clear(): void;
|
|
72
|
+
count(label?: string | undefined): void;
|
|
73
|
+
count(label?: string | undefined): void;
|
|
74
|
+
countReset(label?: string | undefined): void;
|
|
75
|
+
countReset(label?: string | undefined): void;
|
|
76
|
+
dir(item?: any, options?: any): void;
|
|
77
|
+
dir(obj: any, options?: import("util").InspectOptions | undefined): void;
|
|
78
|
+
dirxml(...data: any[]): void;
|
|
79
|
+
dirxml(...data: any[]): void;
|
|
80
|
+
error(...data: any[]): void;
|
|
81
|
+
error(message?: any, ...optionalParams: any[]): void;
|
|
82
|
+
group(...data: any[]): void;
|
|
83
|
+
group(...label: any[]): void;
|
|
84
|
+
groupCollapsed(...data: any[]): void;
|
|
85
|
+
groupCollapsed(...label: any[]): void;
|
|
86
|
+
groupEnd(): void;
|
|
87
|
+
groupEnd(): void;
|
|
88
|
+
info(...data: any[]): void;
|
|
89
|
+
info(message?: any, ...optionalParams: any[]): void;
|
|
90
|
+
log(...data: any[]): void;
|
|
91
|
+
log(message?: any, ...optionalParams: any[]): void;
|
|
92
|
+
table(tabularData?: any, properties?: string[] | undefined): void;
|
|
93
|
+
table(tabularData: any, properties?: readonly string[] | undefined): void;
|
|
94
|
+
time(label?: string | undefined): void;
|
|
95
|
+
time(label?: string | undefined): void;
|
|
96
|
+
timeEnd(label?: string | undefined): void;
|
|
97
|
+
timeEnd(label?: string | undefined): void;
|
|
98
|
+
timeLog(label?: string | undefined, ...data: any[]): void;
|
|
99
|
+
timeLog(label?: string | undefined, ...data: any[]): void;
|
|
100
|
+
timeStamp(label?: string | undefined): void;
|
|
101
|
+
timeStamp(label?: string | undefined): void;
|
|
102
|
+
trace(...data: any[]): void;
|
|
103
|
+
trace(message?: any, ...optionalParams: any[]): void;
|
|
104
|
+
warn(...data: any[]): void;
|
|
105
|
+
warn(message?: any, ...optionalParams: any[]): void;
|
|
106
|
+
Console: console.ConsoleConstructor;
|
|
107
|
+
profile(label?: string | undefined): void;
|
|
108
|
+
profileEnd(label?: string | undefined): void;
|
|
109
|
+
};
|
|
110
|
+
export type WllamaErrorType = 'model_not_loaded' | 'download_error' | 'load_error' | 'kv_cache_full' | 'unknown_error' | 'inference_error';
|
|
111
|
+
export declare class WllamaError extends Error {
|
|
112
|
+
type: WllamaErrorType;
|
|
113
|
+
constructor(message: string, type?: WllamaErrorType);
|
|
114
|
+
}
|
|
115
|
+
/**
|
|
116
|
+
* AbortError is thrown when the user wants to abort the current operation.
|
|
117
|
+
* This is equivalent to AbortError in Fetch API.
|
|
118
|
+
*/
|
|
119
|
+
export declare class WllamaAbortError extends Error {
|
|
120
|
+
name: string;
|
|
121
|
+
constructor();
|
|
122
|
+
}
|
|
123
|
+
/**
|
|
124
|
+
* RuntimeError is thrown when there is an error in the WASM runtime, such as stack overflow, OOM, etc.
|
|
125
|
+
* Stack trace of the error in the WASM runtime can be included in the error object for debugging purpose.
|
|
126
|
+
*/
|
|
127
|
+
export declare class WllamaRuntimeError extends Error {
|
|
128
|
+
name: string;
|
|
129
|
+
stack: string;
|
|
130
|
+
constructor(message: string, stack: string);
|
|
131
|
+
}
|
|
132
|
+
/**
|
|
133
|
+
* Set compatibility options for Wllama.
|
|
134
|
+
* By default, these are set to URL of the latest builds on CDN, which requires internet to download. If you want to use local assets or have your own CDN, follow the instruction from @wllama/wllama-compat package.
|
|
135
|
+
*/
|
|
136
|
+
export interface WllamaCompat {
|
|
137
|
+
worker: string | {
|
|
138
|
+
code: string;
|
|
139
|
+
};
|
|
140
|
+
wasm: string;
|
|
141
|
+
}
|
|
142
|
+
export declare class Wllama {
|
|
143
|
+
cacheManager: CacheManager;
|
|
144
|
+
modelManager: ModelManager;
|
|
145
|
+
private compat;
|
|
146
|
+
private proxy;
|
|
147
|
+
private config;
|
|
148
|
+
private pathConfig;
|
|
149
|
+
private useMultiThread;
|
|
150
|
+
private nbThreads;
|
|
151
|
+
private useEmbeddings;
|
|
152
|
+
private useRerank;
|
|
153
|
+
private loadedContextInfo;
|
|
154
|
+
private seed;
|
|
155
|
+
private bosToken;
|
|
156
|
+
private eosToken;
|
|
157
|
+
private eotToken;
|
|
158
|
+
private eogTokens;
|
|
159
|
+
private addBosToken;
|
|
160
|
+
private addEosToken;
|
|
161
|
+
private mediaMarker?;
|
|
162
|
+
private chatTemplate?;
|
|
163
|
+
private metadata?;
|
|
164
|
+
private hasEncoder;
|
|
165
|
+
private decoderStartToken;
|
|
166
|
+
private chatTemplateKwargs;
|
|
167
|
+
constructor(pathConfig: AssetsPathConfig, wllamaConfig?: WllamaConfig);
|
|
168
|
+
private logger;
|
|
169
|
+
private checkModelLoaded;
|
|
170
|
+
/**
|
|
171
|
+
* Get the libllama version string, e.g. "b6327-4d74393".
|
|
172
|
+
*
|
|
173
|
+
* @returns version string embedded at build time.
|
|
174
|
+
*/
|
|
175
|
+
static getLibllamaVersion(): string;
|
|
176
|
+
/**
|
|
177
|
+
* Set compatibility options for Wllama.
|
|
178
|
+
* @param compat Set to null to disable compatibility, or 'default' to use the default compat resources from CDN.
|
|
179
|
+
* @param mode 'safari' by default; If set to 'firefox_safari', the compat mode will **also** be enabled on Firefox, which will significantly degrade the performance but allow using WebGPU on Firefox.
|
|
180
|
+
*/
|
|
181
|
+
setCompat(compat: WllamaCompat | null | 'default', mode?: 'safari' | 'firefox_safari'): void;
|
|
182
|
+
/**
|
|
183
|
+
* Check if the model is loaded via `loadModel()`
|
|
184
|
+
*/
|
|
185
|
+
isModelLoaded(): boolean;
|
|
186
|
+
/**
|
|
187
|
+
* Get token ID associated to BOS (begin of sentence) token.
|
|
188
|
+
*
|
|
189
|
+
* NOTE: This can only being used after `loadModel` is called.
|
|
190
|
+
*
|
|
191
|
+
* @returns -1 if the model is not loaded.
|
|
192
|
+
*/
|
|
193
|
+
getBOS(): number;
|
|
194
|
+
/**
|
|
195
|
+
* Get token ID associated to EOS (end of sentence) token.
|
|
196
|
+
*
|
|
197
|
+
* NOTE: This can only being used after `loadModel` is called.
|
|
198
|
+
*
|
|
199
|
+
* @returns -1 if the model is not loaded.
|
|
200
|
+
*/
|
|
201
|
+
getEOS(): number;
|
|
202
|
+
/**
|
|
203
|
+
* Get token ID associated to EOT (end of turn) token.
|
|
204
|
+
*
|
|
205
|
+
* NOTE: This can only being used after `loadModel` is called.
|
|
206
|
+
*
|
|
207
|
+
* @returns -1 if the model is not loaded.
|
|
208
|
+
*/
|
|
209
|
+
getEOT(): number;
|
|
210
|
+
/**
|
|
211
|
+
* Check if a given token is end-of-generation token (e.g. EOS, EOT, etc.)
|
|
212
|
+
*
|
|
213
|
+
* @param token the token ID to be checked
|
|
214
|
+
* @returns true if the token is EOS, EOT, or any other end-of-generation tokens
|
|
215
|
+
*/
|
|
216
|
+
isTokenEOG(token: number): boolean;
|
|
217
|
+
/**
|
|
218
|
+
* Get token ID associated to token used by decoder, to start generating output sequence(only usable for encoder-decoder architecture). In other words, encoder uses normal BOS and decoder uses this token.
|
|
219
|
+
*
|
|
220
|
+
* NOTE: This can only being used after `loadModel` is called.
|
|
221
|
+
*
|
|
222
|
+
* @returns -1 if the model is not loaded.
|
|
223
|
+
*/
|
|
224
|
+
getDecoderStartToken(): number;
|
|
225
|
+
/**
|
|
226
|
+
* Get model hyper-parameters and metadata
|
|
227
|
+
*
|
|
228
|
+
* NOTE: This can only being used after `loadModel` is called.
|
|
229
|
+
*
|
|
230
|
+
* @returns ModelMetadata
|
|
231
|
+
*/
|
|
232
|
+
getModelMetadata(): ModelMetadata;
|
|
233
|
+
/**
|
|
234
|
+
* Check if we're currently using multi-thread build.
|
|
235
|
+
*
|
|
236
|
+
* NOTE: This can only being used after `loadModel` is called.
|
|
237
|
+
*
|
|
238
|
+
* @returns true if multi-thread is used.
|
|
239
|
+
*/
|
|
240
|
+
isMultithread(): boolean;
|
|
241
|
+
/**
|
|
242
|
+
* Get number of threads used in the current context.
|
|
243
|
+
*
|
|
244
|
+
* NOTE: This can only being used after `loadModel` is called.
|
|
245
|
+
*
|
|
246
|
+
* @returns number of threads
|
|
247
|
+
*/
|
|
248
|
+
getNumThreads(): number;
|
|
249
|
+
/**
|
|
250
|
+
* Check if the current model uses encoder-decoder architecture
|
|
251
|
+
*
|
|
252
|
+
* NOTE: This can only being used after `loadModel` is called.
|
|
253
|
+
*
|
|
254
|
+
* @returns true if multi-thread is used.
|
|
255
|
+
*/
|
|
256
|
+
isEncoderDecoderArchitecture(): boolean;
|
|
257
|
+
/**
|
|
258
|
+
* Must we add BOS token to the tokenized sequence?
|
|
259
|
+
*
|
|
260
|
+
* NOTE: This can only being used after `loadModel` is called.
|
|
261
|
+
*
|
|
262
|
+
* @returns true if BOS token must be added to the sequence
|
|
263
|
+
*/
|
|
264
|
+
mustAddBosToken(): boolean;
|
|
265
|
+
/**
|
|
266
|
+
* Must we add EOS token to the tokenized sequence?
|
|
267
|
+
*
|
|
268
|
+
* NOTE: This can only being used after `loadModel` is called.
|
|
269
|
+
*
|
|
270
|
+
* @returns true if EOS token must be added to the sequence
|
|
271
|
+
*/
|
|
272
|
+
mustAddEosToken(): boolean;
|
|
273
|
+
/**
|
|
274
|
+
* Get the jinja chat template comes with the model. It only available if the original model (before converting to gguf) has the template in `tokenizer_config.json`
|
|
275
|
+
*
|
|
276
|
+
* NOTE: This can only being used after `loadModel` is called.
|
|
277
|
+
*
|
|
278
|
+
* @returns the jinja template. null if there is no template in gguf
|
|
279
|
+
*/
|
|
280
|
+
getChatTemplate(): string | null;
|
|
281
|
+
/**
|
|
282
|
+
* Check if WebGPU is supported by the current environment.
|
|
283
|
+
* @returns true if WebGPU is supported
|
|
284
|
+
*/
|
|
285
|
+
isSupportWebGPU(): boolean;
|
|
286
|
+
/**
|
|
287
|
+
* Load model from a given URL (or a list of URLs, in case the model is splitted into smaller files)
|
|
288
|
+
* - If the model already been downloaded (via `downloadModel()`), then we will use the cached model
|
|
289
|
+
* - Else, we download the model from internet
|
|
290
|
+
* @param modelSourceOrURL
|
|
291
|
+
* @param params
|
|
292
|
+
*/
|
|
293
|
+
loadModelFromUrl(modelSourceOrURL: ModelSource | string, params?: LoadModelParams & DownloadOptions & {
|
|
294
|
+
useCache?: boolean;
|
|
295
|
+
}): Promise<void>;
|
|
296
|
+
/**
|
|
297
|
+
* Load model from a given Hugging Face model ID and file path.
|
|
298
|
+
*
|
|
299
|
+
* @param hfOptions
|
|
300
|
+
* @param params
|
|
301
|
+
*/
|
|
302
|
+
loadModelFromHF(hfOptions: HuggingFaceParams, params?: LoadModelParams & DownloadOptions & {
|
|
303
|
+
useCache?: boolean;
|
|
304
|
+
}): Promise<void>;
|
|
305
|
+
/**
|
|
306
|
+
* Load model from a given list of Blob.
|
|
307
|
+
*
|
|
308
|
+
* You can pass multiple buffers into the function (in case the model contains multiple shards).
|
|
309
|
+
*
|
|
310
|
+
* @param ggufBlobsOrModel Can be either list of Blobs (in case you use local file), or a Model object (in case you use ModelManager)
|
|
311
|
+
* @param params LoadModelParams
|
|
312
|
+
*/
|
|
313
|
+
loadModel(ggufBlobsOrModel: Blob[] | Model, params?: LoadModelParams): Promise<void>;
|
|
314
|
+
getLoadedContextInfo(): LoadedContextInfo;
|
|
315
|
+
/**
|
|
316
|
+
* Calculate embedding vector for a given text.
|
|
317
|
+
* By default, BOS and EOS tokens will be added automatically. You can use the "skipBOS" and "skipEOS" option to disable it.
|
|
318
|
+
* @param options OAI-compatible embedding creation options
|
|
319
|
+
* @returns OAI-compatible embedding response
|
|
320
|
+
*/
|
|
321
|
+
createEmbedding(options: EmbeddingCreateParams): Promise<CreateEmbeddingResponse>;
|
|
322
|
+
/**
|
|
323
|
+
* Rerank a list of documents against a query.
|
|
324
|
+
* Requires the model to be loaded with embeddings: true and pooling_type: 'rank'.
|
|
325
|
+
* @param options Reranking options (query, documents, top_n)
|
|
326
|
+
* @returns Reranking response with relevance scores sorted highest first
|
|
327
|
+
*/
|
|
328
|
+
createRerank(options: RerankParams): Promise<RerankResponse>;
|
|
329
|
+
/**
|
|
330
|
+
* Make chat completion for a given chat messages.
|
|
331
|
+
* @param options OAI-compatible chat completion options
|
|
332
|
+
* @returns OAI-compatible chat completion response (only the final result when stream=false) or an async iterator of completion chunks (when stream=true)
|
|
333
|
+
*/
|
|
334
|
+
createChatCompletion(options: ChatCompletionParams & {
|
|
335
|
+
stream?: false;
|
|
336
|
+
}): Promise<ChatCompletionResponse>;
|
|
337
|
+
createChatCompletion(options: ChatCompletionParams & StreamParams<ChatCompletionChunk>): Promise<void>;
|
|
338
|
+
createChatCompletion(options: ChatCompletionParams & {
|
|
339
|
+
stream: true;
|
|
340
|
+
}): Promise<AsyncIterable<ChatCompletionChunk>>;
|
|
341
|
+
/**
|
|
342
|
+
* Make (raw) completion for a given text.
|
|
343
|
+
* @param options OAI-compatible completion options
|
|
344
|
+
* @returns OAI-compatible completion response (stream=false), void when done (stream=true + onData), or async iterator (stream=true, no onData)
|
|
345
|
+
*/
|
|
346
|
+
createCompletion(options: RawCompletionParams & {
|
|
347
|
+
stream?: false;
|
|
348
|
+
}): Promise<RawCompletionResponse>;
|
|
349
|
+
createCompletion(options: RawCompletionParams & StreamParams<RawCompletionChunk>): Promise<void>;
|
|
350
|
+
createCompletion(options: RawCompletionParams & {
|
|
351
|
+
stream: true;
|
|
352
|
+
}): Promise<AsyncIterable<RawCompletionChunk>>;
|
|
353
|
+
/**
|
|
354
|
+
* Private implementation of createCompletion
|
|
355
|
+
*/
|
|
356
|
+
private createCompletionImpl;
|
|
357
|
+
/**
|
|
358
|
+
* Same with `createCompletion`, but returns an async iterator instead.
|
|
359
|
+
* Only called when stream=true and no onData is provided.
|
|
360
|
+
*/
|
|
361
|
+
private createCompletionGenerator;
|
|
362
|
+
/**
|
|
363
|
+
* Whether the currently loaded model supports a specific input modality (e.g. image or audio).
|
|
364
|
+
* @param modality
|
|
365
|
+
* @returns
|
|
366
|
+
*/
|
|
367
|
+
supportInputModality(modality: 'image' | 'audio'): boolean;
|
|
368
|
+
/**
|
|
369
|
+
* Unload the model and free all memory.
|
|
370
|
+
*
|
|
371
|
+
* Note: This function will NOT crash if model is not yet loaded
|
|
372
|
+
*/
|
|
373
|
+
exit(): Promise<void>;
|
|
374
|
+
/**
|
|
375
|
+
* [FOR DEBUGGING ONLY] Run ggml backend ops tests without loading any model.
|
|
376
|
+
*
|
|
377
|
+
* Initializes the wasm runtime, executes `test-backend-ops` with the given args, then shuts down.
|
|
378
|
+
*
|
|
379
|
+
* For more info, please refer to guides/debug.md
|
|
380
|
+
*
|
|
381
|
+
* @param args Arguments forwarded to test-backend-ops (e.g. ["-o", "ADD"])
|
|
382
|
+
* @returns retcode (0 = all tests passed) and success flag
|
|
383
|
+
*/
|
|
384
|
+
testBackendOps(args?: string[]): Promise<{
|
|
385
|
+
retcode: number;
|
|
386
|
+
success: boolean;
|
|
387
|
+
}>;
|
|
388
|
+
/**
|
|
389
|
+
* get debug info
|
|
390
|
+
*/
|
|
391
|
+
_getDebugInfo(): Promise<any>;
|
|
392
|
+
private jsonDecode;
|
|
393
|
+
private prepareMultimodalInput;
|
|
394
|
+
private getRerankResult;
|
|
395
|
+
private getResponse;
|
|
396
|
+
getWorkerResources(): WllamaWorkerResources;
|
|
397
|
+
}
|