openai-api-mock 0.1.15 → 0.1.17
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 -0
- package/main.test.js +127 -146
- package/package.json +4 -4
- package/readme.md +3 -1
- package/renovate.json +1 -3
package/LICENSE.md
ADDED
|
@@ -0,0 +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.
|
package/main.test.js
CHANGED
|
@@ -1,6 +1,4 @@
|
|
|
1
1
|
const { mockOpenAIResponse, stopMocking } = require('./main');
|
|
2
|
-
const { Stream } = require('stream');
|
|
3
|
-
|
|
4
2
|
const OpenAI = require('openai');
|
|
5
3
|
const openai = new OpenAI({ apiKey: "OPENAI_API_KEY" });
|
|
6
4
|
|
|
@@ -8,49 +6,66 @@ describe('Mock OpenAI Chat & Image generation API', () => {
|
|
|
8
6
|
beforeEach(() => {
|
|
9
7
|
mockOpenAIResponse(true);
|
|
10
8
|
});
|
|
9
|
+
|
|
11
10
|
afterEach(() => {
|
|
12
11
|
stopMocking();
|
|
13
12
|
});
|
|
14
|
-
|
|
15
|
-
|
|
13
|
+
|
|
14
|
+
describe('Chat Completion Tests', () => {
|
|
15
|
+
it('should mock the chat completion with correct properties', async () => {
|
|
16
16
|
const response = await openai.chat.completions.create({
|
|
17
|
-
model: "gpt-3.5",
|
|
17
|
+
model: "gpt-3.5-turbo",
|
|
18
18
|
messages: [
|
|
19
|
-
{ role: 'system', content: "You'
|
|
19
|
+
{ role: 'system', content: "You're an expert chef" },
|
|
20
20
|
{ role: 'user', content: "Suggest at least 5 recipes" },
|
|
21
21
|
]
|
|
22
22
|
});
|
|
23
|
-
|
|
23
|
+
|
|
24
|
+
expect(response).toHaveProperty('id');
|
|
25
|
+
expect(response).toHaveProperty('object', 'chat.completion');
|
|
26
|
+
expect(response).toHaveProperty('created');
|
|
24
27
|
expect(response.model).toEqual('gpt-3.5-mock');
|
|
25
|
-
|
|
26
|
-
|
|
27
|
-
|
|
28
|
-
|
|
28
|
+
expect(response.choices).toBeInstanceOf(Array);
|
|
29
|
+
expect(response.choices[0]).toHaveProperty('index', 0);
|
|
30
|
+
expect(response.choices[0]).toHaveProperty('message');
|
|
31
|
+
expect(response.choices[0].message).toHaveProperty('role', 'assistant');
|
|
32
|
+
expect(response.choices[0].message).toHaveProperty('content');
|
|
33
|
+
expect(response.choices[0]).toHaveProperty('finish_reason', 'stop');
|
|
34
|
+
expect(response).toHaveProperty('usage');
|
|
35
|
+
expect(response.usage).toHaveProperty('prompt_tokens');
|
|
36
|
+
expect(response.usage).toHaveProperty('completion_tokens');
|
|
37
|
+
expect(response.usage).toHaveProperty('total_tokens');
|
|
38
|
+
});
|
|
29
39
|
|
|
30
|
-
|
|
31
|
-
try {
|
|
40
|
+
it('should mock the streaming chat completion', async () => {
|
|
32
41
|
const response = await openai.chat.completions.create({
|
|
33
|
-
model: "gpt-3.5",
|
|
42
|
+
model: "gpt-3.5-turbo",
|
|
34
43
|
stream: true,
|
|
35
44
|
messages: [
|
|
36
45
|
{ role: 'user', content: "What's the biggest country in the world" },
|
|
37
46
|
]
|
|
38
47
|
});
|
|
39
48
|
|
|
49
|
+
let streamContent = '';
|
|
40
50
|
for await (const part of response) {
|
|
41
|
-
|
|
51
|
+
expect(part).toHaveProperty('id');
|
|
52
|
+
expect(part).toHaveProperty('object', 'chat.completion.chunk');
|
|
53
|
+
expect(part).toHaveProperty('created');
|
|
54
|
+
expect(part).toHaveProperty('model', 'gpt-3.5-mock');
|
|
55
|
+
expect(part.choices[0]).toHaveProperty('index', 0);
|
|
56
|
+
expect(part.choices[0]).toHaveProperty('delta');
|
|
57
|
+
if (part.choices[0]?.delta?.content) {
|
|
58
|
+
streamContent += part.choices[0].delta.content;
|
|
59
|
+
}
|
|
42
60
|
}
|
|
43
|
-
|
|
44
|
-
|
|
45
|
-
}
|
|
46
|
-
});
|
|
61
|
+
expect(streamContent).not.toBe('');
|
|
62
|
+
});
|
|
47
63
|
|
|
48
|
-
|
|
49
|
-
try {
|
|
64
|
+
it('should mock the chat completion with function call', async () => {
|
|
50
65
|
const response = await openai.chat.completions.create({
|
|
51
|
-
model: "gpt-3.5",
|
|
66
|
+
model: "gpt-3.5-turbo",
|
|
52
67
|
messages: [
|
|
53
|
-
{ role: 'system', content: "You'
|
|
68
|
+
{ role: 'system', content: "You're an expert chef" },
|
|
54
69
|
{ role: 'user', content: "Suggest at least 5 recipes" },
|
|
55
70
|
],
|
|
56
71
|
functions: [
|
|
@@ -66,26 +81,10 @@ describe('Mock OpenAI Chat & Image generation API', () => {
|
|
|
66
81
|
items: {
|
|
67
82
|
type: 'object',
|
|
68
83
|
properties: {
|
|
69
|
-
name: {
|
|
70
|
-
|
|
71
|
-
|
|
72
|
-
},
|
|
73
|
-
ingredients: {
|
|
74
|
-
type: 'array',
|
|
75
|
-
description: 'The ingredients needed for the recipe',
|
|
76
|
-
items: {
|
|
77
|
-
type: 'string',
|
|
78
|
-
description: 'The name of the ingredient',
|
|
79
|
-
},
|
|
80
|
-
},
|
|
81
|
-
instructions: {
|
|
82
|
-
type: 'string',
|
|
83
|
-
description: 'Detailed instructions on how to make the recipe',
|
|
84
|
-
},
|
|
85
|
-
serving: {
|
|
86
|
-
type: 'string',
|
|
87
|
-
description: 'The number of people the recipe serves',
|
|
88
|
-
},
|
|
84
|
+
name: { type: 'string' },
|
|
85
|
+
ingredients: { type: 'array', items: { type: 'string' } },
|
|
86
|
+
instructions: { type: 'string' },
|
|
87
|
+
serving: { type: 'string' },
|
|
89
88
|
}
|
|
90
89
|
}
|
|
91
90
|
},
|
|
@@ -96,27 +95,56 @@ describe('Mock OpenAI Chat & Image generation API', () => {
|
|
|
96
95
|
],
|
|
97
96
|
function_call: { name: 'get_recipes' },
|
|
98
97
|
});
|
|
98
|
+
|
|
99
99
|
expect(response.model).toEqual('gpt-3.5-mock');
|
|
100
100
|
expect(response.choices[0]).toHaveProperty('finish_reason', 'function_call');
|
|
101
101
|
expect(response.choices[0].message.function_call).toHaveProperty('name', 'get_recipes');
|
|
102
102
|
expect(response.choices[0].message.function_call).toHaveProperty('arguments');
|
|
103
|
-
|
|
104
|
-
|
|
105
|
-
|
|
106
|
-
|
|
107
|
-
|
|
108
|
-
|
|
109
|
-
|
|
103
|
+
const args = JSON.parse(response.choices[0].message.function_call.arguments);
|
|
104
|
+
expect(args).toHaveProperty('recipes');
|
|
105
|
+
expect(args.recipes).toBeInstanceOf(Array);
|
|
106
|
+
expect(args.recipes.length).toBeGreaterThanOrEqual(5);
|
|
107
|
+
args.recipes.forEach(recipe => {
|
|
108
|
+
expect(recipe).toHaveProperty('name');
|
|
109
|
+
expect(recipe).toHaveProperty('ingredients');
|
|
110
|
+
expect(recipe).toHaveProperty('instructions');
|
|
111
|
+
expect(recipe).toHaveProperty('serving');
|
|
112
|
+
});
|
|
113
|
+
});
|
|
110
114
|
|
|
111
|
-
|
|
112
|
-
try {
|
|
115
|
+
it('should mock the chat completion with tool calls', async () => {
|
|
113
116
|
const response = await openai.chat.completions.create({
|
|
114
|
-
model: "gpt-3.5",
|
|
117
|
+
model: "gpt-3.5-turbo",
|
|
115
118
|
messages: [
|
|
116
|
-
{ role: 'system', content: "You'
|
|
117
|
-
{ role: 'user', content: "Suggest at least 5 recipes" },
|
|
119
|
+
{ role: 'system', content: "You're an expert chef" },
|
|
120
|
+
{ role: 'user', content: "Suggest at least 5 recipes and send them to my email" },
|
|
118
121
|
],
|
|
119
122
|
tools: [
|
|
123
|
+
{
|
|
124
|
+
type: "function",
|
|
125
|
+
function: {
|
|
126
|
+
name: 'get_recipes',
|
|
127
|
+
description: 'Suggest at least 5 recipes based on the ingredients you have',
|
|
128
|
+
parameters: {
|
|
129
|
+
type: "object",
|
|
130
|
+
properties: {
|
|
131
|
+
recipes: {
|
|
132
|
+
type: "array",
|
|
133
|
+
items: {
|
|
134
|
+
type: 'object',
|
|
135
|
+
properties: {
|
|
136
|
+
name: { type: 'string' },
|
|
137
|
+
ingredients: { type: 'array', items: { type: 'string' } },
|
|
138
|
+
instructions: { type: 'string' },
|
|
139
|
+
serving: { type: 'string' },
|
|
140
|
+
}
|
|
141
|
+
}
|
|
142
|
+
},
|
|
143
|
+
}
|
|
144
|
+
},
|
|
145
|
+
required: ["recipes"],
|
|
146
|
+
}
|
|
147
|
+
},
|
|
120
148
|
{
|
|
121
149
|
type: "function",
|
|
122
150
|
function: {
|
|
@@ -125,118 +153,71 @@ describe('Mock OpenAI Chat & Image generation API', () => {
|
|
|
125
153
|
parameters: {
|
|
126
154
|
type: "object",
|
|
127
155
|
properties: {
|
|
128
|
-
email: {
|
|
129
|
-
|
|
130
|
-
|
|
131
|
-
}
|
|
156
|
+
email: { type: "string" },
|
|
157
|
+
subject: { type: "string" },
|
|
158
|
+
body: { type: "string" },
|
|
132
159
|
}
|
|
133
160
|
},
|
|
134
|
-
required: ["email"],
|
|
161
|
+
required: ["email", "subject", "body"],
|
|
135
162
|
}
|
|
136
|
-
|
|
137
|
-
|
|
138
|
-
|
|
139
|
-
tool_choice: { name: 'send_email' },
|
|
163
|
+
}
|
|
164
|
+
],
|
|
165
|
+
tool_choice: "auto",
|
|
140
166
|
});
|
|
167
|
+
|
|
141
168
|
expect(response.model).toEqual('gpt-3.5-mock');
|
|
142
169
|
expect(response.choices[0]).toHaveProperty('finish_reason', 'tool_calls');
|
|
143
|
-
expect(response.choices[0].message.tool_calls
|
|
144
|
-
expect(response.choices[0].message.tool_calls[0].function).toHaveProperty('arguments');
|
|
145
|
-
expect(JSON.parse(response.choices[0].message.tool_calls[0].function.arguments)).toHaveProperty('email');
|
|
170
|
+
expect(response.choices[0].message.tool_calls).toBeInstanceOf(Array);
|
|
146
171
|
|
|
147
|
-
|
|
148
|
-
|
|
149
|
-
}
|
|
150
|
-
|
|
151
|
-
});
|
|
152
|
-
|
|
153
|
-
|
|
154
|
-
it('should mock the chat completion with function call (tools)', async () => {
|
|
155
|
-
const response = await openai.chat.completions.create({
|
|
156
|
-
model: "gpt-3.5",
|
|
157
|
-
messages: [
|
|
158
|
-
{ role: 'system', content: "You'r an expert chef" },
|
|
159
|
-
{ role: 'user', content: "Suggest at least 5 recipes" },
|
|
160
|
-
],
|
|
161
|
-
tools: [
|
|
162
|
-
{
|
|
163
|
-
type: "function",
|
|
164
|
-
function: {
|
|
165
|
-
name: 'get_recipes',
|
|
166
|
-
description: 'Suggest at least 5 recipes based on the ingredients you have',
|
|
167
|
-
parameters: {
|
|
168
|
-
type: "object",
|
|
169
|
-
properties: {
|
|
170
|
-
recipes: {
|
|
171
|
-
type: "array",
|
|
172
|
-
description: "The recipes that can be made with the ingredients",
|
|
173
|
-
items: {
|
|
174
|
-
type: 'object',
|
|
175
|
-
properties: {
|
|
176
|
-
name: {
|
|
177
|
-
type: 'string',
|
|
178
|
-
description: 'The name of the recipe',
|
|
179
|
-
},
|
|
180
|
-
ingredients: {
|
|
181
|
-
type: 'array',
|
|
182
|
-
description: 'The ingredients needed for the recipe',
|
|
183
|
-
items: {
|
|
184
|
-
type: 'string',
|
|
185
|
-
description: 'The name of the ingredient',
|
|
186
|
-
},
|
|
187
|
-
},
|
|
188
|
-
instructions: {
|
|
189
|
-
type: 'string',
|
|
190
|
-
description: 'Detailed instructions on how to make the recipe',
|
|
191
|
-
},
|
|
192
|
-
serving: {
|
|
193
|
-
type: 'string',
|
|
194
|
-
description: 'The number of people the recipe serves',
|
|
195
|
-
},
|
|
196
|
-
}
|
|
197
|
-
}
|
|
198
|
-
},
|
|
199
|
-
}
|
|
200
|
-
},
|
|
201
|
-
required: ["recipes"],
|
|
202
|
-
}
|
|
203
|
-
|
|
204
|
-
}],
|
|
205
|
-
tool_choice: { name: 'get_recipes' },
|
|
172
|
+
const recipesArgs = JSON.parse(response.choices[0].message.tool_calls[0].function.arguments);
|
|
173
|
+
expect(recipesArgs).toHaveProperty('recipes');
|
|
206
174
|
});
|
|
207
|
-
expect(response.model).toEqual('gpt-3.5-mock');
|
|
208
|
-
expect(response.choices[0]).toHaveProperty('finish_reason', 'tool_calls');
|
|
209
|
-
expect(response.choices[0].message.tool_calls[0].function).toHaveProperty('name', 'get_recipes');
|
|
210
|
-
expect(response.choices[0].message.tool_calls[0].function).toHaveProperty('arguments');
|
|
211
|
-
expect(JSON.parse(response.choices[0].message.tool_calls[0].function.arguments)).toHaveProperty('recipes');
|
|
212
175
|
});
|
|
213
176
|
|
|
214
|
-
|
|
215
|
-
|
|
216
|
-
try {
|
|
177
|
+
describe('Image Generation Tests', () => {
|
|
178
|
+
it('should mock image generation with correct properties', async () => {
|
|
217
179
|
const response = await openai.images.generate({
|
|
218
180
|
model: "dall-e-3",
|
|
219
|
-
prompt: "
|
|
181
|
+
prompt: "A futuristic cityscape",
|
|
220
182
|
n: 1,
|
|
221
183
|
size: "1024x1024",
|
|
222
184
|
quality: "hd",
|
|
223
185
|
});
|
|
224
186
|
|
|
187
|
+
expect(response).toHaveProperty('created');
|
|
188
|
+
expect(response).toHaveProperty('data');
|
|
189
|
+
expect(response.data).toBeInstanceOf(Array);
|
|
190
|
+
expect(response.data.length).toBe(1);
|
|
225
191
|
expect(response.data[0]).toHaveProperty('revised_prompt');
|
|
226
192
|
expect(response.data[0]).toHaveProperty('url');
|
|
227
|
-
|
|
228
|
-
|
|
229
|
-
}
|
|
193
|
+
expect(response.data[0].url).toMatch(/^http:\/\//);
|
|
194
|
+
});
|
|
230
195
|
|
|
196
|
+
it('should handle multiple image generation', async () => {
|
|
197
|
+
const response = await openai.images.generate({
|
|
198
|
+
model: "dall-e-3",
|
|
199
|
+
prompt: "Abstract art in various styles",
|
|
200
|
+
n: 3,
|
|
201
|
+
size: "512x512",
|
|
202
|
+
quality: "standard",
|
|
203
|
+
});
|
|
204
|
+
|
|
205
|
+
expect(response.data).toBeInstanceOf(Array);
|
|
206
|
+
expect(response.data.length).toBe(3);
|
|
207
|
+
response.data.forEach(image => {
|
|
208
|
+
expect(image).toHaveProperty('url');
|
|
209
|
+
expect(image.url).toMatch(/^http:\/\//);
|
|
210
|
+
});
|
|
211
|
+
});
|
|
231
212
|
});
|
|
232
213
|
|
|
233
214
|
it('should allow other requests', async () => {
|
|
234
|
-
|
|
235
|
-
|
|
236
|
-
|
|
237
|
-
|
|
238
|
-
|
|
239
|
-
|
|
240
|
-
|
|
241
|
-
})
|
|
215
|
+
const response = await fetch('https://jsonplaceholder.typicode.com/todos');
|
|
216
|
+
const data = await response.json();
|
|
217
|
+
|
|
218
|
+
expect(data[0]).toHaveProperty('id');
|
|
219
|
+
expect(data[0]).toHaveProperty('userId');
|
|
220
|
+
expect(data[0]).toHaveProperty('title');
|
|
221
|
+
expect(data[0]).toHaveProperty('completed');
|
|
222
|
+
});
|
|
242
223
|
});
|
package/package.json
CHANGED
|
@@ -1,6 +1,6 @@
|
|
|
1
1
|
{
|
|
2
2
|
"name": "openai-api-mock",
|
|
3
|
-
"version": "0.1.
|
|
3
|
+
"version": "0.1.17",
|
|
4
4
|
"description": "A Node.js module for mocking OpenAI API responses in a development environment",
|
|
5
5
|
"repository": {
|
|
6
6
|
"type": "git",
|
|
@@ -22,11 +22,11 @@
|
|
|
22
22
|
"license": "MIT",
|
|
23
23
|
"main": "main.js",
|
|
24
24
|
"dependencies": {
|
|
25
|
-
"@faker-js/faker": "^
|
|
26
|
-
"nock": "^13.5.
|
|
25
|
+
"@faker-js/faker": "^9.0.0",
|
|
26
|
+
"nock": "^13.5.4"
|
|
27
27
|
},
|
|
28
28
|
"devDependencies": {
|
|
29
29
|
"jest": "^29.7.0",
|
|
30
|
-
"openai": "^4.
|
|
30
|
+
"openai": "^4.52.4"
|
|
31
31
|
}
|
|
32
32
|
}
|
package/readme.md
CHANGED
|
@@ -30,7 +30,9 @@ This function intercepts HTTP calls to the OpenAI endpoint and returns a mock re
|
|
|
30
30
|
mockOpenAIResponse(force = true);
|
|
31
31
|
```
|
|
32
32
|
The force parameter is a boolean that determines whether the mock response should be used regardless of the environment.
|
|
33
|
-
If force is true, the mock response will be used regardless of the environment.
|
|
33
|
+
If force is true, the mock response will be used regardless of the environment.
|
|
34
|
+
|
|
35
|
+
If force is `false` or not provided, the mock response will only be used if the `NODE_ENV` environment variable is set to `development`.
|
|
34
36
|
|
|
35
37
|
### Example responces
|
|
36
38
|
|