genai-lite 0.4.2 → 0.5.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 +399 -22
- package/dist/adapters/image/ElectronDiffusionAdapter.d.ts +10 -0
- package/dist/adapters/image/ElectronDiffusionAdapter.js +11 -0
- package/dist/adapters/image/GenaiElectronImageAdapter.d.ts +69 -0
- package/dist/adapters/image/GenaiElectronImageAdapter.js +273 -0
- package/dist/adapters/image/MockImageAdapter.d.ts +23 -0
- package/dist/adapters/image/MockImageAdapter.js +55 -0
- package/dist/adapters/image/OpenAIImageAdapter.d.ts +62 -0
- package/dist/adapters/image/OpenAIImageAdapter.js +303 -0
- package/dist/config/image-presets.json +194 -0
- package/dist/config/llm-presets.json +326 -0
- package/dist/image/ImageService.d.ts +53 -0
- package/dist/image/ImageService.js +199 -0
- package/dist/image/config.d.ts +48 -0
- package/dist/image/config.js +221 -0
- package/dist/image/services/ImageAdapterRegistry.d.ts +61 -0
- package/dist/image/services/ImageAdapterRegistry.js +95 -0
- package/dist/image/services/ImageModelResolver.d.ts +26 -0
- package/dist/image/services/ImageModelResolver.js +98 -0
- package/dist/image/services/ImagePresetManager.d.ts +27 -0
- package/dist/image/services/ImagePresetManager.js +52 -0
- package/dist/image/services/ImageRequestValidator.d.ts +37 -0
- package/dist/image/services/ImageRequestValidator.js +133 -0
- package/dist/image/services/ImageSettingsResolver.d.ts +25 -0
- package/dist/image/services/ImageSettingsResolver.js +76 -0
- package/dist/index.d.ts +7 -4
- package/dist/index.js +7 -1
- package/dist/llm/LLMService.d.ts +3 -4
- package/dist/llm/LLMService.js +17 -7
- package/dist/llm/clients/AnthropicClientAdapter.js +2 -2
- package/dist/llm/clients/GeminiClientAdapter.js +2 -2
- package/dist/llm/clients/LlamaCppClientAdapter.d.ts +20 -2
- package/dist/llm/clients/LlamaCppClientAdapter.js +91 -11
- package/dist/llm/clients/LlamaCppServerClient.d.ts +36 -0
- package/dist/llm/clients/LlamaCppServerClient.js +25 -0
- package/dist/llm/clients/OpenAIClientAdapter.js +2 -2
- package/dist/llm/config.d.ts +43 -2
- package/dist/llm/config.js +171 -32
- package/dist/llm/services/ModelResolver.d.ts +8 -4
- package/dist/llm/services/ModelResolver.js +22 -5
- package/dist/providers/fromEnvironment.d.ts +5 -1
- package/dist/providers/fromEnvironment.js +29 -4
- package/dist/shared/adapters/errorUtils.d.ts +26 -0
- package/dist/shared/adapters/errorUtils.js +107 -0
- package/dist/shared/services/AdapterRegistry.d.ts +89 -0
- package/dist/shared/services/AdapterRegistry.js +144 -0
- package/dist/shared/services/PresetManager.d.ts +33 -0
- package/dist/shared/services/PresetManager.js +56 -0
- package/dist/types/image.d.ts +399 -0
- package/dist/types/image.js +8 -0
- package/dist/types.d.ts +6 -0
- package/package.json +1 -1
- package/src/config/presets.json +0 -327
package/README.md
CHANGED
|
@@ -1,11 +1,12 @@
|
|
|
1
1
|
# genai-lite
|
|
2
2
|
|
|
3
|
-
A lightweight, portable Node.js/TypeScript library providing a unified interface for interacting with multiple Generative AI providers—both cloud-based (OpenAI, Anthropic, Google Gemini, Mistral) and local (llama.cpp).
|
|
3
|
+
A lightweight, portable Node.js/TypeScript library providing a unified interface for interacting with multiple Generative AI providers—both cloud-based (OpenAI, Anthropic, Google Gemini, Mistral) and local (llama.cpp, stable-diffusion.cpp). Supports both LLM chat and AI image generation.
|
|
4
4
|
|
|
5
5
|
## Features
|
|
6
6
|
|
|
7
7
|
- 🔌 **Unified API** - Single interface for multiple AI providers
|
|
8
8
|
- 🏠 **Local & Cloud Models** - Run models locally with llama.cpp or use cloud APIs
|
|
9
|
+
- 🖼️ **Image Generation** - First-class support for AI image generation (OpenAI, local diffusion)
|
|
9
10
|
- 🔐 **Flexible API Key Management** - Bring your own key storage solution
|
|
10
11
|
- 📦 **Zero Electron Dependencies** - Works in any Node.js environment
|
|
11
12
|
- 🎯 **TypeScript First** - Full type safety and IntelliSense support
|
|
@@ -56,7 +57,7 @@ const llmService = new LLMService(async () => 'not-needed');
|
|
|
56
57
|
|
|
57
58
|
const response = await llmService.sendMessage({
|
|
58
59
|
providerId: 'llamacpp',
|
|
59
|
-
modelId: '
|
|
60
|
+
modelId: 'llamacpp', // Generic ID for whatever model is loaded
|
|
60
61
|
messages: [
|
|
61
62
|
{ role: 'system', content: 'You are a helpful assistant.' },
|
|
62
63
|
{ role: 'user', content: 'Explain quantum computing briefly.' }
|
|
@@ -70,15 +71,373 @@ if (response.object === 'chat.completion') {
|
|
|
70
71
|
|
|
71
72
|
See the [llama.cpp Integration](#llamacpp-integration) section for setup details.
|
|
72
73
|
|
|
73
|
-
##
|
|
74
|
+
## Image Generation
|
|
74
75
|
|
|
75
|
-
|
|
76
|
+
genai-lite provides first-class support for AI image generation alongside its LLM capabilities. Generate images using cloud providers (OpenAI) or run diffusion models locally via genai-electron.
|
|
77
|
+
|
|
78
|
+
### Quick Start - OpenAI Images
|
|
79
|
+
|
|
80
|
+
```typescript
|
|
81
|
+
import { ImageService, fromEnvironment } from 'genai-lite';
|
|
82
|
+
|
|
83
|
+
// Create service with API key provider
|
|
84
|
+
const imageService = new ImageService(fromEnvironment);
|
|
85
|
+
|
|
86
|
+
const result = await imageService.generateImage({
|
|
87
|
+
providerId: 'openai-images',
|
|
88
|
+
modelId: 'gpt-image-1-mini',
|
|
89
|
+
prompt: 'A serene mountain lake at sunrise, photorealistic',
|
|
90
|
+
settings: {
|
|
91
|
+
width: 1024,
|
|
92
|
+
height: 1024,
|
|
93
|
+
quality: 'high'
|
|
94
|
+
}
|
|
95
|
+
});
|
|
96
|
+
|
|
97
|
+
if (result.object === 'image.result') {
|
|
98
|
+
// Save the generated image
|
|
99
|
+
const fs = require('fs');
|
|
100
|
+
fs.writeFileSync('output.png', result.data[0].data);
|
|
101
|
+
console.log('Image generated successfully!');
|
|
102
|
+
} else {
|
|
103
|
+
console.error('Error:', result.error.message);
|
|
104
|
+
}
|
|
105
|
+
```
|
|
106
|
+
|
|
107
|
+
### Quick Start - Local Diffusion
|
|
108
|
+
|
|
109
|
+
```typescript
|
|
110
|
+
import { ImageService } from 'genai-lite';
|
|
111
|
+
|
|
112
|
+
// Start genai-electron diffusion server first on port 8081
|
|
113
|
+
const imageService = new ImageService(async () => 'not-needed');
|
|
114
|
+
|
|
115
|
+
const result = await imageService.generateImage({
|
|
116
|
+
providerId: 'genai-electron-images',
|
|
117
|
+
modelId: 'stable-diffusion',
|
|
118
|
+
prompt: 'A majestic dragon soaring through clouds, highly detailed',
|
|
119
|
+
settings: {
|
|
120
|
+
width: 1024,
|
|
121
|
+
height: 1024,
|
|
122
|
+
diffusion: {
|
|
123
|
+
negativePrompt: 'blurry, low quality, distorted',
|
|
124
|
+
steps: 30,
|
|
125
|
+
cfgScale: 7.5,
|
|
126
|
+
sampler: 'dpm++2m',
|
|
127
|
+
seed: 42 // Optional: for reproducible results
|
|
128
|
+
}
|
|
129
|
+
}
|
|
130
|
+
});
|
|
131
|
+
|
|
132
|
+
if (result.object === 'image.result') {
|
|
133
|
+
console.log('Generated image with seed:', result.data[0].seed);
|
|
134
|
+
// Save the image
|
|
135
|
+
require('fs').writeFileSync('dragon.png', result.data[0].data);
|
|
136
|
+
}
|
|
137
|
+
```
|
|
138
|
+
|
|
139
|
+
### Supported Image Providers
|
|
140
|
+
|
|
141
|
+
#### OpenAI Images
|
|
142
|
+
|
|
143
|
+
**Models:**
|
|
144
|
+
- `gpt-image-1` - Latest OpenAI image model with advanced features (32K character prompts)
|
|
145
|
+
- `gpt-image-1-mini` - Fast and efficient, default model (32K character prompts)
|
|
146
|
+
- `dall-e-3` - High-quality image generation (4K character prompts)
|
|
147
|
+
- `dall-e-2` - Cost-effective generation (1K character prompts)
|
|
148
|
+
|
|
149
|
+
**Configuration:**
|
|
150
|
+
```bash
|
|
151
|
+
export OPENAI_API_KEY=sk-...
|
|
152
|
+
# Optional: override base URL
|
|
153
|
+
export OPENAI_API_BASE_URL=https://api.openai.com/v1
|
|
154
|
+
```
|
|
155
|
+
|
|
156
|
+
**Capabilities:**
|
|
157
|
+
- Multiple images per request (n parameter, except dall-e-3 which only supports n=1)
|
|
158
|
+
- Quality settings: `auto`, `high`, `medium`, `low`, `hd`, `standard`
|
|
159
|
+
- Style options: `vivid` (hyper-real), `natural` (photographic)
|
|
160
|
+
- Multiple formats: PNG, JPEG, WebP
|
|
161
|
+
- Hosted URLs or base64 response
|
|
162
|
+
|
|
163
|
+
#### Local Diffusion (genai-electron)
|
|
164
|
+
|
|
165
|
+
**Models:**
|
|
166
|
+
- `stable-diffusion` - Generic model ID for whatever model is loaded in genai-electron
|
|
167
|
+
|
|
168
|
+
**Configuration:**
|
|
169
|
+
```bash
|
|
170
|
+
# Optional: override base URL (default: http://localhost:8081)
|
|
171
|
+
export GENAI_ELECTRON_IMAGE_BASE_URL=http://localhost:8081
|
|
172
|
+
```
|
|
173
|
+
|
|
174
|
+
**Capabilities:**
|
|
175
|
+
- Negative prompts for better control
|
|
176
|
+
- Multiple samplers: `euler_a`, `euler`, `heun`, `dpm2`, `dpm++2s_a`, `dpm++2m`, `dpm++2mv2`, `lcm`
|
|
177
|
+
- Adjustable steps (1-150), CFG scale (1.0-30.0)
|
|
178
|
+
- Custom seeds for reproducibility
|
|
179
|
+
- Real-time progress callbacks
|
|
180
|
+
- Batch generation support
|
|
181
|
+
|
|
182
|
+
### Advanced Usage
|
|
183
|
+
|
|
184
|
+
#### Using Image Presets
|
|
185
|
+
|
|
186
|
+
genai-lite includes 12 built-in presets for common use cases:
|
|
187
|
+
|
|
188
|
+
```typescript
|
|
189
|
+
const imageService = new ImageService(fromEnvironment);
|
|
190
|
+
|
|
191
|
+
// List available presets
|
|
192
|
+
const presets = imageService.getPresets();
|
|
193
|
+
console.log(presets.map(p => p.id));
|
|
194
|
+
|
|
195
|
+
// Use a preset
|
|
196
|
+
const result = await imageService.generateImage({
|
|
197
|
+
presetId: 'openai-dalle-3-hd',
|
|
198
|
+
prompt: 'A futuristic city at night'
|
|
199
|
+
});
|
|
200
|
+
|
|
201
|
+
// Override preset settings
|
|
202
|
+
const result = await imageService.generateImage({
|
|
203
|
+
presetId: 'genai-electron-sdxl-quality',
|
|
204
|
+
prompt: 'A portrait of a wise old wizard',
|
|
205
|
+
settings: {
|
|
206
|
+
width: 768, // Override preset's 1024x1024
|
|
207
|
+
height: 1024,
|
|
208
|
+
diffusion: {
|
|
209
|
+
steps: 40 // Override preset's 30 steps
|
|
210
|
+
}
|
|
211
|
+
}
|
|
212
|
+
});
|
|
213
|
+
```
|
|
214
|
+
|
|
215
|
+
**Available Presets:**
|
|
216
|
+
- **OpenAI:** `openai-gpt-image-1-mini-default`, `openai-gpt-image-1-quality`, `openai-dalle-3-hd`, `openai-dalle-3-natural`, `openai-dalle-2-default`, `openai-dalle-2-fast`
|
|
217
|
+
- **Local Diffusion:** `genai-electron-sdxl-quality`, `genai-electron-sdxl-balanced`, `genai-electron-sdxl-fast`, `genai-electron-sdxl-portrait`, `genai-electron-sdxl-turbo`, `genai-electron-sdxl-lightning`
|
|
218
|
+
|
|
219
|
+
#### Progress Callbacks (Local Diffusion)
|
|
220
|
+
|
|
221
|
+
Monitor generation progress in real-time with local diffusion models:
|
|
222
|
+
|
|
223
|
+
```typescript
|
|
224
|
+
const result = await imageService.generateImage({
|
|
225
|
+
providerId: 'genai-electron-images',
|
|
226
|
+
modelId: 'stable-diffusion',
|
|
227
|
+
prompt: 'A detailed landscape painting',
|
|
228
|
+
settings: {
|
|
229
|
+
width: 1024,
|
|
230
|
+
height: 1024,
|
|
231
|
+
diffusion: {
|
|
232
|
+
steps: 30,
|
|
233
|
+
onProgress: (progress) => {
|
|
234
|
+
console.log(`Stage: ${progress.stage}`);
|
|
235
|
+
console.log(`Step ${progress.currentStep}/${progress.totalSteps}`);
|
|
236
|
+
console.log(`Progress: ${progress.percentage?.toFixed(1)}%`);
|
|
237
|
+
|
|
238
|
+
// Update your UI progress bar
|
|
239
|
+
// updateProgressBar(progress.percentage);
|
|
240
|
+
}
|
|
241
|
+
}
|
|
242
|
+
}
|
|
243
|
+
});
|
|
244
|
+
```
|
|
245
|
+
|
|
246
|
+
**Progress stages:**
|
|
247
|
+
- `loading` - Model is being loaded
|
|
248
|
+
- `diffusion` - Actively generating the image
|
|
249
|
+
- `decoding` - Converting latents to final image
|
|
250
|
+
|
|
251
|
+
#### Generating Multiple Images
|
|
252
|
+
|
|
253
|
+
Request multiple variations in a single call:
|
|
254
|
+
|
|
255
|
+
```typescript
|
|
256
|
+
// OpenAI - multiple images
|
|
257
|
+
const result = await imageService.generateImage({
|
|
258
|
+
providerId: 'openai-images',
|
|
259
|
+
modelId: 'gpt-image-1-mini',
|
|
260
|
+
prompt: 'A cute robot assistant',
|
|
261
|
+
count: 4, // Generate 4 variations
|
|
262
|
+
settings: {
|
|
263
|
+
width: 512,
|
|
264
|
+
height: 512
|
|
265
|
+
}
|
|
266
|
+
});
|
|
267
|
+
|
|
268
|
+
// genai-electron - batch generation
|
|
269
|
+
const result = await imageService.generateImage({
|
|
270
|
+
providerId: 'genai-electron-images',
|
|
271
|
+
modelId: 'stable-diffusion',
|
|
272
|
+
prompt: 'Fantasy character concept art',
|
|
273
|
+
count: 3,
|
|
274
|
+
settings: {
|
|
275
|
+
diffusion: {
|
|
276
|
+
steps: 20,
|
|
277
|
+
// Each image gets a different seed automatically
|
|
278
|
+
}
|
|
279
|
+
}
|
|
280
|
+
});
|
|
281
|
+
|
|
282
|
+
// Process all generated images
|
|
283
|
+
result.data.forEach((image, index) => {
|
|
284
|
+
require('fs').writeFileSync(`output-${index}.png`, image.data);
|
|
285
|
+
console.log(`Image ${index} seed:`, image.seed);
|
|
286
|
+
});
|
|
287
|
+
```
|
|
288
|
+
|
|
289
|
+
#### Provider-Specific Settings
|
|
290
|
+
|
|
291
|
+
**OpenAI-specific settings:**
|
|
292
|
+
|
|
293
|
+
```typescript
|
|
294
|
+
const result = await imageService.generateImage({
|
|
295
|
+
providerId: 'openai-images',
|
|
296
|
+
modelId: 'gpt-image-1',
|
|
297
|
+
prompt: 'A professional product photo',
|
|
298
|
+
settings: {
|
|
299
|
+
width: 1024,
|
|
300
|
+
height: 1024,
|
|
301
|
+
quality: 'high',
|
|
302
|
+
style: 'natural',
|
|
303
|
+
openai: {
|
|
304
|
+
outputFormat: 'png', // 'png', 'jpeg', 'webp'
|
|
305
|
+
background: 'transparent', // 'auto', 'transparent', 'white', 'black'
|
|
306
|
+
moderation: 'auto', // 'auto', 'high', 'low'
|
|
307
|
+
compression: 0.8 // 0.0-1.0 for JPEG/WebP
|
|
308
|
+
}
|
|
309
|
+
}
|
|
310
|
+
});
|
|
311
|
+
```
|
|
312
|
+
|
|
313
|
+
**Diffusion-specific settings:**
|
|
314
|
+
|
|
315
|
+
```typescript
|
|
316
|
+
const result = await imageService.generateImage({
|
|
317
|
+
providerId: 'genai-electron-images',
|
|
318
|
+
modelId: 'stable-diffusion',
|
|
319
|
+
prompt: 'A mystical forest with glowing mushrooms',
|
|
320
|
+
settings: {
|
|
321
|
+
width: 1024,
|
|
322
|
+
height: 1024,
|
|
323
|
+
diffusion: {
|
|
324
|
+
negativePrompt: 'ugly, blurry, low quality, oversaturated',
|
|
325
|
+
steps: 30, // More steps = higher quality (1-150)
|
|
326
|
+
cfgScale: 7.5, // Prompt adherence (1.0-30.0)
|
|
327
|
+
sampler: 'dpm++2m', // Sampling algorithm
|
|
328
|
+
seed: 12345 // For reproducibility
|
|
329
|
+
}
|
|
330
|
+
}
|
|
331
|
+
});
|
|
332
|
+
```
|
|
333
|
+
|
|
334
|
+
**Available samplers:**
|
|
335
|
+
- `euler_a` - Fast, good for most use cases
|
|
336
|
+
- `euler` - Similar to euler_a but deterministic
|
|
337
|
+
- `dpm++2m` - High quality, recommended for final images
|
|
338
|
+
- `dpm++2s_a` - Good balance of speed and quality
|
|
339
|
+
- `heun` - High quality but slower
|
|
340
|
+
- `lcm` - Extremely fast (for LCM models)
|
|
341
|
+
|
|
342
|
+
#### Error Handling
|
|
343
|
+
|
|
344
|
+
```typescript
|
|
345
|
+
const result = await imageService.generateImage({
|
|
346
|
+
providerId: 'openai-images',
|
|
347
|
+
modelId: 'gpt-image-1-mini',
|
|
348
|
+
prompt: 'A beautiful sunset'
|
|
349
|
+
});
|
|
350
|
+
|
|
351
|
+
if (result.object === 'error') {
|
|
352
|
+
switch (result.error.type) {
|
|
353
|
+
case 'authentication_error':
|
|
354
|
+
console.error('Invalid API key');
|
|
355
|
+
break;
|
|
356
|
+
case 'rate_limit_error':
|
|
357
|
+
console.error('Rate limit exceeded');
|
|
358
|
+
break;
|
|
359
|
+
case 'validation_error':
|
|
360
|
+
console.error('Invalid request:', result.error.message);
|
|
361
|
+
break;
|
|
362
|
+
case 'network_error':
|
|
363
|
+
console.error('Server not reachable:', result.error.message);
|
|
364
|
+
break;
|
|
365
|
+
default:
|
|
366
|
+
console.error('Error:', result.error.message);
|
|
367
|
+
}
|
|
368
|
+
}
|
|
369
|
+
```
|
|
370
|
+
|
|
371
|
+
### Custom Presets
|
|
372
|
+
|
|
373
|
+
Extend or replace default presets:
|
|
374
|
+
|
|
375
|
+
```typescript
|
|
376
|
+
import { ImageService, fromEnvironment, ImagePreset } from 'genai-lite';
|
|
377
|
+
|
|
378
|
+
const customPresets: ImagePreset[] = [
|
|
379
|
+
{
|
|
380
|
+
id: 'my-portrait-preset',
|
|
381
|
+
displayName: 'Custom Portrait Generator',
|
|
382
|
+
description: 'Optimized for portrait photography',
|
|
383
|
+
providerId: 'genai-electron-images',
|
|
384
|
+
modelId: 'stable-diffusion',
|
|
385
|
+
settings: {
|
|
386
|
+
width: 768,
|
|
387
|
+
height: 1024,
|
|
388
|
+
diffusion: {
|
|
389
|
+
steps: 35,
|
|
390
|
+
cfgScale: 8.0,
|
|
391
|
+
sampler: 'dpm++2m',
|
|
392
|
+
negativePrompt: 'deformed, ugly, bad anatomy'
|
|
393
|
+
}
|
|
394
|
+
}
|
|
395
|
+
}
|
|
396
|
+
];
|
|
397
|
+
|
|
398
|
+
const imageService = new ImageService(fromEnvironment, {
|
|
399
|
+
presets: customPresets,
|
|
400
|
+
presetMode: 'extend' // 'extend' adds to defaults, 'replace' uses only custom
|
|
401
|
+
});
|
|
402
|
+
```
|
|
403
|
+
|
|
404
|
+
### TypeScript Types
|
|
405
|
+
|
|
406
|
+
```typescript
|
|
407
|
+
import type {
|
|
408
|
+
ImageService,
|
|
409
|
+
ImageGenerationRequest,
|
|
410
|
+
ImageGenerationResponse,
|
|
411
|
+
ImageFailureResponse,
|
|
412
|
+
ImageGenerationSettings,
|
|
413
|
+
DiffusionSettings,
|
|
414
|
+
OpenAISpecificSettings,
|
|
415
|
+
ImagePreset,
|
|
416
|
+
ImageProviderInfo,
|
|
417
|
+
ImageModelInfo,
|
|
418
|
+
GeneratedImage,
|
|
419
|
+
ImageProgressCallback
|
|
420
|
+
} from 'genai-lite';
|
|
421
|
+
```
|
|
422
|
+
|
|
423
|
+
## Example Applications
|
|
424
|
+
|
|
425
|
+
### LLM Chat Demo
|
|
426
|
+
For a complete, production-ready example showcasing genai-lite's LLM capabilities, see the **[chat-demo](examples/chat-demo)** interactive web application. Features include:
|
|
76
427
|
- Multi-provider chat interface with all supported providers
|
|
77
428
|
- Template rendering and model presets
|
|
78
429
|
- llama.cpp utilities (tokenization, embeddings, health checks)
|
|
79
430
|
- Settings persistence, export/import features
|
|
80
431
|
|
|
81
|
-
|
|
432
|
+
### Image Generation Demo
|
|
433
|
+
For image generation capabilities, see the **[image-gen-demo](examples/image-gen-demo)** interactive web application. Features include:
|
|
434
|
+
- Multi-provider image generation (OpenAI Images, local diffusion)
|
|
435
|
+
- Size presets and batch generation
|
|
436
|
+
- Full-screen image lightbox with keyboard navigation
|
|
437
|
+
- Progress monitoring for diffusion models
|
|
438
|
+
- Comprehensive settings for all DALL-E models
|
|
439
|
+
|
|
440
|
+
Both demos serve as comprehensive showcases and quick-test environments for library changes.
|
|
82
441
|
|
|
83
442
|
## API Key Management
|
|
84
443
|
|
|
@@ -149,13 +508,12 @@ const llmService = new LLMService(myKeyProvider);
|
|
|
149
508
|
|
|
150
509
|
### llama.cpp (Local Models)
|
|
151
510
|
|
|
152
|
-
Run models locally via [llama.cpp](https://github.com/ggml-org/llama.cpp) server.
|
|
511
|
+
Run models locally via [llama.cpp](https://github.com/ggml-org/llama.cpp) server. Use the generic `'llamacpp'` model ID—the actual model is determined by what you loaded in the llama.cpp server.
|
|
153
512
|
|
|
154
|
-
**
|
|
155
|
-
|
|
156
|
-
|
|
157
|
-
- `
|
|
158
|
-
- `my-custom-model` - Any custom model you've loaded
|
|
513
|
+
**Automatic Capability Detection:** genai-lite automatically detects capabilities (reasoning support, context windows, token limits) for known open-weights models (Qwen3, etc.) by matching the GGUF filename from the server. No configuration needed.
|
|
514
|
+
|
|
515
|
+
**Model ID:**
|
|
516
|
+
- `llamacpp` - Generic ID for whatever model the llama.cpp server has loaded
|
|
159
517
|
|
|
160
518
|
**Setup:**
|
|
161
519
|
|
|
@@ -173,7 +531,7 @@ const service = new LLMService(async () => 'not-needed');
|
|
|
173
531
|
|
|
174
532
|
const response = await service.sendMessage({
|
|
175
533
|
providerId: 'llamacpp',
|
|
176
|
-
modelId: '
|
|
534
|
+
modelId: 'llamacpp', // Generic ID for loaded model
|
|
177
535
|
messages: [{ role: 'user', content: 'Hello!' }]
|
|
178
536
|
});
|
|
179
537
|
```
|
|
@@ -212,6 +570,7 @@ Some models include advanced reasoning/thinking capabilities that enhance their
|
|
|
212
570
|
- **Anthropic**: Claude Sonnet 4, Claude Opus 4, Claude 3.7 Sonnet
|
|
213
571
|
- **Google Gemini**: Gemini 2.5 Pro (always on), Gemini 2.5 Flash, Gemini 2.5 Flash-Lite Preview
|
|
214
572
|
- **OpenAI**: o4-mini (always on)
|
|
573
|
+
- **llama.cpp**: Qwen3, DeepSeek-R1, GPT-OSS (requires `--reasoning-format deepseek` server flag)
|
|
215
574
|
|
|
216
575
|
See the [Reasoning Mode](#reasoning-mode) section for usage details.
|
|
217
576
|
|
|
@@ -794,12 +1153,20 @@ Get GGUF models from Hugging Face, for example:
|
|
|
794
1153
|
# Basic usage
|
|
795
1154
|
llama-server -m /path/to/model.gguf --port 8080
|
|
796
1155
|
|
|
797
|
-
# With
|
|
1156
|
+
# With reasoning support (for Qwen3, DeepSeek-R1, etc.)
|
|
798
1157
|
llama-server -m /path/to/model.gguf \
|
|
799
1158
|
--port 8080 \
|
|
800
|
-
|
|
801
|
-
-
|
|
802
|
-
|
|
1159
|
+
--jinja \
|
|
1160
|
+
--reasoning-format deepseek
|
|
1161
|
+
|
|
1162
|
+
# Full options
|
|
1163
|
+
llama-server -m /path/to/model.gguf \
|
|
1164
|
+
--port 8080 \
|
|
1165
|
+
--jinja \ # Required for reasoning
|
|
1166
|
+
--reasoning-format deepseek \ # Extract reasoning from <think> tags
|
|
1167
|
+
-c 4096 \ # Context size
|
|
1168
|
+
-np 4 \ # Parallel requests
|
|
1169
|
+
--threads 8 # CPU threads
|
|
803
1170
|
```
|
|
804
1171
|
|
|
805
1172
|
### Basic Usage
|
|
@@ -812,7 +1179,7 @@ const service = new LLMService(async () => 'not-needed');
|
|
|
812
1179
|
|
|
813
1180
|
const response = await service.sendMessage({
|
|
814
1181
|
providerId: 'llamacpp',
|
|
815
|
-
modelId: '
|
|
1182
|
+
modelId: 'llamacpp', // Generic ID for loaded model
|
|
816
1183
|
messages: [
|
|
817
1184
|
{ role: 'system', content: 'You are a helpful assistant.' },
|
|
818
1185
|
{ role: 'user', content: 'Explain quantum computing in simple terms.' }
|
|
@@ -861,7 +1228,7 @@ service.registerAdapter(
|
|
|
861
1228
|
// Use them
|
|
862
1229
|
const response = await service.sendMessage({
|
|
863
1230
|
providerId: 'llamacpp-small',
|
|
864
|
-
modelId: '
|
|
1231
|
+
modelId: 'llamacpp',
|
|
865
1232
|
messages: [{ role: 'user', content: 'Hello!' }]
|
|
866
1233
|
});
|
|
867
1234
|
```
|
|
@@ -982,7 +1349,7 @@ if (response.object === 'error') {
|
|
|
982
1349
|
|
|
983
1350
|
### Best Practices
|
|
984
1351
|
|
|
985
|
-
1. **Model
|
|
1352
|
+
1. **Model ID**: Always use `'llamacpp'` as the model ID—the actual model is determined by what you loaded in the server
|
|
986
1353
|
2. **Context Size**: Set appropriate context (`-c` flag) when starting the server
|
|
987
1354
|
3. **Parallel Requests**: Configure slots (`-np`) based on your hardware
|
|
988
1355
|
4. **Health Monitoring**: Enable `checkHealth` for production to detect server issues early
|
|
@@ -1077,7 +1444,9 @@ import type {
|
|
|
1077
1444
|
import {
|
|
1078
1445
|
LlamaCppClientAdapter,
|
|
1079
1446
|
LlamaCppServerClient,
|
|
1080
|
-
createFallbackModelInfo
|
|
1447
|
+
createFallbackModelInfo,
|
|
1448
|
+
detectGgufCapabilities,
|
|
1449
|
+
KNOWN_GGUF_MODELS
|
|
1081
1450
|
} from 'genai-lite';
|
|
1082
1451
|
|
|
1083
1452
|
import type {
|
|
@@ -1090,7 +1459,10 @@ import type {
|
|
|
1090
1459
|
LlamaCppPropsResponse,
|
|
1091
1460
|
LlamaCppMetricsResponse,
|
|
1092
1461
|
LlamaCppSlot,
|
|
1093
|
-
LlamaCppSlotsResponse
|
|
1462
|
+
LlamaCppSlotsResponse,
|
|
1463
|
+
LlamaCppModel,
|
|
1464
|
+
LlamaCppModelsResponse,
|
|
1465
|
+
GgufModelPattern
|
|
1094
1466
|
} from 'genai-lite';
|
|
1095
1467
|
```
|
|
1096
1468
|
|
|
@@ -1474,7 +1846,12 @@ These utilities enable:
|
|
|
1474
1846
|
|
|
1475
1847
|
## Examples
|
|
1476
1848
|
|
|
1477
|
-
|
|
1849
|
+
genai-lite includes two complete demo applications:
|
|
1850
|
+
|
|
1851
|
+
- **[chat-demo](examples/chat-demo)** - Full-featured LLM chat interface with all providers, template rendering, and advanced features
|
|
1852
|
+
- **[image-gen-demo](examples/image-gen-demo)** - Interactive image generation UI with OpenAI and local diffusion support
|
|
1853
|
+
|
|
1854
|
+
Both are production-ready React + Express applications that demonstrate library features and serve as testing environments.
|
|
1478
1855
|
|
|
1479
1856
|
## Contributing
|
|
1480
1857
|
|
|
@@ -0,0 +1,10 @@
|
|
|
1
|
+
/**
|
|
2
|
+
* GenAI Electron Diffusion Adapter
|
|
3
|
+
*
|
|
4
|
+
* Adapter for local diffusion models via genai-electron's diffusion server.
|
|
5
|
+
* Supports stable-diffusion.cpp through HTTP wrapper.
|
|
6
|
+
*
|
|
7
|
+
* Default endpoint: http://localhost:8081
|
|
8
|
+
* Configure via: GENAI_ELECTRON_IMAGE_BASE_URL environment variable
|
|
9
|
+
*/
|
|
10
|
+
export {};
|
|
@@ -0,0 +1,11 @@
|
|
|
1
|
+
"use strict";
|
|
2
|
+
/**
|
|
3
|
+
* GenAI Electron Diffusion Adapter
|
|
4
|
+
*
|
|
5
|
+
* Adapter for local diffusion models via genai-electron's diffusion server.
|
|
6
|
+
* Supports stable-diffusion.cpp through HTTP wrapper.
|
|
7
|
+
*
|
|
8
|
+
* Default endpoint: http://localhost:8081
|
|
9
|
+
* Configure via: GENAI_ELECTRON_IMAGE_BASE_URL environment variable
|
|
10
|
+
*/
|
|
11
|
+
Object.defineProperty(exports, "__esModule", { value: true });
|
|
@@ -0,0 +1,69 @@
|
|
|
1
|
+
/**
|
|
2
|
+
* genai-electron Image Adapter
|
|
3
|
+
*
|
|
4
|
+
* Adapter for local diffusion models via genai-electron's image generation server.
|
|
5
|
+
* Supports stable-diffusion.cpp through HTTP wrapper with async polling for progress.
|
|
6
|
+
*
|
|
7
|
+
* Provider ID: 'genai-electron-images'
|
|
8
|
+
* Default endpoint: http://localhost:8081
|
|
9
|
+
* Configure via: GENAI_ELECTRON_IMAGE_BASE_URL environment variable
|
|
10
|
+
*
|
|
11
|
+
* This adapter uses genai-electron's async image generation API which:
|
|
12
|
+
* - Returns immediately with a generation ID
|
|
13
|
+
* - Allows polling for progress updates
|
|
14
|
+
* - Supports full diffusion settings (negative prompts, steps, samplers, etc.)
|
|
15
|
+
* - Handles batching via count parameter
|
|
16
|
+
*/
|
|
17
|
+
import type { ImageProviderAdapter, ImageGenerationRequest, ImageGenerationResponse, ImageProviderCapabilities, ResolvedImageGenerationSettings, ImageProviderAdapterConfig } from '../../types/image';
|
|
18
|
+
/**
|
|
19
|
+
* Adapter for genai-electron's local diffusion image generation
|
|
20
|
+
*/
|
|
21
|
+
export declare class GenaiElectronImageAdapter implements ImageProviderAdapter {
|
|
22
|
+
readonly id = "genai-electron-images";
|
|
23
|
+
readonly supports: ImageProviderCapabilities;
|
|
24
|
+
private baseURL;
|
|
25
|
+
private timeout;
|
|
26
|
+
private pollInterval;
|
|
27
|
+
constructor(config?: ImageProviderAdapterConfig);
|
|
28
|
+
/**
|
|
29
|
+
* Generates images using genai-electron's async API with progress polling
|
|
30
|
+
*/
|
|
31
|
+
generate(config: {
|
|
32
|
+
request: ImageGenerationRequest;
|
|
33
|
+
resolvedPrompt: string;
|
|
34
|
+
settings: ResolvedImageGenerationSettings;
|
|
35
|
+
apiKey: string | null;
|
|
36
|
+
}): Promise<ImageGenerationResponse>;
|
|
37
|
+
/**
|
|
38
|
+
* Builds the request payload for genai-electron
|
|
39
|
+
*/
|
|
40
|
+
private buildRequestPayload;
|
|
41
|
+
/**
|
|
42
|
+
* Starts generation and returns the generation ID
|
|
43
|
+
*/
|
|
44
|
+
private startGeneration;
|
|
45
|
+
/**
|
|
46
|
+
* Polls for generation completion with progress updates
|
|
47
|
+
*/
|
|
48
|
+
private pollForCompletion;
|
|
49
|
+
/**
|
|
50
|
+
* Converts genai-electron result to ImageGenerationResponse
|
|
51
|
+
*/
|
|
52
|
+
private convertToResponse;
|
|
53
|
+
/**
|
|
54
|
+
* Creates an HTTP error with context
|
|
55
|
+
*/
|
|
56
|
+
private createHttpError;
|
|
57
|
+
/**
|
|
58
|
+
* Creates a generation error from genai-electron error codes
|
|
59
|
+
*/
|
|
60
|
+
private createGenerationError;
|
|
61
|
+
/**
|
|
62
|
+
* Handles errors and converts to standard format
|
|
63
|
+
*/
|
|
64
|
+
private handleError;
|
|
65
|
+
/**
|
|
66
|
+
* Sleep helper for polling
|
|
67
|
+
*/
|
|
68
|
+
private sleep;
|
|
69
|
+
}
|