openai-api-mock 0.3.0 → 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/LICENSE.md +21 -21
- package/README.md +43 -2
- package/dist/index.cjs +1813 -1649
- package/dist/index.cjs.map +1 -1
- package/dist/index.js +1813 -1649
- package/dist/index.js.map +1 -1
- package/index.d.ts +76 -76
- package/package.json +9 -4
package/LICENSE.md
CHANGED
|
@@ -1,21 +1,21 @@
|
|
|
1
|
-
The MIT License (MIT)
|
|
2
|
-
|
|
3
|
-
Copyright (c) 2024 Nabil Chiheb
|
|
4
|
-
|
|
5
|
-
Permission is hereby granted, free of charge, to any person obtaining a copy
|
|
6
|
-
of this software and associated documentation files (the "Software"), to deal
|
|
7
|
-
in the Software without restriction, including without limitation the rights
|
|
8
|
-
to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
|
|
9
|
-
copies of the Software, and to permit persons to whom the Software is
|
|
10
|
-
furnished to do so, subject to the following conditions:
|
|
11
|
-
|
|
12
|
-
The above copyright notice and this permission notice shall be included in all
|
|
13
|
-
copies or substantial portions of the Software.
|
|
14
|
-
|
|
15
|
-
THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
|
|
16
|
-
IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
|
|
17
|
-
FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
|
|
18
|
-
AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
|
|
19
|
-
LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
|
|
20
|
-
OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE
|
|
21
|
-
SOFTWARE.
|
|
1
|
+
The MIT License (MIT)
|
|
2
|
+
|
|
3
|
+
Copyright (c) 2024 Nabil Chiheb
|
|
4
|
+
|
|
5
|
+
Permission is hereby granted, free of charge, to any person obtaining a copy
|
|
6
|
+
of this software and associated documentation files (the "Software"), to deal
|
|
7
|
+
in the Software without restriction, including without limitation the rights
|
|
8
|
+
to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
|
|
9
|
+
copies of the Software, and to permit persons to whom the Software is
|
|
10
|
+
furnished to do so, subject to the following conditions:
|
|
11
|
+
|
|
12
|
+
The above copyright notice and this permission notice shall be included in all
|
|
13
|
+
copies or substantial portions of the Software.
|
|
14
|
+
|
|
15
|
+
THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
|
|
16
|
+
IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
|
|
17
|
+
FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
|
|
18
|
+
AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
|
|
19
|
+
LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
|
|
20
|
+
OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE
|
|
21
|
+
SOFTWARE.
|
package/README.md
CHANGED
|
@@ -12,8 +12,7 @@ The module supports the following OpenAI API endpoints:
|
|
|
12
12
|
- chat completions with streaming
|
|
13
13
|
- chat completions with functions
|
|
14
14
|
- image generations
|
|
15
|
-
|
|
16
|
-
> This module is powering the sandbox mode for [Aipify](https://aipify.co).
|
|
15
|
+
- embeddings
|
|
17
16
|
|
|
18
17
|
## Table of Contents
|
|
19
18
|
|
|
@@ -201,6 +200,46 @@ for await (const part of response) {
|
|
|
201
200
|
}
|
|
202
201
|
```
|
|
203
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
|
+
|
|
204
243
|
## Consistent Outputs for Testing
|
|
205
244
|
|
|
206
245
|
The library provides several mechanisms to achieve consistent, deterministic outputs for reliable testing:
|
|
@@ -274,6 +313,7 @@ This module uses the `nock` library to intercept HTTP calls to OpenAI API endpoi
|
|
|
274
313
|
|
|
275
314
|
- `https://api.openai.com/v1/chat/completions`: This endpoint is used for generating chat completions.
|
|
276
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.
|
|
277
317
|
|
|
278
318
|
When using the `baseUrl` option, the intercepted URLs will use your configured base URL instead:
|
|
279
319
|
|
|
@@ -284,6 +324,7 @@ mockOpenAIResponse(true, { baseUrl: 'https://your-api.example.com' });
|
|
|
284
324
|
// Will intercept:
|
|
285
325
|
// - https://your-api.example.com/v1/chat/completions
|
|
286
326
|
// - https://your-api.example.com/v1/images/generations
|
|
327
|
+
// - https://your-api.example.com/v1/embeddings
|
|
287
328
|
```
|
|
288
329
|
|
|
289
330
|
## TypeScript Support
|