@memberjunction/ai-openai 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 +3 -3
- package/readme.md +197 -32
package/package.json
CHANGED
|
@@ -1,6 +1,6 @@
|
|
|
1
1
|
{
|
|
2
2
|
"name": "@memberjunction/ai-openai",
|
|
3
|
-
"version": "2.
|
|
3
|
+
"version": "2.44.0",
|
|
4
4
|
"description": "MemberJunction Wrapper for OpenAI 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.
|
|
23
|
-
"@memberjunction/global": "2.
|
|
22
|
+
"@memberjunction/ai": "2.44.0",
|
|
23
|
+
"@memberjunction/global": "2.44.0",
|
|
24
24
|
"openai": "4.98.0"
|
|
25
25
|
}
|
|
26
26
|
}
|
package/readme.md
CHANGED
|
@@ -1,17 +1,20 @@
|
|
|
1
1
|
# @memberjunction/ai-openai
|
|
2
2
|
|
|
3
|
-
A comprehensive wrapper for OpenAI's API and models that seamlessly integrates with the MemberJunction AI framework, providing a standardized interface for GPT and
|
|
3
|
+
A comprehensive wrapper for OpenAI's API and models that seamlessly integrates with the MemberJunction AI framework, providing a standardized interface for GPT, embedding, and text-to-speech models.
|
|
4
4
|
|
|
5
5
|
## Features
|
|
6
6
|
|
|
7
|
-
- **OpenAI Integration**: Full integration with OpenAI's chat completion models
|
|
8
|
-
- **Standardized Interface**: Follows MemberJunction's BaseLLM
|
|
7
|
+
- **OpenAI Integration**: Full integration with OpenAI's chat completion, embedding, and TTS models
|
|
8
|
+
- **Standardized Interface**: Follows MemberJunction's BaseLLM, BaseEmbeddings, and BaseAudioGenerator abstract classes
|
|
9
|
+
- **Streaming Support**: Full support for streaming chat completions
|
|
9
10
|
- **Message Formatting**: Handles conversion between MemberJunction and OpenAI message formats
|
|
10
|
-
- **
|
|
11
|
+
- **Multi-modal Support**: Supports text and image content in messages
|
|
12
|
+
- **Response Format Support**: Support for different response formats (Text, JSON, Markdown, ModelSpecific)
|
|
13
|
+
- **Reasoning Models**: Support for reasoning effort levels (o1 models)
|
|
11
14
|
- **Error Handling**: Comprehensive error handling with detailed reporting
|
|
12
15
|
- **Token Usage Tracking**: Automatic tracking of prompt and completion tokens
|
|
13
|
-
- **
|
|
14
|
-
- **Text
|
|
16
|
+
- **Embeddings**: Text embedding generation with multiple models
|
|
17
|
+
- **Text-to-Speech**: Generate speech from text using OpenAI's TTS models
|
|
15
18
|
|
|
16
19
|
## Installation
|
|
17
20
|
|
|
@@ -50,7 +53,8 @@ const chatParams: ChatParams = {
|
|
|
50
53
|
],
|
|
51
54
|
temperature: 0.7,
|
|
52
55
|
maxOutputTokens: 500,
|
|
53
|
-
responseFormat: 'Text'
|
|
56
|
+
responseFormat: 'Text',
|
|
57
|
+
includeLogProbs: false
|
|
54
58
|
};
|
|
55
59
|
|
|
56
60
|
// Get a response
|
|
@@ -67,13 +71,55 @@ try {
|
|
|
67
71
|
}
|
|
68
72
|
```
|
|
69
73
|
|
|
74
|
+
### Streaming Chat Completion
|
|
75
|
+
|
|
76
|
+
```typescript
|
|
77
|
+
const streamingParams: ChatParams = {
|
|
78
|
+
model: 'gpt-4',
|
|
79
|
+
messages: [
|
|
80
|
+
{ role: 'user', content: 'Tell me a story' }
|
|
81
|
+
],
|
|
82
|
+
temperature: 0.8,
|
|
83
|
+
maxOutputTokens: 1000
|
|
84
|
+
};
|
|
85
|
+
|
|
86
|
+
// Stream the response
|
|
87
|
+
await openAI.StreamingChatCompletion(streamingParams, {
|
|
88
|
+
onStart: () => console.log('Streaming started...'),
|
|
89
|
+
onContent: (content) => process.stdout.write(content),
|
|
90
|
+
onComplete: (fullContent) => console.log('\n\nComplete:', fullContent),
|
|
91
|
+
onError: (error) => console.error('Error:', error),
|
|
92
|
+
onUsage: (usage) => console.log('Token usage:', usage)
|
|
93
|
+
});
|
|
94
|
+
```
|
|
95
|
+
|
|
96
|
+
### Multi-modal Messages (Text + Images)
|
|
97
|
+
|
|
98
|
+
```typescript
|
|
99
|
+
const multiModalParams: ChatParams = {
|
|
100
|
+
model: 'gpt-4-vision-preview',
|
|
101
|
+
messages: [
|
|
102
|
+
{
|
|
103
|
+
role: 'user',
|
|
104
|
+
content: [
|
|
105
|
+
{ type: 'text', content: 'What do you see in this image?' },
|
|
106
|
+
{ type: 'image_url', content: 'https://example.com/image.jpg' }
|
|
107
|
+
]
|
|
108
|
+
}
|
|
109
|
+
],
|
|
110
|
+
maxOutputTokens: 500
|
|
111
|
+
};
|
|
112
|
+
|
|
113
|
+
const response = await openAI.ChatCompletion(multiModalParams);
|
|
114
|
+
```
|
|
115
|
+
|
|
70
116
|
### JSON Response Format
|
|
71
117
|
|
|
72
118
|
```typescript
|
|
73
119
|
const jsonParams: ChatParams = {
|
|
74
120
|
model: 'gpt-4',
|
|
75
121
|
messages: [
|
|
76
|
-
{ role: 'system', content: 'You are a helpful assistant.' },
|
|
122
|
+
{ role: 'system', content: 'You are a helpful assistant that outputs JSON.' },
|
|
77
123
|
{ role: 'user', content: 'Generate a JSON object with name, age, and city for 3 fictional people.' }
|
|
78
124
|
],
|
|
79
125
|
temperature: 0.3,
|
|
@@ -86,6 +132,21 @@ const jsonData = JSON.parse(jsonResponse.data.choices[0].message.content);
|
|
|
86
132
|
console.log('Structured Data:', jsonData);
|
|
87
133
|
```
|
|
88
134
|
|
|
135
|
+
### Reasoning Models (o1 series)
|
|
136
|
+
|
|
137
|
+
```typescript
|
|
138
|
+
const reasoningParams: ChatParams = {
|
|
139
|
+
model: 'o1-preview',
|
|
140
|
+
messages: [
|
|
141
|
+
{ role: 'user', content: 'Solve this complex math problem...' }
|
|
142
|
+
],
|
|
143
|
+
effortLevel: 'high', // 'low', 'medium', or 'high'
|
|
144
|
+
maxOutputTokens: 2000
|
|
145
|
+
};
|
|
146
|
+
|
|
147
|
+
const response = await openAI.ChatCompletion(reasoningParams);
|
|
148
|
+
```
|
|
149
|
+
|
|
89
150
|
### Text Summarization
|
|
90
151
|
|
|
91
152
|
```typescript
|
|
@@ -104,16 +165,69 @@ const summary = await openAI.SummarizeText(summarizeParams);
|
|
|
104
165
|
console.log('Summary:', summary.summary);
|
|
105
166
|
```
|
|
106
167
|
|
|
168
|
+
### Text Embeddings
|
|
169
|
+
|
|
170
|
+
```typescript
|
|
171
|
+
import { OpenAIEmbedding } from '@memberjunction/ai-openai';
|
|
172
|
+
|
|
173
|
+
const embedding = new OpenAIEmbedding('your-openai-api-key');
|
|
174
|
+
|
|
175
|
+
// Embed a single text
|
|
176
|
+
const singleResult = await embedding.EmbedText({
|
|
177
|
+
text: 'The quick brown fox jumps over the lazy dog',
|
|
178
|
+
model: 'text-embedding-3-small' // or 'text-embedding-3-large', 'text-embedding-ada-002'
|
|
179
|
+
});
|
|
180
|
+
console.log('Embedding vector:', singleResult.vector);
|
|
181
|
+
|
|
182
|
+
// Embed multiple texts
|
|
183
|
+
const multiResult = await embedding.EmbedTexts({
|
|
184
|
+
texts: ['First text', 'Second text', 'Third text'],
|
|
185
|
+
model: 'text-embedding-3-large'
|
|
186
|
+
});
|
|
187
|
+
console.log('Embedding vectors:', multiResult.vectors);
|
|
188
|
+
|
|
189
|
+
// Get available models
|
|
190
|
+
const models = await embedding.GetEmbeddingModels();
|
|
191
|
+
console.log('Available models:', models);
|
|
192
|
+
```
|
|
193
|
+
|
|
194
|
+
### Text-to-Speech
|
|
195
|
+
|
|
196
|
+
```typescript
|
|
197
|
+
import { OpenAIAudioGenerator } from '@memberjunction/ai-openai';
|
|
198
|
+
|
|
199
|
+
const tts = new OpenAIAudioGenerator('your-openai-api-key');
|
|
200
|
+
|
|
201
|
+
// Generate speech
|
|
202
|
+
const speechResult = await tts.CreateSpeech({
|
|
203
|
+
text: 'Hello, this is a test of OpenAI text-to-speech.',
|
|
204
|
+
model_id: 'gpt-4o-mini-tts',
|
|
205
|
+
voice: 'nova', // 'alloy', 'echo', 'fable', 'onyx', 'nova', or 'shimmer'
|
|
206
|
+
instructions: 'Speak in a cheerful and positive tone'
|
|
207
|
+
});
|
|
208
|
+
|
|
209
|
+
if (speechResult.success) {
|
|
210
|
+
// speechResult.data contains the audio buffer
|
|
211
|
+
// speechResult.content contains base64-encoded audio
|
|
212
|
+
fs.writeFileSync('output.mp3', speechResult.data);
|
|
213
|
+
}
|
|
214
|
+
|
|
215
|
+
// Get available voices and models
|
|
216
|
+
const voices = await tts.GetVoices();
|
|
217
|
+
const models = await tts.GetModels();
|
|
218
|
+
```
|
|
219
|
+
|
|
107
220
|
### Direct Access to OpenAI Client
|
|
108
221
|
|
|
109
222
|
```typescript
|
|
110
223
|
// Access the underlying OpenAI client for advanced usage
|
|
111
224
|
const openAIClient = openAI.OpenAI;
|
|
112
225
|
|
|
113
|
-
// Use the client directly
|
|
114
|
-
const
|
|
115
|
-
model: '
|
|
116
|
-
|
|
226
|
+
// Use the client directly for features not wrapped
|
|
227
|
+
const completion = await openAIClient.completions.create({
|
|
228
|
+
model: 'gpt-3.5-turbo-instruct',
|
|
229
|
+
prompt: 'Say this is a test',
|
|
230
|
+
max_tokens: 7
|
|
117
231
|
});
|
|
118
232
|
```
|
|
119
233
|
|
|
@@ -121,44 +235,81 @@ const embeddings = await openAIClient.embeddings.create({
|
|
|
121
235
|
|
|
122
236
|
### OpenAILLM Class
|
|
123
237
|
|
|
124
|
-
|
|
238
|
+
Extends `BaseLLM` to provide OpenAI-specific chat and completion functionality.
|
|
125
239
|
|
|
126
240
|
#### Constructor
|
|
127
|
-
|
|
128
241
|
```typescript
|
|
129
242
|
new OpenAILLM(apiKey: string)
|
|
130
243
|
```
|
|
131
244
|
|
|
132
245
|
#### Properties
|
|
133
|
-
|
|
134
246
|
- `OpenAI`: (read-only) Returns the underlying OpenAI client instance
|
|
247
|
+
- `SupportsStreaming`: (read-only) Returns `true` - OpenAI supports streaming
|
|
135
248
|
|
|
136
249
|
#### Methods
|
|
137
|
-
|
|
138
250
|
- `ChatCompletion(params: ChatParams): Promise<ChatResult>` - Perform a chat completion
|
|
251
|
+
- `StreamingChatCompletion(params: ChatParams, callbacks: StreamingChatCallbacks): Promise<void>` - Stream a chat completion
|
|
139
252
|
- `SummarizeText(params: SummarizeParams): Promise<SummarizeResult>` - Summarize text
|
|
140
253
|
- `ClassifyText(params: ClassifyParams): Promise<ClassifyResult>` - Classify text (not implemented)
|
|
141
|
-
- `ConvertMJToOpenAIChatMessages(messages: ChatMessage[]): ChatCompletionMessageParam[]` - Convert
|
|
142
|
-
- `ConvertMJToOpenAIRole(role: string):
|
|
254
|
+
- `ConvertMJToOpenAIChatMessages(messages: ChatMessage[]): ChatCompletionMessageParam[]` - Convert MJ to OpenAI format
|
|
255
|
+
- `ConvertMJToOpenAIRole(role: string): 'system' | 'user' | 'assistant'` - Convert MJ roles to OpenAI roles
|
|
143
256
|
|
|
144
|
-
|
|
257
|
+
### OpenAIEmbedding Class
|
|
145
258
|
|
|
146
|
-
|
|
259
|
+
Extends `BaseEmbeddings` to provide OpenAI embedding functionality.
|
|
147
260
|
|
|
148
|
-
|
|
149
|
-
|
|
150
|
-
|
|
151
|
-
|
|
261
|
+
#### Constructor
|
|
262
|
+
```typescript
|
|
263
|
+
new OpenAIEmbedding(apiKey: string)
|
|
264
|
+
```
|
|
152
265
|
|
|
153
|
-
|
|
266
|
+
#### Methods
|
|
267
|
+
- `EmbedText(params: EmbedTextParams): Promise<EmbedTextResult>` - Generate embedding for single text
|
|
268
|
+
- `EmbedTexts(params: EmbedTextsParams): Promise<EmbedTextsResult>` - Generate embeddings for multiple texts
|
|
269
|
+
- `GetEmbeddingModels(): Promise<any>` - Get available embedding models
|
|
270
|
+
|
|
271
|
+
### OpenAIAudioGenerator Class
|
|
272
|
+
|
|
273
|
+
Extends `BaseAudioGenerator` to provide OpenAI text-to-speech functionality.
|
|
154
274
|
|
|
275
|
+
#### Constructor
|
|
155
276
|
```typescript
|
|
156
|
-
|
|
157
|
-
// ...other parameters
|
|
158
|
-
responseFormat: 'JSON'
|
|
159
|
-
};
|
|
277
|
+
new OpenAIAudioGenerator(apiKey: string)
|
|
160
278
|
```
|
|
161
279
|
|
|
280
|
+
#### Methods
|
|
281
|
+
- `CreateSpeech(params: TextToSpeechParams): Promise<SpeechResult>` - Generate speech from text
|
|
282
|
+
- `SpeechToText(params: SpeechToTextParams): Promise<SpeechResult>` - Convert speech to text (not implemented)
|
|
283
|
+
- `GetVoices(): Promise<VoiceInfo[]>` - Get available voices
|
|
284
|
+
- `GetModels(): Promise<AudioModel[]>` - Get available TTS models
|
|
285
|
+
- `GetPronounciationDictionaries(): Promise<PronounciationDictionary[]>` - Get pronunciation dictionaries (empty)
|
|
286
|
+
- `GetSupportedMethods(): Promise<string[]>` - Get supported methods
|
|
287
|
+
|
|
288
|
+
## Embedding Models
|
|
289
|
+
|
|
290
|
+
- **text-embedding-3-large**: Most capable model (3,072 dimensions)
|
|
291
|
+
- **text-embedding-3-small**: Balanced performance (1,536 dimensions)
|
|
292
|
+
- **text-embedding-ada-002**: Legacy 2nd generation model (1,536 dimensions)
|
|
293
|
+
|
|
294
|
+
## TTS Voices
|
|
295
|
+
|
|
296
|
+
- **alloy**: Neutral and balanced
|
|
297
|
+
- **echo**: Warm and conversational
|
|
298
|
+
- **fable**: Expressive and animated
|
|
299
|
+
- **onyx**: Deep and authoritative
|
|
300
|
+
- **nova**: Friendly and upbeat
|
|
301
|
+
- **shimmer**: Soft and gentle
|
|
302
|
+
|
|
303
|
+
## Response Formats
|
|
304
|
+
|
|
305
|
+
The OpenAILLM class supports various response formats:
|
|
306
|
+
|
|
307
|
+
- `Text`: Regular text responses (default)
|
|
308
|
+
- `JSON`: Structured JSON responses (requires compatible model)
|
|
309
|
+
- `Markdown`: Markdown-formatted responses
|
|
310
|
+
- `Any`: Model decides the format
|
|
311
|
+
- `ModelSpecific`: Custom formats with `modelSpecificResponseFormat` parameter
|
|
312
|
+
|
|
162
313
|
## Error Handling
|
|
163
314
|
|
|
164
315
|
The wrapper provides comprehensive error information:
|
|
@@ -177,11 +328,25 @@ try {
|
|
|
177
328
|
}
|
|
178
329
|
```
|
|
179
330
|
|
|
331
|
+
## Integration with MemberJunction
|
|
332
|
+
|
|
333
|
+
This package seamlessly integrates with the MemberJunction AI framework:
|
|
334
|
+
|
|
335
|
+
```typescript
|
|
336
|
+
import { AIEngine } from '@memberjunction/ai';
|
|
337
|
+
|
|
338
|
+
// The OpenAI classes are automatically registered
|
|
339
|
+
const engine = new AIEngine();
|
|
340
|
+
const llm = engine.GetLLM('OpenAILLM', 'your-api-key');
|
|
341
|
+
const embeddings = engine.GetEmbedding('OpenAIEmbedding', 'your-api-key');
|
|
342
|
+
const tts = engine.GetAudioGenerator('OpenAIAudioGenerator', 'your-api-key');
|
|
343
|
+
```
|
|
344
|
+
|
|
180
345
|
## Dependencies
|
|
181
346
|
|
|
182
|
-
- `openai`: Official OpenAI Node.js SDK (v4.
|
|
183
|
-
- `@memberjunction/ai`: MemberJunction AI core framework
|
|
184
|
-
- `@memberjunction/global`: MemberJunction global utilities
|
|
347
|
+
- `openai`: Official OpenAI Node.js SDK (v4.98.0)
|
|
348
|
+
- `@memberjunction/ai`: MemberJunction AI core framework (v2.43.0)
|
|
349
|
+
- `@memberjunction/global`: MemberJunction global utilities (v2.43.0)
|
|
185
350
|
|
|
186
351
|
## License
|
|
187
352
|
|