@blank-utils/llm 0.1.0 → 0.2.1

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.
@@ -0,0 +1,309 @@
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
+ export { LLMProvider, useLLM, useChat, useStream, useCompletion, LLMLoading, LLMReady, };
309
+ //# 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;AAEH,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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,CAAC;CACpB;AAED;;;;;;;;;GASG;AACH,iBAAS,UAAU,CAAC,EAAE,QAAQ,EAAE,SAAS,EAAE,EAAE,eAAe,kDAe3D;AAMD,MAAM,WAAW,aAAa;IAC5B,iCAAiC;IACjC,QAAQ,EAAE,KAAK,CAAC,SAAS,CAAC;IAE1B,oCAAoC;IACpC,QAAQ,CAAC,EAAE,KAAK,CAAC,SAAS,CAAC;CAC5B;AAED;;;;;;;;;GASG;AACH,iBAAS,QAAQ,CAAC,EAAE,QAAQ,EAAE,QAAe,EAAE,EAAE,aAAa,2CAQ7D;AAED,OAAO,EACL,WAAW,EACX,MAAM,EACN,OAAO,EACP,SAAS,EACT,aAAa,EACb,UAAU,EACV,QAAQ,GACT,CAAC"}
@@ -118,7 +118,7 @@ class WebLLMProvider {
118
118
  }
119
119
  async load(modelId, onProgress) {
120
120
  const resolvedModel = resolveModelId(modelId);
121
- const { CreateMLCEngine } = await import("../index-mv82xhnr.js");
121
+ const { CreateMLCEngine } = await import("../index-yyxm9rp0.js");
122
122
  const initProgressCallback = (report) => {
123
123
  if (onProgress) {
124
124
  const progress = {
@@ -308,7 +308,7 @@ class TransformersProvider {
308
308
  }
309
309
  async load(modelId, onProgress) {
310
310
  const resolvedModel = modelId in TRANSFORMERS_MODELS ? TRANSFORMERS_MODELS[modelId] : modelId;
311
- const { pipeline, env } = await import("../transformers.web-nb96jrhe.js");
311
+ const { pipeline, env } = await import("../transformers.web-1qr6h84s.js");
312
312
  env.allowLocalModels = false;
313
313
  env.useBrowserCache = true;
314
314
  let deviceOption = "wasm";
@@ -359,7 +359,7 @@ class TransformersProvider {
359
359
  throw new Error("Model not loaded. Call load() first.");
360
360
  }
361
361
  const prompt = formatPrompt(messages, this.currentModel);
362
- const { TextStreamer } = await import("../transformers.web-nb96jrhe.js");
362
+ const { TextStreamer } = await import("../transformers.web-1qr6h84s.js");
363
363
  let fullText = "";
364
364
  const streamer = new TextStreamer(this.pipeline.tokenizer, {
365
365
  skip_prompt: true,
@@ -521,7 +521,7 @@ function createLoadingIndicator(containerSelector) {
521
521
  element: indicator
522
522
  };
523
523
  }
524
- // src/index.ts
524
+ // src/core.ts
525
525
  function normalizeMessages(input, systemPrompt) {
526
526
  const messages = [];
527
527
  if (systemPrompt) {
@@ -599,7 +599,6 @@ async function isWebGPUSupported() {
599
599
  const caps = await detectCapabilities();
600
600
  return caps.webgpu;
601
601
  }
602
- var src_default = createLLM;
603
602
 
604
603
  // src/react/index.tsx
605
604
  import { jsxDEV, Fragment } from "react/jsx-dev-runtime";
@@ -949,6 +948,7 @@ export {
949
948
  LLMProvider,
950
949
  LLMLoading
951
950
  };
951
+ export { checkWebGPU, checkWasm, detectCapabilities, logCapabilities, DEFAULT_WEBLLM_MODEL, WEBLLM_MODELS, WebLLMProvider, createWebLLMProvider, DEFAULT_TRANSFORMERS_MODEL, TRANSFORMERS_MODELS, TransformersProvider, createTransformersProvider, createOutputStreamer, attachToElements, createChatUI, createLoadingIndicator, createLLM, isWebGPUSupported, LLMProvider, useLLM, useChat, useStream, useCompletion, LLMLoading, LLMReady };
952
952
 
953
- //# debugId=9760E9C5A8B8695164756E2164756E21
953
+ //# debugId=6F31D13141376EAA64756E2164756E21
954
954
  //# sourceMappingURL=index.js.map