@memberjunction/ai 2.44.0 โ 2.46.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.
|
@@ -1,3 +1,4 @@
|
|
|
1
|
+
/// <reference types="node" />
|
|
1
2
|
import { BaseParams, BaseResult, ModelUsage } from "./baseModel";
|
|
2
3
|
export declare const ChatMessageRole: {
|
|
3
4
|
readonly system: "system";
|
|
@@ -35,6 +36,7 @@ export declare class ChatParams extends BaseParams {
|
|
|
35
36
|
effortLevel?: string;
|
|
36
37
|
enableCaching?: boolean;
|
|
37
38
|
includeLogProbs?: boolean;
|
|
39
|
+
cancellationToken?: AbortSignal;
|
|
38
40
|
}
|
|
39
41
|
export declare function GetUserMessageFromChatParams(p: ChatParams): ChatMessageContent | undefined;
|
|
40
42
|
export declare function GetSystemPromptFromChatParams(p: ChatParams): ChatMessageContent | undefined;
|
|
@@ -1 +1 @@
|
|
|
1
|
-
{"version":3,"file":"chat.types.d.ts","sourceRoot":"","sources":["../../src/generic/chat.types.ts"],"names":[],"mappings":"AAAA,OAAO,EAAE,UAAU,EAAE,UAAU,EAAE,UAAU,EAAE,MAAM,aAAa,CAAA;AAOhE,eAAO,MAAM,eAAe;;;;CAIlB,CAAC;AAEX,MAAM,MAAM,eAAe,GAAG,OAAO,eAAe,CAAC,MAAM,OAAO,eAAe,CAAC,CAAC;AAOnF,MAAM,MAAM,uBAAuB,GAAG;IAKlC,IAAI,EAAE,MAAM,GAAG,WAAW,GAAG,WAAW,GAAG,WAAW,GAAG,UAAU,CAAC;IAMpE,OAAO,EAAE,MAAM,CAAC;CACnB,CAAA;AAKD,MAAM,MAAM,kBAAkB,GAAG,MAAM,GAAG,uBAAuB,EAAE,CAAC;AAKpE,MAAM,MAAM,WAAW,GAAG;IAItB,IAAI,EAAE,eAAe,CAAC;IAItB,OAAO,EAAE,kBAAkB,CAAC;CAC/B,CAAA;AAKD,MAAM,MAAM,qBAAqB,GAAG;IAIhC,IAAI,E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C;IAKnB,SAAS,CAAC,EAAE,aAAa,CAAC;CAC7B"}
|
|
@@ -1 +1 @@
|
|
|
1
|
-
{"version":3,"file":"chat.types.js","sourceRoot":"","sources":["../../src/generic/chat.types.ts"],"names":[],"mappings":";;;AAAA,2CAAgE;AAOnD,QAAA,eAAe,GAAG;IAC3B,MAAM,EAAE,QAAQ;IAChB,IAAI,EAAE,MAAM;IACZ,SAAS,EAAE,WAAW;CAChB,CAAC;AA0GX,MAAa,UAAW,SAAQ,sBAAU;IAA1C;;QAII,aAAQ,GAAkB,EAAE,CAAC;QAO7B,cAAS,GAAa,KAAK,CAAC;QAsB5B,kBAAa,GAAa,IAAI,CAAC;QAQ/B,oBAAe,GAAa,KAAK,CAAC;
|
|
1
|
+
{"version":3,"file":"chat.types.js","sourceRoot":"","sources":["../../src/generic/chat.types.ts"],"names":[],"mappings":";;;AAAA,2CAAgE;AAOnD,QAAA,eAAe,GAAG;IAC3B,MAAM,EAAE,QAAQ;IAChB,IAAI,EAAE,MAAM;IACZ,SAAS,EAAE,WAAW;CAChB,CAAC;AA0GX,MAAa,UAAW,SAAQ,sBAAU;IAA1C;;QAII,aAAQ,GAAkB,EAAE,CAAC;QAO7B,cAAS,GAAa,KAAK,CAAC;QAsB5B,kBAAa,GAAa,IAAI,CAAC;QAQ/B,oBAAe,GAAa,KAAK,CAAC;IAQtC,CAAC;CAAA;AAjDD,gCAiDC;AAMD,SAAgB,4BAA4B,CAAC,CAAa;IACtD,OAAO,CAAC,CAAC,QAAQ,CAAC,IAAI,CAAC,CAAC,CAAC,EAAE,CAAC,CAAC,CAAC,IAAI,KAAK,uBAAe,CAAC,IAAI,CAAC,EAAE,OAAO,CAAC;AAC1E,CAAC;AAFD,oEAEC;AAMD,SAAgB,6BAA6B,CAAC,CAAa;IACvD,OAAO,CAAC,CAAC,QAAQ,CAAC,IAAI,CAAC,CAAC,CAAC,EAAE,CAAC,CAAC,CAAC,IAAI,KAAK,uBAAe,CAAC,MAAM,CAAC,EAAE,OAAO,CAAC;AAC5E,CAAC;AAFD,sEAEC;AAqGD,MAAa,UAAW,SAAQ,sBAAU;CASzC;AATD,gCASC"}
|
package/package.json
CHANGED
|
@@ -1,6 +1,6 @@
|
|
|
1
1
|
{
|
|
2
2
|
"name": "@memberjunction/ai",
|
|
3
|
-
"version": "2.
|
|
3
|
+
"version": "2.46.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.46.0",
|
|
19
19
|
"dotenv": "^16.4.1",
|
|
20
20
|
"rxjs": "^7.8.1",
|
|
21
21
|
"typeorm": "^0.3.20"
|
package/readme.md
CHANGED
|
@@ -241,6 +241,187 @@ const params: ChatParams = {
|
|
|
241
241
|
await llm.ChatCompletion(params);
|
|
242
242
|
```
|
|
243
243
|
|
|
244
|
+
### Cancellable Operations with AbortSignal
|
|
245
|
+
|
|
246
|
+
The MemberJunction AI Core package supports cancellation of long-running operations using the standard JavaScript `AbortSignal` pattern. This provides a clean, standardized way to cancel AI operations when needed.
|
|
247
|
+
|
|
248
|
+
#### Understanding the AbortSignal Pattern
|
|
249
|
+
|
|
250
|
+
The `AbortSignal` pattern uses a **separation of concerns** approach:
|
|
251
|
+
|
|
252
|
+
- **Controller (Caller)**: Creates the `AbortController` and decides **when** to cancel
|
|
253
|
+
- **Worker (AI Operations)**: Receives the `AbortSignal` token and handles **how** to cancel
|
|
254
|
+
|
|
255
|
+
#### Basic Cancellation Example
|
|
256
|
+
|
|
257
|
+
```typescript
|
|
258
|
+
import { ChatParams, BaseLLM } from '@memberjunction/ai';
|
|
259
|
+
|
|
260
|
+
async function cancellableAIChat() {
|
|
261
|
+
// Create the cancellation controller (the "boss")
|
|
262
|
+
const controller = new AbortController();
|
|
263
|
+
|
|
264
|
+
// Set up automatic timeout cancellation
|
|
265
|
+
const timeout = setTimeout(() => {
|
|
266
|
+
controller.abort(); // Cancel after 30 seconds
|
|
267
|
+
}, 30000);
|
|
268
|
+
|
|
269
|
+
try {
|
|
270
|
+
const params: ChatParams = {
|
|
271
|
+
model: 'gpt-4',
|
|
272
|
+
messages: [
|
|
273
|
+
{ role: 'user', content: 'Write a very long story...' }
|
|
274
|
+
],
|
|
275
|
+
cancellationToken: controller.signal // Pass the signal token
|
|
276
|
+
};
|
|
277
|
+
|
|
278
|
+
const result = await llm.ChatCompletion(params);
|
|
279
|
+
clearTimeout(timeout); // Clear timeout if completed successfully
|
|
280
|
+
return result;
|
|
281
|
+
|
|
282
|
+
} catch (error) {
|
|
283
|
+
if (error.message.includes('cancelled')) {
|
|
284
|
+
console.log('Operation was cancelled');
|
|
285
|
+
} else {
|
|
286
|
+
console.error('Operation failed:', error);
|
|
287
|
+
}
|
|
288
|
+
}
|
|
289
|
+
}
|
|
290
|
+
```
|
|
291
|
+
|
|
292
|
+
#### User-Initiated Cancellation
|
|
293
|
+
|
|
294
|
+
Perfect for UI applications where users can cancel operations:
|
|
295
|
+
|
|
296
|
+
```typescript
|
|
297
|
+
class AIInterface {
|
|
298
|
+
private currentController: AbortController | null = null;
|
|
299
|
+
|
|
300
|
+
async startAIConversation() {
|
|
301
|
+
// Create new controller for this conversation
|
|
302
|
+
this.currentController = new AbortController();
|
|
303
|
+
|
|
304
|
+
try {
|
|
305
|
+
const result = await llm.ChatCompletion({
|
|
306
|
+
model: 'gpt-4',
|
|
307
|
+
messages: [{ role: 'user', content: 'Generate a detailed report...' }],
|
|
308
|
+
cancellationToken: this.currentController.signal
|
|
309
|
+
});
|
|
310
|
+
|
|
311
|
+
console.log('AI Response:', result.data.choices[0].message.content);
|
|
312
|
+
} catch (error) {
|
|
313
|
+
if (error.message.includes('cancelled')) {
|
|
314
|
+
console.log('User cancelled the conversation');
|
|
315
|
+
}
|
|
316
|
+
} finally {
|
|
317
|
+
this.currentController = null;
|
|
318
|
+
}
|
|
319
|
+
}
|
|
320
|
+
|
|
321
|
+
// Called when user clicks "Cancel" button
|
|
322
|
+
cancelConversation() {
|
|
323
|
+
if (this.currentController) {
|
|
324
|
+
this.currentController.abort(); // Instant cancellation
|
|
325
|
+
}
|
|
326
|
+
}
|
|
327
|
+
}
|
|
328
|
+
```
|
|
329
|
+
|
|
330
|
+
#### Multiple Cancellation Sources
|
|
331
|
+
|
|
332
|
+
One signal can be cancelled from multiple sources:
|
|
333
|
+
|
|
334
|
+
```typescript
|
|
335
|
+
async function smartAIExecution() {
|
|
336
|
+
const controller = new AbortController();
|
|
337
|
+
const signal = controller.signal;
|
|
338
|
+
|
|
339
|
+
// 1. User cancel button
|
|
340
|
+
document.getElementById('cancel')?.addEventListener('click', () => {
|
|
341
|
+
controller.abort(); // User cancellation
|
|
342
|
+
});
|
|
343
|
+
|
|
344
|
+
// 2. Resource limit cancellation
|
|
345
|
+
if (await checkMemoryUsage() > MAX_MEMORY) {
|
|
346
|
+
controller.abort(); // Resource limit cancellation
|
|
347
|
+
}
|
|
348
|
+
|
|
349
|
+
// 3. Window unload cancellation
|
|
350
|
+
window.addEventListener('beforeunload', () => {
|
|
351
|
+
controller.abort(); // Page closing cancellation
|
|
352
|
+
});
|
|
353
|
+
|
|
354
|
+
// 4. Timeout cancellation
|
|
355
|
+
setTimeout(() => controller.abort(), 60000); // 1 minute timeout
|
|
356
|
+
|
|
357
|
+
try {
|
|
358
|
+
const result = await llm.ChatCompletion({
|
|
359
|
+
model: 'gpt-4',
|
|
360
|
+
messages: [{ role: 'user', content: 'Complex analysis task...' }],
|
|
361
|
+
cancellationToken: signal // One token, many cancel sources!
|
|
362
|
+
});
|
|
363
|
+
|
|
364
|
+
return result;
|
|
365
|
+
} catch (error) {
|
|
366
|
+
// The AI operation doesn't know WHY it was cancelled - just that it should stop
|
|
367
|
+
console.log('AI operation was cancelled:', error.message);
|
|
368
|
+
}
|
|
369
|
+
}
|
|
370
|
+
```
|
|
371
|
+
|
|
372
|
+
#### How It Works Internally
|
|
373
|
+
|
|
374
|
+
The AI Core package implements cancellation at multiple levels:
|
|
375
|
+
|
|
376
|
+
1. **BaseLLM Level**: Checks cancellation token before and during operations
|
|
377
|
+
2. **Provider Level**: Native cancellation support where available (e.g., fetch API)
|
|
378
|
+
3. **Fallback Pattern**: Promise.race for providers without native cancellation
|
|
379
|
+
|
|
380
|
+
```typescript
|
|
381
|
+
// Internal implementation example (simplified)
|
|
382
|
+
async ChatCompletion(params: ChatParams): Promise<ChatResult> {
|
|
383
|
+
// Check if already cancelled before starting
|
|
384
|
+
if (params.cancellationToken?.aborted) {
|
|
385
|
+
throw new Error('Operation was cancelled');
|
|
386
|
+
}
|
|
387
|
+
|
|
388
|
+
// For providers with native cancellation support
|
|
389
|
+
if (this.hasNativeCancellation) {
|
|
390
|
+
return await this.callProviderAPI({
|
|
391
|
+
...params,
|
|
392
|
+
signal: params.cancellationToken // Native fetch cancellation
|
|
393
|
+
});
|
|
394
|
+
}
|
|
395
|
+
|
|
396
|
+
// Fallback: Promise.race pattern for other providers
|
|
397
|
+
const promises = [
|
|
398
|
+
this.callProviderAPI(params) // The actual AI call
|
|
399
|
+
];
|
|
400
|
+
|
|
401
|
+
if (params.cancellationToken) {
|
|
402
|
+
promises.push(
|
|
403
|
+
new Promise<never>((_, reject) => {
|
|
404
|
+
params.cancellationToken!.addEventListener('abort', () => {
|
|
405
|
+
reject(new Error('Operation was cancelled'));
|
|
406
|
+
});
|
|
407
|
+
})
|
|
408
|
+
);
|
|
409
|
+
}
|
|
410
|
+
|
|
411
|
+
return await Promise.race(promises);
|
|
412
|
+
}
|
|
413
|
+
```
|
|
414
|
+
|
|
415
|
+
#### Key Benefits
|
|
416
|
+
|
|
417
|
+
1. **๐ฏ Responsive UX**: Users can cancel long-running AI operations instantly
|
|
418
|
+
2. **๐พ Resource Management**: Prevent runaway operations from consuming resources
|
|
419
|
+
3. **๐ Composable**: Easy to combine user actions, timeouts, and resource limits
|
|
420
|
+
4. **๐ฑ Standard API**: Uses native JavaScript AbortSignal - no custom patterns
|
|
421
|
+
5. **๐งน Clean Cleanup**: Automatic resource cleanup when operations are cancelled
|
|
422
|
+
|
|
423
|
+
The cancellation token pattern provides a **robust, standardized way** to make AI operations responsive and resource-efficient!
|
|
424
|
+
|
|
244
425
|
### Text Summarization
|
|
245
426
|
|
|
246
427
|
For summarizing longer text content:
|