@memberjunction/ai 2.42.1 → 2.44.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/package.json +2 -2
- package/readme.md +209 -7
package/package.json
CHANGED
|
@@ -1,6 +1,6 @@
|
|
|
1
1
|
{
|
|
2
2
|
"name": "@memberjunction/ai",
|
|
3
|
-
"version": "2.
|
|
3
|
+
"version": "2.44.0",
|
|
4
4
|
"description": "MemberJunction: AI Module - LLMs and other AI services wrapped and abstracted for use in MemberJunction",
|
|
5
5
|
"main": "dist/index.js",
|
|
6
6
|
"types": "dist/index.d.ts",
|
|
@@ -15,7 +15,7 @@
|
|
|
15
15
|
"author": "MemberJunction.com",
|
|
16
16
|
"license": "ISC",
|
|
17
17
|
"dependencies": {
|
|
18
|
-
"@memberjunction/global": "2.
|
|
18
|
+
"@memberjunction/global": "2.44.0",
|
|
19
19
|
"dotenv": "^16.4.1",
|
|
20
20
|
"rxjs": "^7.8.1",
|
|
21
21
|
"typeorm": "^0.3.20"
|
package/readme.md
CHANGED
|
@@ -2,6 +2,10 @@
|
|
|
2
2
|
|
|
3
3
|
The MemberJunction AI Core package provides a comprehensive abstraction layer for working with various AI models (LLMs, Video and Audio Generation, Text-To-Speech (TTS), embedding models, etc.) in a provider-agnostic way, allowing your application to easily switch between different AI providers without refactoring your code.
|
|
4
4
|
|
|
5
|
+
## Overview
|
|
6
|
+
|
|
7
|
+
This package serves as the foundation for all AI capabilities in the MemberJunction ecosystem. It defines abstract base classes and interfaces that are implemented by provider-specific packages, enabling seamless integration with various AI services while maintaining a consistent API.
|
|
8
|
+
|
|
5
9
|
## Standalone Usage
|
|
6
10
|
|
|
7
11
|
**IMPORTANT**: This package can be used completely independently from the rest of the MemberJunction framework:
|
|
@@ -17,12 +21,16 @@ The `@memberjunction/ai` package and all provider packages in `@memberjunction/a
|
|
|
17
21
|
|
|
18
22
|
- **Provider Abstraction**: Work with AI models without tightly coupling to specific vendor APIs
|
|
19
23
|
- **Runtime Optionality**: Switch between AI providers at runtime based on configuration
|
|
20
|
-
- **Base Classes**: Abstract base classes for different AI model types (LLMs, embedding models, etc.)
|
|
24
|
+
- **Base Classes**: Abstract base classes for different AI model types (LLMs, embedding models, audio, video, etc.)
|
|
21
25
|
- **Standard Interfaces**: Consistent interfaces for common AI operations like chat, summarization, and classification
|
|
22
26
|
- **Streaming Support**: Stream responses from supported LLM providers for real-time UIs
|
|
27
|
+
- **Parallel Processing**: Execute multiple chat completions in parallel with progress callbacks
|
|
28
|
+
- **Multi-modal Support**: Handle text, images, videos, audio, and files in chat messages
|
|
23
29
|
- **Type Definitions**: Comprehensive TypeScript type definitions for all AI operations
|
|
24
30
|
- **Error Handling**: Standardized error handling and reporting across all providers
|
|
25
31
|
- **Token Usage Tracking**: Consistent tracking of token usage across providers
|
|
32
|
+
- **Response Format Control**: Specify output formats (Text, Markdown, JSON, or provider-specific)
|
|
33
|
+
- **Additional Settings**: Provider-specific configuration through a flexible settings system
|
|
26
34
|
|
|
27
35
|
## Installation
|
|
28
36
|
|
|
@@ -124,12 +132,45 @@ console.log(result.data.choices[0].message.content);
|
|
|
124
132
|
|
|
125
133
|
### Base Models
|
|
126
134
|
|
|
127
|
-
|
|
128
|
-
|
|
129
|
-
-
|
|
130
|
-
-
|
|
131
|
-
-
|
|
132
|
-
|
|
135
|
+
#### BaseModel
|
|
136
|
+
The foundational abstract class for all AI models. Provides:
|
|
137
|
+
- Protected API key management
|
|
138
|
+
- Base parameter and result types
|
|
139
|
+
- Model usage tracking
|
|
140
|
+
|
|
141
|
+
#### BaseLLM
|
|
142
|
+
Abstract class for text generation models. Features:
|
|
143
|
+
- Standard and streaming chat completions
|
|
144
|
+
- Parallel chat completions with callbacks
|
|
145
|
+
- Text summarization
|
|
146
|
+
- Text classification
|
|
147
|
+
- Additional provider-specific settings management
|
|
148
|
+
- Response format control (Any, Text, Markdown, JSON, ModelSpecific)
|
|
149
|
+
- Support for reasoning budget tokens (for reasoning models)
|
|
150
|
+
|
|
151
|
+
#### BaseEmbeddings
|
|
152
|
+
Abstract class for text embedding models. Provides:
|
|
153
|
+
- Single text embedding generation
|
|
154
|
+
- Batch text embedding generation
|
|
155
|
+
- Model listing capabilities
|
|
156
|
+
- Additional settings management
|
|
157
|
+
|
|
158
|
+
#### BaseAudioGenerator
|
|
159
|
+
Abstract class for audio processing models. Supports:
|
|
160
|
+
- Text-to-speech generation
|
|
161
|
+
- Speech-to-text transcription
|
|
162
|
+
- Voice listing and management
|
|
163
|
+
- Model and pronunciation dictionary queries
|
|
164
|
+
- Configurable voice settings (stability, similarity, speed, etc.)
|
|
165
|
+
|
|
166
|
+
#### BaseVideoGenerator
|
|
167
|
+
Abstract class for video generation models. Enables:
|
|
168
|
+
- Avatar-based video creation
|
|
169
|
+
- Video translation capabilities
|
|
170
|
+
- Avatar management and listing
|
|
171
|
+
|
|
172
|
+
#### BaseDiffusion
|
|
173
|
+
Abstract class for image generation models (placeholder for future implementation)
|
|
133
174
|
|
|
134
175
|
## LLM Operations
|
|
135
176
|
|
|
@@ -321,5 +362,166 @@ This architecture allows for a clean separation between common streaming logic a
|
|
|
321
362
|
- `@memberjunction/global` - MemberJunction global utilities including class factory
|
|
322
363
|
- `rxjs` - Reactive extensions for JavaScript
|
|
323
364
|
|
|
365
|
+
## API Reference
|
|
366
|
+
|
|
367
|
+
### Result Types
|
|
368
|
+
|
|
369
|
+
#### BaseResult
|
|
370
|
+
All operations return results extending `BaseResult`:
|
|
371
|
+
```typescript
|
|
372
|
+
class BaseResult {
|
|
373
|
+
success: boolean;
|
|
374
|
+
startTime: Date;
|
|
375
|
+
endTime: Date;
|
|
376
|
+
errorMessage: string;
|
|
377
|
+
exception: any;
|
|
378
|
+
timeElapsed: number; // Computed getter
|
|
379
|
+
}
|
|
380
|
+
```
|
|
381
|
+
|
|
382
|
+
#### ChatResult
|
|
383
|
+
Extends `BaseResult` with chat-specific data:
|
|
384
|
+
```typescript
|
|
385
|
+
class ChatResult extends BaseResult {
|
|
386
|
+
data: {
|
|
387
|
+
choices: Array<{
|
|
388
|
+
message: ChatCompletionMessage;
|
|
389
|
+
index: number;
|
|
390
|
+
finishReason?: string;
|
|
391
|
+
}>;
|
|
392
|
+
usage: ModelUsage;
|
|
393
|
+
};
|
|
394
|
+
statusText?: string;
|
|
395
|
+
}
|
|
396
|
+
```
|
|
397
|
+
|
|
398
|
+
### Chat Message Types
|
|
399
|
+
|
|
400
|
+
#### ChatMessage
|
|
401
|
+
Supports multi-modal content:
|
|
402
|
+
```typescript
|
|
403
|
+
type ChatMessage = {
|
|
404
|
+
role: 'system' | 'user' | 'assistant';
|
|
405
|
+
content: string | ChatMessageContentBlock[];
|
|
406
|
+
}
|
|
407
|
+
|
|
408
|
+
type ChatMessageContentBlock = {
|
|
409
|
+
type: 'text' | 'image_url' | 'video_url' | 'audio_url' | 'file_url';
|
|
410
|
+
content: string; // URL or base64 encoded content
|
|
411
|
+
}
|
|
412
|
+
```
|
|
413
|
+
|
|
414
|
+
### Streaming Callbacks
|
|
415
|
+
|
|
416
|
+
```typescript
|
|
417
|
+
interface StreamingChatCallbacks {
|
|
418
|
+
OnContent?: (chunk: string, isComplete: boolean) => void;
|
|
419
|
+
OnComplete?: (finalResponse: ChatResult) => void;
|
|
420
|
+
OnError?: (error: any) => void;
|
|
421
|
+
}
|
|
422
|
+
|
|
423
|
+
interface ParallelChatCompletionsCallbacks {
|
|
424
|
+
OnCompletion?: (response: ChatResult, index: number) => void;
|
|
425
|
+
OnError?: (error: any, index: number) => void;
|
|
426
|
+
OnAllCompleted?: (responses: ChatResult[]) => void;
|
|
427
|
+
}
|
|
428
|
+
```
|
|
429
|
+
|
|
430
|
+
## Configuration
|
|
431
|
+
|
|
432
|
+
### API Key Management
|
|
433
|
+
|
|
434
|
+
The package includes a flexible API key management system through the `AIAPIKeys` class:
|
|
435
|
+
|
|
436
|
+
```typescript
|
|
437
|
+
import { GetAIAPIKey } from '@memberjunction/ai';
|
|
438
|
+
|
|
439
|
+
// Get API key for a specific provider
|
|
440
|
+
const apiKey = GetAIAPIKey('OpenAI');
|
|
441
|
+
```
|
|
442
|
+
|
|
443
|
+
By default, it looks for environment variables with the pattern: `AI_VENDOR_API_KEY__[PROVIDER_NAME]`
|
|
444
|
+
|
|
445
|
+
Example:
|
|
446
|
+
```bash
|
|
447
|
+
AI_VENDOR_API_KEY__OPENAI=your-api-key
|
|
448
|
+
AI_VENDOR_API_KEY__ANTHROPIC=your-api-key
|
|
449
|
+
AI_VENDOR_API_KEY__MISTRAL=your-api-key
|
|
450
|
+
```
|
|
451
|
+
|
|
452
|
+
You can extend the `AIAPIKeys` class to implement custom API key retrieval logic.
|
|
453
|
+
|
|
454
|
+
### Provider-Specific Settings
|
|
455
|
+
|
|
456
|
+
Many providers support additional configuration beyond the API key:
|
|
457
|
+
|
|
458
|
+
```typescript
|
|
459
|
+
const llm = new SomeLLM('api-key');
|
|
460
|
+
llm.SetAdditionalSettings({
|
|
461
|
+
baseURL: 'https://custom-endpoint.com',
|
|
462
|
+
organization: 'my-org',
|
|
463
|
+
// Provider-specific settings
|
|
464
|
+
});
|
|
465
|
+
```
|
|
466
|
+
|
|
467
|
+
## Integration with MemberJunction
|
|
468
|
+
|
|
469
|
+
While this package can be used standalone, it integrates seamlessly with the MemberJunction framework:
|
|
470
|
+
|
|
471
|
+
- Uses `@memberjunction/global` for class factory pattern and registration
|
|
472
|
+
- Compatible with MemberJunction's metadata system
|
|
473
|
+
- Can leverage MemberJunction's configuration management when available
|
|
474
|
+
|
|
475
|
+
## Dependencies
|
|
476
|
+
|
|
477
|
+
- `@memberjunction/global` (^2.43.0) - MemberJunction global utilities including class factory
|
|
478
|
+
- `rxjs` (^7.8.1) - Reactive extensions for JavaScript (used in streaming implementations)
|
|
479
|
+
- `dotenv` (^16.4.1) - Environment variable management
|
|
480
|
+
- `typeorm` (^0.3.20) - ORM functionality (optional, only if using with full MemberJunction)
|
|
481
|
+
|
|
482
|
+
## Development
|
|
483
|
+
|
|
484
|
+
### Building
|
|
485
|
+
|
|
486
|
+
```bash
|
|
487
|
+
cd packages/AI/Core
|
|
488
|
+
npm run build
|
|
489
|
+
```
|
|
490
|
+
|
|
491
|
+
### TypeScript Configuration
|
|
492
|
+
|
|
493
|
+
The package is configured with TypeScript strict mode and targets ES2022. See `tsconfig.json` for full compiler options.
|
|
494
|
+
|
|
495
|
+
## Best Practices
|
|
496
|
+
|
|
497
|
+
1. **Always use the class factory pattern** for maximum flexibility
|
|
498
|
+
2. **Handle errors gracefully** - check `result.success` before accessing data
|
|
499
|
+
3. **Monitor token usage** to manage costs and stay within limits
|
|
500
|
+
4. **Use streaming for long responses** to improve user experience
|
|
501
|
+
5. **Leverage parallel completions** for comparison or reliability
|
|
502
|
+
6. **Cache API keys** using the built-in caching mechanism
|
|
503
|
+
7. **Specify response formats** when you need structured output
|
|
504
|
+
|
|
505
|
+
## Troubleshooting
|
|
506
|
+
|
|
507
|
+
### Common Issues
|
|
508
|
+
|
|
509
|
+
1. **"API key cannot be empty" error**
|
|
510
|
+
- Ensure you're passing a valid API key to the constructor
|
|
511
|
+
- Check environment variables are properly set
|
|
512
|
+
|
|
513
|
+
2. **Provider not found when using class factory**
|
|
514
|
+
- Make sure to import and call the provider's Load function
|
|
515
|
+
- Verify the provider class name matches exactly
|
|
516
|
+
|
|
517
|
+
3. **Streaming not working**
|
|
518
|
+
- Check if the provider supports streaming (`llm.SupportsStreaming`)
|
|
519
|
+
- Ensure streaming callbacks are properly defined
|
|
520
|
+
|
|
521
|
+
4. **Type errors with content blocks**
|
|
522
|
+
- Use the provided type guards and interfaces
|
|
523
|
+
- Ensure content format matches the expected structure
|
|
524
|
+
|
|
324
525
|
## License
|
|
526
|
+
|
|
325
527
|
See the [repository root](../../../LICENSE) for license information.
|