openai-api-mock 0.1.21 → 0.1.22
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/main.js +5 -10
- package/main.test.js +3 -2
- package/package.json +3 -3
- package/readme.md +7 -7
- package/src/chat.js +3 -2
- package/src/chat.stream.js +4 -3
- package/src/image.js +3 -3
- package/src/utils/fakeDataGenerators.js +2 -2
- package/src/utils/responseGenerators.js +4 -3
package/main.js
CHANGED
|
@@ -1,9 +1,7 @@
|
|
|
1
|
-
|
|
2
|
-
|
|
3
|
-
|
|
4
|
-
|
|
5
|
-
const { createChatStream } = require('./src/chat.stream.js');
|
|
6
|
-
const { getImageResponce } = require('./src/image.js');
|
|
1
|
+
import nock from 'nock';
|
|
2
|
+
import { getChatResponce } from './src/chat.js';
|
|
3
|
+
import { createChatStream } from './src/chat.stream.js';
|
|
4
|
+
import { getImageResponce } from './src/image.js';
|
|
7
5
|
|
|
8
6
|
const OPEN_AI_BASE_URL = 'https://api.openai.com';
|
|
9
7
|
const CHAT_COMPLETIONS_ENDPOINT = '/v1/chat/completions';
|
|
@@ -41,7 +39,4 @@ function stopMocking() {
|
|
|
41
39
|
nock.cleanAll();
|
|
42
40
|
}
|
|
43
41
|
|
|
44
|
-
|
|
45
|
-
mockOpenAIResponse,
|
|
46
|
-
stopMocking,
|
|
47
|
-
};
|
|
42
|
+
export { mockOpenAIResponse, stopMocking };
|
package/main.test.js
CHANGED
|
@@ -1,5 +1,6 @@
|
|
|
1
|
-
|
|
2
|
-
|
|
1
|
+
import { mockOpenAIResponse, stopMocking } from './main.js'
|
|
2
|
+
import OpenAI from 'openai';
|
|
3
|
+
|
|
3
4
|
const openai = new OpenAI({ apiKey: "OPENAI_API_KEY" });
|
|
4
5
|
|
|
5
6
|
describe('Mock OpenAI Chat & Image generation API', () => {
|
package/package.json
CHANGED
|
@@ -1,6 +1,6 @@
|
|
|
1
1
|
{
|
|
2
2
|
"name": "openai-api-mock",
|
|
3
|
-
"version": "0.1.
|
|
3
|
+
"version": "0.1.22",
|
|
4
4
|
"description": "A Node.js module for mocking OpenAI API responses in a development environment",
|
|
5
5
|
"repository": {
|
|
6
6
|
"type": "git",
|
|
@@ -17,7 +17,7 @@
|
|
|
17
17
|
"interceptor"
|
|
18
18
|
],
|
|
19
19
|
"scripts": {
|
|
20
|
-
"test":
|
|
20
|
+
"test":"node --experimental-vm-modules node_modules/jest/bin/jest.js"
|
|
21
21
|
},
|
|
22
22
|
"author": "Chiheb Nabil",
|
|
23
23
|
"license": "MIT",
|
|
@@ -30,4 +30,4 @@
|
|
|
30
30
|
"jest": "^29.7.0",
|
|
31
31
|
"openai": "^4.52.4"
|
|
32
32
|
}
|
|
33
|
-
}
|
|
33
|
+
}
|
package/readme.md
CHANGED
|
@@ -20,10 +20,9 @@ You can install this module using npm as a dev dependency :
|
|
|
20
20
|
npm install -D openai-api-mock
|
|
21
21
|
```
|
|
22
22
|
|
|
23
|
-
|
|
24
23
|
## Usage
|
|
25
24
|
```js
|
|
26
|
-
|
|
25
|
+
import { mockOpenAIResponse } from 'openai-api-mock';
|
|
27
26
|
```
|
|
28
27
|
|
|
29
28
|
Then, call the mockOpenAIResponse function to set up the mock response:
|
|
@@ -31,17 +30,18 @@ Then, call the mockOpenAIResponse function to set up the mock response:
|
|
|
31
30
|
```js
|
|
32
31
|
mockOpenAIResponse();
|
|
33
32
|
```
|
|
34
|
-
This function intercepts HTTP calls to the OpenAI endpoint and returns a mock response. The mock response is generated based on the requestBody of the code
|
|
33
|
+
This function intercepts HTTP calls to the OpenAI endpoint and returns a mock response. The mock response is generated based on the requestBody of the code, and it supports complex (function call) requestBody structures like arrays and nested objects.
|
|
35
34
|
|
|
36
35
|
```js
|
|
37
|
-
mockOpenAIResponse(
|
|
36
|
+
mockOpenAIResponse(true);
|
|
38
37
|
```
|
|
38
|
+
|
|
39
39
|
The force parameter is a boolean that determines whether the mock response should be used regardless of the environment.
|
|
40
40
|
If force is true, the mock response will be used regardless of the environment.
|
|
41
41
|
|
|
42
42
|
If force is `false` or not provided, the mock response will only be used if the `NODE_ENV` environment variable is set to `development`.
|
|
43
43
|
|
|
44
|
-
### Example
|
|
44
|
+
### Example responses
|
|
45
45
|
|
|
46
46
|
```js
|
|
47
47
|
// Call the mockOpenAIResponse function once to set up the mock
|
|
@@ -51,7 +51,7 @@ mockOpenAIResponse()
|
|
|
51
51
|
const response = await openai.chat.completions.create({
|
|
52
52
|
model: "gpt-3.5",
|
|
53
53
|
messages: [
|
|
54
|
-
{ role: 'system', content: "You'
|
|
54
|
+
{ role: 'system', content: "You're an expert chef" },
|
|
55
55
|
{ role: 'user', content: "Suggest at least 5 recipes" },
|
|
56
56
|
]
|
|
57
57
|
});
|
|
@@ -85,7 +85,7 @@ const response = await openai.chat.completions.create({
|
|
|
85
85
|
model: "gpt-3.5",
|
|
86
86
|
stream : true,
|
|
87
87
|
messages: [
|
|
88
|
-
{ role: 'system', content: "You'
|
|
88
|
+
{ role: 'system', content: "You're an expert chef" },
|
|
89
89
|
{ role: 'user', content: "Suggest at least 5 recipes" },
|
|
90
90
|
]
|
|
91
91
|
});
|
package/src/chat.js
CHANGED
|
@@ -1,4 +1,4 @@
|
|
|
1
|
-
|
|
1
|
+
import { createDefaultResponse, createFunctionCallingResponse } from './utils/responseGenerators.js';
|
|
2
2
|
|
|
3
3
|
function getChatResponce(requestBody) {
|
|
4
4
|
const created = Math.floor(Date.now() / 1000);
|
|
@@ -10,6 +10,7 @@ function getChatResponce(requestBody) {
|
|
|
10
10
|
return createFunctionCallingResponse(requestBody, created);
|
|
11
11
|
}
|
|
12
12
|
|
|
13
|
-
|
|
13
|
+
|
|
14
|
+
export {
|
|
14
15
|
getChatResponce,
|
|
15
16
|
};
|
package/src/chat.stream.js
CHANGED
|
@@ -1,5 +1,5 @@
|
|
|
1
|
-
|
|
2
|
-
|
|
1
|
+
import { Readable } from 'node:stream';
|
|
2
|
+
import { getSteamChatObject } from './utils/responseGenerators.js'
|
|
3
3
|
|
|
4
4
|
function createChatStream() {
|
|
5
5
|
const stream = new Readable({
|
|
@@ -27,6 +27,7 @@ function createChatStream() {
|
|
|
27
27
|
return stream;
|
|
28
28
|
}
|
|
29
29
|
|
|
30
|
-
|
|
30
|
+
|
|
31
|
+
export {
|
|
31
32
|
createChatStream,
|
|
32
33
|
};
|
package/src/image.js
CHANGED
|
@@ -1,5 +1,4 @@
|
|
|
1
|
-
|
|
2
|
-
|
|
1
|
+
import { faker } from '@faker-js/faker';
|
|
3
2
|
|
|
4
3
|
function getImageResponce(requestBody) {
|
|
5
4
|
|
|
@@ -19,6 +18,7 @@ function getImageResponce(requestBody) {
|
|
|
19
18
|
}
|
|
20
19
|
|
|
21
20
|
|
|
22
|
-
|
|
21
|
+
|
|
22
|
+
export {
|
|
23
23
|
getImageResponce,
|
|
24
24
|
};
|
|
@@ -1,4 +1,4 @@
|
|
|
1
|
-
|
|
1
|
+
import { faker } from '@faker-js/faker';
|
|
2
2
|
|
|
3
3
|
function generateToolCallArguments(requestBody) {
|
|
4
4
|
const { parameters } = requestBody.tools[0].function;
|
|
@@ -97,7 +97,7 @@ function generateFakeStringData(name) {
|
|
|
97
97
|
}
|
|
98
98
|
}
|
|
99
99
|
|
|
100
|
-
|
|
100
|
+
export {
|
|
101
101
|
generateToolCallArguments,
|
|
102
102
|
generateFunctionCallArguments,
|
|
103
103
|
generateFakeData,
|
|
@@ -1,5 +1,5 @@
|
|
|
1
|
-
|
|
2
|
-
|
|
1
|
+
import { faker } from '@faker-js/faker';
|
|
2
|
+
import { generateFunctionCallArguments, generateToolCallArguments } from './fakeDataGenerators.js'
|
|
3
3
|
|
|
4
4
|
function createDefaultResponse(created) {
|
|
5
5
|
return {
|
|
@@ -102,7 +102,8 @@ function getSteamChatObject() {
|
|
|
102
102
|
return JSON.stringify(ob);
|
|
103
103
|
}
|
|
104
104
|
|
|
105
|
-
|
|
105
|
+
|
|
106
|
+
export {
|
|
106
107
|
createDefaultResponse,
|
|
107
108
|
createFunctionCallingResponse,
|
|
108
109
|
createToolCallObject,
|