openai-api-mock 0.1.27 → 0.1.29
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/__tests__/mock_delay.test.js +28 -0
- package/dist/index.cjs +2 -3
- package/dist/index.cjs.map +1 -1
- package/dist/index.js +2 -3
- package/dist/index.js.map +1 -1
- package/package.json +1 -1
- package/src/index.js +3 -5
|
@@ -0,0 +1,28 @@
|
|
|
1
|
+
import fetch from 'node-fetch';
|
|
2
|
+
import { mockOpenAIResponse, stopMocking } from '../src/index.js';
|
|
3
|
+
import OpenAI from 'openai';
|
|
4
|
+
|
|
5
|
+
const openai = new OpenAI({ apiKey: "OPENAI_API_KEY" });
|
|
6
|
+
|
|
7
|
+
describe('Mock OpenAI Chat with a delay', () => {
|
|
8
|
+
|
|
9
|
+
beforeEach(() => {
|
|
10
|
+
mockOpenAIResponse(true, { latency: 1000 , logRequests: true });
|
|
11
|
+
});
|
|
12
|
+
|
|
13
|
+
afterEach(() => {
|
|
14
|
+
stopMocking();
|
|
15
|
+
});
|
|
16
|
+
|
|
17
|
+
test('should mock OpenAI Chat with a delay', async () => {
|
|
18
|
+
const response = await openai.chat.completions.create({
|
|
19
|
+
model: "gpt-3.5",
|
|
20
|
+
messages: [
|
|
21
|
+
{ role: 'system', content: "You're an expert chef" },
|
|
22
|
+
{ role: 'user', content: "Suggest at least 5 recipes" },
|
|
23
|
+
]
|
|
24
|
+
});
|
|
25
|
+
|
|
26
|
+
expect(response).toBeDefined();
|
|
27
|
+
});
|
|
28
|
+
});
|
package/dist/index.cjs
CHANGED
|
@@ -2199,8 +2199,7 @@ function mockOpenAIResponse(force = false, options = {}) {
|
|
|
2199
2199
|
if (env !== "development" && !force) {
|
|
2200
2200
|
return { isActive: false, stopMocking };
|
|
2201
2201
|
}
|
|
2202
|
-
|
|
2203
|
-
nock(OPEN_AI_BASE_URL).post(CHAT_COMPLETIONS_ENDPOINT).reply(function(uri, requestBody) {
|
|
2202
|
+
nock(OPEN_AI_BASE_URL).post(CHAT_COMPLETIONS_ENDPOINT).delay(latency).reply(function(uri, requestBody) {
|
|
2204
2203
|
if (logRequests) {
|
|
2205
2204
|
console.log(`[openai-api-mock] Chat request:`, JSON.stringify(requestBody, null, 2));
|
|
2206
2205
|
}
|
|
@@ -2222,7 +2221,7 @@ function mockOpenAIResponse(force = false, options = {}) {
|
|
|
2222
2221
|
return [500, { error: { message: "Internal server error in mock" } }];
|
|
2223
2222
|
}
|
|
2224
2223
|
});
|
|
2225
|
-
nock(OPEN_AI_BASE_URL).post(IMAGE_GENERATIONS_ENDPOINT).reply(function(uri, requestBody) {
|
|
2224
|
+
nock(OPEN_AI_BASE_URL).post(IMAGE_GENERATIONS_ENDPOINT).delay(latency).reply(function(uri, requestBody) {
|
|
2226
2225
|
if (logRequests) {
|
|
2227
2226
|
console.log(`[openai-api-mock] Image request:`, JSON.stringify(requestBody, null, 2));
|
|
2228
2227
|
}
|