openai-api-mock 0.1.6 → 0.1.7

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 CHANGED
@@ -2,33 +2,36 @@ const nock = require('nock');
2
2
  const { getChatResponce } = require('./src/chat.js');
3
3
  const { getImageResponce } = require('./src/image.js');
4
4
 
5
+ const OPEN_AI_BASE_URL = 'https://api.openai.com';
6
+ const CHAT_COMPLETIONS_ENDPOINT = '/v1/chat/completions';
7
+ const IMAGE_GENERATIONS_ENDPOINT = '/v1/images/generations';
8
+
5
9
  function mockOpenAIResponse(force = false) {
6
- // Define the OpenAI endpoints
7
- const openaiCompletionEndpoint = 'https://api.openai.com/v1/chat/completions';
8
- const imageEndpoint = 'https://api.openai.com/v1/images/generations';
9
10
  var env = process.env.NODE_ENV || 'development';
10
-
11
11
  // Intercept the HTTP call and return the mock response
12
12
  if (env === 'development' || force) {
13
- nock(openaiCompletionEndpoint)
14
- .post('')
13
+ nock(OPEN_AI_BASE_URL)
14
+ .post(CHAT_COMPLETIONS_ENDPOINT)
15
15
  .reply(function (uri, requestBody) {
16
16
  return [200, getChatResponce(requestBody)];
17
17
  });
18
18
 
19
- nock(imageEndpoint)
20
- .post('')
19
+ nock(OPEN_AI_BASE_URL)
20
+ .post(IMAGE_GENERATIONS_ENDPOINT)
21
21
  .reply(function (uri, requestBody) {
22
22
  return [200, getImageResponce(requestBody)];
23
23
  });
24
24
 
25
25
  // Mocking only the chat completion endpoint, not blocking other requests
26
- nock.emitter.on('no match', function (req) {
27
- nock.enableNetConnect(req);
28
- });
26
+ nock.enableNetConnect(host => host !== "api.openai.com");
29
27
  }
30
28
  }
31
29
 
30
+ function stopMocking() {
31
+ nock.cleanAll();
32
+ }
33
+
32
34
  module.exports = {
33
- mockOpenAIResponse
34
- };
35
+ mockOpenAIResponse,
36
+ stopMocking,
37
+ };
package/main.test.js ADDED
@@ -0,0 +1,120 @@
1
+ const { mockOpenAIResponse, stopMocking } = require('./main');
2
+
3
+ const OpenAI = require('openai');
4
+ const openai = new OpenAI({apiKey : "OPENAI_API_KEY"});
5
+
6
+ describe('Mock OpenAI Chat & Image generation API', () => {
7
+ beforeEach(() => {
8
+ mockOpenAIResponse(true);
9
+ });
10
+ afterEach(() => {
11
+ stopMocking();
12
+ });
13
+ it('should mock the chat completion', async () => {
14
+ try {
15
+ const response = await openai.chat.completions.create({
16
+ model: "gpt-3.5",
17
+ messages: [
18
+ { role: 'system', content: "You'r an expert chef" },
19
+ { role: 'user', content: "Suggest at least 5 recipes" },
20
+ ]
21
+ });
22
+
23
+ expect(response.choices[0]).toHaveProperty('finish_reason', 'stop');
24
+ expect(response.model).toEqual('gpt-3.5-mock');
25
+ } catch (error) {
26
+ throw new Error(error);
27
+ }
28
+ });
29
+
30
+ it('should mock the chat completion with function call', async () => {
31
+ try {
32
+ const response = await openai.chat.completions.create({
33
+ model: "gpt-3.5",
34
+ messages: [
35
+ { role: 'system', content: "You'r an expert chef" },
36
+ { role: 'user', content: "Suggest at least 5 recipes" },
37
+ ],
38
+ functions: [
39
+ {
40
+ name: 'get_recipes',
41
+ description: 'Suggest at least 5 recipes based on the ingredients you have',
42
+ parameters: {
43
+ type: "object",
44
+ properties: {
45
+ recipes: {
46
+ type: "array",
47
+ description: "The recipes that can be made with the ingredients",
48
+ items: {
49
+ type: 'object',
50
+ properties: {
51
+ name: {
52
+ type: 'string',
53
+ description: 'The name of the recipe',
54
+ },
55
+ ingredients: {
56
+ type: 'array',
57
+ description: 'The ingredients needed for the recipe',
58
+ items: {
59
+ type: 'string',
60
+ description: 'The name of the ingredient',
61
+ },
62
+ },
63
+ instructions: {
64
+ type: 'string',
65
+ description: 'Detailed instructions on how to make the recipe',
66
+ },
67
+ serving: {
68
+ type: 'string',
69
+ description: 'The number of people the recipe serves',
70
+ },
71
+ }
72
+ }
73
+ },
74
+ }
75
+ },
76
+ required: ["recipes"],
77
+ },
78
+ ],
79
+ function_call: { name: 'get_recipes' },
80
+ });
81
+ expect(response.model).toEqual('gpt-3.5-mock');
82
+ expect(response.choices[0]).toHaveProperty('finish_reason', 'function_call');
83
+ expect(response.choices[0].message.function_call).toHaveProperty('name', 'get_recipes');
84
+ expect(response.choices[0].message.function_call).toHaveProperty('arguments');
85
+ expect(JSON.parse(response.choices[0].message.function_call.arguments)).toHaveProperty('recipes');
86
+
87
+ } catch (error) {
88
+ throw new Error(error);
89
+ }
90
+ });
91
+
92
+
93
+ it('should mock image generation', async () => {
94
+ try {
95
+ const response = await openai.images.generate({
96
+ model: "dall-e-3",
97
+ prompt: "Kurt Cobain",
98
+ n: 1,
99
+ size: "1024x1024",
100
+ quality: "hd",
101
+ });
102
+
103
+ expect(response.data[0]).toHaveProperty('revised_prompt');
104
+ expect(response.data[0]).toHaveProperty('url');
105
+ } catch (error) {
106
+ throw new Error(error);
107
+ }
108
+
109
+ });
110
+
111
+ it('should allow other requests', async () => {
112
+ try {
113
+ const response = await fetch('https://jsonplaceholder.typicode.com/todos');
114
+ const data = await response.json();
115
+ expect(data[0]).toHaveProperty('completed');
116
+ } catch (error) {
117
+ throw new Error(error);
118
+ }
119
+ })
120
+ });
package/package.json CHANGED
@@ -1,6 +1,6 @@
1
1
  {
2
2
  "name": "openai-api-mock",
3
- "version": "0.1.6",
3
+ "version": "0.1.7",
4
4
  "description": "A Node.js module for mocking OpenAI API responses in a development environment",
5
5
  "repository": {
6
6
  "type": "git",
@@ -13,11 +13,18 @@
13
13
  "development",
14
14
  "testing"
15
15
  ],
16
+ "scripts": {
17
+ "test": "jest"
18
+ },
16
19
  "author": "Chiheb Nabil",
17
20
  "license": "MIT",
18
21
  "main": "main.js",
19
22
  "dependencies": {
20
23
  "@faker-js/faker": "^8.4.0",
21
24
  "nock": "^13.5.1"
25
+ },
26
+ "devDependencies": {
27
+ "jest": "^29.7.0",
28
+ "openai": "^4.26.0"
22
29
  }
23
30
  }
package/src/chat.js CHANGED
@@ -119,4 +119,4 @@ function generateFunctionCallArguments(requestBody) {
119
119
 
120
120
  module.exports = {
121
121
  getChatResponce,
122
- };
122
+ };
package/src/image.js CHANGED
@@ -19,8 +19,6 @@ function getImageResponce(requestBody) {
19
19
  }
20
20
 
21
21
 
22
-
23
-
24
22
  module.exports = {
25
23
  getImageResponce,
26
- };
24
+ };