@memberjunction/ai 4.4.0 → 5.0.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 +0 -242
package/package.json
CHANGED
|
@@ -1,7 +1,7 @@
|
|
|
1
1
|
{
|
|
2
2
|
"name": "@memberjunction/ai",
|
|
3
3
|
"type": "module",
|
|
4
|
-
"version": "
|
|
4
|
+
"version": "5.0.0",
|
|
5
5
|
"description": "MemberJunction: AI - core components for abstracting LLMs and other AI model types that are usable anywhere without ANY other MJ dependencies past @memberjunction/global which itself has zero additional dependencies.",
|
|
6
6
|
"main": "dist/index.js",
|
|
7
7
|
"types": "dist/index.d.ts",
|
|
@@ -17,7 +17,7 @@
|
|
|
17
17
|
"author": "MemberJunction.com",
|
|
18
18
|
"license": "ISC",
|
|
19
19
|
"dependencies": {
|
|
20
|
-
"@memberjunction/global": "
|
|
20
|
+
"@memberjunction/global": "5.0.0",
|
|
21
21
|
"dotenv": "^17.2.4",
|
|
22
22
|
"rxjs": "^7.8.2"
|
|
23
23
|
},
|
package/README.md
DELETED
|
@@ -1,242 +0,0 @@
|
|
|
1
|
-
# @memberjunction/ai
|
|
2
|
-
|
|
3
|
-
Core abstractions and base classes for the MemberJunction AI Framework. This package defines provider-agnostic interfaces for Large Language Models (LLMs), embeddings, image generation, audio, video, reranking, and more. It has **zero MemberJunction dependencies** beyond `@memberjunction/global`, making it suitable for standalone use in any TypeScript/JavaScript project.
|
|
4
|
-
|
|
5
|
-
## Architecture
|
|
6
|
-
|
|
7
|
-
```mermaid
|
|
8
|
-
graph TD
|
|
9
|
-
subgraph "@memberjunction/ai"
|
|
10
|
-
BM["BaseModel"]
|
|
11
|
-
style BM fill:#2d6a9f,stroke:#1a4971,color:#fff
|
|
12
|
-
|
|
13
|
-
BLLM["BaseLLM"]
|
|
14
|
-
style BLLM fill:#2d6a9f,stroke:#1a4971,color:#fff
|
|
15
|
-
|
|
16
|
-
BE["BaseEmbeddings"]
|
|
17
|
-
style BE fill:#2d6a9f,stroke:#1a4971,color:#fff
|
|
18
|
-
|
|
19
|
-
BIG["BaseImageGenerator"]
|
|
20
|
-
style BIG fill:#2d6a9f,stroke:#1a4971,color:#fff
|
|
21
|
-
|
|
22
|
-
BA["BaseAudio"]
|
|
23
|
-
style BA fill:#2d6a9f,stroke:#1a4971,color:#fff
|
|
24
|
-
|
|
25
|
-
BV["BaseVideo"]
|
|
26
|
-
style BV fill:#2d6a9f,stroke:#1a4971,color:#fff
|
|
27
|
-
|
|
28
|
-
BR["BaseReranker"]
|
|
29
|
-
style BR fill:#2d6a9f,stroke:#1a4971,color:#fff
|
|
30
|
-
|
|
31
|
-
CT["Chat Types"]
|
|
32
|
-
style CT fill:#7c5295,stroke:#563a6b,color:#fff
|
|
33
|
-
|
|
34
|
-
ET["Embed Types"]
|
|
35
|
-
style ET fill:#7c5295,stroke:#563a6b,color:#fff
|
|
36
|
-
|
|
37
|
-
ERR["ErrorAnalyzer"]
|
|
38
|
-
style ERR fill:#b8762f,stroke:#8a5722,color:#fff
|
|
39
|
-
|
|
40
|
-
AK["AIAPIKeys"]
|
|
41
|
-
style AK fill:#2d8659,stroke:#1a5c3a,color:#fff
|
|
42
|
-
|
|
43
|
-
BM --> BLLM
|
|
44
|
-
BM --> BE
|
|
45
|
-
BM --> BIG
|
|
46
|
-
BM --> BA
|
|
47
|
-
BM --> BV
|
|
48
|
-
BM --> BR
|
|
49
|
-
end
|
|
50
|
-
|
|
51
|
-
P1["OpenAI Provider"]
|
|
52
|
-
style P1 fill:#2d8659,stroke:#1a5c3a,color:#fff
|
|
53
|
-
P2["Anthropic Provider"]
|
|
54
|
-
style P2 fill:#2d8659,stroke:#1a5c3a,color:#fff
|
|
55
|
-
P3["Other Providers"]
|
|
56
|
-
style P3 fill:#2d8659,stroke:#1a5c3a,color:#fff
|
|
57
|
-
|
|
58
|
-
BLLM --> P1
|
|
59
|
-
BLLM --> P2
|
|
60
|
-
BLLM --> P3
|
|
61
|
-
```
|
|
62
|
-
|
|
63
|
-
## Installation
|
|
64
|
-
|
|
65
|
-
```bash
|
|
66
|
-
npm install @memberjunction/ai
|
|
67
|
-
```
|
|
68
|
-
|
|
69
|
-
## Key Exports
|
|
70
|
-
|
|
71
|
-
### Base Classes
|
|
72
|
-
|
|
73
|
-
| Class | Purpose |
|
|
74
|
-
|---|---|
|
|
75
|
-
| `BaseModel` | Root base class for all AI model types; manages API key storage |
|
|
76
|
-
| `BaseLLM` | Abstract base for chat completion providers with streaming, parallel execution, and thinking model support |
|
|
77
|
-
| `BaseEmbeddings` | Abstract base for text embedding providers |
|
|
78
|
-
| `BaseImageGenerator` | Abstract base for image generation, editing, and variation providers |
|
|
79
|
-
| `BaseAudio` | Abstract base for text-to-speech and speech-to-text providers |
|
|
80
|
-
| `BaseVideo` | Abstract base for video generation providers |
|
|
81
|
-
| `BaseReranker` | Abstract base for document reranking providers |
|
|
82
|
-
|
|
83
|
-
### Type Definitions
|
|
84
|
-
|
|
85
|
-
| Type / Class | Purpose |
|
|
86
|
-
|---|---|
|
|
87
|
-
| `ChatParams` | Parameters for chat completion requests (messages, streaming, sampling controls) |
|
|
88
|
-
| `ChatResult` | Result of a chat completion including choices, usage, and cache info |
|
|
89
|
-
| `ChatMessage` | Individual message with role, content (text or multimodal blocks), and optional metadata |
|
|
90
|
-
| `ChatMessageContentBlock` | Multimodal content block (text, image, video, audio, file) |
|
|
91
|
-
| `StreamingChatCallbacks` | Callbacks for real-time streaming responses |
|
|
92
|
-
| `ParallelChatCompletionsCallbacks` | Callbacks for batch parallel completions |
|
|
93
|
-
| `EmbedTextParams` / `EmbedTextResult` | Parameters and results for single text embedding |
|
|
94
|
-
| `EmbedTextsParams` / `EmbedTextsResult` | Parameters and results for batch text embeddings |
|
|
95
|
-
| `ImageGenerationParams` / `ImageGenerationResult` | Image generation parameters and results |
|
|
96
|
-
| `SummarizeParams` / `SummarizeResult` | Text summarization parameters and results |
|
|
97
|
-
| `ClassifyParams` / `ClassifyResult` | Text classification parameters and results |
|
|
98
|
-
| `RerankParams` / `RerankResult` | Document reranking parameters and results |
|
|
99
|
-
|
|
100
|
-
### Utilities
|
|
101
|
-
|
|
102
|
-
| Export | Purpose |
|
|
103
|
-
|---|---|
|
|
104
|
-
| `BaseResult` | Common result base with success flag, timing, and error info |
|
|
105
|
-
| `ModelUsage` | Token usage and cost tracking (prompt tokens, completion tokens, cost, currency) |
|
|
106
|
-
| `AIAPIKeys` | API key management via environment variables (`AI_VENDOR_API_KEY__<DRIVER>`) |
|
|
107
|
-
| `GetAIAPIKey()` | Helper to resolve API keys with optional runtime overrides |
|
|
108
|
-
| `ErrorAnalyzer` | Standardized error analysis across all providers with severity and failover hints |
|
|
109
|
-
| `AIErrorInfo` / `AIErrorType` | Structured error types (rate limit, authentication, context length, etc.) |
|
|
110
|
-
| `serializeMessageContent()` / `deserializeMessageContent()` | Content block serialization for database storage |
|
|
111
|
-
| `parseBase64DataUrl()` / `createBase64DataUrl()` | Base64 data URL utilities |
|
|
112
|
-
|
|
113
|
-
## Usage
|
|
114
|
-
|
|
115
|
-
### Basic Chat Completion
|
|
116
|
-
|
|
117
|
-
```typescript
|
|
118
|
-
import { ChatParams, ChatMessageRole } from '@memberjunction/ai';
|
|
119
|
-
import { OpenAILLM } from '@memberjunction/ai-openai';
|
|
120
|
-
|
|
121
|
-
const llm = new OpenAILLM('your-api-key');
|
|
122
|
-
|
|
123
|
-
const params = new ChatParams();
|
|
124
|
-
params.model = 'gpt-4';
|
|
125
|
-
params.messages = [
|
|
126
|
-
{ role: ChatMessageRole.user, content: 'What is the capital of France?' }
|
|
127
|
-
];
|
|
128
|
-
|
|
129
|
-
const result = await llm.ChatCompletion(params);
|
|
130
|
-
console.log(result.data.choices[0].message.content);
|
|
131
|
-
```
|
|
132
|
-
|
|
133
|
-
### Streaming Chat Completion
|
|
134
|
-
|
|
135
|
-
```typescript
|
|
136
|
-
const params = new ChatParams();
|
|
137
|
-
params.model = 'gpt-4';
|
|
138
|
-
params.streaming = true;
|
|
139
|
-
params.messages = [
|
|
140
|
-
{ role: ChatMessageRole.user, content: 'Explain quantum computing' }
|
|
141
|
-
];
|
|
142
|
-
params.streamingCallbacks = {
|
|
143
|
-
OnContent: (chunk, isComplete) => process.stdout.write(chunk),
|
|
144
|
-
OnComplete: (result) => console.log('\nDone!'),
|
|
145
|
-
OnError: (error) => console.error('Stream error:', error)
|
|
146
|
-
};
|
|
147
|
-
|
|
148
|
-
await llm.ChatCompletion(params);
|
|
149
|
-
```
|
|
150
|
-
|
|
151
|
-
### Parallel Chat Completions
|
|
152
|
-
|
|
153
|
-
```typescript
|
|
154
|
-
const paramsArray = [
|
|
155
|
-
{ ...baseParams, temperature: 0.3 },
|
|
156
|
-
{ ...baseParams, temperature: 0.7 },
|
|
157
|
-
{ ...baseParams, temperature: 1.0 }
|
|
158
|
-
];
|
|
159
|
-
|
|
160
|
-
const results = await llm.ChatCompletions(paramsArray, {
|
|
161
|
-
OnCompletion: (result, index) => console.log(`Completion ${index} done`),
|
|
162
|
-
OnAllCompleted: (results) => console.log(`All ${results.length} done`)
|
|
163
|
-
});
|
|
164
|
-
```
|
|
165
|
-
|
|
166
|
-
### Multimodal Content
|
|
167
|
-
|
|
168
|
-
```typescript
|
|
169
|
-
const params = new ChatParams();
|
|
170
|
-
params.model = 'gpt-4o';
|
|
171
|
-
params.messages = [
|
|
172
|
-
{
|
|
173
|
-
role: ChatMessageRole.user,
|
|
174
|
-
content: [
|
|
175
|
-
{ type: 'text', content: 'What is in this image?' },
|
|
176
|
-
{ type: 'image_url', content: 'data:image/png;base64,...' }
|
|
177
|
-
]
|
|
178
|
-
}
|
|
179
|
-
];
|
|
180
|
-
```
|
|
181
|
-
|
|
182
|
-
### Text Embeddings
|
|
183
|
-
|
|
184
|
-
```typescript
|
|
185
|
-
import { EmbedTextParams } from '@memberjunction/ai';
|
|
186
|
-
import { OpenAIEmbeddings } from '@memberjunction/ai-openai';
|
|
187
|
-
|
|
188
|
-
const embedder = new OpenAIEmbeddings('your-api-key');
|
|
189
|
-
const result = await embedder.EmbedText({
|
|
190
|
-
model: 'text-embedding-ada-002',
|
|
191
|
-
text: 'Sample text to embed'
|
|
192
|
-
});
|
|
193
|
-
console.log(result.embedding); // number[]
|
|
194
|
-
```
|
|
195
|
-
|
|
196
|
-
### API Key Management
|
|
197
|
-
|
|
198
|
-
```typescript
|
|
199
|
-
import { GetAIAPIKey } from '@memberjunction/ai';
|
|
200
|
-
|
|
201
|
-
// Reads from environment: AI_VENDOR_API_KEY__OPENAILLM
|
|
202
|
-
const key = GetAIAPIKey('OpenAILLM');
|
|
203
|
-
|
|
204
|
-
// With runtime override
|
|
205
|
-
const key2 = GetAIAPIKey('AnthropicLLM', [
|
|
206
|
-
{ driverClass: 'AnthropicLLM', apiKey: 'sk-ant-...' }
|
|
207
|
-
]);
|
|
208
|
-
```
|
|
209
|
-
|
|
210
|
-
## Provider Implementation
|
|
211
|
-
|
|
212
|
-
To create a new AI provider, extend the appropriate base class:
|
|
213
|
-
|
|
214
|
-
```typescript
|
|
215
|
-
import { BaseLLM, ChatParams, ChatResult } from '@memberjunction/ai';
|
|
216
|
-
|
|
217
|
-
export class MyProviderLLM extends BaseLLM {
|
|
218
|
-
protected async nonStreamingChatCompletion(params: ChatParams): Promise<ChatResult> {
|
|
219
|
-
// Implement provider-specific chat completion
|
|
220
|
-
}
|
|
221
|
-
|
|
222
|
-
public async ClassifyText(params: ClassifyParams): Promise<ClassifyResult> {
|
|
223
|
-
// Implement or throw if not supported
|
|
224
|
-
}
|
|
225
|
-
|
|
226
|
-
public async SummarizeText(params: SummarizeParams): Promise<SummarizeResult> {
|
|
227
|
-
// Implement or throw if not supported
|
|
228
|
-
}
|
|
229
|
-
|
|
230
|
-
// For streaming support, override these:
|
|
231
|
-
public get SupportsStreaming(): boolean { return true; }
|
|
232
|
-
protected async createStreamingRequest(params: ChatParams): Promise<AsyncIterable<unknown>> { /* ... */ }
|
|
233
|
-
protected processStreamingChunk(chunk: unknown): { content: string } { /* ... */ }
|
|
234
|
-
protected finalizeStreamingResponse(content: string, lastChunk: unknown, usage: unknown): ChatResult { /* ... */ }
|
|
235
|
-
}
|
|
236
|
-
```
|
|
237
|
-
|
|
238
|
-
## Dependencies
|
|
239
|
-
|
|
240
|
-
- `@memberjunction/global` -- Class factory and global utilities (zero transitive dependencies)
|
|
241
|
-
- `dotenv` -- Environment variable loading
|
|
242
|
-
- `rxjs` -- Reactive extensions (used internally)
|