@memberjunction/ai-openai 2.32.2 → 2.33.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 +188 -2
package/package.json
CHANGED
|
@@ -1,6 +1,6 @@
|
|
|
1
1
|
{
|
|
2
2
|
"name": "@memberjunction/ai-openai",
|
|
3
|
-
"version": "2.
|
|
3
|
+
"version": "2.33.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.33.0",
|
|
23
|
+
"@memberjunction/global": "2.33.0",
|
|
24
24
|
"openai": "4.83.0"
|
|
25
25
|
}
|
|
26
26
|
}
|
package/readme.md
CHANGED
|
@@ -1,2 +1,188 @@
|
|
|
1
|
-
# @memberjunction/ai-
|
|
2
|
-
|
|
1
|
+
# @memberjunction/ai-openai
|
|
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 other OpenAI models.
|
|
4
|
+
|
|
5
|
+
## Features
|
|
6
|
+
|
|
7
|
+
- **OpenAI Integration**: Full integration with OpenAI's chat completion models
|
|
8
|
+
- **Standardized Interface**: Follows MemberJunction's BaseLLM abstract class for consistency
|
|
9
|
+
- **Message Formatting**: Handles conversion between MemberJunction and OpenAI message formats
|
|
10
|
+
- **Response Format Support**: Support for different response formats (Text, JSON, etc.)
|
|
11
|
+
- **Error Handling**: Comprehensive error handling with detailed reporting
|
|
12
|
+
- **Token Usage Tracking**: Automatic tracking of prompt and completion tokens
|
|
13
|
+
- **Chat Completion**: Chat-based interaction with models like GPT-4 and GPT-3.5
|
|
14
|
+
- **Text Summarization**: Summarize text content using OpenAI models
|
|
15
|
+
|
|
16
|
+
## Installation
|
|
17
|
+
|
|
18
|
+
```bash
|
|
19
|
+
npm install @memberjunction/ai-openai
|
|
20
|
+
```
|
|
21
|
+
|
|
22
|
+
## Requirements
|
|
23
|
+
|
|
24
|
+
- Node.js 16+
|
|
25
|
+
- An OpenAI API key
|
|
26
|
+
- MemberJunction Core libraries
|
|
27
|
+
|
|
28
|
+
## Usage
|
|
29
|
+
|
|
30
|
+
### Basic Setup
|
|
31
|
+
|
|
32
|
+
```typescript
|
|
33
|
+
import { OpenAILLM } from '@memberjunction/ai-openai';
|
|
34
|
+
|
|
35
|
+
// Initialize with your API key
|
|
36
|
+
const openAI = new OpenAILLM('your-openai-api-key');
|
|
37
|
+
```
|
|
38
|
+
|
|
39
|
+
### Chat Completion
|
|
40
|
+
|
|
41
|
+
```typescript
|
|
42
|
+
import { ChatParams } from '@memberjunction/ai';
|
|
43
|
+
|
|
44
|
+
// Create chat parameters
|
|
45
|
+
const chatParams: ChatParams = {
|
|
46
|
+
model: 'gpt-4',
|
|
47
|
+
messages: [
|
|
48
|
+
{ role: 'system', content: 'You are a helpful assistant.' },
|
|
49
|
+
{ role: 'user', content: 'What is machine learning?' }
|
|
50
|
+
],
|
|
51
|
+
temperature: 0.7,
|
|
52
|
+
maxOutputTokens: 500,
|
|
53
|
+
responseFormat: 'Text'
|
|
54
|
+
};
|
|
55
|
+
|
|
56
|
+
// Get a response
|
|
57
|
+
try {
|
|
58
|
+
const response = await openAI.ChatCompletion(chatParams);
|
|
59
|
+
if (response.success) {
|
|
60
|
+
console.log('Response:', response.data.choices[0].message.content);
|
|
61
|
+
console.log('Token Usage:', response.data.usage);
|
|
62
|
+
} else {
|
|
63
|
+
console.error('Error:', response.errorMessage);
|
|
64
|
+
}
|
|
65
|
+
} catch (error) {
|
|
66
|
+
console.error('Exception:', error);
|
|
67
|
+
}
|
|
68
|
+
```
|
|
69
|
+
|
|
70
|
+
### JSON Response Format
|
|
71
|
+
|
|
72
|
+
```typescript
|
|
73
|
+
const jsonParams: ChatParams = {
|
|
74
|
+
model: 'gpt-4',
|
|
75
|
+
messages: [
|
|
76
|
+
{ role: 'system', content: 'You are a helpful assistant.' },
|
|
77
|
+
{ role: 'user', content: 'Generate a JSON object with name, age, and city for 3 fictional people.' }
|
|
78
|
+
],
|
|
79
|
+
temperature: 0.3,
|
|
80
|
+
maxOutputTokens: 500,
|
|
81
|
+
responseFormat: 'JSON'
|
|
82
|
+
};
|
|
83
|
+
|
|
84
|
+
const jsonResponse = await openAI.ChatCompletion(jsonParams);
|
|
85
|
+
const jsonData = JSON.parse(jsonResponse.data.choices[0].message.content);
|
|
86
|
+
console.log('Structured Data:', jsonData);
|
|
87
|
+
```
|
|
88
|
+
|
|
89
|
+
### Text Summarization
|
|
90
|
+
|
|
91
|
+
```typescript
|
|
92
|
+
import { SummarizeParams } from '@memberjunction/ai';
|
|
93
|
+
|
|
94
|
+
const text = `Long text that you want to summarize...`;
|
|
95
|
+
|
|
96
|
+
const summarizeParams: SummarizeParams = {
|
|
97
|
+
text: text,
|
|
98
|
+
model: 'gpt-3.5-turbo',
|
|
99
|
+
temperature: 0.3,
|
|
100
|
+
maxWords: 100
|
|
101
|
+
};
|
|
102
|
+
|
|
103
|
+
const summary = await openAI.SummarizeText(summarizeParams);
|
|
104
|
+
console.log('Summary:', summary.summary);
|
|
105
|
+
```
|
|
106
|
+
|
|
107
|
+
### Direct Access to OpenAI Client
|
|
108
|
+
|
|
109
|
+
```typescript
|
|
110
|
+
// Access the underlying OpenAI client for advanced usage
|
|
111
|
+
const openAIClient = openAI.OpenAI;
|
|
112
|
+
|
|
113
|
+
// Use the client directly if needed
|
|
114
|
+
const embeddings = await openAIClient.embeddings.create({
|
|
115
|
+
model: 'text-embedding-ada-002',
|
|
116
|
+
input: 'The quick brown fox jumps over the lazy dog'
|
|
117
|
+
});
|
|
118
|
+
```
|
|
119
|
+
|
|
120
|
+
## API Reference
|
|
121
|
+
|
|
122
|
+
### OpenAILLM Class
|
|
123
|
+
|
|
124
|
+
A class that extends BaseLLM to provide OpenAI-specific functionality.
|
|
125
|
+
|
|
126
|
+
#### Constructor
|
|
127
|
+
|
|
128
|
+
```typescript
|
|
129
|
+
new OpenAILLM(apiKey: string)
|
|
130
|
+
```
|
|
131
|
+
|
|
132
|
+
#### Properties
|
|
133
|
+
|
|
134
|
+
- `OpenAI`: (read-only) Returns the underlying OpenAI client instance
|
|
135
|
+
|
|
136
|
+
#### Methods
|
|
137
|
+
|
|
138
|
+
- `ChatCompletion(params: ChatParams): Promise<ChatResult>` - Perform a chat completion
|
|
139
|
+
- `SummarizeText(params: SummarizeParams): Promise<SummarizeResult>` - Summarize text
|
|
140
|
+
- `ClassifyText(params: ClassifyParams): Promise<ClassifyResult>` - Classify text (not implemented)
|
|
141
|
+
- `ConvertMJToOpenAIChatMessages(messages: ChatMessage[]): ChatCompletionMessageParam[]` - Convert MemberJunction messages to OpenAI messages
|
|
142
|
+
- `ConvertMJToOpenAIRole(role: string): string` - Convert MemberJunction roles to OpenAI roles
|
|
143
|
+
|
|
144
|
+
## Response Formats
|
|
145
|
+
|
|
146
|
+
The OpenAILLM class supports various response formats:
|
|
147
|
+
|
|
148
|
+
- `Text`: Regular text responses (default)
|
|
149
|
+
- `JSON`: Structured JSON responses
|
|
150
|
+
- `Markdown`: Markdown-formatted responses
|
|
151
|
+
- `ModelSpecific`: Custom formats supported by specific models
|
|
152
|
+
|
|
153
|
+
Example with JSON response:
|
|
154
|
+
|
|
155
|
+
```typescript
|
|
156
|
+
const params: ChatParams = {
|
|
157
|
+
// ...other parameters
|
|
158
|
+
responseFormat: 'JSON'
|
|
159
|
+
};
|
|
160
|
+
```
|
|
161
|
+
|
|
162
|
+
## Error Handling
|
|
163
|
+
|
|
164
|
+
The wrapper provides comprehensive error information:
|
|
165
|
+
|
|
166
|
+
```typescript
|
|
167
|
+
try {
|
|
168
|
+
const response = await openAI.ChatCompletion(params);
|
|
169
|
+
if (!response.success) {
|
|
170
|
+
console.error('Error:', response.errorMessage);
|
|
171
|
+
console.error('Status:', response.statusText);
|
|
172
|
+
console.error('Time Elapsed:', response.timeElapsed, 'ms');
|
|
173
|
+
console.error('Exception:', response.exception);
|
|
174
|
+
}
|
|
175
|
+
} catch (error) {
|
|
176
|
+
console.error('Exception occurred:', error);
|
|
177
|
+
}
|
|
178
|
+
```
|
|
179
|
+
|
|
180
|
+
## Dependencies
|
|
181
|
+
|
|
182
|
+
- `openai`: Official OpenAI Node.js SDK (v4.x)
|
|
183
|
+
- `@memberjunction/ai`: MemberJunction AI core framework
|
|
184
|
+
- `@memberjunction/global`: MemberJunction global utilities
|
|
185
|
+
|
|
186
|
+
## License
|
|
187
|
+
|
|
188
|
+
ISC
|