@memberjunction/ai-mistral 2.32.2 → 2.34.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.
@@ -4,7 +4,30 @@ export declare class MistralLLM extends BaseLLM {
4
4
  private _client;
5
5
  constructor(apiKey: string);
6
6
  get Client(): Mistral;
7
- ChatCompletion(params: ChatParams): Promise<ChatResult>;
7
+ /**
8
+ * Mistral supports streaming
9
+ */
10
+ get SupportsStreaming(): boolean;
11
+ /**
12
+ * Implementation of non-streaming chat completion for Mistral
13
+ */
14
+ protected nonStreamingChatCompletion(params: ChatParams): Promise<ChatResult>;
15
+ /**
16
+ * Create a streaming request for Mistral
17
+ */
18
+ protected createStreamingRequest(params: ChatParams): Promise<any>;
19
+ /**
20
+ * Process a streaming chunk from Mistral
21
+ */
22
+ protected processStreamingChunk(chunk: any): {
23
+ content: string;
24
+ finishReason?: string;
25
+ usage?: any;
26
+ };
27
+ /**
28
+ * Create the final response from streaming results for Mistral
29
+ */
30
+ protected finalizeStreamingResponse(accumulatedContent: string | null | undefined, lastChunk: any | null | undefined, usage: any | null | undefined): ChatResult;
8
31
  SummarizeText(params: SummarizeParams): Promise<SummarizeResult>;
9
32
  ClassifyText(params: ClassifyParams): Promise<ClassifyResult>;
10
33
  }
@@ -1 +1 @@
1
- {"version":3,"file":"mistral.d.ts","sourceRoot":"","sources":["../../src/models/mistral.ts"],"names":[],"mappings":"AAAA,OAAO,EAAE,OAAO,EAAE,UAAU,EAAE,UAAU,EAAoB,cAAc,EAAE,cAAc,EAAE,eAAe,EAAE,eAAe,EAAE,MAAM,oBAAoB,CAAC;AAEzJ,OAAO,EAAE,OAAO,EAAE,MAAM,sBAAsB,CAAC;AAG/C,qBACa,UAAW,SAAQ,OAAO;IACnC,OAAO,CAAC,OAAO,CAAU;gBAEb,MAAM,EAAE,MAAM;IAO1B,IAAW,MAAM,IAAI,OAAO,CAAuB;IAEtC,cAAc,CAAC,MAAM,EAAE,UAAU,G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e,CAAC;IAIhE,YAAY,CAAC,MAAM,EAAE,cAAc,GAAG,OAAO,CAAC,cAAc,CAAC;CAG7E;AAED,wBAAgB,cAAc,SAE7B"}
@@ -18,7 +18,16 @@ let MistralLLM = class MistralLLM extends ai_1.BaseLLM {
18
18
  });
19
19
  }
20
20
  get Client() { return this._client; }
21
- async ChatCompletion(params) {
21
+ /**
22
+ * Mistral supports streaming
23
+ */
24
+ get SupportsStreaming() {
25
+ return true;
26
+ }
27
+ /**
28
+ * Implementation of non-streaming chat completion for Mistral
29
+ */
30
+ async nonStreamingChatCompletion(params) {
22
31
  const startTime = new Date();
23
32
  let responseFormat = undefined;
24
33
  if (params.responseFormat) {
@@ -69,6 +78,82 @@ let MistralLLM = class MistralLLM extends ai_1.BaseLLM {
69
78
  exception: null,
70
79
  };
71
80
  }
81
+ /**
82
+ * Create a streaming request for Mistral
83
+ */
84
+ async createStreamingRequest(params) {
85
+ let responseFormat = undefined;
86
+ if (params.responseFormat === 'JSON') {
87
+ responseFormat = { type: "json_object" };
88
+ }
89
+ return this.Client.chat.stream({
90
+ model: params.model,
91
+ messages: params.messages,
92
+ maxTokens: params.maxOutputTokens,
93
+ responseFormat: responseFormat
94
+ });
95
+ }
96
+ /**
97
+ * Process a streaming chunk from Mistral
98
+ */
99
+ processStreamingChunk(chunk) {
100
+ let content = '';
101
+ let usage = null;
102
+ if (chunk?.data?.choices && chunk.data.choices.length > 0) {
103
+ const choice = chunk.data.choices[0];
104
+ // Extract content from the choice delta
105
+ if (choice?.delta?.content) {
106
+ if (typeof choice.delta.content === 'string') {
107
+ content = choice.delta.content;
108
+ }
109
+ else if (Array.isArray(choice.delta.content)) {
110
+ content = choice.delta.content.join('');
111
+ }
112
+ }
113
+ // Save usage information if available
114
+ if (chunk?.data?.usage) {
115
+ usage = {
116
+ promptTokens: chunk.data.usage.promptTokens || 0,
117
+ completionTokens: chunk.data.usage.completionTokens || 0,
118
+ totalTokens: chunk.data.usage.totalTokens || 0
119
+ };
120
+ }
121
+ }
122
+ return {
123
+ content,
124
+ finishReason: chunk?.data?.choices?.[0]?.finishReason,
125
+ usage
126
+ };
127
+ }
128
+ /**
129
+ * Create the final response from streaming results for Mistral
130
+ */
131
+ finalizeStreamingResponse(accumulatedContent, lastChunk, usage) {
132
+ // Create dates (will be overridden by base class)
133
+ const now = new Date();
134
+ // Create a proper ChatResult instance with constructor params
135
+ const result = new ai_1.ChatResult(true, now, now);
136
+ // Set all properties
137
+ result.data = {
138
+ choices: [{
139
+ message: {
140
+ role: 'assistant',
141
+ content: accumulatedContent ? accumulatedContent : ''
142
+ },
143
+ finish_reason: lastChunk?.data?.choices?.[0]?.finishReason || 'stop',
144
+ index: 0
145
+ }],
146
+ usage: usage || {
147
+ promptTokens: 0,
148
+ completionTokens: 0,
149
+ totalTokens: 0
150
+ }
151
+ };
152
+ result.statusText = 'success';
153
+ result.errorMessage = null;
154
+ result.exception = null;
155
+ return result;
156
+ }
72
157
  async SummarizeText(params) {
73
158
  throw new Error("Method not implemented.");
74
159
  }
@@ -1 +1 @@
1
- {"version":3,"file":"mistral.js","sourceRoot":"","sources":["../../src/models/mistral.ts"],"names":[],"mappings":";;;;;;;;;AAAA,2CAAyJ;AACzJ,mDAAuD;AACvD,oDAA+C;AAIxC,IAAM,UAAU,GAAhB,MAAM,UAAW,SAAQ,YAAO;IAGnC,YAAY,MAAc;QACtB,KAAK,CAAC,MAAM,CAAC,CAAC;QACd,IAAI,CAAC,OAAO,GAAG,IAAI,mBAAO,CAAC;YACvB,MAAM,EAAE,MAAM;SACjB,CAAC,CAAC;IACP,CAAC;IAED,IAAW,MAAM,KAAa,OAAO,IAAI,CAAC,OAAO,CAAC,CAAA,CAAC;IAE5C,KAAK,CAAC,cAAc,CAAC,MAAkB;QAC1C,MAAM,SAAS,GAAG,IAAI,IAAI,EAAE,CAAC;QAE7B,IAAI,cAAc,GAA+B,SAAS,CAAC;QAC3D,IAAI,MAAM,CAAC,cAAc,EAAE,CAAC;YACxB,IAAG,MAA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QAE9C,qBAAqB;QACrB,MAAM,CAAC,IAAI,GAAG;YACV,OAAO,EAAE,CAAC;oBACN,OAAO,EAAE;wBACL,IAAI,EAAE,WAAW;wBACjB,OAAO,EAAE,kBAAkB,CAAC,CAAC,CAAC,kBAAkB,CAAC,CAAC,CAAC,EAAE;qBACxD;oBACD,aAAa,EAAE,SAAS,EAAE,IAAI,EAAE,OAAO,EAAE,CAAC,CAAC,CAAC,EAAE,YAAY,IAAI,MAAM;oBACpE,KAAK,EAAE,CAAC;iBACX,CAAC;YACF,KAAK,EAAE,KAAK,IAAI;gBACZ,YAAY,EAAE,CAAC;gBACf,gBAAgB,EAAE,CAAC;gBACnB,WAAW,EAAE,CAAC;aACjB;SACJ,CAAC;QAEF,MAAM,CAAC,UAAU,GAAG,SAAS,CAAC;QAC9B,MAAM,CAAC,YAAY,GAAG,IAAI,CAAC;QAC3B,MAAM,CAAC,SAAS,GAAG,IAAI,CAAC;QAExB,OAAO,MAAM,CAAC;IAClB,CAAC;IAEM,KAAK,CAAC,aAAa,CAAC,MAAuB;QAC9C,MAAM,IAAI,KAAK,CAAC,yBAAyB,CAAC,CAAC;IAC/C,CAAC;IAEM,KAAK,CAAC,YAAY,CAAC,MAAsB;QAC5C,MAAM,IAAI,KAAK,CAAC,yBAAyB,CAAC,CAAC;IAC/C,CAAC;CACJ,CAAA;AAvLY,gCAAU;qBAAV,UAAU;IADtB,IAAA,sBAAa,EAAC,YAAO,EAAE,YAAY,CAAC;GACxB,UAAU,CAuLtB;AAED,SAAgB,cAAc;IAC1B,iFAAiF;AACrF,CAAC;AAFD,wCAEC"}
package/package.json CHANGED
@@ -1,6 +1,6 @@
1
1
  {
2
2
  "name": "@memberjunction/ai-mistral",
3
- "version": "2.32.2",
3
+ "version": "2.34.0",
4
4
  "description": "MemberJunction Wrapper for Mistral AI's AI Models",
5
5
  "main": "dist/index.js",
6
6
  "types": "dist/index.d.ts",
@@ -19,8 +19,8 @@
19
19
  "typescript": "^5.4.5"
20
20
  },
21
21
  "dependencies": {
22
- "@memberjunction/ai": "2.32.2",
23
- "@memberjunction/global": "2.32.2",
22
+ "@memberjunction/ai": "2.34.0",
23
+ "@memberjunction/global": "2.34.0",
24
24
  "@mistralai/mistralai": "^1.5.0",
25
25
  "axios-retry": "4.3.0"
26
26
  }
package/readme.md CHANGED
@@ -1,2 +1,203 @@
1
- # @memberjunction/ai-gemini
2
- Simple wrapper class for Mistral AI's AI Models to use with the MemberJunction framework.
1
+ # @memberjunction/ai-mistral
2
+
3
+ A comprehensive wrapper for Mistral AI's models, enabling seamless integration with the MemberJunction AI framework for natural language processing tasks.
4
+
5
+ ## Features
6
+
7
+ - **Mistral AI Integration**: Connect to Mistral's powerful language models
8
+ - **Standardized Interface**: Implements MemberJunction's BaseLLM abstract class
9
+ - **Token Usage Tracking**: Automatic tracking of prompt and completion tokens
10
+ - **Response Format Control**: Support for standard text and JSON response formats
11
+ - **Error Handling**: Robust error handling with detailed reporting
12
+ - **Chat Completion**: Full support for chat-based interactions with Mistral models
13
+
14
+ ## Installation
15
+
16
+ ```bash
17
+ npm install @memberjunction/ai-mistral
18
+ ```
19
+
20
+ ## Requirements
21
+
22
+ - Node.js 16+
23
+ - A Mistral AI API key
24
+ - MemberJunction Core libraries
25
+
26
+ ## Usage
27
+
28
+ ### Basic Setup
29
+
30
+ ```typescript
31
+ import { MistralLLM } from '@memberjunction/ai-mistral';
32
+
33
+ // Initialize with your Mistral API key
34
+ const mistralLLM = new MistralLLM('your-mistral-api-key');
35
+ ```
36
+
37
+ ### Chat Completion
38
+
39
+ ```typescript
40
+ import { ChatParams } from '@memberjunction/ai';
41
+
42
+ // Create chat parameters
43
+ const chatParams: ChatParams = {
44
+ model: 'mistral-large-latest', // or other models like 'open-mistral-7b', 'mistral-small-latest'
45
+ messages: [
46
+ { role: 'system', content: 'You are a helpful assistant.' },
47
+ { role: 'user', content: 'What are the main principles of machine learning?' }
48
+ ],
49
+ temperature: 0.7,
50
+ maxOutputTokens: 1000
51
+ };
52
+
53
+ // Get a response
54
+ try {
55
+ const response = await mistralLLM.ChatCompletion(chatParams);
56
+ if (response.success) {
57
+ console.log('Response:', response.data.choices[0].message.content);
58
+ console.log('Token Usage:', response.data.usage);
59
+ console.log('Time Elapsed (ms):', response.timeElapsed);
60
+ } else {
61
+ console.error('Error:', response.errorMessage);
62
+ }
63
+ } catch (error) {
64
+ console.error('Exception:', error);
65
+ }
66
+ ```
67
+
68
+ ### JSON Response Format
69
+
70
+ ```typescript
71
+ // Request a structured JSON response
72
+ const jsonParams: ChatParams = {
73
+ model: 'mistral-large-latest',
74
+ messages: [
75
+ { role: 'system', content: 'You are a helpful assistant.' },
76
+ { role: 'user', content: 'Give me data about the top 3 machine learning algorithms in JSON format' }
77
+ ],
78
+ maxOutputTokens: 1000,
79
+ responseFormat: 'JSON' // This will add the appropriate response_format parameter
80
+ };
81
+
82
+ const jsonResponse = await mistralLLM.ChatCompletion(jsonParams);
83
+
84
+ // Parse the JSON response
85
+ if (jsonResponse.success) {
86
+ const structuredData = JSON.parse(jsonResponse.data.choices[0].message.content);
87
+ console.log('Structured Data:', structuredData);
88
+ }
89
+ ```
90
+
91
+ ### Direct Access to Mistral Client
92
+
93
+ ```typescript
94
+ // Access the underlying Mistral client for advanced usage
95
+ const mistralClient = mistralLLM.Client;
96
+
97
+ // Use the client directly if needed
98
+ const modelList = await mistralClient.listModels();
99
+ console.log('Available models:', modelList);
100
+ ```
101
+
102
+ ## Supported Models
103
+
104
+ Mistral AI offers several models with different capabilities and price points:
105
+
106
+ - `mistral-large-latest`: Mistral's most powerful model (at the time of writing)
107
+ - `mistral-medium-latest`: Mid-tier model balancing performance and cost
108
+ - `mistral-small-latest`: Smaller, more efficient model
109
+ - `open-mistral-7b`: Open-source 7B parameter model
110
+ - `open-mixtral-8x7b`: Open-source mixture-of-experts model
111
+
112
+ Check the [Mistral AI documentation](https://docs.mistral.ai/) for the latest list of supported models.
113
+
114
+ ## API Reference
115
+
116
+ ### MistralLLM Class
117
+
118
+ A class that extends BaseLLM to provide Mistral-specific functionality.
119
+
120
+ #### Constructor
121
+
122
+ ```typescript
123
+ new MistralLLM(apiKey: string)
124
+ ```
125
+
126
+ #### Properties
127
+
128
+ - `Client`: (read-only) Returns the underlying Mistral client instance
129
+
130
+ #### Methods
131
+
132
+ - `ChatCompletion(params: ChatParams): Promise<ChatResult>` - Perform a chat completion
133
+ - `SummarizeText(params: SummarizeParams): Promise<SummarizeResult>` - Not implemented yet
134
+ - `ClassifyText(params: ClassifyParams): Promise<ClassifyResult>` - Not implemented yet
135
+
136
+ ## Response Format Control
137
+
138
+ The wrapper supports different response formats:
139
+
140
+ ```typescript
141
+ // For JSON responses
142
+ const params: ChatParams = {
143
+ // ...other parameters
144
+ responseFormat: 'JSON'
145
+ };
146
+
147
+ // For regular text responses (default)
148
+ const textParams: ChatParams = {
149
+ // ...other parameters
150
+ // No need to specify responseFormat for regular text
151
+ };
152
+ ```
153
+
154
+ ## Error Handling
155
+
156
+ The wrapper provides detailed error information:
157
+
158
+ ```typescript
159
+ try {
160
+ const response = await mistralLLM.ChatCompletion(params);
161
+ if (!response.success) {
162
+ console.error('Error:', response.errorMessage);
163
+ console.error('Status:', response.statusText);
164
+ console.error('Exception:', response.exception);
165
+ }
166
+ } catch (error) {
167
+ console.error('Exception occurred:', error);
168
+ }
169
+ ```
170
+
171
+ ## Token Usage Tracking
172
+
173
+ Monitor token usage for billing and quota management:
174
+
175
+ ```typescript
176
+ const response = await mistralLLM.ChatCompletion(params);
177
+ if (response.success) {
178
+ console.log('Prompt Tokens:', response.data.usage.promptTokens);
179
+ console.log('Completion Tokens:', response.data.usage.completionTokens);
180
+ console.log('Total Tokens:', response.data.usage.totalTokens);
181
+ }
182
+ ```
183
+
184
+ ## Limitations
185
+
186
+ Currently, the wrapper implements:
187
+ - Chat completion functionality with token usage tracking
188
+
189
+ Future implementations may include:
190
+ - `SummarizeText` functionality
191
+ - `ClassifyText` functionality
192
+ - Streaming responses
193
+
194
+ ## Dependencies
195
+
196
+ - `@mistralai/mistralai`: Official Mistral AI Node.js SDK
197
+ - `@memberjunction/ai`: MemberJunction AI core framework
198
+ - `@memberjunction/global`: MemberJunction global utilities
199
+ - `axios-retry`: Retry mechanism for API calls
200
+
201
+ ## License
202
+
203
+ ISC