openai-api-mock 0.3.1 → 0.4.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 +376 -333
- package/dist/index.cjs +1805 -1661
- package/dist/index.cjs.map +1 -1
- package/dist/index.js +1805 -1661
- package/dist/index.js.map +1 -1
- package/package.json +74 -69
package/README.md
CHANGED
|
@@ -1,333 +1,376 @@
|
|
|
1
|
-
# OpenAI API Mock
|
|
2
|
-
|
|
3
|
-
This is a Node.js module for mocking OpenAI API responses in a development environment .
|
|
4
|
-
|
|
5
|
-
[](https://github.com/chihebnabil/openai-api-mock/actions/workflows/test.yml)
|
|
6
|
-
|
|
7
|
-
It's useful for testing and development purposes when you don't want to make actual API calls.
|
|
8
|
-
|
|
9
|
-
The module supports the following OpenAI API endpoints:
|
|
10
|
-
|
|
11
|
-
- chat completions
|
|
12
|
-
- chat completions with streaming
|
|
13
|
-
- chat completions with functions
|
|
14
|
-
- image generations
|
|
15
|
-
|
|
16
|
-
|
|
17
|
-
|
|
18
|
-
|
|
19
|
-
- [
|
|
20
|
-
- [
|
|
21
|
-
- [
|
|
22
|
-
- [
|
|
23
|
-
- [
|
|
24
|
-
- [
|
|
25
|
-
|
|
26
|
-
|
|
27
|
-
|
|
28
|
-
|
|
29
|
-
|
|
30
|
-
|
|
31
|
-
|
|
32
|
-
|
|
33
|
-
|
|
34
|
-
|
|
35
|
-
|
|
36
|
-
|
|
37
|
-
|
|
38
|
-
|
|
39
|
-
|
|
40
|
-
|
|
41
|
-
|
|
42
|
-
|
|
43
|
-
|
|
44
|
-
|
|
45
|
-
|
|
46
|
-
|
|
47
|
-
|
|
48
|
-
|
|
49
|
-
|
|
50
|
-
|
|
51
|
-
|
|
52
|
-
|
|
53
|
-
|
|
54
|
-
|
|
55
|
-
|
|
56
|
-
|
|
57
|
-
|
|
58
|
-
|
|
59
|
-
|
|
60
|
-
|
|
61
|
-
|
|
62
|
-
|
|
63
|
-
|
|
64
|
-
|
|
65
|
-
|
|
66
|
-
|
|
67
|
-
|
|
68
|
-
|
|
69
|
-
- `
|
|
70
|
-
|
|
71
|
-
- `
|
|
72
|
-
- `
|
|
73
|
-
- `
|
|
74
|
-
- `
|
|
75
|
-
- `
|
|
76
|
-
|
|
77
|
-
|
|
78
|
-
|
|
79
|
-
|
|
80
|
-
|
|
81
|
-
|
|
82
|
-
|
|
83
|
-
|
|
84
|
-
|
|
85
|
-
|
|
86
|
-
|
|
87
|
-
|
|
88
|
-
|
|
89
|
-
|
|
90
|
-
mock.
|
|
91
|
-
|
|
92
|
-
|
|
93
|
-
|
|
94
|
-
const
|
|
95
|
-
|
|
96
|
-
}
|
|
97
|
-
|
|
98
|
-
|
|
99
|
-
|
|
100
|
-
|
|
101
|
-
}
|
|
102
|
-
|
|
103
|
-
|
|
104
|
-
|
|
105
|
-
|
|
106
|
-
|
|
107
|
-
|
|
108
|
-
|
|
109
|
-
|
|
110
|
-
|
|
111
|
-
|
|
112
|
-
|
|
113
|
-
|
|
114
|
-
|
|
115
|
-
|
|
116
|
-
|
|
117
|
-
|
|
118
|
-
|
|
119
|
-
|
|
120
|
-
|
|
121
|
-
|
|
122
|
-
|
|
123
|
-
|
|
124
|
-
|
|
125
|
-
|
|
126
|
-
|
|
127
|
-
|
|
128
|
-
|
|
129
|
-
|
|
130
|
-
|
|
131
|
-
|
|
132
|
-
|
|
133
|
-
|
|
134
|
-
|
|
135
|
-
|
|
136
|
-
}
|
|
137
|
-
|
|
138
|
-
|
|
139
|
-
|
|
140
|
-
|
|
141
|
-
|
|
142
|
-
-
|
|
143
|
-
-
|
|
144
|
-
|
|
145
|
-
|
|
146
|
-
|
|
147
|
-
|
|
148
|
-
|
|
149
|
-
mockOpenAIResponse
|
|
150
|
-
|
|
151
|
-
|
|
152
|
-
|
|
153
|
-
|
|
154
|
-
|
|
155
|
-
|
|
156
|
-
{ role: '
|
|
157
|
-
|
|
158
|
-
|
|
159
|
-
|
|
160
|
-
|
|
161
|
-
|
|
162
|
-
|
|
163
|
-
|
|
164
|
-
|
|
165
|
-
|
|
166
|
-
|
|
167
|
-
|
|
168
|
-
|
|
169
|
-
|
|
170
|
-
|
|
171
|
-
|
|
172
|
-
|
|
173
|
-
|
|
174
|
-
|
|
175
|
-
|
|
176
|
-
|
|
177
|
-
|
|
178
|
-
}
|
|
179
|
-
|
|
180
|
-
|
|
181
|
-
|
|
182
|
-
|
|
183
|
-
|
|
184
|
-
|
|
185
|
-
mockOpenAIResponse
|
|
186
|
-
|
|
187
|
-
|
|
188
|
-
|
|
189
|
-
|
|
190
|
-
|
|
191
|
-
|
|
192
|
-
{ role: '
|
|
193
|
-
|
|
194
|
-
|
|
195
|
-
|
|
196
|
-
|
|
197
|
-
|
|
198
|
-
|
|
199
|
-
|
|
200
|
-
|
|
201
|
-
|
|
202
|
-
|
|
203
|
-
|
|
204
|
-
|
|
205
|
-
|
|
206
|
-
|
|
207
|
-
|
|
208
|
-
|
|
209
|
-
|
|
210
|
-
|
|
211
|
-
|
|
212
|
-
|
|
213
|
-
|
|
214
|
-
|
|
215
|
-
|
|
216
|
-
|
|
217
|
-
|
|
218
|
-
|
|
219
|
-
|
|
220
|
-
|
|
221
|
-
|
|
222
|
-
|
|
223
|
-
|
|
224
|
-
|
|
225
|
-
//
|
|
226
|
-
|
|
227
|
-
```
|
|
228
|
-
|
|
229
|
-
|
|
230
|
-
|
|
231
|
-
|
|
232
|
-
|
|
233
|
-
|
|
234
|
-
|
|
235
|
-
|
|
236
|
-
|
|
237
|
-
|
|
238
|
-
|
|
239
|
-
|
|
240
|
-
|
|
241
|
-
|
|
242
|
-
|
|
243
|
-
|
|
244
|
-
|
|
245
|
-
|
|
246
|
-
|
|
247
|
-
###
|
|
248
|
-
|
|
249
|
-
|
|
250
|
-
|
|
251
|
-
```js
|
|
252
|
-
|
|
253
|
-
|
|
254
|
-
|
|
255
|
-
|
|
256
|
-
const
|
|
257
|
-
|
|
258
|
-
|
|
259
|
-
|
|
260
|
-
|
|
261
|
-
|
|
262
|
-
|
|
263
|
-
|
|
264
|
-
|
|
265
|
-
|
|
266
|
-
|
|
267
|
-
|
|
268
|
-
|
|
269
|
-
|
|
270
|
-
|
|
271
|
-
|
|
272
|
-
|
|
273
|
-
|
|
274
|
-
|
|
275
|
-
|
|
276
|
-
|
|
277
|
-
|
|
278
|
-
|
|
279
|
-
|
|
280
|
-
|
|
281
|
-
|
|
282
|
-
|
|
283
|
-
//
|
|
284
|
-
|
|
285
|
-
|
|
286
|
-
|
|
287
|
-
|
|
288
|
-
|
|
289
|
-
|
|
290
|
-
|
|
291
|
-
|
|
292
|
-
|
|
293
|
-
|
|
294
|
-
|
|
295
|
-
|
|
296
|
-
|
|
297
|
-
|
|
298
|
-
|
|
299
|
-
|
|
300
|
-
|
|
301
|
-
|
|
302
|
-
|
|
303
|
-
|
|
304
|
-
|
|
305
|
-
|
|
306
|
-
|
|
307
|
-
|
|
308
|
-
|
|
309
|
-
|
|
310
|
-
|
|
311
|
-
|
|
312
|
-
|
|
313
|
-
|
|
314
|
-
|
|
315
|
-
|
|
316
|
-
|
|
317
|
-
|
|
318
|
-
|
|
319
|
-
|
|
320
|
-
|
|
321
|
-
|
|
322
|
-
|
|
323
|
-
|
|
324
|
-
|
|
325
|
-
|
|
326
|
-
|
|
327
|
-
|
|
328
|
-
|
|
329
|
-
|
|
330
|
-
|
|
331
|
-
|
|
332
|
-
|
|
333
|
-
|
|
1
|
+
# OpenAI API Mock
|
|
2
|
+
|
|
3
|
+
This is a Node.js module for mocking OpenAI API responses in a development environment .
|
|
4
|
+
|
|
5
|
+
[](https://github.com/chihebnabil/openai-api-mock/actions/workflows/test.yml)
|
|
6
|
+
|
|
7
|
+
It's useful for testing and development purposes when you don't want to make actual API calls.
|
|
8
|
+
|
|
9
|
+
The module supports the following OpenAI API endpoints:
|
|
10
|
+
|
|
11
|
+
- chat completions
|
|
12
|
+
- chat completions with streaming
|
|
13
|
+
- chat completions with functions
|
|
14
|
+
- image generations
|
|
15
|
+
- embeddings
|
|
16
|
+
|
|
17
|
+
## Table of Contents
|
|
18
|
+
|
|
19
|
+
- [Installation](#installation)
|
|
20
|
+
- [Usage](#usage)
|
|
21
|
+
- [Consistent Outputs for Testing](#consistent-outputs-for-testing)
|
|
22
|
+
- [Intercepted URLs](#intercepted-urls)
|
|
23
|
+
- [TypeScript Support](#typescript-support)
|
|
24
|
+
- [Dependencies](#dependencies)
|
|
25
|
+
- [License](#license)
|
|
26
|
+
|
|
27
|
+
## Installation
|
|
28
|
+
|
|
29
|
+
You can install this module using npm as a dev dependency :
|
|
30
|
+
|
|
31
|
+
```sh
|
|
32
|
+
npm install -D openai-api-mock
|
|
33
|
+
```
|
|
34
|
+
|
|
35
|
+
## Usage
|
|
36
|
+
|
|
37
|
+
The module supports both ESM and CommonJS imports:
|
|
38
|
+
|
|
39
|
+
```js
|
|
40
|
+
// ESM
|
|
41
|
+
import { mockOpenAIResponse } from 'openai-api-mock';
|
|
42
|
+
|
|
43
|
+
// CommonJS
|
|
44
|
+
const { mockOpenAIResponse } = require('openai-api-mock');
|
|
45
|
+
```
|
|
46
|
+
|
|
47
|
+
Then, call the mockOpenAIResponse function to set up the mock response:
|
|
48
|
+
|
|
49
|
+
```js
|
|
50
|
+
// Basic usage
|
|
51
|
+
mockOpenAIResponse();
|
|
52
|
+
|
|
53
|
+
// Force mocking regardless of environment
|
|
54
|
+
mockOpenAIResponse(true);
|
|
55
|
+
|
|
56
|
+
// With configuration options
|
|
57
|
+
mockOpenAIResponse(false, {
|
|
58
|
+
includeErrors: true, // Simulate random API errors
|
|
59
|
+
latency: 1000, // Add 1 second delay to responses
|
|
60
|
+
logRequests: true, // Log incoming requests to console
|
|
61
|
+
seed: 12345, // Seed for consistent/deterministic responses
|
|
62
|
+
useFixedResponses: true, // Use predefined fixed response templates
|
|
63
|
+
baseUrl: 'https://api.openai.com', // Base URL for OpenAI API or compatible service
|
|
64
|
+
});
|
|
65
|
+
```
|
|
66
|
+
|
|
67
|
+
The function accepts two parameters:
|
|
68
|
+
|
|
69
|
+
- `force` (boolean): Determines whether the mock response should be used regardless of the environment. If false or not provided, mocking only occurs in development environment.
|
|
70
|
+
- `options` (object): Additional configuration options
|
|
71
|
+
- `includeErrors` (boolean): When true, randomly simulates API errors
|
|
72
|
+
- `latency` (number): Adds artificial delay to responses in milliseconds
|
|
73
|
+
- `logRequests` (boolean): Logs incoming requests to console for debugging
|
|
74
|
+
- `seed` (number|string): Seed value for consistent/deterministic responses using faker.js
|
|
75
|
+
- `useFixedResponses` (boolean): Use predefined fixed response templates for completely consistent responses
|
|
76
|
+
- `baseUrl` (string): Base URL for the OpenAI API or OpenAI-compatible service (defaults to `https://api.openai.com`)
|
|
77
|
+
|
|
78
|
+
The function returns an object with control methods:
|
|
79
|
+
|
|
80
|
+
```js
|
|
81
|
+
const mock = mockOpenAIResponse();
|
|
82
|
+
|
|
83
|
+
// Check if mocking is active
|
|
84
|
+
console.log(mock.isActive);
|
|
85
|
+
|
|
86
|
+
// Stop all mocks
|
|
87
|
+
mock.stopMocking();
|
|
88
|
+
|
|
89
|
+
// Seed management for consistent outputs
|
|
90
|
+
mock.setSeed(12345); // Set a new seed for deterministic responses
|
|
91
|
+
mock.resetSeed(); // Reset to random responses
|
|
92
|
+
|
|
93
|
+
// Template management
|
|
94
|
+
const templates = mock.getResponseTemplates(); // Get available templates
|
|
95
|
+
const customTemplate = mock.createResponseTemplate('SIMPLE_CHAT', {
|
|
96
|
+
choices: [{ message: { content: 'Custom response' } }],
|
|
97
|
+
});
|
|
98
|
+
|
|
99
|
+
// Add custom endpoint mock (uses configured base URL)
|
|
100
|
+
mock.addCustomEndpoint('POST', '/v1/custom', (uri, body) => {
|
|
101
|
+
return [200, { custom: 'response' }];
|
|
102
|
+
});
|
|
103
|
+
```
|
|
104
|
+
|
|
105
|
+
### Using with OpenAI-Compatible Services
|
|
106
|
+
|
|
107
|
+
The library supports mocking any OpenAI-compatible API by configuring the `baseUrl` option. This is useful when working with services like Azure OpenAI, local models, or other OpenAI-compatible endpoints.
|
|
108
|
+
|
|
109
|
+
```js
|
|
110
|
+
// Mock Azure OpenAI Service
|
|
111
|
+
mockOpenAIResponse(true, {
|
|
112
|
+
baseUrl: 'https://your-resource.openai.azure.com',
|
|
113
|
+
logRequests: true,
|
|
114
|
+
});
|
|
115
|
+
|
|
116
|
+
// Mock local OpenAI-compatible server (e.g., Ollama, LocalAI)
|
|
117
|
+
mockOpenAIResponse(true, {
|
|
118
|
+
baseUrl: 'http://localhost:11434', // Ollama default port
|
|
119
|
+
logRequests: true,
|
|
120
|
+
});
|
|
121
|
+
|
|
122
|
+
// Mock other OpenAI-compatible services
|
|
123
|
+
mockOpenAIResponse(true, {
|
|
124
|
+
baseUrl: 'https://api.anthropic.com', // or other compatible endpoints
|
|
125
|
+
logRequests: true,
|
|
126
|
+
});
|
|
127
|
+
|
|
128
|
+
// Your existing OpenAI client code will work unchanged
|
|
129
|
+
const openai = new OpenAI({
|
|
130
|
+
apiKey: 'your-api-key',
|
|
131
|
+
baseURL: 'https://your-resource.openai.azure.com', // This will be mocked
|
|
132
|
+
});
|
|
133
|
+
|
|
134
|
+
const response = await openai.chat.completions.create({
|
|
135
|
+
model: 'gpt-4',
|
|
136
|
+
messages: [{ role: 'user', content: 'Hello!' }],
|
|
137
|
+
});
|
|
138
|
+
```
|
|
139
|
+
|
|
140
|
+
When using custom `baseUrl`, the mock will:
|
|
141
|
+
|
|
142
|
+
- Intercept requests to the specified base URL instead of `api.openai.com`
|
|
143
|
+
- Block network connections to that specific host while allowing other network requests
|
|
144
|
+
- Apply all the same mocking behavior (errors, latency, seeding, etc.) to the custom endpoint
|
|
145
|
+
|
|
146
|
+
### Example responses
|
|
147
|
+
|
|
148
|
+
```js
|
|
149
|
+
// Call the mockOpenAIResponse function once to set up the mock
|
|
150
|
+
mockOpenAIResponse();
|
|
151
|
+
|
|
152
|
+
// Now, when you call the OpenAI API, it will return a mock response
|
|
153
|
+
const response = await openai.chat.completions.create({
|
|
154
|
+
model: 'gpt-3.5',
|
|
155
|
+
messages: [
|
|
156
|
+
{ role: 'system', content: "You're an expert chef" },
|
|
157
|
+
{ role: 'user', content: 'Suggest at least 5 recipes' },
|
|
158
|
+
],
|
|
159
|
+
});
|
|
160
|
+
```
|
|
161
|
+
|
|
162
|
+
In this example, the `response` constant will contain mock data, simulating a response from the OpenAI API:
|
|
163
|
+
|
|
164
|
+
```javascript
|
|
165
|
+
{
|
|
166
|
+
choices: [
|
|
167
|
+
{
|
|
168
|
+
finish_reason: 'stop',
|
|
169
|
+
index: 0,
|
|
170
|
+
message: [Object],
|
|
171
|
+
logprobs: null
|
|
172
|
+
}
|
|
173
|
+
],
|
|
174
|
+
created: 1707040459,
|
|
175
|
+
id: 'chatcmpl-tggOnwW8Lp2XiwQ8dmHHAcNYJ8CfzR',
|
|
176
|
+
model: 'gpt-3.5-mock',
|
|
177
|
+
object: 'chat.completion',
|
|
178
|
+
usage: { completion_tokens: 17, prompt_tokens: 57, total_tokens: 74 }
|
|
179
|
+
}
|
|
180
|
+
```
|
|
181
|
+
|
|
182
|
+
The library also supports mocking `stream` responses
|
|
183
|
+
|
|
184
|
+
```js
|
|
185
|
+
// Call the mockOpenAIResponse function once to set up the mock
|
|
186
|
+
mockOpenAIResponse();
|
|
187
|
+
// Now, when you call the OpenAI API, it will return a mock response
|
|
188
|
+
const response = await openai.chat.completions.create({
|
|
189
|
+
model: 'gpt-3.5',
|
|
190
|
+
stream: true,
|
|
191
|
+
messages: [
|
|
192
|
+
{ role: 'system', content: "You're an expert chef" },
|
|
193
|
+
{ role: 'user', content: 'Suggest at least 5 recipes' },
|
|
194
|
+
],
|
|
195
|
+
});
|
|
196
|
+
|
|
197
|
+
// then read it
|
|
198
|
+
for await (const part of response) {
|
|
199
|
+
console.log(part.choices[0]?.delta?.content || '');
|
|
200
|
+
}
|
|
201
|
+
```
|
|
202
|
+
|
|
203
|
+
### Embeddings
|
|
204
|
+
|
|
205
|
+
The embeddings endpoint (`/v1/embeddings`) is mocked with full API compatibility: model dimension defaults (`1536` for `ada-002`/`3-small`, `3072` for `3-large`), the `dimensions` parameter, `encoding_format: 'base64'`, array and token-array inputs, usage reporting, and the same validation errors as the real API (missing parameters, non-embedding models, oversized arrays, etc.).
|
|
206
|
+
|
|
207
|
+
```js
|
|
208
|
+
mockOpenAIResponse(true);
|
|
209
|
+
|
|
210
|
+
const response = await openai.embeddings.create({
|
|
211
|
+
model: 'text-embedding-3-small',
|
|
212
|
+
input: 'The food was delicious and the waiter...',
|
|
213
|
+
});
|
|
214
|
+
|
|
215
|
+
// {
|
|
216
|
+
// object: 'list',
|
|
217
|
+
// data: [
|
|
218
|
+
// {
|
|
219
|
+
// object: 'embedding',
|
|
220
|
+
// index: 0,
|
|
221
|
+
// embedding: [0.0231, -0.0093, ...], // 1536 floats
|
|
222
|
+
// },
|
|
223
|
+
// ],
|
|
224
|
+
// model: 'text-embedding-3-small',
|
|
225
|
+
// usage: { prompt_tokens: 11, total_tokens: 11 },
|
|
226
|
+
// }
|
|
227
|
+
```
|
|
228
|
+
|
|
229
|
+
**Deterministic vectors.** Embedding vectors are derived from a hash of the input (not random), so the same input always produces the exact same unit-length vector — independent of seeds, call order, or test runs. This makes them ideal for snapshot tests and for verifying cosine-similarity/deduplication logic in CI:
|
|
230
|
+
|
|
231
|
+
```js
|
|
232
|
+
const [a, b] = await Promise.all([
|
|
233
|
+
openai.embeddings.create({ model: 'text-embedding-3-small', input: 'hello world' }),
|
|
234
|
+
openai.embeddings.create({ model: 'text-embedding-3-small', input: 'hello world' }),
|
|
235
|
+
]);
|
|
236
|
+
|
|
237
|
+
console.log(JSON.stringify(a) === JSON.stringify(b)); // true
|
|
238
|
+
console.log(a.data[0].embedding); // always the same 1536 floats
|
|
239
|
+
```
|
|
240
|
+
|
|
241
|
+
> Note: vectors are deterministic but not semantically meaningful — two different strings do not produce similar vectors. The value is reproducibility, not semantic search accuracy.
|
|
242
|
+
|
|
243
|
+
## Consistent Outputs for Testing
|
|
244
|
+
|
|
245
|
+
The library provides several mechanisms to achieve consistent, deterministic outputs for reliable testing:
|
|
246
|
+
|
|
247
|
+
### Seed-based Consistency
|
|
248
|
+
|
|
249
|
+
Use seeds to ensure reproducible responses across test runs:
|
|
250
|
+
|
|
251
|
+
```js
|
|
252
|
+
// Set up mock with a fixed seed
|
|
253
|
+
const mock = mockOpenAIResponse(true, { seed: 12345 });
|
|
254
|
+
|
|
255
|
+
// Multiple calls will return identical responses
|
|
256
|
+
const response1 = await openai.chat.completions.create({
|
|
257
|
+
model: 'gpt-3.5-turbo',
|
|
258
|
+
messages: [{ role: 'user', content: 'Hello' }],
|
|
259
|
+
});
|
|
260
|
+
|
|
261
|
+
const response2 = await openai.chat.completions.create({
|
|
262
|
+
model: 'gpt-3.5-turbo',
|
|
263
|
+
messages: [{ role: 'user', content: 'Hello' }],
|
|
264
|
+
});
|
|
265
|
+
|
|
266
|
+
// response1 and response2 will be identical
|
|
267
|
+
console.log(JSON.stringify(response1) === JSON.stringify(response2)); // true
|
|
268
|
+
```
|
|
269
|
+
|
|
270
|
+
### Fixed Response Templates
|
|
271
|
+
|
|
272
|
+
For maximum consistency, use predefined response templates:
|
|
273
|
+
|
|
274
|
+
```js
|
|
275
|
+
// Enable fixed responses
|
|
276
|
+
const mock = mockOpenAIResponse(true, { useFixedResponses: true });
|
|
277
|
+
|
|
278
|
+
const response = await openai.chat.completions.create({
|
|
279
|
+
model: 'gpt-3.5-turbo',
|
|
280
|
+
messages: [{ role: 'user', content: 'Any message' }],
|
|
281
|
+
});
|
|
282
|
+
|
|
283
|
+
// Will always return the same fixed response
|
|
284
|
+
console.log(response.choices[0].message.content);
|
|
285
|
+
// "This is a consistent test response."
|
|
286
|
+
```
|
|
287
|
+
|
|
288
|
+
### Runtime Seed Management
|
|
289
|
+
|
|
290
|
+
Change seeds during runtime for different test scenarios:
|
|
291
|
+
|
|
292
|
+
```js
|
|
293
|
+
const mock = mockOpenAIResponse(true);
|
|
294
|
+
|
|
295
|
+
// Test scenario A
|
|
296
|
+
mock.setSeed(12345);
|
|
297
|
+
const responseA = await openai.chat.completions.create({...});
|
|
298
|
+
|
|
299
|
+
// Test scenario B
|
|
300
|
+
mock.setSeed(54321);
|
|
301
|
+
const responseB = await openai.chat.completions.create({...});
|
|
302
|
+
|
|
303
|
+
// Reset to random behavior
|
|
304
|
+
mock.resetSeed();
|
|
305
|
+
const responseRandom = await openai.chat.completions.create({...});
|
|
306
|
+
```
|
|
307
|
+
|
|
308
|
+
For comprehensive examples and best practices, see [CONSISTENCY_EXAMPLES.md](./CONSISTENCY_EXAMPLES.md).
|
|
309
|
+
|
|
310
|
+
## Intercepted URLs
|
|
311
|
+
|
|
312
|
+
This module uses the `nock` library to intercept HTTP calls to OpenAI API endpoints. By default, it intercepts:
|
|
313
|
+
|
|
314
|
+
- `https://api.openai.com/v1/chat/completions`: This endpoint is used for generating chat completions.
|
|
315
|
+
- `https://api.openai.com/v1/images/generations`: This endpoint is used for generating images.
|
|
316
|
+
- `https://api.openai.com/v1/embeddings`: This endpoint is used for generating embeddings.
|
|
317
|
+
|
|
318
|
+
When using the `baseUrl` option, the intercepted URLs will use your configured base URL instead:
|
|
319
|
+
|
|
320
|
+
```js
|
|
321
|
+
// Custom base URL example
|
|
322
|
+
mockOpenAIResponse(true, { baseUrl: 'https://your-api.example.com' });
|
|
323
|
+
|
|
324
|
+
// Will intercept:
|
|
325
|
+
// - https://your-api.example.com/v1/chat/completions
|
|
326
|
+
// - https://your-api.example.com/v1/images/generations
|
|
327
|
+
// - https://your-api.example.com/v1/embeddings
|
|
328
|
+
```
|
|
329
|
+
|
|
330
|
+
## TypeScript Support
|
|
331
|
+
|
|
332
|
+
This package includes TypeScript definitions out of the box. After installing the package, you can use it with full type support:
|
|
333
|
+
|
|
334
|
+
```typescript
|
|
335
|
+
import { mockOpenAIResponse, MockOptions } from 'openai-api-mock';
|
|
336
|
+
|
|
337
|
+
// Configure with TypeScript types
|
|
338
|
+
const options: MockOptions = {
|
|
339
|
+
includeErrors: true, // Optional: simulate random API errors
|
|
340
|
+
latency: 1000, // Optional: add 1 second delay
|
|
341
|
+
logRequests: true, // Optional: log requests to console
|
|
342
|
+
seed: 12345, // Optional: seed for consistent responses
|
|
343
|
+
useFixedResponses: true, // Optional: use fixed response templates
|
|
344
|
+
baseUrl: 'https://api.openai.com', // Optional: custom base URL
|
|
345
|
+
};
|
|
346
|
+
|
|
347
|
+
const mock = mockOpenAIResponse(true, options);
|
|
348
|
+
|
|
349
|
+
// TypeScript provides full type checking and autocompletion
|
|
350
|
+
console.log(mock.isActive); // boolean
|
|
351
|
+
mock.stopMocking(); // function
|
|
352
|
+
mock.setSeed(54321); // function with type checking
|
|
353
|
+
mock.resetSeed(); // function
|
|
354
|
+
|
|
355
|
+
// Template methods with type safety
|
|
356
|
+
const templates = mock.getResponseTemplates(); // Record<string, any>
|
|
357
|
+
const customTemplate = mock.createResponseTemplate('SIMPLE_CHAT', {
|
|
358
|
+
choices: [{ message: { content: 'Custom content' } }],
|
|
359
|
+
});
|
|
360
|
+
|
|
361
|
+
// Custom endpoints with type safety
|
|
362
|
+
mock.addCustomEndpoint('POST', '/v1/custom', (uri, body) => {
|
|
363
|
+
return [200, { custom: 'response' }];
|
|
364
|
+
});
|
|
365
|
+
```
|
|
366
|
+
|
|
367
|
+
## Dependencies
|
|
368
|
+
|
|
369
|
+
This module depends on the following npm packages:
|
|
370
|
+
|
|
371
|
+
- `nock` : For intercepting HTTP calls.
|
|
372
|
+
- `@faker-js/faker` : For generating fake data.
|
|
373
|
+
|
|
374
|
+
## License
|
|
375
|
+
|
|
376
|
+
This project is licensed under the MIT License.
|