@memberjunction/ai-openai 5.0.0 → 5.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 +164 -0
- package/package.json +3 -3
package/README.md
ADDED
|
@@ -0,0 +1,164 @@
|
|
|
1
|
+
# @memberjunction/ai-openai
|
|
2
|
+
|
|
3
|
+
MemberJunction AI provider for OpenAI. This is the foundational LLM provider in MemberJunction, implementing `BaseLLM` and `BaseEmbeddings` from `@memberjunction/ai`. Many other providers (Groq, Cerebras, Fireworks, OpenRouter, LMStudio, xAI) extend this package since they use OpenAI-compatible APIs.
|
|
4
|
+
|
|
5
|
+
## Architecture
|
|
6
|
+
|
|
7
|
+
```mermaid
|
|
8
|
+
graph TD
|
|
9
|
+
A["OpenAILLM<br/>(Provider)"] -->|extends| B["BaseLLM<br/>(@memberjunction/ai)"]
|
|
10
|
+
C["OpenAIEmbedding<br/>(Provider)"] -->|extends| D["BaseEmbeddings<br/>(@memberjunction/ai)"]
|
|
11
|
+
A -->|wraps| E["OpenAI SDK<br/>(openai npm)"]
|
|
12
|
+
C -->|wraps| E
|
|
13
|
+
A -->|provides| F["Chat + Streaming"]
|
|
14
|
+
A -->|provides| G["Thinking Extraction"]
|
|
15
|
+
A -->|provides| H["JSON / Response<br/>Format Control"]
|
|
16
|
+
B -->|registered via| I["@RegisterClass"]
|
|
17
|
+
D -->|registered via| I
|
|
18
|
+
|
|
19
|
+
subgraph Subclasses["OpenAI-Compatible Subclasses"]
|
|
20
|
+
J["GroqLLM"]
|
|
21
|
+
K["CerebrasLLM"]
|
|
22
|
+
L["FireworksLLM"]
|
|
23
|
+
M["OpenRouterLLM"]
|
|
24
|
+
N["LMStudioLLM"]
|
|
25
|
+
O["xAILLM"]
|
|
26
|
+
end
|
|
27
|
+
J -->|extends| A
|
|
28
|
+
K -->|extends| A
|
|
29
|
+
L -->|extends| A
|
|
30
|
+
M -->|extends| A
|
|
31
|
+
N -->|extends| A
|
|
32
|
+
O -->|extends| A
|
|
33
|
+
|
|
34
|
+
style A fill:#7c5295,stroke:#563a6b,color:#fff
|
|
35
|
+
style C fill:#7c5295,stroke:#563a6b,color:#fff
|
|
36
|
+
style B fill:#2d6a9f,stroke:#1a4971,color:#fff
|
|
37
|
+
style D fill:#2d6a9f,stroke:#1a4971,color:#fff
|
|
38
|
+
style E fill:#2d8659,stroke:#1a5c3a,color:#fff
|
|
39
|
+
style F fill:#b8762f,stroke:#8a5722,color:#fff
|
|
40
|
+
style G fill:#b8762f,stroke:#8a5722,color:#fff
|
|
41
|
+
style H fill:#b8762f,stroke:#8a5722,color:#fff
|
|
42
|
+
style I fill:#b8762f,stroke:#8a5722,color:#fff
|
|
43
|
+
```
|
|
44
|
+
|
|
45
|
+
## Features
|
|
46
|
+
|
|
47
|
+
- **Chat Completions**: Full support for GPT-4, GPT-4o, o1, o3, and other OpenAI models
|
|
48
|
+
- **Streaming**: Real-time response streaming with chunk processing
|
|
49
|
+
- **Thinking/Reasoning**: Extraction of thinking content from `<think>` blocks in reasoning model responses
|
|
50
|
+
- **Embeddings**: Text embedding generation via OpenAI embedding models
|
|
51
|
+
- **Multimodal Input**: Support for text and image content in messages
|
|
52
|
+
- **Response Formats**: JSON mode, text, and other format controls
|
|
53
|
+
- **Effort Level**: Maps MJ effort levels to OpenAI reasoning effort parameters
|
|
54
|
+
- **Error Analysis**: Integrated error analysis via `ErrorAnalyzer`
|
|
55
|
+
- **Extensible Base**: Designed as the foundation for OpenAI-compatible providers
|
|
56
|
+
|
|
57
|
+
## Installation
|
|
58
|
+
|
|
59
|
+
```bash
|
|
60
|
+
npm install @memberjunction/ai-openai
|
|
61
|
+
```
|
|
62
|
+
|
|
63
|
+
## Usage
|
|
64
|
+
|
|
65
|
+
### Chat Completion
|
|
66
|
+
|
|
67
|
+
```typescript
|
|
68
|
+
import { OpenAILLM } from '@memberjunction/ai-openai';
|
|
69
|
+
|
|
70
|
+
const llm = new OpenAILLM('your-openai-api-key');
|
|
71
|
+
|
|
72
|
+
const result = await llm.ChatCompletion({
|
|
73
|
+
model: 'gpt-4o',
|
|
74
|
+
messages: [
|
|
75
|
+
{ role: 'system', content: 'You are a helpful assistant.' },
|
|
76
|
+
{ role: 'user', content: 'Explain quantum computing.' }
|
|
77
|
+
],
|
|
78
|
+
temperature: 0.7,
|
|
79
|
+
maxOutputTokens: 1000
|
|
80
|
+
});
|
|
81
|
+
|
|
82
|
+
if (result.success) {
|
|
83
|
+
console.log(result.data.choices[0].message.content);
|
|
84
|
+
}
|
|
85
|
+
```
|
|
86
|
+
|
|
87
|
+
### Streaming
|
|
88
|
+
|
|
89
|
+
```typescript
|
|
90
|
+
const result = await llm.ChatCompletion({
|
|
91
|
+
model: 'gpt-4o',
|
|
92
|
+
messages: [{ role: 'user', content: 'Write a detailed essay.' }],
|
|
93
|
+
streaming: true,
|
|
94
|
+
streamingCallbacks: {
|
|
95
|
+
OnContent: (content) => process.stdout.write(content),
|
|
96
|
+
OnComplete: (result) => console.log('\nDone!')
|
|
97
|
+
}
|
|
98
|
+
});
|
|
99
|
+
```
|
|
100
|
+
|
|
101
|
+
### Embeddings
|
|
102
|
+
|
|
103
|
+
```typescript
|
|
104
|
+
import { OpenAIEmbedding } from '@memberjunction/ai-openai';
|
|
105
|
+
|
|
106
|
+
const embedder = new OpenAIEmbedding('your-openai-api-key');
|
|
107
|
+
|
|
108
|
+
const result = await embedder.EmbedText({
|
|
109
|
+
text: 'Sample text for embedding',
|
|
110
|
+
model: 'text-embedding-3-small'
|
|
111
|
+
});
|
|
112
|
+
|
|
113
|
+
console.log(`Dimensions: ${result.vector.length}`);
|
|
114
|
+
```
|
|
115
|
+
|
|
116
|
+
## Supported Parameters
|
|
117
|
+
|
|
118
|
+
| Parameter | Supported | Notes |
|
|
119
|
+
|-----------|-----------|-------|
|
|
120
|
+
| temperature | Yes | 0.0 - 2.0 |
|
|
121
|
+
| maxOutputTokens | Yes | Maximum tokens to generate |
|
|
122
|
+
| topP | Yes | Nucleus sampling |
|
|
123
|
+
| frequencyPenalty | Yes | -2.0 to 2.0 |
|
|
124
|
+
| presencePenalty | Yes | -2.0 to 2.0 |
|
|
125
|
+
| seed | Yes | Deterministic outputs |
|
|
126
|
+
| stopSequences | Yes | Custom stop sequences |
|
|
127
|
+
| responseFormat | Yes | JSON, text modes |
|
|
128
|
+
| streaming | Yes | Real-time streaming |
|
|
129
|
+
| effortLevel | Yes | Maps to reasoning_effort |
|
|
130
|
+
| topK | No | Not supported by OpenAI |
|
|
131
|
+
| minP | No | Not supported by OpenAI |
|
|
132
|
+
|
|
133
|
+
## Extending for Compatible APIs
|
|
134
|
+
|
|
135
|
+
This provider is designed as a base class for any OpenAI-compatible API. To create a new provider, override the base URL:
|
|
136
|
+
|
|
137
|
+
```typescript
|
|
138
|
+
import { OpenAILLM } from '@memberjunction/ai-openai';
|
|
139
|
+
import { RegisterClass } from '@memberjunction/global';
|
|
140
|
+
import { BaseLLM } from '@memberjunction/ai';
|
|
141
|
+
|
|
142
|
+
@RegisterClass(BaseLLM, 'MyProviderLLM')
|
|
143
|
+
export class MyProviderLLM extends OpenAILLM {
|
|
144
|
+
constructor(apiKey: string) {
|
|
145
|
+
super(apiKey);
|
|
146
|
+
// Override the base URL
|
|
147
|
+
this._openai = new OpenAI({
|
|
148
|
+
apiKey: apiKey,
|
|
149
|
+
baseURL: 'https://api.my-provider.com/v1'
|
|
150
|
+
});
|
|
151
|
+
}
|
|
152
|
+
}
|
|
153
|
+
```
|
|
154
|
+
|
|
155
|
+
## Class Registration
|
|
156
|
+
|
|
157
|
+
- `OpenAILLM` -- Registered via `@RegisterClass(BaseLLM, 'OpenAILLM')`
|
|
158
|
+
- `OpenAIEmbedding` -- Registered via `@RegisterClass(BaseEmbeddings, 'OpenAIEmbedding')`
|
|
159
|
+
|
|
160
|
+
## Dependencies
|
|
161
|
+
|
|
162
|
+
- `@memberjunction/ai` - Core AI abstractions
|
|
163
|
+
- `@memberjunction/global` - Class registration
|
|
164
|
+
- `openai` - Official OpenAI SDK
|
package/package.json
CHANGED
|
@@ -1,7 +1,7 @@
|
|
|
1
1
|
{
|
|
2
2
|
"name": "@memberjunction/ai-openai",
|
|
3
3
|
"type": "module",
|
|
4
|
-
"version": "5.
|
|
4
|
+
"version": "5.1.0",
|
|
5
5
|
"description": "MemberJunction Wrapper for OpenAI AI Models",
|
|
6
6
|
"main": "dist/index.js",
|
|
7
7
|
"types": "dist/index.d.ts",
|
|
@@ -21,8 +21,8 @@
|
|
|
21
21
|
"typescript": "^5.9.3"
|
|
22
22
|
},
|
|
23
23
|
"dependencies": {
|
|
24
|
-
"@memberjunction/ai": "5.
|
|
25
|
-
"@memberjunction/global": "5.
|
|
24
|
+
"@memberjunction/ai": "5.1.0",
|
|
25
|
+
"@memberjunction/global": "5.1.0",
|
|
26
26
|
"openai": "6.18.0"
|
|
27
27
|
},
|
|
28
28
|
"repository": {
|