webslm-react 0.7.0 → 0.9.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/README.md CHANGED
@@ -1,6 +1,6 @@
1
1
  # webslm-react
2
2
 
3
- React hooks for [webslm](https://www.npmjs.com/package/webslm): run a small language model in the browser, ask it, chat with it and search the Hugging Face Hub, from components.
3
+ React hooks for [webslm](https://www.npmjs.com/package/webslm): run a small language model in the browser, ask it, chat with it, embed texts with an encoder and search the Hugging Face Hub, from components.
4
4
 
5
5
  ```sh
6
6
  npm install webslm webslm-react
@@ -94,6 +94,7 @@ export function Chat() {
94
94
  - `useLLM(model, options)` returns `{ llm, ask, stream, chat, status, progress, error }` (see above).
95
95
  - `useModel(source, options)` returns `{ model, status, progress, error }` with `status` one of `"idle" | "loading" | "ready" | "error"`. It loads when the source (a hub repo id or a `ModelSource`) or a load option changes, and unloads the previous model and on unmount. Pass `null` to load nothing.
96
96
  - `useChat(model, options)` returns `{ messages, send, stop, clear, setMessages, isGenerating, streamingText, lastResult, error }`. `send(text, media?)` sends images and videos (`[{ type: "image", image: file }]`) to vision-language models; each message's `content` is its text and `media` its images and videos. Options are webslm's generation options plus `system`, `initialMessages`, `onFinish` and `onError`.
97
+ - `useEmbeddings(source, options)` loads an encoder (an embedding model: `sentence-transformers/all-MiniLM-L6-v2`, `minishlab/potion-base-2M`...) and returns `{ model, embed, status, progress, error }`; `embed(texts, { pooling, normalize })` is the model's `embed`, callable while it loads (it waits): `const { vectors } = await embed(["A cat sleeps.", "A kitten naps."])`.
97
98
  - `useModelSearch(query, options)` returns `{ results, status, error }`, searching the Hub as the query changes (debounced, stale requests cancelled). Each result says whether webslm can run it, whether it is gated, and its size.
98
99
  - `useCapabilities()` reports WebGPU, SIMD, threads and cross-origin isolation.
99
100
 
package/dist/index.d.ts CHANGED
@@ -1,6 +1,6 @@
1
1
  /**
2
- * React bindings for webslm: load a model, ask it, chat with it and search
3
- * the Hugging Face Hub from components.
2
+ * React bindings for webslm: load a model, ask it, chat with it, embed texts
3
+ * with an encoder and search the Hugging Face Hub from components.
4
4
  *
5
5
  * ```tsx
6
6
  * const { ask, status } = useLLM();
@@ -9,8 +9,9 @@
9
9
  * @packageDocumentation
10
10
  */
11
11
  export { useCapabilities } from "./use-capabilities.js";
12
+ export { useEmbeddings, type UseEmbeddingsResult } from "./use-embeddings.js";
12
13
  export { useChat, type UseChatOptions, type UseChatResult } from "./use-chat.js";
13
14
  export { useLLM, type UseLLMAsk, type UseLLMResult } from "./use-llm.js";
14
15
  export { useModel, type ModelStatus, type UseModelOptions, type UseModelResult, } from "./use-model.js";
15
16
  export { useModelSearch, type UseModelSearchOptions, type UseModelSearchResult, } from "./use-model-search.js";
16
- export type { Answer, AskOptions, Capabilities, Chat, ChatOptions, ChatMessage, MediaPart, GenerationResult, HubModel, LLM, LoadProgress, Model, ModelInfo, ModelSource, PromptOptions, TextStream, WebslmError, } from "webslm";
17
+ export type { Answer, AskOptions, Capabilities, Chat, ChatOptions, ChatMessage, MediaPart, EmbedOptions, Embeddings, GenerationResult, HubModel, LLM, LoadProgress, Model, ModelInfo, ModelSource, PromptOptions, TextStream, WebslmError, } from "webslm";
package/dist/index.js CHANGED
@@ -1,6 +1,6 @@
1
1
  /**
2
- * React bindings for webslm: load a model, ask it, chat with it and search
3
- * the Hugging Face Hub from components.
2
+ * React bindings for webslm: load a model, ask it, chat with it, embed texts
3
+ * with an encoder and search the Hugging Face Hub from components.
4
4
  *
5
5
  * ```tsx
6
6
  * const { ask, status } = useLLM();
@@ -9,6 +9,7 @@
9
9
  * @packageDocumentation
10
10
  */
11
11
  export { useCapabilities } from "./use-capabilities.js";
12
+ export { useEmbeddings } from "./use-embeddings.js";
12
13
  export { useChat } from "./use-chat.js";
13
14
  export { useLLM } from "./use-llm.js";
14
15
  export { useModel, } from "./use-model.js";
@@ -0,0 +1,24 @@
1
+ import { WebslmError, type EmbedOptions, type Embeddings, type LoadProgress, type Model, type ModelSource } from "webslm";
2
+ import { type ModelStatus, type UseModelOptions } from "./use-load.js";
3
+ export interface UseEmbeddingsResult {
4
+ /** The loaded encoder, once `status` is `"ready"`. */
5
+ readonly model: Model | undefined;
6
+ readonly status: ModelStatus;
7
+ /** Latest progress event of the current load. */
8
+ readonly progress: LoadProgress | undefined;
9
+ readonly error: WebslmError | undefined;
10
+ /** Embeds texts (see `Model.embed`). Callable during the load: it waits for the model. */
11
+ readonly embed: (texts: string | readonly string[], options?: EmbedOptions) => Promise<Embeddings>;
12
+ }
13
+ /**
14
+ * Loads an encoder (an embedding model: BERT, ModernBERT, model2vec) and
15
+ * gives an `embed` function that can be called right away: it waits for the
16
+ * load. Pass `null` to load nothing. The model is unloaded when the source
17
+ * or a load option changes and on unmount.
18
+ *
19
+ * ```tsx
20
+ * const { embed, status } = useEmbeddings("sentence-transformers/all-MiniLM-L6-v2");
21
+ * const { vectors } = await embed(["A cat sleeps.", "A kitten naps."]);
22
+ * ```
23
+ */
24
+ export declare function useEmbeddings(source: string | ModelSource | null | undefined, options?: UseModelOptions): UseEmbeddingsResult;
@@ -0,0 +1,25 @@
1
+ import { useCallback } from "react";
2
+ import { loadModel, WebslmError, } from "webslm";
3
+ import { useLoad } from "./use-load.js";
4
+ /**
5
+ * Loads an encoder (an embedding model: BERT, ModernBERT, model2vec) and
6
+ * gives an `embed` function that can be called right away: it waits for the
7
+ * load. Pass `null` to load nothing. The model is unloaded when the source
8
+ * or a load option changes and on unmount.
9
+ *
10
+ * ```tsx
11
+ * const { embed, status } = useEmbeddings("sentence-transformers/all-MiniLM-L6-v2");
12
+ * const { vectors } = await embed(["A cat sleeps.", "A kitten naps."]);
13
+ * ```
14
+ */
15
+ export function useEmbeddings(source, options = {}) {
16
+ const { value, status, progress, error, pending } = useLoad(source, options, loadModel);
17
+ const embed = useCallback((texts, embedOptions) => {
18
+ const current = pending.current;
19
+ if (current === undefined) {
20
+ return Promise.reject(new WebslmError("INVALID_STATE", "no model is loaded or loading"));
21
+ }
22
+ return current.then((model) => model.embed(texts, embedOptions));
23
+ }, [pending]);
24
+ return { model: value, status, progress, error, embed };
25
+ }
package/package.json CHANGED
@@ -1,6 +1,6 @@
1
1
  {
2
2
  "name": "webslm-react",
3
- "version": "0.7.0",
3
+ "version": "0.9.0",
4
4
  "description": "React hooks for webslm: load small language models in the browser, chat with them and search the Hugging Face Hub.",
5
5
  "license": "MIT",
6
6
  "author": "Maxime Golfier",
@@ -38,7 +38,7 @@
38
38
  ],
39
39
  "peerDependencies": {
40
40
  "react": ">=18",
41
- "webslm": "^0.7.0"
41
+ "webslm": "^0.9.0"
42
42
  },
43
43
  "devDependencies": {
44
44
  "@testing-library/dom": "^10.4.2",
@@ -51,7 +51,7 @@
51
51
  "react-dom": "^19.3.1",
52
52
  "typescript": "^7.0.2",
53
53
  "vitest": "^5.1.2",
54
- "webslm": "^0.7.0"
54
+ "webslm": "^0.9.0"
55
55
  },
56
56
  "scripts": {
57
57
  "build": "node -e \"require('fs').rmSync('dist',{recursive:true,force:true})\" && tsc -p tsconfig.build.json",