|
| 1 | +# @react-llm/headless |
| 2 | + |
| 3 | +Easy-to-use headless React Hooks to run LLMs in the browser with WebGPU. As simple as `useLLM()`. |
| 4 | + |
| 5 | +### [**Live Demo**](https://chat.matt-rickard.com) |
| 6 | + |
| 7 | + |
| 8 | + |
| 9 | +**Features**: |
| 10 | + |
| 11 | +* Supports [Vicuna 13B](https://lmsys.org/blog/2023-03-30-vicuna/) |
| 12 | +* Use custom system prompts and "user:"/"assistant:" role names |
| 13 | +* Completion options like `max tokens` and `stop sequences` |
| 14 | +* No data leaves the browser. Accelerated via WebGPU. |
| 15 | +* Hooks built to 'Bring your own UI' |
| 16 | +* Persistent storage for conversations in browser storage. Hooks for loading and saving conversations. |
| 17 | +* Model caching for faster subsequent loads |
| 18 | + |
| 19 | +## Installation |
| 20 | + |
| 21 | +```bash |
| 22 | +npm install @react-llm/headless |
| 23 | +``` |
| 24 | + |
| 25 | + |
| 26 | +## **useLLM** API |
| 27 | +### Types |
| 28 | +```typescript |
| 29 | +// Model Initialization |
| 30 | +init: () => void; |
| 31 | + |
| 32 | +// Model Generation |
| 33 | +send: (msg: string, maxTokens: number, stopSequences: string[]) => void; |
| 34 | +onMessage: (msg: GenerateTextResponse) => void; |
| 35 | +setOnMessage: (cb: (msg: GenerateTextResponse) => void) => void; |
| 36 | + |
| 37 | +// Model Status |
| 38 | +loadingStatus: InitProgressReport; |
| 39 | +isGenerating: boolean; |
| 40 | +gpuDevice: GPUDeviceInfo; |
| 41 | + |
| 42 | +// Model Configuration |
| 43 | +userRoleName: string; |
| 44 | +setUserRoleName: (roleName: string) => void; |
| 45 | +assistantRoleName: string; |
| 46 | +setAssistantRoleName: (roleName: string) => void; |
| 47 | + |
| 48 | +// Conversation Management |
| 49 | +conversation: Conversation | undefined; |
| 50 | +allConversations: Conversation[] | undefined; |
| 51 | +createConversation: (title?: string, prompt?: string) => void; |
| 52 | +setConversationId: (conversationId: string) => void; |
| 53 | +deleteConversation: (conversationId: string) => void; |
| 54 | +deleteAllConversations: () => void; |
| 55 | +deleteMessages: () => void; |
| 56 | +setConversationTitle: (conversationId: string, title: string) => void; |
| 57 | +``` |
| 58 | + |
| 59 | +### Hooks |
| 60 | +```typescript |
| 61 | +import useLLM from '@react-llm/headless'; |
| 62 | + |
| 63 | +const MyComponent = () => { |
| 64 | + const { |
| 65 | + conversation, |
| 66 | + allConversations, |
| 67 | + loadingStatus, |
| 68 | + isGenerating, |
| 69 | + createConversation, |
| 70 | + setConversationId, |
| 71 | + deleteConversation, |
| 72 | + deleteAllConversations, |
| 73 | + deleteMessages, |
| 74 | + setConversationTitle, |
| 75 | + onMessage, |
| 76 | + setOnMessage, |
| 77 | + userRoleName, |
| 78 | + setUserRoleName, |
| 79 | + assistantRoleName, |
| 80 | + setAssistantRoleName, |
| 81 | + gpuDevice, |
| 82 | + send, |
| 83 | + init, |
| 84 | + } = useLLM(); |
| 85 | + |
| 86 | + // Component logic... |
| 87 | + |
| 88 | + return null; |
| 89 | +}; |
| 90 | +``` |
| 91 | + |
| 92 | + |
| 93 | +### Packages |
| 94 | + |
| 95 | +* `@react-llm/headless` - Headless React Hooks for running LLMs in the browser |
| 96 | +* `@react-llm/retro-ui` - Retro-themed UI for the hooks |
| 97 | + |
| 98 | +## How does it work? |
| 99 | + |
| 100 | +This library is a set of React Hooks that provide a simple interface to run LLMs in the browser. It uses Vicuna 13B. |
| 101 | + |
| 102 | +* SentencePiece tokenizer (compiled for the browser via Emscripten) |
| 103 | +* Vicuna 13B (transformed to Apache TVM format) |
| 104 | +* Apache TVM and MLC Relax (compiled for the browser via Emscripten) |
| 105 | +* Off-the-main-thread WebWorker to run the model (bundled with the library) |
| 106 | + |
| 107 | + |
| 108 | +The model, tokenizer, and TVM runtime are loaded from a CDN (huggingface). The model is cached in browser storage for faster subsequent loads. |
| 109 | + |
| 110 | + |
| 111 | + |
| 112 | + |
| 113 | +### Example |
| 114 | +See [packages/retro-ui](packages/retro-ui) for the full demo code. This is a simple example of how to use the hooks. To run it, after cloning the repo, |
| 115 | + |
| 116 | +```bash |
| 117 | +cd packages/retro-ui |
| 118 | +pnpm install |
| 119 | +pnpm dev |
| 120 | +``` |
| 121 | + |
| 122 | + |
| 123 | +### License |
| 124 | +MIT |
| 125 | + |
| 126 | +The code under `packages/headless/worker/lib/tvm` is licensed under Apache 2.0. |
0 commit comments