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