@memberjunction/ai 3.4.0 → 4.1.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 +242 -0
- package/dist/generic/apiKeyDictionary.d.ts +22 -0
- package/dist/generic/apiKeyDictionary.d.ts.map +1 -1
- package/dist/generic/apiKeyDictionary.js +20 -13
- package/dist/generic/apiKeyDictionary.js.map +1 -1
- package/dist/generic/baseAudio.d.ts +107 -2
- package/dist/generic/baseAudio.d.ts.map +1 -1
- package/dist/generic/baseAudio.js +26 -20
- package/dist/generic/baseAudio.js.map +1 -1
- package/dist/generic/baseDiffusion.d.ts +5 -1
- package/dist/generic/baseDiffusion.d.ts.map +1 -1
- package/dist/generic/baseDiffusion.js +5 -17
- package/dist/generic/baseDiffusion.js.map +1 -1
- package/dist/generic/baseEmbeddings.d.ts +25 -2
- package/dist/generic/baseEmbeddings.d.ts.map +1 -1
- package/dist/generic/baseEmbeddings.js +26 -8
- package/dist/generic/baseEmbeddings.js.map +1 -1
- package/dist/generic/baseImage.d.ts +262 -2
- package/dist/generic/baseImage.d.ts.map +1 -1
- package/dist/generic/baseImage.js +83 -20
- package/dist/generic/baseImage.js.map +1 -1
- package/dist/generic/baseLLM.d.ts +100 -4
- package/dist/generic/baseLLM.d.ts.map +1 -1
- package/dist/generic/baseLLM.js +122 -14
- package/dist/generic/baseLLM.js.map +1 -1
- package/dist/generic/baseModel.d.ts +91 -0
- package/dist/generic/baseModel.d.ts.map +1 -1
- package/dist/generic/baseModel.js +37 -11
- package/dist/generic/baseModel.js.map +1 -1
- package/dist/generic/baseReranker.d.ts +81 -2
- package/dist/generic/baseReranker.d.ts.map +1 -1
- package/dist/generic/baseReranker.js +73 -6
- package/dist/generic/baseReranker.js.map +1 -1
- package/dist/generic/baseVideo.d.ts +24 -1
- package/dist/generic/baseVideo.d.ts.map +1 -1
- package/dist/generic/baseVideo.js +11 -14
- package/dist/generic/baseVideo.js.map +1 -1
- package/dist/generic/chat.types.d.ts +291 -2
- package/dist/generic/chat.types.d.ts.map +1 -1
- package/dist/generic/chat.types.js +105 -32
- package/dist/generic/chat.types.js.map +1 -1
- package/dist/generic/classify.types.d.ts +5 -2
- package/dist/generic/classify.types.d.ts.map +1 -1
- package/dist/generic/classify.types.js +8 -11
- package/dist/generic/classify.types.js.map +1 -1
- package/dist/generic/embed.types.d.ts +1 -1
- package/dist/generic/embed.types.js +1 -2
- package/dist/generic/errorAnalyzer.d.ts +94 -0
- package/dist/generic/errorAnalyzer.d.ts.map +1 -1
- package/dist/generic/errorAnalyzer.js +160 -28
- package/dist/generic/errorAnalyzer.js.map +1 -1
- package/dist/generic/errorTypes.d.ts +116 -0
- package/dist/generic/errorTypes.d.ts.map +1 -1
- package/dist/generic/errorTypes.js +1 -2
- package/dist/generic/reranker.types.d.ts +74 -0
- package/dist/generic/reranker.types.d.ts.map +1 -1
- package/dist/generic/reranker.types.js +10 -2
- package/dist/generic/reranker.types.js.map +1 -1
- package/dist/generic/summarize.types.d.ts +5 -2
- package/dist/generic/summarize.types.d.ts.map +1 -1
- package/dist/generic/summarize.types.js +8 -9
- package/dist/generic/summarize.types.js.map +1 -1
- package/dist/index.d.ts +15 -15
- package/dist/index.js +15 -31
- package/dist/index.js.map +1 -1
- package/package.json +8 -7
- package/readme.md +167 -967
|
@@ -1,45 +1,141 @@
|
|
|
1
|
-
import { SummarizeParams, SummarizeResult } from "./summarize.types";
|
|
2
|
-
import { BaseModel } from "./baseModel";
|
|
3
|
-
import { ChatParams, ChatResult, ParallelChatCompletionsCallbacks, ChatCompletionMessage } from "./chat.types";
|
|
4
|
-
import { ClassifyParams, ClassifyResult } from "./classify.types";
|
|
1
|
+
import { SummarizeParams, SummarizeResult } from "./summarize.types.js";
|
|
2
|
+
import { BaseModel } from "./baseModel.js";
|
|
3
|
+
import { ChatParams, ChatResult, ParallelChatCompletionsCallbacks, ChatCompletionMessage } from "./chat.types.js";
|
|
4
|
+
import { ClassifyParams, ClassifyResult } from "./classify.types.js";
|
|
5
|
+
/**
|
|
6
|
+
* Type for thinking stream state management
|
|
7
|
+
*/
|
|
5
8
|
type ThinkingStreamState = {
|
|
6
9
|
accumulatedThinking: string;
|
|
7
10
|
inThinkingBlock: boolean;
|
|
8
11
|
pendingContent: string;
|
|
9
12
|
thinkingComplete: boolean;
|
|
10
13
|
};
|
|
14
|
+
/**
|
|
15
|
+
* Base class for all LLM sub-class implementations. Not all sub-classes will support all methods.
|
|
16
|
+
* If a method is not supported an exception will be thrown.
|
|
17
|
+
*/
|
|
11
18
|
export declare abstract class BaseLLM extends BaseModel {
|
|
19
|
+
/**
|
|
20
|
+
* Protected property to store additional provider-specific settings
|
|
21
|
+
*/
|
|
12
22
|
protected _additionalSettings: Record<string, any>;
|
|
23
|
+
/**
|
|
24
|
+
* Get the current additional settings
|
|
25
|
+
*/
|
|
13
26
|
get AdditionalSettings(): Record<string, any>;
|
|
27
|
+
/**
|
|
28
|
+
* Set additional provider-specific settings
|
|
29
|
+
* Subclasses should override this method to validate required settings
|
|
30
|
+
*
|
|
31
|
+
* @param settings Provider-specific settings
|
|
32
|
+
*/
|
|
14
33
|
SetAdditionalSettings(settings: Record<string, any>): void;
|
|
34
|
+
/**
|
|
35
|
+
* Clear all additional settings
|
|
36
|
+
* This is useful for resetting the state of the provider
|
|
37
|
+
* or when switching between different configurations.
|
|
38
|
+
*/
|
|
15
39
|
ClearAdditionalSettings(): void;
|
|
40
|
+
/**
|
|
41
|
+
* Process a chat completion request. If streaming is enabled and supported,
|
|
42
|
+
* this will route to the streaming implementation.
|
|
43
|
+
*/
|
|
16
44
|
ChatCompletion(params: ChatParams): Promise<ChatResult>;
|
|
45
|
+
/**
|
|
46
|
+
* Process multiple chat completion requests in parallel. This is useful for:
|
|
47
|
+
* - Generating multiple variations with different parameters (temperature, etc.)
|
|
48
|
+
* - Getting multiple responses to compare or select from
|
|
49
|
+
* - Improving reliability by sending the same request multiple times
|
|
50
|
+
*
|
|
51
|
+
* @param paramsArray Array of chat completion parameter objects
|
|
52
|
+
* @param callbacks Optional callbacks for progress and individual completions
|
|
53
|
+
* @returns Promise resolving to an array of ChatResults in the same order as the input params
|
|
54
|
+
*/
|
|
17
55
|
ChatCompletions(paramsArray: ChatParams[], callbacks?: ParallelChatCompletionsCallbacks): Promise<ChatResult[]>;
|
|
56
|
+
/**
|
|
57
|
+
* Implementation for non-streaming chat completion
|
|
58
|
+
*/
|
|
18
59
|
protected abstract nonStreamingChatCompletion(params: ChatParams): Promise<ChatResult>;
|
|
19
60
|
abstract ClassifyText(params: ClassifyParams): Promise<ClassifyResult>;
|
|
20
61
|
abstract SummarizeText(params: SummarizeParams): Promise<SummarizeResult>;
|
|
62
|
+
/**
|
|
63
|
+
* Check if this provider supports streaming
|
|
64
|
+
* @returns true if streaming is supported, false otherwise
|
|
65
|
+
*/
|
|
21
66
|
get SupportsStreaming(): boolean;
|
|
67
|
+
/**
|
|
68
|
+
* Template method for handling streaming chat completion
|
|
69
|
+
* This implements the common pattern across providers while delegating
|
|
70
|
+
* provider-specific logic to abstract methods.
|
|
71
|
+
*/
|
|
22
72
|
protected handleStreamingChatCompletion(params: ChatParams): Promise<ChatResult>;
|
|
73
|
+
/**
|
|
74
|
+
* Create a provider-specific streaming request
|
|
75
|
+
* @param params Chat parameters
|
|
76
|
+
* @returns A stream object that can be iterated with for await
|
|
77
|
+
*/
|
|
23
78
|
protected abstract createStreamingRequest(params: ChatParams): Promise<any>;
|
|
79
|
+
/**
|
|
80
|
+
* Process a streaming chunk from the provider
|
|
81
|
+
* @param chunk The raw chunk from the provider
|
|
82
|
+
* @returns Processed content and metadata
|
|
83
|
+
*/
|
|
24
84
|
protected abstract processStreamingChunk(chunk: any): {
|
|
25
85
|
content: string;
|
|
26
86
|
finishReason?: string | undefined;
|
|
27
87
|
usage?: any | null;
|
|
28
88
|
};
|
|
89
|
+
/**
|
|
90
|
+
* Create the final response object from streaming results
|
|
91
|
+
* @param accumulatedContent The complete content accumulated from all chunks
|
|
92
|
+
* @param lastChunk The last chunk received from the stream
|
|
93
|
+
* @param usage The usage information (tokens, etc.)
|
|
94
|
+
* @returns A complete ChatResult object
|
|
95
|
+
*/
|
|
29
96
|
protected abstract finalizeStreamingResponse(accumulatedContent: string | null | undefined, lastChunk: any | null | undefined, usage: any | null | undefined): ChatResult;
|
|
97
|
+
/**
|
|
98
|
+
* State tracking for streaming thinking extraction
|
|
99
|
+
* Providers should initialize this if they support thinking models
|
|
100
|
+
*/
|
|
30
101
|
protected thinkingStreamState: ThinkingStreamState | null;
|
|
102
|
+
/**
|
|
103
|
+
* Check if the provider supports thinking models
|
|
104
|
+
* Providers should override this to return true if they support thinking extraction
|
|
105
|
+
*/
|
|
31
106
|
protected supportsThinkingModels(): boolean;
|
|
107
|
+
/**
|
|
108
|
+
* Get the thinking tag format for this provider
|
|
109
|
+
* Providers can override this to customize the thinking tag format
|
|
110
|
+
*/
|
|
32
111
|
protected getThinkingTagFormat(): {
|
|
33
112
|
open: string;
|
|
34
113
|
close: string;
|
|
35
114
|
};
|
|
115
|
+
/**
|
|
116
|
+
* Extract thinking content from non-streaming content
|
|
117
|
+
* This method handles case-insensitive extraction of thinking blocks
|
|
118
|
+
*/
|
|
36
119
|
protected extractThinkingFromContent(content: string): {
|
|
37
120
|
content: string;
|
|
38
121
|
thinking?: string;
|
|
39
122
|
};
|
|
123
|
+
/**
|
|
124
|
+
* Initialize thinking stream state for streaming extraction
|
|
125
|
+
*/
|
|
40
126
|
protected initializeThinkingStreamState(): void;
|
|
127
|
+
/**
|
|
128
|
+
* Reset thinking stream state
|
|
129
|
+
*/
|
|
41
130
|
protected resetThinkingStreamState(): void;
|
|
131
|
+
/**
|
|
132
|
+
* Process streaming chunk with thinking extraction
|
|
133
|
+
* This method handles case-insensitive extraction across chunk boundaries
|
|
134
|
+
*/
|
|
42
135
|
protected processStreamChunkWithThinking(rawContent: string): string;
|
|
136
|
+
/**
|
|
137
|
+
* Add thinking content to a chat completion message
|
|
138
|
+
*/
|
|
43
139
|
protected addThinkingToMessage(message: ChatCompletionMessage, thinkingContent?: string): ChatCompletionMessage;
|
|
44
140
|
}
|
|
45
141
|
export {};
|
|
@@ -1 +1 @@
|
|
|
1
|
-
{"version":3,"file":"baseLLM.d.ts","sourceRoot":"","sources":["../../src/generic/baseLLM.ts"],"names":[],"mappings":"AAAA,OAAO,EAAE,eAAe,EAAE,eAAe,EAAE,MAAM,mBAAmB,CAAC;AACrE,OAAO,EAAE,SAAS,EAAc,MAAM,aAAa,CAAC;AACpD,OAAO,EAAE,UAAU,EAAE,UAAU,EAA0B,gCAAgC,EAAE,qBAAqB,EAAE,MAAM,cAAc,CAAC;AACvI,OAAO,EAAE,cAAc,EAAE,cAAc,EAAE,MAAM,kBAAkB,CAAC;
|
|
1
|
+
{"version":3,"file":"baseLLM.d.ts","sourceRoot":"","sources":["../../src/generic/baseLLM.ts"],"names":[],"mappings":"AAAA,OAAO,EAAE,eAAe,EAAE,eAAe,EAAE,MAAM,mBAAmB,CAAC;AACrE,OAAO,EAAE,SAAS,EAAc,MAAM,aAAa,CAAC;AACpD,OAAO,EAAE,UAAU,EAAE,UAAU,EAA0B,gCAAgC,EAAE,qBAAqB,EAAE,MAAM,cAAc,CAAC;AACvI,OAAO,EAAE,cAAc,EAAE,cAAc,EAAE,MAAM,kBAAkB,CAAC;AAGlE;;GAEG;AACH,KAAK,mBAAmB,GAAG;IACvB,mBAAmB,EAAE,MAAM,CAAC;IAC5B,eAAe,EAAE,OAAO,CAAC;IACzB,cAAc,EAAE,MAAM,CAAC;IACvB,gBAAgB,EAAE,OAAO,CAAC;CAC7B,CAAC;AAEF;;;GAGG;AACH,8BAAsB,OAAQ,SAAQ,SAAS;IAC3C;;OAEG;IACH,SAAS,CAAC,mBAAmB,EAAE,MAAM,CAAC,MAAM,EAAE,GAAG,CAAC,CAAM;IAExD;;OAEG;IACH,IAAW,kBAAkB,IAAI,MAAM,CAAC,MAAM,EAAE,GAAG,CAAC,CAEnD;IAED;;;;;OAKG;IACI,q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,SAAS,GAC9B,UAAU;IAIb;;;OAGG;IACH,SAAS,CAAC,mBAAmB,EAAE,mBAAmB,GAAG,IAAI,CAAQ;IAEjE;;;OAGG;IACH,SAAS,CAAC,sBAAsB,IAAI,OAAO;IAI3C;;;OAGG;IACH,SAAS,CAAC,oBAAoB,IAAI;QAAE,IAAI,EAAE,MAAM,CAAC;QAAC,KAAK,EAAE,MAAM,CAAA;KAAE;IAIjE;;;OAGG;IACH,SAAS,CAAC,0BAA0B,CAAC,OAAO,EAAE,MAAM,GAAG;QACnD,OAAO,EAAE,MAAM,CAAC;QAChB,QAAQ,CAAC,EAAE,MAAM,CAAC;KACrB;IA8BD;;OAEG;IACH,SAAS,CAAC,6BAA6B,IAAI,IAAI;IAS/C;;OAEG;IACH,SAAS,CAAC,wBAAwB,IAAI,IAAI;IAI1C;;;OAGG;IACH,SAAS,CAAC,8BAA8B,CAAC,UAAU,EAAE,MAAM,GAAG,MAAM;IAkEpE;;OAEG;IACH,SAAS,CAAC,oBAAoB,CAC1B,OAAO,EAAE,qBAAqB,EAC9B,eAAe,CAAC,EAAE,MAAM,GACzB,qBAAqB;CAM3B"}
|
package/dist/generic/baseLLM.js
CHANGED
|
@@ -1,59 +1,105 @@
|
|
|
1
|
-
|
|
2
|
-
|
|
3
|
-
|
|
4
|
-
|
|
5
|
-
|
|
6
|
-
|
|
7
|
-
|
|
1
|
+
import { BaseModel } from "./baseModel.js";
|
|
2
|
+
import { ChatResult } from "./chat.types.js";
|
|
3
|
+
import { ErrorAnalyzer } from "./errorAnalyzer.js";
|
|
4
|
+
/**
|
|
5
|
+
* Base class for all LLM sub-class implementations. Not all sub-classes will support all methods.
|
|
6
|
+
* If a method is not supported an exception will be thrown.
|
|
7
|
+
*/
|
|
8
|
+
export class BaseLLM extends BaseModel {
|
|
8
9
|
constructor() {
|
|
9
10
|
super(...arguments);
|
|
11
|
+
/**
|
|
12
|
+
* Protected property to store additional provider-specific settings
|
|
13
|
+
*/
|
|
10
14
|
this._additionalSettings = {};
|
|
15
|
+
// ============ Thinking Model Support Helper Methods ============
|
|
16
|
+
/**
|
|
17
|
+
* State tracking for streaming thinking extraction
|
|
18
|
+
* Providers should initialize this if they support thinking models
|
|
19
|
+
*/
|
|
11
20
|
this.thinkingStreamState = null;
|
|
12
21
|
}
|
|
22
|
+
/**
|
|
23
|
+
* Get the current additional settings
|
|
24
|
+
*/
|
|
13
25
|
get AdditionalSettings() {
|
|
14
26
|
return this._additionalSettings;
|
|
15
27
|
}
|
|
28
|
+
/**
|
|
29
|
+
* Set additional provider-specific settings
|
|
30
|
+
* Subclasses should override this method to validate required settings
|
|
31
|
+
*
|
|
32
|
+
* @param settings Provider-specific settings
|
|
33
|
+
*/
|
|
16
34
|
SetAdditionalSettings(settings) {
|
|
17
35
|
this._additionalSettings = { ...this._additionalSettings, ...settings };
|
|
18
36
|
}
|
|
37
|
+
/**
|
|
38
|
+
* Clear all additional settings
|
|
39
|
+
* This is useful for resetting the state of the provider
|
|
40
|
+
* or when switching between different configurations.
|
|
41
|
+
*/
|
|
19
42
|
ClearAdditionalSettings() {
|
|
20
43
|
this._additionalSettings = {};
|
|
21
44
|
}
|
|
45
|
+
/**
|
|
46
|
+
* Process a chat completion request. If streaming is enabled and supported,
|
|
47
|
+
* this will route to the streaming implementation.
|
|
48
|
+
*/
|
|
22
49
|
async ChatCompletion(params) {
|
|
23
50
|
if (params.enableCaching === undefined) {
|
|
24
|
-
params.enableCaching = true;
|
|
51
|
+
params.enableCaching = true; // default to true
|
|
25
52
|
}
|
|
53
|
+
// Check if streaming is requested and if we support it
|
|
26
54
|
if (params.streaming && params.streamingCallbacks && this.SupportsStreaming) {
|
|
27
55
|
return this.handleStreamingChatCompletion(params);
|
|
28
56
|
}
|
|
57
|
+
// Continue with normal non-streaming implementation
|
|
29
58
|
return this.nonStreamingChatCompletion(params);
|
|
30
59
|
}
|
|
60
|
+
/**
|
|
61
|
+
* Process multiple chat completion requests in parallel. This is useful for:
|
|
62
|
+
* - Generating multiple variations with different parameters (temperature, etc.)
|
|
63
|
+
* - Getting multiple responses to compare or select from
|
|
64
|
+
* - Improving reliability by sending the same request multiple times
|
|
65
|
+
*
|
|
66
|
+
* @param paramsArray Array of chat completion parameter objects
|
|
67
|
+
* @param callbacks Optional callbacks for progress and individual completions
|
|
68
|
+
* @returns Promise resolving to an array of ChatResults in the same order as the input params
|
|
69
|
+
*/
|
|
31
70
|
async ChatCompletions(paramsArray, callbacks) {
|
|
32
71
|
if (!paramsArray || paramsArray.length === 0) {
|
|
33
72
|
return [];
|
|
34
73
|
}
|
|
74
|
+
// Create promises for each completion
|
|
35
75
|
const promises = paramsArray.map((params, index) => this.ChatCompletion(params)
|
|
36
76
|
.then(response => {
|
|
77
|
+
// Call the OnCompletion callback if provided
|
|
37
78
|
if (callbacks?.OnCompletion) {
|
|
38
79
|
callbacks.OnCompletion(response, index);
|
|
39
80
|
}
|
|
40
81
|
return response;
|
|
41
82
|
})
|
|
42
83
|
.catch(error => {
|
|
84
|
+
// Call the OnError callback if provided
|
|
43
85
|
if (callbacks?.OnError) {
|
|
44
86
|
callbacks.OnError(error, index);
|
|
45
87
|
}
|
|
46
|
-
throw error;
|
|
88
|
+
throw error; // Re-throw to be caught by Promise.allSettled
|
|
47
89
|
}));
|
|
90
|
+
// Use Promise.allSettled to get results even if some fail
|
|
48
91
|
const results = await Promise.allSettled(promises);
|
|
92
|
+
// Convert the settled results to an array of ChatResults
|
|
93
|
+
// For rejected promises, create error ChatResults
|
|
49
94
|
const chatResults = results.map((result, index) => {
|
|
50
95
|
if (result.status === 'fulfilled') {
|
|
51
96
|
return result.value;
|
|
52
97
|
}
|
|
53
98
|
else {
|
|
99
|
+
// Create an error result
|
|
54
100
|
const startTime = new Date();
|
|
55
101
|
const endTime = new Date();
|
|
56
|
-
const errorResult = new
|
|
102
|
+
const errorResult = new ChatResult(false, startTime, endTime);
|
|
57
103
|
errorResult.data = {
|
|
58
104
|
choices: [],
|
|
59
105
|
usage: {
|
|
@@ -65,24 +111,37 @@ class BaseLLM extends baseModel_1.BaseModel {
|
|
|
65
111
|
errorResult.statusText = 'error';
|
|
66
112
|
errorResult.errorMessage = result.reason?.message || 'Unknown error';
|
|
67
113
|
errorResult.exception = { exception: result.reason };
|
|
68
|
-
errorResult.errorInfo =
|
|
114
|
+
errorResult.errorInfo = ErrorAnalyzer.analyzeError(result.reason, this.constructor.name);
|
|
69
115
|
return errorResult;
|
|
70
116
|
}
|
|
71
117
|
});
|
|
118
|
+
// Call the OnAllCompleted callback if provided
|
|
72
119
|
if (callbacks?.OnAllCompleted) {
|
|
73
120
|
callbacks.OnAllCompleted(chatResults);
|
|
74
121
|
}
|
|
75
122
|
return chatResults;
|
|
76
123
|
}
|
|
124
|
+
/**
|
|
125
|
+
* Check if this provider supports streaming
|
|
126
|
+
* @returns true if streaming is supported, false otherwise
|
|
127
|
+
*/
|
|
77
128
|
get SupportsStreaming() {
|
|
129
|
+
// Default to false, providers that support streaming should override
|
|
78
130
|
return false;
|
|
79
131
|
}
|
|
132
|
+
/**
|
|
133
|
+
* Template method for handling streaming chat completion
|
|
134
|
+
* This implements the common pattern across providers while delegating
|
|
135
|
+
* provider-specific logic to abstract methods.
|
|
136
|
+
*/
|
|
80
137
|
async handleStreamingChatCompletion(params) {
|
|
81
138
|
const startTime = new Date();
|
|
82
139
|
return new Promise((resolve, reject) => {
|
|
83
140
|
(async () => {
|
|
84
141
|
try {
|
|
142
|
+
// Get provider-specific stream
|
|
85
143
|
const stream = await this.createStreamingRequest(params);
|
|
144
|
+
// Track accumulated response for final result
|
|
86
145
|
let accumulatedContent = '';
|
|
87
146
|
let lastChunk = null;
|
|
88
147
|
let usage = {
|
|
@@ -90,9 +149,11 @@ class BaseLLM extends baseModel_1.BaseModel {
|
|
|
90
149
|
completionTokens: 0,
|
|
91
150
|
totalTokens: 0
|
|
92
151
|
};
|
|
152
|
+
// Guard against null or undefined stream
|
|
93
153
|
if (!stream) {
|
|
94
154
|
throw new Error("Stream is null or undefined");
|
|
95
155
|
}
|
|
156
|
+
// Process each chunk using provider-specific implementation
|
|
96
157
|
try {
|
|
97
158
|
for await (const chunk of stream) {
|
|
98
159
|
if (chunk) {
|
|
@@ -104,6 +165,7 @@ class BaseLLM extends baseModel_1.BaseModel {
|
|
|
104
165
|
}
|
|
105
166
|
}
|
|
106
167
|
lastChunk = chunk;
|
|
168
|
+
// Update usage if available
|
|
107
169
|
if (processed?.usage) {
|
|
108
170
|
usage = processed.usage;
|
|
109
171
|
}
|
|
@@ -111,18 +173,26 @@ class BaseLLM extends baseModel_1.BaseModel {
|
|
|
111
173
|
}
|
|
112
174
|
}
|
|
113
175
|
catch (streamError) {
|
|
176
|
+
// If there's an error in the for-await loop, log and continue
|
|
114
177
|
console.error("Error processing stream chunks:", streamError);
|
|
115
178
|
}
|
|
179
|
+
// Stream complete, call OnContent one last time with isComplete=true
|
|
116
180
|
if (params.streamingCallbacks?.OnContent) {
|
|
117
181
|
params.streamingCallbacks.OnContent('', true);
|
|
118
182
|
}
|
|
183
|
+
// Create final result object using provider-specific implementation
|
|
119
184
|
const endTime = new Date();
|
|
120
185
|
const result = this.finalizeStreamingResponse(accumulatedContent, lastChunk, usage);
|
|
186
|
+
// Guard against null result
|
|
121
187
|
if (!result) {
|
|
122
188
|
throw new Error("Failed to create result");
|
|
123
189
|
}
|
|
190
|
+
// Override timestamps - the provider implementation is responsible for
|
|
191
|
+
// properly constructing ChatResult with the required constructor arguments
|
|
124
192
|
result.startTime = startTime;
|
|
125
193
|
result.endTime = endTime;
|
|
194
|
+
// timeElapsed is a getter that computes based on startTime and endTime
|
|
195
|
+
// Call OnComplete with final result
|
|
126
196
|
if (params.streamingCallbacks?.OnComplete) {
|
|
127
197
|
params.streamingCallbacks.OnComplete(result);
|
|
128
198
|
}
|
|
@@ -133,7 +203,8 @@ class BaseLLM extends baseModel_1.BaseModel {
|
|
|
133
203
|
params.streamingCallbacks.OnError(error);
|
|
134
204
|
}
|
|
135
205
|
const endTime = new Date();
|
|
136
|
-
|
|
206
|
+
// Create a proper ChatResult by extending BaseResult
|
|
207
|
+
const errorResult = new ChatResult(false, startTime, endTime);
|
|
137
208
|
errorResult.data = {
|
|
138
209
|
choices: [],
|
|
139
210
|
usage: {
|
|
@@ -145,18 +216,30 @@ class BaseLLM extends baseModel_1.BaseModel {
|
|
|
145
216
|
errorResult.statusText = 'error';
|
|
146
217
|
errorResult.errorMessage = error?.message || 'Unknown error';
|
|
147
218
|
errorResult.exception = { exception: error };
|
|
148
|
-
errorResult.errorInfo =
|
|
219
|
+
errorResult.errorInfo = ErrorAnalyzer.analyzeError(error, this.constructor.name);
|
|
149
220
|
reject(errorResult);
|
|
150
221
|
}
|
|
151
222
|
})();
|
|
152
223
|
});
|
|
153
224
|
}
|
|
225
|
+
/**
|
|
226
|
+
* Check if the provider supports thinking models
|
|
227
|
+
* Providers should override this to return true if they support thinking extraction
|
|
228
|
+
*/
|
|
154
229
|
supportsThinkingModels() {
|
|
155
230
|
return false;
|
|
156
231
|
}
|
|
232
|
+
/**
|
|
233
|
+
* Get the thinking tag format for this provider
|
|
234
|
+
* Providers can override this to customize the thinking tag format
|
|
235
|
+
*/
|
|
157
236
|
getThinkingTagFormat() {
|
|
158
237
|
return { open: '<think>', close: '</think>' };
|
|
159
238
|
}
|
|
239
|
+
/**
|
|
240
|
+
* Extract thinking content from non-streaming content
|
|
241
|
+
* This method handles case-insensitive extraction of thinking blocks
|
|
242
|
+
*/
|
|
160
243
|
extractThinkingFromContent(content) {
|
|
161
244
|
if (!content)
|
|
162
245
|
return {
|
|
@@ -169,7 +252,9 @@ class BaseLLM extends baseModel_1.BaseModel {
|
|
|
169
252
|
const openTagLower = tags.open.toLowerCase();
|
|
170
253
|
const closeTagLower = tags.close.toLowerCase();
|
|
171
254
|
const contentLower = processedContent.toLowerCase();
|
|
255
|
+
// Check if content starts with thinking tag (case-insensitive)
|
|
172
256
|
if (contentLower.startsWith(openTagLower) && contentLower.includes(closeTagLower)) {
|
|
257
|
+
// Find the actual positions in the original content
|
|
173
258
|
const thinkStart = tags.open.length;
|
|
174
259
|
const thinkEndIndex = contentLower.indexOf(closeTagLower);
|
|
175
260
|
if (thinkEndIndex !== -1) {
|
|
@@ -179,6 +264,9 @@ class BaseLLM extends baseModel_1.BaseModel {
|
|
|
179
264
|
}
|
|
180
265
|
return { content: processedContent, thinking: thinkingContent };
|
|
181
266
|
}
|
|
267
|
+
/**
|
|
268
|
+
* Initialize thinking stream state for streaming extraction
|
|
269
|
+
*/
|
|
182
270
|
initializeThinkingStreamState() {
|
|
183
271
|
this.thinkingStreamState = {
|
|
184
272
|
accumulatedThinking: '',
|
|
@@ -187,15 +275,24 @@ class BaseLLM extends baseModel_1.BaseModel {
|
|
|
187
275
|
thinkingComplete: false
|
|
188
276
|
};
|
|
189
277
|
}
|
|
278
|
+
/**
|
|
279
|
+
* Reset thinking stream state
|
|
280
|
+
*/
|
|
190
281
|
resetThinkingStreamState() {
|
|
191
282
|
this.thinkingStreamState = null;
|
|
192
283
|
}
|
|
284
|
+
/**
|
|
285
|
+
* Process streaming chunk with thinking extraction
|
|
286
|
+
* This method handles case-insensitive extraction across chunk boundaries
|
|
287
|
+
*/
|
|
193
288
|
processStreamChunkWithThinking(rawContent) {
|
|
194
289
|
if (!this.thinkingStreamState) {
|
|
290
|
+
// If thinking extraction is not enabled, return content as-is
|
|
195
291
|
return rawContent;
|
|
196
292
|
}
|
|
197
293
|
let contentToEmit = '';
|
|
198
294
|
this.thinkingStreamState.pendingContent += rawContent;
|
|
295
|
+
// If thinking is already complete, just return the pending content
|
|
199
296
|
if (this.thinkingStreamState.thinkingComplete) {
|
|
200
297
|
contentToEmit = this.thinkingStreamState.pendingContent;
|
|
201
298
|
this.thinkingStreamState.pendingContent = '';
|
|
@@ -204,36 +301,48 @@ class BaseLLM extends baseModel_1.BaseModel {
|
|
|
204
301
|
const tags = this.getThinkingTagFormat();
|
|
205
302
|
const pending = this.thinkingStreamState.pendingContent;
|
|
206
303
|
const pendingLower = pending.toLowerCase();
|
|
304
|
+
// Check if we're starting a thinking block (case-insensitive)
|
|
207
305
|
if (!this.thinkingStreamState.inThinkingBlock && pendingLower.includes(tags.open.toLowerCase())) {
|
|
208
306
|
const thinkStartIndex = pendingLower.indexOf(tags.open.toLowerCase());
|
|
307
|
+
// If <think> is not at the start, emit content before it
|
|
209
308
|
if (thinkStartIndex > 0) {
|
|
210
309
|
contentToEmit = pending.substring(0, thinkStartIndex);
|
|
211
310
|
this.thinkingStreamState.pendingContent = pending.substring(thinkStartIndex);
|
|
212
311
|
return contentToEmit;
|
|
213
312
|
}
|
|
313
|
+
// We're now in a thinking block
|
|
214
314
|
this.thinkingStreamState.inThinkingBlock = true;
|
|
315
|
+
// Remove the opening tag
|
|
215
316
|
this.thinkingStreamState.pendingContent = pending.substring(tags.open.length);
|
|
216
317
|
}
|
|
318
|
+
// If we're in a thinking block, look for the end (case-insensitive)
|
|
217
319
|
if (this.thinkingStreamState.inThinkingBlock) {
|
|
218
320
|
const pendingInBlockLower = this.thinkingStreamState.pendingContent.toLowerCase();
|
|
219
321
|
const thinkEndIndex = pendingInBlockLower.indexOf(tags.close.toLowerCase());
|
|
220
322
|
if (thinkEndIndex !== -1) {
|
|
323
|
+
// Found the end of thinking block
|
|
221
324
|
this.thinkingStreamState.accumulatedThinking += this.thinkingStreamState.pendingContent.substring(0, thinkEndIndex);
|
|
222
325
|
this.thinkingStreamState.pendingContent = this.thinkingStreamState.pendingContent.substring(thinkEndIndex + tags.close.length);
|
|
223
326
|
this.thinkingStreamState.inThinkingBlock = false;
|
|
224
327
|
this.thinkingStreamState.thinkingComplete = true;
|
|
328
|
+
// Process any remaining content recursively
|
|
225
329
|
return this.processStreamChunkWithThinking('');
|
|
226
330
|
}
|
|
227
331
|
else {
|
|
332
|
+
// Still accumulating thinking content
|
|
228
333
|
this.thinkingStreamState.accumulatedThinking += this.thinkingStreamState.pendingContent;
|
|
229
334
|
this.thinkingStreamState.pendingContent = '';
|
|
230
335
|
return '';
|
|
231
336
|
}
|
|
232
337
|
}
|
|
338
|
+
// Not in thinking block and no thinking tags found
|
|
233
339
|
contentToEmit = this.thinkingStreamState.pendingContent;
|
|
234
340
|
this.thinkingStreamState.pendingContent = '';
|
|
235
341
|
return contentToEmit;
|
|
236
342
|
}
|
|
343
|
+
/**
|
|
344
|
+
* Add thinking content to a chat completion message
|
|
345
|
+
*/
|
|
237
346
|
addThinkingToMessage(message, thinkingContent) {
|
|
238
347
|
if (thinkingContent) {
|
|
239
348
|
message.thinking = thinkingContent;
|
|
@@ -241,5 +350,4 @@ class BaseLLM extends baseModel_1.BaseModel {
|
|
|
241
350
|
return message;
|
|
242
351
|
}
|
|
243
352
|
}
|
|
244
|
-
exports.BaseLLM = BaseLLM;
|
|
245
353
|
//# sourceMappingURL=baseLLM.js.map
|
|
@@ -1 +1 @@
|
|
|
1
|
-
{"version":3,"file":"baseLLM.js","sourceRoot":"","sources":["../../src/generic/baseLLM.ts"],"names":[],"mappings":"
|
|
1
|
+
{"version":3,"file":"baseLLM.js","sourceRoot":"","sources":["../../src/generic/baseLLM.ts"],"names":[],"mappings":"AACA,OAAO,EAAE,SAAS,EAAc,MAAM,aAAa,CAAC;AACpD,OAAO,EAAc,UAAU,EAAmF,MAAM,cAAc,CAAC;AAEvI,OAAO,EAAE,aAAa,EAAE,MAAM,iBAAiB,CAAC;AAYhD;;;GAGG;AACH,MAAM,OAAgB,OAAQ,SAAQ,SAAS;IAA/C;;QACI;;WAEG;QACO,wBAAmB,GAAwB,EAAE,CAAC;QAyRxD,kEAAkE;QAElE;;;WAGG;QACO,wBAAmB,GAA+B,IAAI,CAAC;IA4JrE,CAAC;IAzbG;;OAEG;IACH,IAAW,kBAAkB;QACzB,OAAO,IAAI,CAAC,mBAAmB,CAAC;IACpC,CAAC;IAED;;;;;OAKG;IACI,qBAAqB,CAAC,QAA6B;QACtD,IAAI,CAAC,mBAAmB,GAAG,EAAC,GAAG,IAAI,CAAC,mBAAmB,EAAE,GAAG,QAAQ,EAAC,CAAC;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ClB,OAAO,CAAC,QAAQ,GAAG,eAAe,CAAC;QACvC,CAAC;QACD,OAAO,OAAO,CAAC;IACnB,CAAC;CACJ"}
|
|
@@ -5,33 +5,124 @@ export declare class BaseResult {
|
|
|
5
5
|
endTime: Date;
|
|
6
6
|
errorMessage: string;
|
|
7
7
|
exception: any;
|
|
8
|
+
/**
|
|
9
|
+
* Structured error information for better error handling and retry logic
|
|
10
|
+
*/
|
|
8
11
|
errorInfo?: AIErrorInfo;
|
|
9
12
|
get timeElapsed(): number;
|
|
10
13
|
constructor(success: boolean, startTime: Date, endTime: Date);
|
|
11
14
|
}
|
|
12
15
|
export declare class BaseParams {
|
|
16
|
+
/**
|
|
17
|
+
* Model name, required.
|
|
18
|
+
*/
|
|
13
19
|
model: string;
|
|
20
|
+
/**
|
|
21
|
+
* Model temperature, optional.
|
|
22
|
+
*/
|
|
14
23
|
temperature?: number;
|
|
24
|
+
/**
|
|
25
|
+
* Specifies the format that the model should output. Not all models support all formats. If not specified, the default is 'Any'.
|
|
26
|
+
*/
|
|
15
27
|
responseFormat?: 'Any' | 'Text' | 'Markdown' | 'JSON' | 'ModelSpecific';
|
|
28
|
+
/**
|
|
29
|
+
* The standard response formats may not be sufficient for all models. This field allows for a model-specific response format to be specified. For this field to be used, responseFormat must be set to 'ModelSpecific'.
|
|
30
|
+
*/
|
|
16
31
|
modelSpecificResponseFormat?: any;
|
|
32
|
+
/**
|
|
33
|
+
* Model max output response tokens, optional.
|
|
34
|
+
*/
|
|
17
35
|
maxOutputTokens?: number;
|
|
36
|
+
/**
|
|
37
|
+
* Model max budget tokens that we may use for reasoning in reasoning models, optional.
|
|
38
|
+
*/
|
|
18
39
|
reasoningBudgetTokens?: number;
|
|
40
|
+
/**
|
|
41
|
+
* Optional seed for reproducible outputs.
|
|
42
|
+
* Not all models support seeding, but when supported, using the same seed
|
|
43
|
+
* with the same inputs should produce identical outputs.
|
|
44
|
+
*/
|
|
19
45
|
seed?: number;
|
|
46
|
+
/**
|
|
47
|
+
* Optional array of sequences where the model will stop generating further tokens.
|
|
48
|
+
* The returned text will not contain the stop sequence.
|
|
49
|
+
*/
|
|
20
50
|
stopSequences?: string[];
|
|
21
51
|
}
|
|
52
|
+
/**
|
|
53
|
+
* Represents token usage and cost information for an AI model execution.
|
|
54
|
+
*
|
|
55
|
+
* This class tracks the number of tokens used in both the prompt (input) and
|
|
56
|
+
* completion (output) phases of an AI model execution, along with optional
|
|
57
|
+
* cost information when provided by the AI provider.
|
|
58
|
+
*
|
|
59
|
+
* @class ModelUsage
|
|
60
|
+
* @since 2.43.0
|
|
61
|
+
*/
|
|
22
62
|
export declare class ModelUsage {
|
|
63
|
+
/**
|
|
64
|
+
* Creates a new ModelUsage instance.
|
|
65
|
+
*
|
|
66
|
+
* @param {number} promptTokens - Number of tokens used in the prompt/input
|
|
67
|
+
* @param {number} completionTokens - Number of tokens generated in the completion/output
|
|
68
|
+
* @param {number} [cost] - Optional cost of the execution
|
|
69
|
+
* @param {string} [costCurrency] - Optional currency code for the cost (e.g., 'USD', 'EUR', 'GBP')
|
|
70
|
+
*/
|
|
23
71
|
constructor(promptTokens: number, completionTokens: number, cost?: number, costCurrency?: string);
|
|
72
|
+
/**
|
|
73
|
+
* Number of tokens used in the prompt/input phase.
|
|
74
|
+
* This includes all tokens from system messages, user messages, and any
|
|
75
|
+
* other context provided to the model.
|
|
76
|
+
*/
|
|
24
77
|
promptTokens: number;
|
|
78
|
+
/**
|
|
79
|
+
* Number of tokens generated by the model in its response.
|
|
80
|
+
* This represents the length of the model's output.
|
|
81
|
+
*/
|
|
25
82
|
completionTokens: number;
|
|
83
|
+
/**
|
|
84
|
+
* Optional cost of this execution.
|
|
85
|
+
* The currency is specified in the costCurrency field.
|
|
86
|
+
* Some providers (like Anthropic) provide this information directly in their API responses.
|
|
87
|
+
*/
|
|
26
88
|
cost?: number;
|
|
89
|
+
/**
|
|
90
|
+
* Optional ISO 4217 currency code for the cost field.
|
|
91
|
+
* Examples: 'USD', 'EUR', 'GBP', 'JPY', etc.
|
|
92
|
+
* If not specified when cost is provided, the currency is provider-specific.
|
|
93
|
+
*/
|
|
27
94
|
costCurrency?: string;
|
|
95
|
+
/**
|
|
96
|
+
* Optional queue time in milliseconds before the model started processing the request.
|
|
97
|
+
* This is a provider-specific timing metric that may not be available from all providers.
|
|
98
|
+
*/
|
|
28
99
|
queueTime?: number;
|
|
100
|
+
/**
|
|
101
|
+
* Optional time in milliseconds for the model to ingest and process the prompt.
|
|
102
|
+
* This is a provider-specific timing metric that may not be available from all providers.
|
|
103
|
+
*/
|
|
29
104
|
promptTime?: number;
|
|
105
|
+
/**
|
|
106
|
+
* Optional time in milliseconds for the model to generate the completion/response tokens.
|
|
107
|
+
* This is a provider-specific timing metric that may not be available from all providers.
|
|
108
|
+
*/
|
|
30
109
|
completionTime?: number;
|
|
110
|
+
/**
|
|
111
|
+
* Calculated total number of tokens (prompt + completion).
|
|
112
|
+
* This is useful for tracking overall token usage against limits.
|
|
113
|
+
*
|
|
114
|
+
* @returns {number} The sum of promptTokens and completionTokens
|
|
115
|
+
*/
|
|
31
116
|
get totalTokens(): number;
|
|
32
117
|
}
|
|
118
|
+
/**
|
|
119
|
+
* Base AI model class, used for everything else in the MemberJunction AI environment
|
|
120
|
+
*/
|
|
33
121
|
export declare abstract class BaseModel {
|
|
34
122
|
private _apiKey;
|
|
123
|
+
/**
|
|
124
|
+
* Only sub-classes can access the API key
|
|
125
|
+
*/
|
|
35
126
|
protected get apiKey(): string;
|
|
36
127
|
constructor(apiKey: string);
|
|
37
128
|
}
|