@blank-utils/llm 0.2.6 → 0.2.7
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/dist/backends/transformers.d.ts +45 -0
- package/dist/backends/transformers.d.ts.map +1 -0
- package/dist/backends/webllm.d.ts +35 -0
- package/dist/backends/webllm.d.ts.map +1 -0
- package/dist/core.d.ts +71 -0
- package/dist/core.d.ts.map +1 -0
- package/dist/detect.d.ts +21 -0
- package/dist/detect.d.ts.map +1 -0
- package/dist/helpers.d.ts +35 -0
- package/dist/helpers.d.ts.map +1 -0
- package/dist/index.d.ts +27 -0
- package/dist/index.d.ts.map +1 -0
- package/dist/models.d.ts +74 -0
- package/dist/models.d.ts.map +1 -0
- package/dist/react/chat-input.d.ts +64 -0
- package/dist/react/chat-input.d.ts.map +1 -0
- package/dist/react/components.d.ts +64 -0
- package/dist/react/components.d.ts.map +1 -0
- package/dist/react/index.d.ts +311 -0
- package/dist/react/index.d.ts.map +1 -0
- package/dist/types.d.ts +180 -0
- package/dist/types.d.ts.map +1 -0
- package/package.json +1 -1
|
@@ -0,0 +1,45 @@
|
|
|
1
|
+
/**
|
|
2
|
+
* Transformers.js Backend Implementation
|
|
3
|
+
* Fallback backend using HuggingFace Transformers.js with ONNX runtime
|
|
4
|
+
*/
|
|
5
|
+
import type { LLMProvider, ChatMessage, GenerateOptions, StreamCallback, LoadProgressCallback, Backend, Device, Quantization } from '../types';
|
|
6
|
+
/**
|
|
7
|
+
* Default model for Transformers.js backend
|
|
8
|
+
* Using Qwen2.5 0.5B as it's well-tested with ONNX
|
|
9
|
+
*/
|
|
10
|
+
import { DEFAULT_TRANSFORMERS_MODEL, TRANSFORMERS_MODELS, type TransformersModelID } from '../models';
|
|
11
|
+
export { DEFAULT_TRANSFORMERS_MODEL, TRANSFORMERS_MODELS };
|
|
12
|
+
export type TransformersModelAlias = TransformersModelID;
|
|
13
|
+
/**
|
|
14
|
+
* Model size estimates for UI display
|
|
15
|
+
*/
|
|
16
|
+
export declare const TRANSFORMERS_MODEL_SIZES: Record<TransformersModelAlias, string>;
|
|
17
|
+
/**
|
|
18
|
+
* Configuration for TransformersProvider
|
|
19
|
+
*/
|
|
20
|
+
export interface TransformersProviderConfig {
|
|
21
|
+
device?: Device;
|
|
22
|
+
quantization?: Quantization;
|
|
23
|
+
}
|
|
24
|
+
/**
|
|
25
|
+
* Transformers.js provider implementation
|
|
26
|
+
*/
|
|
27
|
+
export declare class TransformersProvider implements LLMProvider {
|
|
28
|
+
readonly backend: Backend;
|
|
29
|
+
private pipeline;
|
|
30
|
+
private currentModel;
|
|
31
|
+
private device;
|
|
32
|
+
private quantization;
|
|
33
|
+
constructor(config?: TransformersProviderConfig);
|
|
34
|
+
get isReady(): boolean;
|
|
35
|
+
get modelId(): string | null;
|
|
36
|
+
load(modelId: string, onProgress?: LoadProgressCallback): Promise<void>;
|
|
37
|
+
chat(messages: ChatMessage[], options?: GenerateOptions): Promise<string>;
|
|
38
|
+
stream(messages: ChatMessage[], onToken: StreamCallback, options?: GenerateOptions): Promise<string>;
|
|
39
|
+
unload(): Promise<void>;
|
|
40
|
+
}
|
|
41
|
+
/**
|
|
42
|
+
* Create a Transformers.js provider instance
|
|
43
|
+
*/
|
|
44
|
+
export declare function createTransformersProvider(config?: TransformersProviderConfig): TransformersProvider;
|
|
45
|
+
//# sourceMappingURL=transformers.d.ts.map
|
|
@@ -0,0 +1 @@
|
|
|
1
|
+
{"version":3,"file":"transformers.d.ts","sourceRoot":"","sources":["../../src/backends/transformers.ts"],"names":[],"mappings":"AAAA;;;GAGG;AAEH,OAAO,KAAK,EACV,WAAW,EACX,WAAW,EACX,eAAe,EACf,cAAc,EACd,oBAAoB,EAEpB,OAAO,EACP,MAAM,EACN,YAAY,EACb,MAAM,UAAU,CAAC;AAKlB;;;GAGG;AACH,OAAO,EAAE,0BAA0B,EAAE,mBAAmB,EAAE,KAAK,mBAAmB,EAAE,MAAM,WAAW,CAAC;AACtG,OAAO,EAA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
5
|
+
import type { LLMProvider, ChatMessage, GenerateOptions, StreamCallback, LoadProgressCallback, Backend } from '../types';
|
|
6
|
+
/**
|
|
7
|
+
* Default model for WebLLM backend
|
|
8
|
+
* Using Phi 3.5 Mini as it's well-tested and reasonably sized
|
|
9
|
+
*/
|
|
10
|
+
import { DEFAULT_WEBLLM_MODEL, WEBLLM_MODELS, type WebLLMModelID } from '../models';
|
|
11
|
+
export { DEFAULT_WEBLLM_MODEL, WEBLLM_MODELS };
|
|
12
|
+
export type WebLLMModelAlias = WebLLMModelID;
|
|
13
|
+
/**
|
|
14
|
+
* Model size estimates for UI display
|
|
15
|
+
*/
|
|
16
|
+
export declare const WEBLLM_MODEL_SIZES: Record<WebLLMModelAlias, string>;
|
|
17
|
+
/**
|
|
18
|
+
* WebLLM provider implementation
|
|
19
|
+
*/
|
|
20
|
+
export declare class WebLLMProvider implements LLMProvider {
|
|
21
|
+
readonly backend: Backend;
|
|
22
|
+
private engine;
|
|
23
|
+
private currentModel;
|
|
24
|
+
get isReady(): boolean;
|
|
25
|
+
get modelId(): string | null;
|
|
26
|
+
load(modelId: string, onProgress?: LoadProgressCallback): Promise<void>;
|
|
27
|
+
chat(messages: ChatMessage[], options?: GenerateOptions): Promise<string>;
|
|
28
|
+
stream(messages: ChatMessage[], onToken: StreamCallback, options?: GenerateOptions): Promise<string>;
|
|
29
|
+
unload(): Promise<void>;
|
|
30
|
+
}
|
|
31
|
+
/**
|
|
32
|
+
* Create a WebLLM provider instance
|
|
33
|
+
*/
|
|
34
|
+
export declare function createWebLLMProvider(): WebLLMProvider;
|
|
35
|
+
//# sourceMappingURL=webllm.d.ts.map
|
|
@@ -0,0 +1 @@
|
|
|
1
|
+
{"version":3,"file":"webllm.d.ts","sourceRoot":"","sources":["../../src/backends/webllm.ts"],"names":[],"mappings":"AAAA;;;GAGG;AAEH,OAAO,KAAK,EACV,WAAW,EACX,WAAW,EACX,eAAe,EACf,cAAc,EACd,oBAAoB,EAEpB,OAAO,EACR,MAAM,UAAU,CAAC;AAMlB;;;GAGG;AACH,OAAO,EAAE,oBAAoB,EAAE,aAAa,EAAE,KAAK,aAAa,EAAE,MAAM,WAAW,CAAC;AACpF,OAAO,EAAE,oBAAoB,EAAE,aAAa,EAAE,CAAC;AAE/C,MAAM,MAAM,gBAAgB,GAAG,aAAa,CAAC;AAE7C;;GAEG;AACH,eAAO,MAAM,kBAAkB,EAAE,MAAM,CAAC,gBAAgB,EAAE,MAAM,CA6B/D,CAAC;AAYF;;GAEG;AACH,qBAAa,cAAe,YAAW,WAAW;IAChD,QAAQ,CAAC,OAAO,EAAE,OAAO,CAAY;IAErC,OAAO,CAAC,MAAM,CAA0B;IACxC,OAAO,CAAC,YAAY,CAAuB;IAE3C,IAAI,OAAO,IAAI,OAAO,CAErB;IAED,IAAI,OAAO,IAAI,MAAM,GAAG,IAAI,CAE3B;IAEK,IAAI,CAAC,OAAO,EAAE,MAAM,EAAE,UAAU,CAAC,EAAE,oBAAoB,GAAG,OAAO,CAAC,IAAI,CAAC;IAwBvE,IAAI,CAAC,QAAQ,EAAE,WAAW,EAAE,EAAE,OAAO,CAAC,EAAE,eAAe,GAAG,OAAO,CAAC,MAAM,CAAC;IAmBzE,MAAM,CACV,QAAQ,EAAE,WAAW,EAAE,EACvB,OAAO,EAAE,cAAc,EACvB,OAAO,CAAC,EAAE,eAAe,GACxB,OAAO,CAAC,MAAM,CAAC;IA8BZ,MAAM,IAAI,OAAO,CAAC,IAAI,CAAC;CAO9B;AAED;;GAEG;AACH,wBAAgB,oBAAoB,IAAI,cAAc,CAErD"}
|
package/dist/core.d.ts
ADDED
|
@@ -0,0 +1,71 @@
|
|
|
1
|
+
/**
|
|
2
|
+
* Core LLM functionality - Separated from index.ts to avoid circular dependencies
|
|
3
|
+
* with React integration.
|
|
4
|
+
*
|
|
5
|
+
* @module local-llm/core
|
|
6
|
+
*/
|
|
7
|
+
export type { Backend, Device, Quantization, LLMConfig, MessageRole, ChatMessage, StreamCallback, LoadProgress, LoadProgressCallback, GenerateOptions, LLMProvider as LLMProviderInterface, AttachOptions, BrowserCapabilities, SupportedModel, } from './types';
|
|
8
|
+
export { checkWebGPU, checkWasm, detectCapabilities, logCapabilities } from './detect';
|
|
9
|
+
export { WebLLMProvider, createWebLLMProvider, DEFAULT_WEBLLM_MODEL, WEBLLM_MODELS, } from './backends/webllm';
|
|
10
|
+
export { TransformersProvider, createTransformersProvider, DEFAULT_TRANSFORMERS_MODEL, TRANSFORMERS_MODELS, } from './backends/transformers';
|
|
11
|
+
export { createOutputStreamer, attachToElements, createChatUI, createLoadingIndicator, } from './helpers';
|
|
12
|
+
import type { LLMConfig, ChatMessage, GenerateOptions, StreamCallback, AttachOptions } from './types';
|
|
13
|
+
/**
|
|
14
|
+
* Main LLM interface with simplified API
|
|
15
|
+
*/
|
|
16
|
+
export interface LocalLLM {
|
|
17
|
+
/**
|
|
18
|
+
* Whether the model is loaded and ready
|
|
19
|
+
*/
|
|
20
|
+
readonly isReady: boolean;
|
|
21
|
+
/**
|
|
22
|
+
* The current model ID
|
|
23
|
+
*/
|
|
24
|
+
readonly modelId: string | null;
|
|
25
|
+
/**
|
|
26
|
+
* The backend being used
|
|
27
|
+
*/
|
|
28
|
+
readonly backend: 'webllm' | 'transformers';
|
|
29
|
+
/**
|
|
30
|
+
* Generate a chat response
|
|
31
|
+
*/
|
|
32
|
+
chat(messages: ChatMessage[] | string, options?: GenerateOptions): Promise<string>;
|
|
33
|
+
/**
|
|
34
|
+
* Generate with streaming output
|
|
35
|
+
*/
|
|
36
|
+
stream(messages: ChatMessage[] | string, onToken: StreamCallback, options?: GenerateOptions): Promise<string>;
|
|
37
|
+
/**
|
|
38
|
+
* Attach to input/output elements for automatic generation
|
|
39
|
+
*/
|
|
40
|
+
attachToInput(inputSelector: string | HTMLInputElement | HTMLTextAreaElement, outputSelector: string | HTMLElement, options?: AttachOptions): () => void;
|
|
41
|
+
/**
|
|
42
|
+
* Unload the model and free resources
|
|
43
|
+
*/
|
|
44
|
+
unload(): Promise<void>;
|
|
45
|
+
}
|
|
46
|
+
/**
|
|
47
|
+
* Create a LocalLLM instance
|
|
48
|
+
*
|
|
49
|
+
* @param config - Configuration options
|
|
50
|
+
* @returns Promise that resolves to a LocalLLM instance once the model is loaded
|
|
51
|
+
*
|
|
52
|
+
* @example
|
|
53
|
+
* ```typescript
|
|
54
|
+
* // Simple usage with defaults
|
|
55
|
+
* const llm = await createLLM();
|
|
56
|
+
*
|
|
57
|
+
* // With configuration
|
|
58
|
+
* const llm = await createLLM({
|
|
59
|
+
* model: 'phi-3-mini',
|
|
60
|
+
* backend: 'webllm',
|
|
61
|
+
* systemPrompt: 'You are a helpful assistant.',
|
|
62
|
+
* onLoadProgress: (p) => console.log(p.progress)
|
|
63
|
+
* });
|
|
64
|
+
* ```
|
|
65
|
+
*/
|
|
66
|
+
export declare function createLLM(config?: LLMConfig): Promise<LocalLLM>;
|
|
67
|
+
/**
|
|
68
|
+
* Quick helper to test if the current browser supports WebGPU
|
|
69
|
+
*/
|
|
70
|
+
export declare function isWebGPUSupported(): Promise<boolean>;
|
|
71
|
+
//# sourceMappingURL=core.d.ts.map
|
|
@@ -0,0 +1 @@
|
|
|
1
|
+
{"version":3,"file":"core.d.ts","sourceRoot":"","sources":["../src/core.ts"],"names":[],"mappings":"AAAA;;;;;GAKG;AAGH,YAAY,EACV,OAAO,EACP,MAAM,EACN,YAAY,EACZ,SAAS,EACT,WAAW,EACX,WAAW,EACX,cAAc,EACd,YAAY,EACZ,oBAAoB,EACpB,eAAe,EACf,WAAW,IAAI,oBAAoB,EACnC,aAAa,EACb,mBAAmB,EACnB,cAAc,GACf,MAAM,SAAS,CAAC;AAGjB,OAAO,EAAE,WAAW,EAAE,SAAS,EAAE,kBAAkB,EAAE,eAAe,EAAE,MAAM,UAAU,CAAC;AAGvF,OAAO,EACL,cAAc,EACd,oBAAoB,EACpB,oBAAoB,EACpB,aAAa,GACd,MAAM,mBAAmB,CAAC;AAE3B,OAAO,EACL,oBAAoB,E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ED
|
@@ -0,0 +1,21 @@
|
|
|
1
|
+
/**
|
|
2
|
+
* Browser capability detection utilities
|
|
3
|
+
*/
|
|
4
|
+
import type { BrowserCapabilities } from './types';
|
|
5
|
+
/**
|
|
6
|
+
* Check if WebGPU is available in the current browser
|
|
7
|
+
*/
|
|
8
|
+
export declare function checkWebGPU(): Promise<boolean>;
|
|
9
|
+
/**
|
|
10
|
+
* Check if WebAssembly is available
|
|
11
|
+
*/
|
|
12
|
+
export declare function checkWasm(): boolean;
|
|
13
|
+
/**
|
|
14
|
+
* Detect browser capabilities and recommend backend/device
|
|
15
|
+
*/
|
|
16
|
+
export declare function detectCapabilities(): Promise<BrowserCapabilities>;
|
|
17
|
+
/**
|
|
18
|
+
* Log capability detection results to console
|
|
19
|
+
*/
|
|
20
|
+
export declare function logCapabilities(): Promise<BrowserCapabilities>;
|
|
21
|
+
//# sourceMappingURL=detect.d.ts.map
|
|
@@ -0,0 +1 @@
|
|
|
1
|
+
{"version":3,"file":"detect.d.ts","sourceRoot":"","sources":["../src/detect.ts"],"names":[],"mappings":"AAAA;;GAEG;AAEH,OAAO,KAAK,EAAE,mBAAmB,EAAmB,MAAM,SAAS,CAAC;AAEpE;;GAEG;AACH,wBAAsB,WAAW,IAAI,OAAO,CAAC,OAAO,CAAC,CAapD;AAED;;GAEG;AACH,wBAAgB,SAAS,IAAI,OAAO,CAYnC;AAED;;GAEG;AACH,wBAAsB,kBAAkB,IAAI,OAAO,CAAC,mBAAmB,CAAC,CAuBvE;AAED;;GAEG;AACH,wBAAsB,eAAe,IAAI,OAAO,CAAC,mBAAmB,CAAC,CAUpE"}
|
|
@@ -0,0 +1,35 @@
|
|
|
1
|
+
/**
|
|
2
|
+
* DOM Helper Utilities
|
|
3
|
+
* Easy integration with HTML input/output elements
|
|
4
|
+
*/
|
|
5
|
+
import type { StreamCallback, AttachOptions } from './types';
|
|
6
|
+
/**
|
|
7
|
+
* Creates a streaming callback that updates an output element
|
|
8
|
+
*/
|
|
9
|
+
export declare function createOutputStreamer(outputSelector: string | HTMLElement, options?: {
|
|
10
|
+
append?: boolean;
|
|
11
|
+
scrollToBottom?: boolean;
|
|
12
|
+
}): StreamCallback;
|
|
13
|
+
/**
|
|
14
|
+
* Attach LLM to input/output elements with automatic handling
|
|
15
|
+
*/
|
|
16
|
+
export declare function attachToElements(inputSelector: string | HTMLInputElement | HTMLTextAreaElement, outputSelector: string | HTMLElement, generateFn: (input: string, onToken: StreamCallback) => Promise<string>, options?: AttachOptions): () => void;
|
|
17
|
+
/**
|
|
18
|
+
* Create a simple chat UI in a container
|
|
19
|
+
*/
|
|
20
|
+
export declare function createChatUI(containerSelector: string | HTMLElement): {
|
|
21
|
+
input: HTMLTextAreaElement;
|
|
22
|
+
output: HTMLDivElement;
|
|
23
|
+
sendButton: HTMLButtonElement;
|
|
24
|
+
cleanup: () => void;
|
|
25
|
+
};
|
|
26
|
+
/**
|
|
27
|
+
* Create a loading indicator
|
|
28
|
+
*/
|
|
29
|
+
export declare function createLoadingIndicator(containerSelector: string | HTMLElement): {
|
|
30
|
+
show: () => void;
|
|
31
|
+
hide: () => void;
|
|
32
|
+
setProgress: (percent: number, status?: string) => void;
|
|
33
|
+
element: HTMLDivElement;
|
|
34
|
+
};
|
|
35
|
+
//# sourceMappingURL=helpers.d.ts.map
|
|
@@ -0,0 +1 @@
|
|
|
1
|
+
{"version":3,"file":"helpers.d.ts","sourceRoot":"","sources":["../src/helpers.ts"],"names":[],"mappings":"AAAA;;;GAGG;AAEH,OAAO,KAAK,EAAE,cAAc,EAAE,aAAa,EAAE,MAAM,SAAS,CAAC;AAgB7D;;GAEG;AACH,wBAAgB,oBAAoB,CAClC,cAAc,EAAE,MAAM,GAAG,WAAW,EACpC,OAAO,CAAC,EAAE;IACR,MAAM,CAAC,EAAE,OAAO,CAAC;IACjB,cAAc,CAAC,EAAE,OAAO,CAAC;CAC1B,GACA,cAAc,CAoBhB;AAED;;GAEG;AACH,wBAAgB,gBAAgB,CAC9B,aAAa,EAAE,MAAM,GAAG,gBAAgB,GAAG,mBAAmB,EAC9D,cAAc,EAAE,MAAM,GAAG,WAAW,EACpC,UAAU,EAAE,CAAC,KAAK,EAAE,MAAM,EAAE,OAAO,EAAE,cAAc,KAAK,OAAO,CAAC,MAAM,CAAC,EACvE,OAAO,CAAC,EAAE,aAAa,GACtB,MAAM,IAAI,CA8DZ;AAED;;GAEG;AACH,wBAAgB,YAAY,CAAC,iBAAiB,EAAE,MAAM,GAAG,WAAW,GAAG;IACrE,KAAK,EAAE,mBAAmB,CAAC;IAC3B,MAAM,EAAE,cAAc,CAAC;IACvB,UAAU,EAAE,iBAAiB,CAAC;IAC9B,OAAO,EAAE,MAAM,IAAI,CAAC;CACrB,CAyBA;AAED;;GAEG;AACH,wBAAgB,sBAAsB,CAAC,iBAAiB,EAAE,MAAM,GAAG,WAAW,GAAG;IAC/E,IAAI,EAAE,MAAM,IAAI,CAAC;IACjB,IAAI,EAAE,MAAM,IAAI,CAAC;IACjB,WAAW,EAAE,CAAC,OAAO,EAAE,MAAM,EAAE,MAAM,CAAC,EAAE,MAAM,KAAK,IAAI,CAAC;IACxD,OAAO,EAAE,cAAc,CAAC;CACzB,CAwCA"}
|
package/dist/index.d.ts
ADDED
|
@@ -0,0 +1,27 @@
|
|
|
1
|
+
/**
|
|
2
|
+
* Local LLM - Browser-based LLM inference library
|
|
3
|
+
*
|
|
4
|
+
* A simple, generalized library for interacting with LLMs directly in the browser.
|
|
5
|
+
* Works in any codebase with WebGPU or WASM support.
|
|
6
|
+
*
|
|
7
|
+
* @example
|
|
8
|
+
* ```typescript
|
|
9
|
+
* import { createLLM } from '@blank-utils/llm';
|
|
10
|
+
*
|
|
11
|
+
* const llm = await createLLM({
|
|
12
|
+
* onLoadProgress: (p) => console.log(`Loading: ${p.progress}%`)
|
|
13
|
+
* });
|
|
14
|
+
*
|
|
15
|
+
* // Streaming generation
|
|
16
|
+
* await llm.stream('Tell me a joke', (token) => console.log(token));
|
|
17
|
+
*
|
|
18
|
+
* // Attach to DOM elements
|
|
19
|
+
* llm.attachToInput('#input', '#output');
|
|
20
|
+
* ```
|
|
21
|
+
*
|
|
22
|
+
* @module @blank-utils/llm
|
|
23
|
+
*/
|
|
24
|
+
export { type Backend, type Device, type Quantization, type LLMConfig, type MessageRole, type ChatMessage, type StreamCallback, type LoadProgress, type LoadProgressCallback, type GenerateOptions, type LLMProviderInterface, type AttachOptions, type BrowserCapabilities, checkWebGPU, checkWasm, detectCapabilities, logCapabilities, WebLLMProvider, createWebLLMProvider, DEFAULT_WEBLLM_MODEL, WEBLLM_MODELS, TransformersProvider, createTransformersProvider, DEFAULT_TRANSFORMERS_MODEL, TRANSFORMERS_MODELS, createOutputStreamer, attachToElements, createChatUI, createLoadingIndicator, type LocalLLM, createLLM, isWebGPUSupported, } from './core';
|
|
25
|
+
export { createLLM as default } from './core';
|
|
26
|
+
export { LLMProvider, useLLM, useChat, useStream, useCompletion, LLMLoading, LLMReady, Chat, ChatInput, type LLMContextValue, type LLMProviderProps, type UseChatOptions, type UseChatReturn, type UseStreamOptions, type UseStreamReturn, type UseCompletionOptions, type UseCompletionReturn, type LLMLoadingProps, type LLMReadyProps, type ChatProps, type ChatInputProps, } from './react';
|
|
27
|
+
//# sourceMappingURL=index.d.ts.map
|
|
@@ -0,0 +1 @@
|
|
|
1
|
+
{"version":3,"file":"index.d.ts","sourceRoot":"","sources":["../src/index.ts"],"names":[],"mappings":"AAAA;;;;;;;;;;;;;;;;;;;;;;GAsBG;AAGH,OAAO,EAEL,KAAK,OAAO,EACZ,KAAK,MAAM,EACX,KAAK,YAAY,EACjB,KAAK,SAAS,EACd,KAAK,WAAW,EAChB,KAAK,WAAW,EAChB,KAAK,cAAc,EACnB,KAAK,YAAY,EACjB,KAAK,oBAAoB,EACzB,KAAK,eAAe,EACpB,KAAK,oBAAoB,EACzB,KAAK,aAAa,EAClB,KAAK,mBAAmB,EAGxB,WAAW,EACX,SAAS,EACT,kBAAkB,EAClB,eAAe,EAGf,cAAc,EACd,oBAAoB,EACpB,oBAAoB,EACpB,aAAa,EACb,oBAAoB,EACpB,0BAA0B,EAC1B,0BAA0B,EAC1B,mBAAmB,EAGnB,oBAAoB,EACpB,gBAAgB,EAChB,YAAY,EACZ,sBAAsB,EAGtB,KAAK,QAAQ,EACb,SAAS,EACT,iBAAiB,GAClB,MAAM,QAAQ,CAAC;AAGhB,OAAO,EAAE,SAAS,IAAI,OAAO,EAAE,MAAM,QAAQ,CAAC;AAQ9C,OAAO,EAEL,WAAW,EACX,MAAM,EAGN,OAAO,EACP,SAAS,EACT,aAAa,EAGb,UAAU,EACV,QAAQ,EAGR,IAAI,EACJ,SAAS,EAGT,KAAK,eAAe,EACpB,KAAK,gBAAgB,EACrB,KAAK,cAAc,EACnB,KAAK,aAAa,EAClB,KAAK,gBAAgB,EACrB,KAAK,eAAe,EACpB,KAAK,oBAAoB,EACzB,KAAK,mBAAmB,EACxB,KAAK,eAAe,EACpB,KAAK,aAAa,EAClB,KAAK,SAAS,EACd,KAAK,cAAc,GACpB,MAAM,SAAS,CAAC"}
|
package/dist/models.d.ts
ADDED
|
@@ -0,0 +1,74 @@
|
|
|
1
|
+
/**
|
|
2
|
+
* Supported Models Configuration
|
|
3
|
+
*/
|
|
4
|
+
/**
|
|
5
|
+
* Default model for WebLLM backend
|
|
6
|
+
* Using Phi 3.5 Mini as it's well-tested and reasonably sized
|
|
7
|
+
*/
|
|
8
|
+
export declare const DEFAULT_WEBLLM_MODEL = "Phi-3.5-mini-instruct-q4f16_1-MLC";
|
|
9
|
+
/**
|
|
10
|
+
* Popular WebLLM model options with correct MLC model IDs
|
|
11
|
+
* These IDs must match exactly what's in web-llm's prebuiltAppConfig
|
|
12
|
+
*
|
|
13
|
+
* @see https://github.com/mlc-ai/web-llm/blob/main/src/config.ts
|
|
14
|
+
*/
|
|
15
|
+
export declare const WEBLLM_MODELS: {
|
|
16
|
+
readonly 'llama-3.2-1b': "Llama-3.2-1B-Instruct-q4f16_1-MLC";
|
|
17
|
+
readonly 'llama-3.2-3b': "Llama-3.2-3B-Instruct-q4f16_1-MLC";
|
|
18
|
+
readonly 'llama-3.1-8b': "Llama-3.1-8B-Instruct-q4f16_1-MLC";
|
|
19
|
+
readonly 'llama-3.1-8b-1k': "Llama-3.1-8B-Instruct-q4f16_1-MLC-1k";
|
|
20
|
+
readonly 'phi-3.5-mini': "Phi-3.5-mini-instruct-q4f16_1-MLC";
|
|
21
|
+
readonly 'phi-3.5-mini-1k': "Phi-3.5-mini-instruct-q4f16_1-MLC-1k";
|
|
22
|
+
readonly 'phi-3.5-vision': "Phi-3.5-vision-instruct-q4f16_1-MLC";
|
|
23
|
+
readonly 'qwen-2.5-0.5b': "Qwen2.5-0.5B-Instruct-q4f16_1-MLC";
|
|
24
|
+
readonly 'qwen-2.5-1.5b': "Qwen2.5-1.5B-Instruct-q4f16_1-MLC";
|
|
25
|
+
readonly 'qwen-2.5-3b': "Qwen2.5-3B-Instruct-q4f16_1-MLC";
|
|
26
|
+
readonly 'qwen-2.5-7b': "Qwen2.5-7B-Instruct-q4f16_1-MLC";
|
|
27
|
+
readonly 'qwen-2.5-coder-0.5b': "Qwen2.5-Coder-0.5B-Instruct-q4f16_1-MLC";
|
|
28
|
+
readonly 'qwen-2.5-coder-1.5b': "Qwen2.5-Coder-1.5B-Instruct-q4f16_1-MLC";
|
|
29
|
+
readonly 'qwen-3-0.6b': "Qwen3-0.6B-q4f16_1-MLC";
|
|
30
|
+
readonly 'qwen-3-1.7b': "Qwen3-1.7B-q4f16_1-MLC";
|
|
31
|
+
readonly 'qwen-3-4b': "Qwen3-4B-q4f16_1-MLC";
|
|
32
|
+
readonly 'qwen-3-8b': "Qwen3-8B-q4f16_1-MLC";
|
|
33
|
+
readonly 'gemma-2-2b': "gemma-2-2b-it-q4f16_1-MLC";
|
|
34
|
+
readonly 'gemma-2-2b-1k': "gemma-2-2b-it-q4f16_1-MLC-1k";
|
|
35
|
+
readonly 'gemma-2-9b': "gemma-2-9b-it-q4f16_1-MLC";
|
|
36
|
+
readonly 'smollm2-135m': "SmolLM2-135M-Instruct-q0f16-MLC";
|
|
37
|
+
readonly 'smollm2-360m': "SmolLM2-360M-Instruct-q4f16_1-MLC";
|
|
38
|
+
readonly 'smollm2-1.7b': "SmolLM2-1.7B-Instruct-q4f16_1-MLC";
|
|
39
|
+
readonly 'mistral-7b': "Mistral-7B-Instruct-v0.3-q4f16_1-MLC";
|
|
40
|
+
readonly 'deepseek-r1-qwen-7b': "DeepSeek-R1-Distill-Qwen-7B-q4f16_1-MLC";
|
|
41
|
+
readonly 'deepseek-r1-llama-8b': "DeepSeek-R1-Distill-Llama-8B-q4f16_1-MLC";
|
|
42
|
+
readonly 'hermes-3-llama-3.2-3b': "Hermes-3-Llama-3.2-3B-q4f16_1-MLC";
|
|
43
|
+
readonly 'hermes-3-llama-3.1-8b': "Hermes-3-Llama-3.1-8B-q4f16_1-MLC";
|
|
44
|
+
};
|
|
45
|
+
/**
|
|
46
|
+
* Default model for Transformers.js backend
|
|
47
|
+
* Using Qwen2.5 0.5B as it's well-tested with ONNX
|
|
48
|
+
*/
|
|
49
|
+
export declare const DEFAULT_TRANSFORMERS_MODEL = "onnx-community/Qwen2.5-0.5B-Instruct";
|
|
50
|
+
/**
|
|
51
|
+
* Transformers.js compatible models (must have ONNX weights)
|
|
52
|
+
* These are specifically converted for browser use via transformers.js
|
|
53
|
+
*
|
|
54
|
+
* @see https://huggingface.co/onnx-community for more models
|
|
55
|
+
*/
|
|
56
|
+
export declare const TRANSFORMERS_MODELS: {
|
|
57
|
+
readonly 'qwen-2.5-0.5b': "onnx-community/Qwen2.5-0.5B-Instruct";
|
|
58
|
+
readonly 'qwen-2.5-1.5b': "onnx-community/Qwen2.5-1.5B-Instruct";
|
|
59
|
+
readonly 'qwen-2.5-coder-0.5b': "onnx-community/Qwen2.5-Coder-0.5B-Instruct";
|
|
60
|
+
readonly 'qwen-2.5-coder-1.5b': "onnx-community/Qwen2.5-Coder-1.5B-Instruct";
|
|
61
|
+
readonly 'qwen-3-0.6b': "onnx-community/Qwen3-0.6B-ONNX";
|
|
62
|
+
readonly 'smollm2-135m': "HuggingFaceTB/SmolLM2-135M-Instruct";
|
|
63
|
+
readonly 'smollm2-360m': "HuggingFaceTB/SmolLM2-360M-Instruct";
|
|
64
|
+
readonly 'smollm2-1.7b': "HuggingFaceTB/SmolLM2-1.7B-Instruct";
|
|
65
|
+
readonly 'phi-3-mini': "Xenova/Phi-3-mini-4k-instruct";
|
|
66
|
+
readonly tinyllama: "Xenova/TinyLlama-1.1B-Chat-v1.0";
|
|
67
|
+
};
|
|
68
|
+
export type WebLLMModelID = keyof typeof WEBLLM_MODELS;
|
|
69
|
+
export type TransformersModelID = keyof typeof TRANSFORMERS_MODELS;
|
|
70
|
+
/**
|
|
71
|
+
* Union of all supported model IDs for type safety
|
|
72
|
+
*/
|
|
73
|
+
export type SupportedModel = WebLLMModelID | TransformersModelID | (string & {});
|
|
74
|
+
//# sourceMappingURL=models.d.ts.map
|
|
@@ -0,0 +1 @@
|
|
|
1
|
+
{"version":3,"file":"models.d.ts","sourceRoot":"","sources":["../src/models.ts"],"names":[],"mappings":"AAAA;;GAEG;AAEH;;;GAGG;AACH,eAAO,MAAM,oBAAoB,sCAAsC,CAAC;AAExE;;;;;GAKG;AACH,eAAO,MAAM,aAAa;;;;;;;;;;;;;;;;;;;;;;;;;;;;;CAgDhB,CAAC;AAGX;;;GAGG;AACH,eAAO,MAAM,0BAA0B,yCAAyC,CAAC;AAEjF;;;;;GAKG;AACH,eAAO,MAAM,mBAAmB;;;;;;;;;;;CAoBtB,CAAC;AAEX,MAAM,MAAM,aAAa,GAAG,MAAM,OAAO,aAAa,CAAC;AACvD,MAAM,MAAM,mBAAmB,GAAG,MAAM,OAAO,mBAAmB,CAAC;AAEnE;;GAEG;AACH,MAAM,MAAM,cAAc,GAAG,aAAa,GAAG,mBAAmB,GAAG,CAAC,MAAM,GAAG,EAAE,CAAC,CAAC"}
|
|
@@ -0,0 +1,64 @@
|
|
|
1
|
+
/**
|
|
2
|
+
* ChatInput — Self-contained auto-resizing textarea with send button, image paste/upload, and model selector slot.
|
|
3
|
+
* Zero external dependencies. All styles embedded via CSS-in-JS.
|
|
4
|
+
* Brutalist minimal aesthetic — flat, no decoration.
|
|
5
|
+
*
|
|
6
|
+
* Supports: Ctrl+V paste, drag-and-drop, and click-to-upload for images.
|
|
7
|
+
*
|
|
8
|
+
* @example
|
|
9
|
+
* ```tsx
|
|
10
|
+
* <ChatInput
|
|
11
|
+
* value={input}
|
|
12
|
+
* onChange={setInput}
|
|
13
|
+
* onSend={handleSend}
|
|
14
|
+
* disabled={isGenerating}
|
|
15
|
+
* placeholder="Type a message..."
|
|
16
|
+
* images={images}
|
|
17
|
+
* onImageAdd={handleAddImage}
|
|
18
|
+
* onImageRemove={handleRemoveImage}
|
|
19
|
+
* modelSelector={<MyDropdown />}
|
|
20
|
+
* />
|
|
21
|
+
* ```
|
|
22
|
+
*/
|
|
23
|
+
import * as React from 'react';
|
|
24
|
+
export interface ImageAttachment {
|
|
25
|
+
id: string;
|
|
26
|
+
dataUrl: string;
|
|
27
|
+
file: File;
|
|
28
|
+
name: string;
|
|
29
|
+
}
|
|
30
|
+
export interface ChatInputProps {
|
|
31
|
+
/** Current value of the input */
|
|
32
|
+
value: string;
|
|
33
|
+
/** Called when value changes */
|
|
34
|
+
onChange: (value: string) => void;
|
|
35
|
+
/** Called when user submits (Enter or send button) */
|
|
36
|
+
onSend: () => void;
|
|
37
|
+
/** Called when user clicks stop */
|
|
38
|
+
onStop?: () => void;
|
|
39
|
+
/** Whether the input is disabled (e.g. during generation) */
|
|
40
|
+
disabled?: boolean;
|
|
41
|
+
/** Whether currently generating (shows stop button instead of send) */
|
|
42
|
+
isGenerating?: boolean;
|
|
43
|
+
/** Placeholder text */
|
|
44
|
+
placeholder?: string;
|
|
45
|
+
/** Maximum rows before scrolling */
|
|
46
|
+
maxRows?: number;
|
|
47
|
+
/** Optional action elements to render in the toolbar */
|
|
48
|
+
actions?: React.ReactNode;
|
|
49
|
+
/** Theme */
|
|
50
|
+
theme?: 'dark' | 'light';
|
|
51
|
+
/** Additional className for the container */
|
|
52
|
+
className?: string;
|
|
53
|
+
/** Current images attached */
|
|
54
|
+
images?: ImageAttachment[];
|
|
55
|
+
/** Called when an image is added (paste/drop/upload) */
|
|
56
|
+
onImageAdd?: (image: ImageAttachment) => void;
|
|
57
|
+
/** Called when an image is removed */
|
|
58
|
+
onImageRemove?: (id: string) => void;
|
|
59
|
+
/** Optional slot for a model selector dropdown */
|
|
60
|
+
modelSelector?: React.ReactNode;
|
|
61
|
+
}
|
|
62
|
+
declare function ChatInput({ value, onChange, onSend, onStop, disabled, isGenerating, placeholder, maxRows, actions, theme, className, images, onImageAdd, onImageRemove, modelSelector, }: ChatInputProps): import("react/jsx-runtime").JSX.Element;
|
|
63
|
+
export { ChatInput };
|
|
64
|
+
//# sourceMappingURL=chat-input.d.ts.map
|
|
@@ -0,0 +1 @@
|
|
|
1
|
+
{"version":3,"file":"chat-input.d.ts","sourceRoot":"","sources":["../../src/react/chat-input.tsx"],"names":[],"mappings":"AAAA;;;;;;;;;;;;;;;;;;;;;GAqBG;AAEH,OAAO,KAAK,KAAK,MAAM,OAAO,CAAC;AAO/B,MAAM,WAAW,eAAe;IAC9B,EAAE,EAAE,MAAM,CAAC;IACX,OAAO,EAAE,MAAM,CAAC;IAChB,IAAI,EAAE,IAAI,CAAC;IACX,IAAI,EAAE,MAAM,CAAC;CACd;AAED,MAAM,WAAW,cAAc;IAC7B,iCAAiC;IACjC,KAAK,EAAE,MAAM,CAAC;IAEd,gCAAgC;IAChC,QAAQ,EAAE,CAAC,KAAK,EAAE,MAAM,KAAK,IAAI,CAAC;IAElC,sDAAsD;IACtD,MAAM,EAAE,MAAM,IAAI,CAAC;IAEnB,mCAAmC;IACnC,MAAM,CAAC,EAAE,MAAM,IAAI,CAAC;IAEpB,6DAA6D;IAC7D,QAAQ,CAAC,EAAE,OAAO,CAAC;IAEnB,uEAAuE;IACvE,YAAY,CAAC,EAAE,OAAO,CAAC;IAEvB,uBAAuB;IACvB,WAAW,CAAC,EAAE,MAAM,CAAC;IAErB,oCAAoC;IACpC,OAAO,CAAC,EAAE,MAAM,CAAC;IAEjB,wDAAwD;IACxD,OAAO,CAAC,EAAE,KAAK,CAAC,SAAS,CAAC;IAE1B,YAAY;IACZ,KAAK,CAAC,EAAE,MAAM,GAAG,OAAO,CAAC;IAEzB,6CAA6C;IAC7C,SAAS,CAAC,EAAE,MAAM,CAAC;IAEnB,8BAA8B;IAC9B,MAAM,CAAC,EAAE,eAAe,EAAE,CAAC;IAE3B,wDAAwD;IACxD,UAAU,CAAC,EAAE,CAAC,KAAK,EAAE,eAAe,KAAK,IAAI,CAAC;IAE9C,sCAAsC;IACtC,aAAa,CAAC,EAAE,CAAC,EAAE,EAAE,MAAM,KAAK,IAAI,CAAC;IAErC,kDAAkD;IAClD,aAAa,CAAC,EAAE,KAAK,CAAC,SAAS,CAAC;CACjC;AA8PD,iBAAS,SAAS,CAAC,EACjB,KAAK,EACL,QAAQ,EACR,MAAM,EACN,MAAM,EACN,QAAgB,EAChB,YAAoB,EACpB,WAAiC,EACjC,OAAW,EACX,OAAO,EACP,KAAc,EACd,SAAS,EACT,MAAW,EACX,UAAU,EACV,aAAa,EACb,aAAa,GACd,EAAE,cAAc,2CA8LhB;AAED,OAAO,EAAE,SAAS,EAAE,CAAC"}
|
|
@@ -0,0 +1,64 @@
|
|
|
1
|
+
/**
|
|
2
|
+
* Chat — A complete, self-contained chat interface for @blank-utils/llm.
|
|
3
|
+
* Drop inside <LLMProvider> and get a working chat UI in one line.
|
|
4
|
+
*
|
|
5
|
+
* Brutalist minimal aesthetic — flat black, high contrast, no decoration.
|
|
6
|
+
*
|
|
7
|
+
* @example
|
|
8
|
+
* ```tsx
|
|
9
|
+
* import { LLMProvider, Chat, ChatApp } from "@blank-utils/llm/react";
|
|
10
|
+
*
|
|
11
|
+
* // Option A: Zero setup (includes provider + model switching)
|
|
12
|
+
* <ChatApp defaultModel="qwen-2.5-0.5b" />
|
|
13
|
+
*
|
|
14
|
+
* // Option B: Manual provider
|
|
15
|
+
* <LLMProvider model="qwen-2.5-0.5b">
|
|
16
|
+
* <Chat />
|
|
17
|
+
* </LLMProvider>
|
|
18
|
+
* ```
|
|
19
|
+
*/
|
|
20
|
+
import * as React from 'react';
|
|
21
|
+
import { type UseChatOptions } from './index';
|
|
22
|
+
import { type SupportedModel } from '../models';
|
|
23
|
+
export interface ChatProps {
|
|
24
|
+
/** System prompt for the conversation */
|
|
25
|
+
systemPrompt?: string;
|
|
26
|
+
/** Placeholder text for the input */
|
|
27
|
+
placeholder?: string;
|
|
28
|
+
/** Theme */
|
|
29
|
+
theme?: 'dark' | 'light';
|
|
30
|
+
/** Additional className for the outermost container */
|
|
31
|
+
className?: string;
|
|
32
|
+
/** Maximum height of the chat container. Default: '600px' */
|
|
33
|
+
maxHeight?: string;
|
|
34
|
+
/** Options passed to useChat internally */
|
|
35
|
+
chatOptions?: Omit<UseChatOptions, 'systemPrompt'>;
|
|
36
|
+
/** Custom actions rendered in the input toolbar */
|
|
37
|
+
inputActions?: React.ReactNode;
|
|
38
|
+
/** Called when a message is sent */
|
|
39
|
+
onSend?: (message: string) => void;
|
|
40
|
+
/** Called when a response is received */
|
|
41
|
+
onResponse?: (response: string) => void;
|
|
42
|
+
/** Called on error */
|
|
43
|
+
onError?: (error: Error) => void;
|
|
44
|
+
/** Whether to show the model info header. Default: true */
|
|
45
|
+
showHeader?: boolean;
|
|
46
|
+
/** Whether to show loading progress. Default: true */
|
|
47
|
+
showProgress?: boolean;
|
|
48
|
+
/** Custom welcome message when chat is empty */
|
|
49
|
+
welcomeMessage?: string;
|
|
50
|
+
/** Called when the user selects a new model in the dropdown */
|
|
51
|
+
onModelChange?: (modelId: string) => void;
|
|
52
|
+
}
|
|
53
|
+
export interface ChatAppProps extends ChatProps {
|
|
54
|
+
/** Default model ID to start with */
|
|
55
|
+
defaultModel?: SupportedModel;
|
|
56
|
+
/** Default backend to use */
|
|
57
|
+
defaultBackend?: 'webllm' | 'transformers' | 'auto';
|
|
58
|
+
/** Auto-load model on mount. Default: true */
|
|
59
|
+
autoLoad?: boolean;
|
|
60
|
+
}
|
|
61
|
+
declare function Chat({ systemPrompt, placeholder, theme, className, maxHeight, inputActions, onSend: onSendProp, onResponse, onError: onErrorProp, showHeader, showProgress, welcomeMessage, onModelChange, }: ChatProps): import("react/jsx-runtime").JSX.Element;
|
|
62
|
+
declare function ChatApp({ defaultModel, defaultBackend, autoLoad, onModelChange, ...chatProps }: ChatAppProps): import("react/jsx-runtime").JSX.Element;
|
|
63
|
+
export { Chat, ChatApp };
|
|
64
|
+
//# sourceMappingURL=components.d.ts.map
|
|
@@ -0,0 +1 @@
|
|
|
1
|
+
{"version":3,"file":"components.d.ts","sourceRoot":"","sources":["../../src/react/components.tsx"],"names":[],"mappings":"AAAA;;;;;;;;;;;;;;;;;;GAkBG;AAEH,OAAO,KAAK,KAAK,MAAM,OAAO,CAAC;AAE/B,OAAO,EAAU,KAAK,cAAc,EAAsC,MAAM,SAAS,CAAC;AAE1F,OAAO,EAAsC,KAAK,cAAc,EAAE,MAAM,WAAW,CAAC;AAepF,MAAM,WAAW,SAAS;IACxB,yCAAyC;IACzC,YAAY,CAAC,EAAE,MAAM,CAAC;IACtB,qCAAqC;IACrC,WAAW,CAAC,EAAE,MAAM,CAAC;IACrB,YAAY;IACZ,KAAK,CAAC,EAAE,MAAM,GAAG,OAAO,CAAC;IACzB,uDAAuD;IACvD,SAAS,CAAC,EAAE,MAAM,CAAC;IACnB,6DAA6D;IAC7D,SAAS,CAAC,EAAE,MAAM,CAAC;IACnB,2CAA2C;IAC3C,WAAW,CAAC,EAAE,IAAI,CAAC,cAAc,EAAE,cAAc,CAAC,CAAC;IACnD,mDAAmD;IACnD,YAAY,CAAC,EAAE,KAAK,CAAC,SAAS,CAAC;IAC/B,oCAAoC;IACpC,MAAM,CAAC,EAAE,CAAC,OAAO,EAAE,MAAM,KAAK,IAAI,CAAC;IACnC,yCAAyC;IACzC,UAAU,CAAC,EAAE,CAAC,QAAQ,EAAE,MAAM,KAAK,IAAI,CAAC;IACxC,sBAAsB;IACtB,OAAO,CAAC,EAAE,CAAC,KAAK,EAAE,KAAK,KAAK,IAAI,CAAC;IACjC,2DAA2D;IAC3D,UAAU,CAAC,EAAE,OAAO,CAAC;IACrB,sDAAsD;IACtD,YAAY,CAAC,EAAE,OAAO,CAAC;IACvB,gDAAgD;IAChD,cAAc,CAAC,EAAE,MAAM,CAAC;IACxB,+DAA+D;IAC/D,aAAa,CAAC,EAAE,CAAC,OAAO,EAAE,MAAM,KAAK,IAAI,CAAC;CAC3C;AAED,MAAM,WAAW,YAAa,SAAQ,SAAS;IAC7C,qCAAqC;IACrC,YAAY,CAAC,EAAE,cAAc,CAAC;IAC9B,6BAA6B;IAC7B,cAAc,CAAC,EAAE,QAAQ,GAAG,cAAc,GAAG,MAAM,CAAC;IACpD,8CAA8C;IAC9C,QAAQ,CAAC,EAAE,OAAO,CAAC;CACpB;AAsbD,iBAAS,IAAI,CAAC,EACZ,YAAoC,EACpC,WAAiC,EACjC,KAAc,EACd,SAAS,EACT,SAAmB,EACnB,YAAY,EACZ,MAAM,EAAE,UAAU,EAClB,UAAU,EACV,OAAO,EAAE,WAAW,EACpB,UAAiB,EACjB,YAAmB,EACnB,cAAkC,EAClC,aAAa,GACd,EAAE,SAAS,2CAqPX;AAMD,iBAAS,OAAO,CAAC,EACf,YAA8B,EAC9B,cAAuB,EACvB,QAAe,EACf,aAAa,EACb,GAAG,SAAS,EACb,EAAE,YAAY,2CAqBd;AAED,OAAO,EAAE,IAAI,EAAE,OAAO,EAAE,CAAC"}
|
|
@@ -0,0 +1,311 @@
|
|
|
1
|
+
/**
|
|
2
|
+
* React Integration for Local LLM
|
|
3
|
+
*
|
|
4
|
+
* Provides React context, hooks, and components for easy LLM integration.
|
|
5
|
+
*
|
|
6
|
+
* @example
|
|
7
|
+
* ```tsx
|
|
8
|
+
* import { LLMProvider, useChat } from 'local-llm/react';
|
|
9
|
+
*
|
|
10
|
+
* function App() {
|
|
11
|
+
* return (
|
|
12
|
+
* <LLMProvider model="qwen-2.5-0.5b">
|
|
13
|
+
* <ChatComponent />
|
|
14
|
+
* </LLMProvider>
|
|
15
|
+
* );
|
|
16
|
+
* }
|
|
17
|
+
*
|
|
18
|
+
* function ChatComponent() {
|
|
19
|
+
* const { messages, send, isGenerating } = useChat();
|
|
20
|
+
*
|
|
21
|
+
* return (
|
|
22
|
+
* <div>
|
|
23
|
+
* {messages.map((m, i) => <p key={i}>{m.content}</p>)}
|
|
24
|
+
* <button onClick={() => send('Hello!')}>Send</button>
|
|
25
|
+
* </div>
|
|
26
|
+
* );
|
|
27
|
+
* }
|
|
28
|
+
* ```
|
|
29
|
+
*/
|
|
30
|
+
import * as React from 'react';
|
|
31
|
+
import type { LLMConfig, ChatMessage, GenerateOptions, LoadProgress, Backend } from '../types';
|
|
32
|
+
import { type LocalLLM } from '../core';
|
|
33
|
+
export interface LLMContextValue {
|
|
34
|
+
/** The LLM instance (null while loading) */
|
|
35
|
+
llm: LocalLLM | null;
|
|
36
|
+
/** Whether the model is currently loading */
|
|
37
|
+
isLoading: boolean;
|
|
38
|
+
/** Whether the model is ready for inference */
|
|
39
|
+
isReady: boolean;
|
|
40
|
+
/** Current loading progress */
|
|
41
|
+
loadProgress: LoadProgress | null;
|
|
42
|
+
/** Error if loading failed */
|
|
43
|
+
error: Error | null;
|
|
44
|
+
/** Current model ID */
|
|
45
|
+
modelId: string | null;
|
|
46
|
+
/** Backend being used */
|
|
47
|
+
backend: Backend | null;
|
|
48
|
+
/** Manually reload the model */
|
|
49
|
+
reload: () => Promise<void>;
|
|
50
|
+
/** Unload the model */
|
|
51
|
+
unload: () => Promise<void>;
|
|
52
|
+
}
|
|
53
|
+
export interface LLMProviderProps extends Omit<LLMConfig, 'onLoadProgress'> {
|
|
54
|
+
children: React.ReactNode;
|
|
55
|
+
/**
|
|
56
|
+
* Auto-load the model on mount
|
|
57
|
+
* @default true
|
|
58
|
+
*/
|
|
59
|
+
autoLoad?: boolean;
|
|
60
|
+
/**
|
|
61
|
+
* Callback when model finishes loading
|
|
62
|
+
*/
|
|
63
|
+
onLoad?: (llm: LocalLLM) => void;
|
|
64
|
+
/**
|
|
65
|
+
* Callback on loading progress
|
|
66
|
+
*/
|
|
67
|
+
onProgress?: (progress: LoadProgress) => void;
|
|
68
|
+
/**
|
|
69
|
+
* Callback on error
|
|
70
|
+
*/
|
|
71
|
+
onError?: (error: Error) => void;
|
|
72
|
+
}
|
|
73
|
+
/**
|
|
74
|
+
* Provider component that manages LLM lifecycle
|
|
75
|
+
*
|
|
76
|
+
* @example
|
|
77
|
+
* ```tsx
|
|
78
|
+
* <LLMProvider
|
|
79
|
+
* model="qwen-2.5-0.5b"
|
|
80
|
+
* backend="auto"
|
|
81
|
+
* onProgress={(p) => console.log(p.progress)}
|
|
82
|
+
* >
|
|
83
|
+
* <App />
|
|
84
|
+
* </LLMProvider>
|
|
85
|
+
* ```
|
|
86
|
+
*/
|
|
87
|
+
declare function LLMProvider({ children, autoLoad, onLoad, onProgress, onError, ...config }: LLMProviderProps): import("react/jsx-runtime").JSX.Element;
|
|
88
|
+
/**
|
|
89
|
+
* Access the LLM context
|
|
90
|
+
*
|
|
91
|
+
* @throws If used outside of LLMProvider
|
|
92
|
+
*
|
|
93
|
+
* @example
|
|
94
|
+
* ```tsx
|
|
95
|
+
* const { llm, isReady, loadProgress } = useLLM();
|
|
96
|
+
* ```
|
|
97
|
+
*/
|
|
98
|
+
declare function useLLM(): LLMContextValue;
|
|
99
|
+
export interface UseChatOptions {
|
|
100
|
+
/** Initial messages */
|
|
101
|
+
initialMessages?: ChatMessage[];
|
|
102
|
+
/** System prompt */
|
|
103
|
+
systemPrompt?: string;
|
|
104
|
+
/** Generation options */
|
|
105
|
+
generateOptions?: GenerateOptions;
|
|
106
|
+
/**
|
|
107
|
+
* Queue messages while model is loading
|
|
108
|
+
* When true, users can send messages before model loads - they'll be processed once ready
|
|
109
|
+
* @default true
|
|
110
|
+
*/
|
|
111
|
+
queueWhileLoading?: boolean;
|
|
112
|
+
/** Called when generation starts */
|
|
113
|
+
onStart?: () => void;
|
|
114
|
+
/** Called on each token (streaming) */
|
|
115
|
+
onToken?: (token: string, fullText: string) => void;
|
|
116
|
+
/** Called when generation completes */
|
|
117
|
+
onFinish?: (response: string) => void;
|
|
118
|
+
/** Called on error */
|
|
119
|
+
onError?: (error: Error) => void;
|
|
120
|
+
}
|
|
121
|
+
export interface UseChatReturn {
|
|
122
|
+
/** All messages in the conversation */
|
|
123
|
+
messages: ChatMessage[];
|
|
124
|
+
/** Current input value (for controlled input) */
|
|
125
|
+
input: string;
|
|
126
|
+
/** Set the input value */
|
|
127
|
+
setInput: (input: string) => void;
|
|
128
|
+
/** Whether currently generating a response */
|
|
129
|
+
isGenerating: boolean;
|
|
130
|
+
/** Whether a message is queued waiting for model to load */
|
|
131
|
+
isPending: boolean;
|
|
132
|
+
/** Current streaming text (while generating) */
|
|
133
|
+
streamingText: string;
|
|
134
|
+
/** Send a message and get a response */
|
|
135
|
+
send: (content?: string) => Promise<string>;
|
|
136
|
+
/** Stop the current generation */
|
|
137
|
+
stop: () => void;
|
|
138
|
+
/** Clear all messages */
|
|
139
|
+
clear: () => void;
|
|
140
|
+
/** Add a message without generating a response */
|
|
141
|
+
append: (message: ChatMessage) => void;
|
|
142
|
+
/** Reload/regenerate the last assistant message */
|
|
143
|
+
reload: () => Promise<string>;
|
|
144
|
+
}
|
|
145
|
+
/**
|
|
146
|
+
* Hook for managing a chat conversation with the LLM
|
|
147
|
+
*
|
|
148
|
+
* Supports **eager loading** - users can send messages while the model loads.
|
|
149
|
+
* Messages are queued and processed automatically once the model is ready.
|
|
150
|
+
*
|
|
151
|
+
* @example
|
|
152
|
+
* ```tsx
|
|
153
|
+
* function ChatComponent() {
|
|
154
|
+
* const { isLoading, loadProgress } = useLLM();
|
|
155
|
+
* const {
|
|
156
|
+
* messages,
|
|
157
|
+
* input,
|
|
158
|
+
* setInput,
|
|
159
|
+
* send,
|
|
160
|
+
* isGenerating,
|
|
161
|
+
* isPending, // true if message is queued waiting for model
|
|
162
|
+
* streamingText,
|
|
163
|
+
* } = useChat({
|
|
164
|
+
* systemPrompt: 'You are a helpful assistant.',
|
|
165
|
+
* queueWhileLoading: true, // default: true
|
|
166
|
+
* });
|
|
167
|
+
*
|
|
168
|
+
* return (
|
|
169
|
+
* <div>
|
|
170
|
+
* {isLoading && <p>Loading model... {loadProgress?.progress}%</p>}
|
|
171
|
+
*
|
|
172
|
+
* {messages.map((m, i) => (
|
|
173
|
+
* <div key={i} className={m.role}>
|
|
174
|
+
* {m.content}
|
|
175
|
+
* </div>
|
|
176
|
+
* ))}
|
|
177
|
+
*
|
|
178
|
+
* {isPending && <p className="pending">Waiting for model to load...</p>}
|
|
179
|
+
* {isGenerating && <div className="assistant">{streamingText}</div>}
|
|
180
|
+
*
|
|
181
|
+
* {/* Users can type immediately, even before model loads *\/}
|
|
182
|
+
* <input
|
|
183
|
+
* value={input}
|
|
184
|
+
* onChange={(e) => setInput(e.target.value)}
|
|
185
|
+
* onKeyDown={(e) => e.key === 'Enter' && send()}
|
|
186
|
+
* placeholder={isLoading ? 'Type now, send when ready...' : 'Type a message...'}
|
|
187
|
+
* />
|
|
188
|
+
* <button onClick={() => send()} disabled={isGenerating}>
|
|
189
|
+
* {isPending ? 'Queued...' : 'Send'}
|
|
190
|
+
* </button>
|
|
191
|
+
* </div>
|
|
192
|
+
* );
|
|
193
|
+
* }
|
|
194
|
+
* ```
|
|
195
|
+
*/
|
|
196
|
+
declare function useChat(options?: UseChatOptions): UseChatReturn;
|
|
197
|
+
export interface UseStreamOptions {
|
|
198
|
+
/** Generation options */
|
|
199
|
+
generateOptions?: GenerateOptions;
|
|
200
|
+
/** Called on each token */
|
|
201
|
+
onToken?: (token: string, fullText: string) => void;
|
|
202
|
+
/** Called when complete */
|
|
203
|
+
onFinish?: (response: string) => void;
|
|
204
|
+
/** Called on error */
|
|
205
|
+
onError?: (error: Error) => void;
|
|
206
|
+
}
|
|
207
|
+
export interface UseStreamReturn {
|
|
208
|
+
/** Current streamed text */
|
|
209
|
+
text: string;
|
|
210
|
+
/** Whether currently streaming */
|
|
211
|
+
isStreaming: boolean;
|
|
212
|
+
/** Start streaming a response */
|
|
213
|
+
stream: (messages: ChatMessage[] | string) => Promise<string>;
|
|
214
|
+
/** Stop streaming */
|
|
215
|
+
stop: () => void;
|
|
216
|
+
/** Clear the text */
|
|
217
|
+
clear: () => void;
|
|
218
|
+
}
|
|
219
|
+
/**
|
|
220
|
+
* Hook for simple streaming generation
|
|
221
|
+
*
|
|
222
|
+
* @example
|
|
223
|
+
* ```tsx
|
|
224
|
+
* function StreamComponent() {
|
|
225
|
+
* const { text, isStreaming, stream, clear } = useStream();
|
|
226
|
+
*
|
|
227
|
+
* return (
|
|
228
|
+
* <div>
|
|
229
|
+
* <pre>{text}</pre>
|
|
230
|
+
* <button onClick={() => stream('Tell me a story')} disabled={isStreaming}>
|
|
231
|
+
* Generate
|
|
232
|
+
* </button>
|
|
233
|
+
* <button onClick={clear}>Clear</button>
|
|
234
|
+
* </div>
|
|
235
|
+
* );
|
|
236
|
+
* }
|
|
237
|
+
* ```
|
|
238
|
+
*/
|
|
239
|
+
declare function useStream(options?: UseStreamOptions): UseStreamReturn;
|
|
240
|
+
export interface UseCompletionOptions {
|
|
241
|
+
/** Generation options */
|
|
242
|
+
generateOptions?: GenerateOptions;
|
|
243
|
+
}
|
|
244
|
+
export interface UseCompletionReturn {
|
|
245
|
+
/** Current completion text */
|
|
246
|
+
completion: string;
|
|
247
|
+
/** Whether currently generating */
|
|
248
|
+
isLoading: boolean;
|
|
249
|
+
/** Generate a completion (non-streaming) */
|
|
250
|
+
complete: (prompt: string) => Promise<string>;
|
|
251
|
+
/** Clear the completion */
|
|
252
|
+
clear: () => void;
|
|
253
|
+
}
|
|
254
|
+
/**
|
|
255
|
+
* Hook for simple non-streaming completion
|
|
256
|
+
*
|
|
257
|
+
* @example
|
|
258
|
+
* ```tsx
|
|
259
|
+
* function CompletionComponent() {
|
|
260
|
+
* const { completion, isLoading, complete } = useCompletion();
|
|
261
|
+
*
|
|
262
|
+
* return (
|
|
263
|
+
* <div>
|
|
264
|
+
* <p>{completion}</p>
|
|
265
|
+
* <button onClick={() => complete('Summarize this')} disabled={isLoading}>
|
|
266
|
+
* Complete
|
|
267
|
+
* </button>
|
|
268
|
+
* </div>
|
|
269
|
+
* );
|
|
270
|
+
* }
|
|
271
|
+
* ```
|
|
272
|
+
*/
|
|
273
|
+
declare function useCompletion(options?: UseCompletionOptions): UseCompletionReturn;
|
|
274
|
+
export interface LLMLoadingProps {
|
|
275
|
+
/** Custom loading UI */
|
|
276
|
+
children?: React.ReactNode;
|
|
277
|
+
/** Class name for the wrapper */
|
|
278
|
+
className?: string;
|
|
279
|
+
}
|
|
280
|
+
/**
|
|
281
|
+
* Component that shows loading state while LLM is loading
|
|
282
|
+
*
|
|
283
|
+
* @example
|
|
284
|
+
* ```tsx
|
|
285
|
+
* <LLMLoading>
|
|
286
|
+
* <p>Loading model...</p>
|
|
287
|
+
* </LLMLoading>
|
|
288
|
+
* ```
|
|
289
|
+
*/
|
|
290
|
+
declare function LLMLoading({ children, className }: LLMLoadingProps): import("react/jsx-runtime").JSX.Element | null;
|
|
291
|
+
export interface LLMReadyProps {
|
|
292
|
+
/** Content to show when ready */
|
|
293
|
+
children: React.ReactNode;
|
|
294
|
+
/** Content to show while loading */
|
|
295
|
+
fallback?: React.ReactNode;
|
|
296
|
+
}
|
|
297
|
+
/**
|
|
298
|
+
* Component that only renders children when LLM is ready
|
|
299
|
+
*
|
|
300
|
+
* @example
|
|
301
|
+
* ```tsx
|
|
302
|
+
* <LLMReady fallback={<Loading />}>
|
|
303
|
+
* <ChatInterface />
|
|
304
|
+
* </LLMReady>
|
|
305
|
+
* ```
|
|
306
|
+
*/
|
|
307
|
+
declare function LLMReady({ children, fallback }: LLMReadyProps): import("react/jsx-runtime").JSX.Element;
|
|
308
|
+
import { Chat, ChatApp, type ChatProps, type ChatAppProps } from './components';
|
|
309
|
+
import { ChatInput, type ChatInputProps } from './chat-input';
|
|
310
|
+
export { LLMProvider, useLLM, useChat, useStream, useCompletion, LLMLoading, LLMReady, Chat, ChatApp, ChatInput, type ChatProps, type ChatAppProps, type ChatInputProps, };
|
|
311
|
+
//# sourceMappingURL=index.d.ts.map
|
|
@@ -0,0 +1 @@
|
|
|
1
|
+
{"version":3,"file":"index.d.ts","sourceRoot":"","sources":["../../src/react/index.tsx"],"names":[],"mappings":"AAAA;;;;;;;;;;;;;;;;;;;;;;;;;;;;GA4BG;AACH,OAAO,KAAK,KAAK,MAAM,OAAO,CAAC;AAW/B,OAAO,KAAK,EACV,SAAS,EACT,WAAW,EACX,eAAe,EACf,YAAY,EACZ,OAAO,EACR,MAAM,UAAU,CAAC;AAElB,OAAO,EAAa,KAAK,QAAQ,EAAE,MAAM,SAAS,CAAC;AAQnD,MAAM,WAAW,eAAe;IAC9B,4CAA4C;IAC5C,GAAG,EAAE,QAAQ,GAAG,IAAI,CAAC;IAErB,6CAA6C;IAC7C,SAAS,EAAE,OAAO,CAAC;IAEnB,+CAA+C;IAC/C,OAAO,EAAE,OAAO,CAAC;IAEjB,+BAA+B;IAC/B,YAAY,EAAE,YAAY,GAAG,IAAI,CAAC;IAElC,8BAA8B;IAC9B,KAAK,EAAE,KAAK,GAAG,IAAI,CAAC;IAEpB,uBAAuB;IACvB,OAAO,EAAE,MAAM,GAAG,IAAI,CAAC;IAEvB,yBAAyB;IACzB,OAAO,EAAE,OAAO,GAAG,IAAI,CAAC;IAExB,gCAAgC;IAChC,MAAM,EAAE,MAAM,OAAO,CAAC,IAAI,CAAC,CAAC;IAE5B,uBAAuB;IACvB,MAAM,EAAE,MAAM,OAAO,CAAC,IAAI,CAAC,CAAC;CAC7B;AAQD,MAAM,WAAW,gBAAiB,SAAQ,IAAI,CAAC,S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EAAE,aAAa,2CAQ7D;AAGD,OAAO,EAAE,IAAI,EAAE,OAAO,EAAE,KAAK,SAAS,EAAE,KAAK,YAAY,EAAE,MAAM,cAAc,CAAC;AAChF,OAAO,EAAE,SAAS,EAAE,KAAK,cAAc,EAAE,MAAM,cAAc,CAAC;AAE9D,OAAO,EAEL,WAAW,EACX,MAAM,EAGN,OAAO,EACP,SAAS,EACT,aAAa,EAGb,UAAU,EACV,QAAQ,EAGR,IAAI,EACJ,OAAO,EACP,SAAS,EAGT,KAAK,SAAS,EACd,KAAK,YAAY,EACjB,KAAK,cAAc,GACpB,CAAC"}
|
package/dist/types.d.ts
ADDED
|
@@ -0,0 +1,180 @@
|
|
|
1
|
+
/**
|
|
2
|
+
* Local LLM - Browser-based LLM inference library
|
|
3
|
+
* Shared TypeScript types and interfaces
|
|
4
|
+
*/
|
|
5
|
+
/**
|
|
6
|
+
* Supported backend engines
|
|
7
|
+
*/
|
|
8
|
+
export type Backend = 'webllm' | 'transformers' | 'auto';
|
|
9
|
+
/**
|
|
10
|
+
* Device to run inference on
|
|
11
|
+
*/
|
|
12
|
+
export type Device = 'webgpu' | 'wasm' | 'auto';
|
|
13
|
+
/**
|
|
14
|
+
* Quantization options for model loading
|
|
15
|
+
*/
|
|
16
|
+
export type Quantization = 'q4' | 'q8' | 'fp16' | 'fp32';
|
|
17
|
+
import type { SupportedModel } from './models';
|
|
18
|
+
export type { SupportedModel };
|
|
19
|
+
/**
|
|
20
|
+
* Configuration for creating an LLM instance
|
|
21
|
+
*/
|
|
22
|
+
export interface LLMConfig {
|
|
23
|
+
/**
|
|
24
|
+
* Model identifier. For WebLLM, use MLC model IDs.
|
|
25
|
+
* For Transformers.js, use HuggingFace model IDs.
|
|
26
|
+
* @default 'Phi-3-mini-4k-instruct-q4f16_1-MLC' for WebLLM
|
|
27
|
+
*/
|
|
28
|
+
model?: SupportedModel;
|
|
29
|
+
/**
|
|
30
|
+
* Which backend to use
|
|
31
|
+
* @default 'auto' - Will prefer WebLLM if WebGPU available
|
|
32
|
+
*/
|
|
33
|
+
backend?: Backend;
|
|
34
|
+
/**
|
|
35
|
+
* Device to run on
|
|
36
|
+
* @default 'auto' - Prefers WebGPU, falls back to WASM
|
|
37
|
+
*/
|
|
38
|
+
device?: Device;
|
|
39
|
+
/**
|
|
40
|
+
* Quantization level (transformers.js only)
|
|
41
|
+
* @default 'q4'
|
|
42
|
+
*/
|
|
43
|
+
quantization?: Quantization;
|
|
44
|
+
/**
|
|
45
|
+
* System prompt to use for all conversations
|
|
46
|
+
*/
|
|
47
|
+
systemPrompt?: string;
|
|
48
|
+
/**
|
|
49
|
+
* Callback for model loading progress
|
|
50
|
+
*/
|
|
51
|
+
onLoadProgress?: LoadProgressCallback;
|
|
52
|
+
}
|
|
53
|
+
/**
|
|
54
|
+
* Role in a chat conversation
|
|
55
|
+
*/
|
|
56
|
+
export type MessageRole = 'system' | 'user' | 'assistant';
|
|
57
|
+
/**
|
|
58
|
+
* A single message in a chat conversation
|
|
59
|
+
*/
|
|
60
|
+
export interface ChatMessage {
|
|
61
|
+
role: MessageRole;
|
|
62
|
+
content: string;
|
|
63
|
+
}
|
|
64
|
+
/**
|
|
65
|
+
* Callback for streaming token output
|
|
66
|
+
*/
|
|
67
|
+
export type StreamCallback = (token: string, fullText: string) => void;
|
|
68
|
+
/**
|
|
69
|
+
* Loading progress information
|
|
70
|
+
*/
|
|
71
|
+
export interface LoadProgress {
|
|
72
|
+
/** Progress percentage (0-100) */
|
|
73
|
+
progress: number;
|
|
74
|
+
/** Current status text */
|
|
75
|
+
status: string;
|
|
76
|
+
/** Bytes loaded (if available) */
|
|
77
|
+
loaded?: number;
|
|
78
|
+
/** Total bytes (if available) */
|
|
79
|
+
total?: number;
|
|
80
|
+
}
|
|
81
|
+
/**
|
|
82
|
+
* Callback for model loading progress
|
|
83
|
+
*/
|
|
84
|
+
export type LoadProgressCallback = (progress: LoadProgress) => void;
|
|
85
|
+
/**
|
|
86
|
+
* Options for text generation
|
|
87
|
+
*/
|
|
88
|
+
export interface GenerateOptions {
|
|
89
|
+
/**
|
|
90
|
+
* Temperature for sampling (0-2)
|
|
91
|
+
* @default 0.7
|
|
92
|
+
*/
|
|
93
|
+
temperature?: number;
|
|
94
|
+
/**
|
|
95
|
+
* Maximum tokens to generate
|
|
96
|
+
* @default 512
|
|
97
|
+
*/
|
|
98
|
+
maxTokens?: number;
|
|
99
|
+
/**
|
|
100
|
+
* Top-p sampling
|
|
101
|
+
* @default 0.95
|
|
102
|
+
*/
|
|
103
|
+
topP?: number;
|
|
104
|
+
/**
|
|
105
|
+
* Stop sequences
|
|
106
|
+
*/
|
|
107
|
+
stopSequences?: string[];
|
|
108
|
+
}
|
|
109
|
+
/**
|
|
110
|
+
* Unified interface for LLM backends
|
|
111
|
+
*/
|
|
112
|
+
export interface LLMProvider {
|
|
113
|
+
/**
|
|
114
|
+
* Backend identifier
|
|
115
|
+
*/
|
|
116
|
+
readonly backend: Backend;
|
|
117
|
+
/**
|
|
118
|
+
* Whether the model is loaded and ready
|
|
119
|
+
*/
|
|
120
|
+
readonly isReady: boolean;
|
|
121
|
+
/**
|
|
122
|
+
* Current model ID
|
|
123
|
+
*/
|
|
124
|
+
readonly modelId: string | null;
|
|
125
|
+
/**
|
|
126
|
+
* Load a model
|
|
127
|
+
*/
|
|
128
|
+
load(modelId: string, onProgress?: LoadProgressCallback): Promise<void>;
|
|
129
|
+
/**
|
|
130
|
+
* Generate a response (non-streaming)
|
|
131
|
+
*/
|
|
132
|
+
chat(messages: ChatMessage[], options?: GenerateOptions): Promise<string>;
|
|
133
|
+
/**
|
|
134
|
+
* Generate a response with streaming
|
|
135
|
+
*/
|
|
136
|
+
stream(messages: ChatMessage[], onToken: StreamCallback, options?: GenerateOptions): Promise<string>;
|
|
137
|
+
/**
|
|
138
|
+
* Unload the model and free resources
|
|
139
|
+
*/
|
|
140
|
+
unload(): Promise<void>;
|
|
141
|
+
}
|
|
142
|
+
/**
|
|
143
|
+
* Options for attaching to an input element
|
|
144
|
+
*/
|
|
145
|
+
export interface AttachOptions {
|
|
146
|
+
/**
|
|
147
|
+
* Trigger generation on Enter key
|
|
148
|
+
* @default true
|
|
149
|
+
*/
|
|
150
|
+
triggerOnEnter?: boolean;
|
|
151
|
+
/**
|
|
152
|
+
* Clear input after sending
|
|
153
|
+
* @default true
|
|
154
|
+
*/
|
|
155
|
+
clearOnSend?: boolean;
|
|
156
|
+
/**
|
|
157
|
+
* Show loading indicator
|
|
158
|
+
* @default true
|
|
159
|
+
*/
|
|
160
|
+
showLoading?: boolean;
|
|
161
|
+
/**
|
|
162
|
+
* Custom loading text
|
|
163
|
+
* @default 'Thinking...'
|
|
164
|
+
*/
|
|
165
|
+
loadingText?: string;
|
|
166
|
+
}
|
|
167
|
+
/**
|
|
168
|
+
* Browser capability information
|
|
169
|
+
*/
|
|
170
|
+
export interface BrowserCapabilities {
|
|
171
|
+
/** WebGPU is available */
|
|
172
|
+
webgpu: boolean;
|
|
173
|
+
/** WebAssembly is available */
|
|
174
|
+
wasm: boolean;
|
|
175
|
+
/** Recommended backend based on capabilities */
|
|
176
|
+
recommendedBackend: Backend;
|
|
177
|
+
/** Recommended device based on capabilities */
|
|
178
|
+
recommendedDevice: Device;
|
|
179
|
+
}
|
|
180
|
+
//# sourceMappingURL=types.d.ts.map
|
|
@@ -0,0 +1 @@
|
|
|
1
|
+
{"version":3,"file":"types.d.ts","sourceRoot":"","sources":["../src/types.ts"],"names":[],"mappings":"AAAA;;;GAGG;AAMH;;GAEG;AACH,MAAM,MAAM,OAAO,GAAG,QAAQ,GAAG,cAAc,GAAG,MAAM,CAAC;AAEzD;;GAEG;AACH,MAAM,MAAM,MAAM,GAAG,QAAQ,GAAG,MAAM,GAAG,MAAM,CAAC;AAEhD;;GAEG;AACH,MAAM,MAAM,YAAY,GAAG,IAAI,GAAG,IAAI,GAAG,MAAM,GAAG,MAAM,CAAC;AAEzD,OAAO,KAAK,EAAE,cAAc,EAAE,MAAM,UAAU,CAAC;AAC/C,Y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