openai-api-mock 0.1.9 → 0.1.11

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/package.json CHANGED
@@ -1,6 +1,6 @@
1
1
  {
2
2
  "name": "openai-api-mock",
3
- "version": "0.1.9",
3
+ "version": "0.1.11",
4
4
  "description": "A Node.js module for mocking OpenAI API responses in a development environment",
5
5
  "repository": {
6
6
  "type": "git",
package/src/chat.js CHANGED
@@ -1,147 +1,181 @@
1
- const { faker } = require('@faker-js/faker');
1
+ const { faker } = require("@faker-js/faker");
2
2
 
3
3
  function getChatResponce(requestBody) {
4
- const created = Math.floor(Date.now() / 1000);
4
+ const created = Math.floor(Date.now() / 1000);
5
5
 
6
- if (!requestBody.functions && !requestBody.tools) {
7
- return createDefaultResponse(created);
8
- }
6
+ if (!requestBody.functions && !requestBody.tools) {
7
+ return createDefaultResponse(created);
8
+ }
9
9
 
10
- return createFunctionCallingResponse(requestBody, created);
10
+ return createFunctionCallingResponse(requestBody, created);
11
11
  }
12
12
 
13
13
  function createDefaultResponse(created) {
14
- return {
15
- choices: [
16
- {
17
- finish_reason: 'stop',
18
- index: 0,
19
- message: {
20
- content: faker.lorem.words(5),
21
- role: 'assistant',
22
- },
23
- logprobs: null,
24
- },
25
- ],
26
- created: created,
27
- id: `chatcmpl-${faker.string.alphanumeric(30)}`,
28
- model: `gpt-3.5-mock`,
29
- object: 'chat.completion',
30
- usage: {
31
- completion_tokens: 17,
32
- prompt_tokens: 57,
33
- total_tokens: 74,
14
+ return {
15
+ choices: [
16
+ {
17
+ finish_reason: "stop",
18
+ index: 0,
19
+ message: {
20
+ content: faker.lorem.words(5),
21
+ role: "assistant",
34
22
  },
35
- };
23
+ logprobs: null,
24
+ },
25
+ ],
26
+ created: created,
27
+ id: `chatcmpl-${faker.string.alphanumeric(30)}`,
28
+ model: `gpt-3.5-mock`,
29
+ object: "chat.completion",
30
+ usage: {
31
+ completion_tokens: 17,
32
+ prompt_tokens: 57,
33
+ total_tokens: 74,
34
+ },
35
+ };
36
36
  }
37
37
 
38
38
  function createFunctionCallingResponse(requestBody, created) {
39
- const isTool = Boolean(requestBody.tools);
40
- const functionOrToolCallObject = isTool ? [createToolCallObject(requestBody)] : createFunctionCallObject(requestBody);
41
- const functionOrToolCall = isTool ? 'tool_calls' : 'function_call';
42
-
43
- const functionCallingResponse = {
44
- id: `chatcmpl-${faker.string.alphanumeric(30)}`,
45
- object: 'chat.completion',
46
- created: created,
47
- model: 'gpt-3.5-mock',
48
- choices: [
49
- {
50
- index: 0,
51
- message: {
52
- role: 'assistant',
53
- content: null,
54
- [functionOrToolCall]: functionOrToolCallObject,
55
- },
56
- finish_reason: functionOrToolCall,
57
- },
58
- ],
59
- usage: {
60
- prompt_tokens: 81,
61
- completion_tokens: 19,
62
- total_tokens: 100,
39
+ const isTool = Boolean(requestBody.tools);
40
+ const functionOrToolCallObject = isTool
41
+ ? [createToolCallObject(requestBody)]
42
+ : createFunctionCallObject(requestBody);
43
+ const functionOrToolCall = isTool ? "tool_calls" : "function_call";
44
+
45
+ const functionCallingResponse = {
46
+ id: `chatcmpl-${faker.string.alphanumeric(30)}`,
47
+ object: "chat.completion",
48
+ created: created,
49
+ model: "gpt-3.5-mock",
50
+ choices: [
51
+ {
52
+ index: 0,
53
+ message: {
54
+ role: "assistant",
55
+ content: null,
56
+ [functionOrToolCall]: functionOrToolCallObject,
63
57
  },
64
- };
65
-
66
- return functionCallingResponse;
58
+ finish_reason: functionOrToolCall,
59
+ },
60
+ ],
61
+ usage: {
62
+ prompt_tokens: 81,
63
+ completion_tokens: 19,
64
+ total_tokens: 100,
65
+ },
66
+ };
67
+
68
+ return functionCallingResponse;
67
69
  }
68
70
 
69
71
  function createToolCallObject(requestBody) {
70
- return {
71
- id: `call-${faker.string.alphanumeric(30)}`,
72
- type: "function",
73
- "function": {
74
- name: `${requestBody.tools[0].function.name}`,
75
- arguments: `${generateToolCallArguments(requestBody)}`
76
- }
77
- };
72
+ return {
73
+ id: `call-${faker.string.alphanumeric(30)}`,
74
+ type: "function",
75
+ function: {
76
+ name: `${requestBody.tools[0].function.name}`,
77
+ arguments: `${generateToolCallArguments(requestBody)}`,
78
+ },
79
+ };
78
80
  }
79
81
  function createFunctionCallObject(requestBody) {
80
- return {
81
- name: `${requestBody.functions[0].name}`,
82
- arguments: `${generateFunctionCallArguments(requestBody)}`,
83
- };
82
+ return {
83
+ name: `${requestBody.functions[0].name}`,
84
+ arguments: `${generateFunctionCallArguments(requestBody)}`,
85
+ };
84
86
  }
85
87
 
86
88
  function generateToolCallArguments(requestBody) {
87
- const { parameters } = requestBody.tools[0].function;
88
- const argumentsObject = {};
89
+ const { parameters } = requestBody.tools[0].function;
90
+ const argumentsObject = {};
89
91
 
90
- Object.entries(parameters.properties).forEach(([paramName, paramDetails]) => {
91
- argumentsObject[paramName] = generateFakeData(paramDetails.type, paramDetails);
92
- });
92
+ Object.entries(parameters.properties).forEach(([paramName, paramDetails]) => {
93
+ argumentsObject[paramName] = generateFakeData(
94
+ paramDetails.type,
95
+ paramDetails
96
+ );
97
+ });
93
98
 
94
- return JSON.stringify(argumentsObject, null, 2);
99
+ return JSON.stringify(argumentsObject, null, 2);
95
100
  }
96
101
 
97
- function generateFakeData(type, properties) {
98
- switch (type) {
99
- case 'string':
100
- return faker.lorem.words(5);
101
- case 'number':
102
- return faker.number.int();
103
- case 'array':
104
- return generateFakeArray(properties);
105
- case 'object':
106
- return generateFakeObject(properties);
107
- case 'boolean':
108
- return faker.datatype.boolean();
109
- default:
110
- return faker.lorem.words(1);
111
- }
102
+ function generateFakeData(type, properties, name) {
103
+ switch (type) {
104
+ case "string":
105
+ if (name === "name") {
106
+ return faker.person.fullName();
107
+ } else if (name === "email") {
108
+ return faker.internet.email();
109
+ } else if (name === "price") {
110
+ return faker.commerce.price({ min: 100 });
111
+ } else if (name === "company") {
112
+ return faker.company.bs();
113
+ } else if (name === "phone") {
114
+ return faker.phone.number();
115
+ } else if (name === "address") {
116
+ return faker.address.streetAddress();
117
+ } else if (name === "date") {
118
+ return faker.date.past();
119
+ } else if (name === "jobTitle") {
120
+ return faker.name.jobTitle();
121
+ } else if (name === "creditCardNumber") {
122
+ return faker.finance.creditCardNumber();
123
+ } else if (name === "currencyCode") {
124
+ return faker.finance.currencyCode();
125
+ } else if (name === "productName") {
126
+ return faker.commerce.productName();
127
+ } else if (name === "uuid") {
128
+ return faker.datatype.uuid();
129
+ } else {
130
+ return faker.lorem.words(5);
131
+ }
132
+ case "number":
133
+ return faker.number.int({ max: 100 });
134
+ case "array":
135
+ return generateFakeArray(properties);
136
+ case "object":
137
+ return generateFakeObject(properties);
138
+ case "boolean":
139
+ return faker.datatype.boolean();
140
+ default:
141
+ return faker.lorem.words(1);
142
+ }
112
143
  }
113
144
 
114
145
  function generateFakeArray(properties) {
115
- const arrayItemsType = properties.items.type;
116
- return Array.from({ length: 5 }, () => {
117
- if (arrayItemsType === 'string') {
118
- return faker.lorem.words(1);
119
- } else if (arrayItemsType === 'object') {
120
- const itemProperties = properties.items.properties;
121
- return generateFakeData('object', { properties: itemProperties });
122
- }
123
- });
146
+ const arrayItemsType = properties.items.type;
147
+ return Array.from({ length: 5 }, () => {
148
+ if (arrayItemsType === "string") {
149
+ return faker.lorem.words(1);
150
+ } else if (arrayItemsType === "object") {
151
+ const itemProperties = properties.items.properties;
152
+ return generateFakeData("object", { properties: itemProperties });
153
+ }
154
+ });
124
155
  }
125
156
 
126
157
  function generateFakeObject(properties) {
127
- const itemObject = {};
128
- Object.entries(properties.properties).forEach(([itemName, itemDetails]) => {
129
- itemObject[itemName] = generateFakeData(itemDetails.type, itemDetails);
130
- });
131
- return itemObject;
158
+ const itemObject = {};
159
+ Object.entries(properties.properties).forEach(([itemName, itemDetails]) => {
160
+ itemObject[itemName] = generateFakeData(itemDetails.type, itemDetails, itemName);
161
+ });
162
+ return itemObject;
132
163
  }
133
164
 
134
165
  function generateFunctionCallArguments(requestBody) {
135
- const { parameters } = requestBody.functions[0];
136
- const argumentsObject = {};
166
+ const { parameters } = requestBody.functions[0];
167
+ const argumentsObject = {};
137
168
 
138
- Object.entries(parameters.properties).forEach(([paramName, paramDetails]) => {
139
- argumentsObject[paramName] = generateFakeData(paramDetails.type, paramDetails);
140
- });
169
+ Object.entries(parameters.properties).forEach(([paramName, paramDetails]) => {
170
+ argumentsObject[paramName] = generateFakeData(
171
+ paramDetails.type,
172
+ paramDetails
173
+ );
174
+ });
141
175
 
142
- return JSON.stringify(argumentsObject, null, 2);
176
+ return JSON.stringify(argumentsObject, null, 2);
143
177
  }
144
178
 
145
179
  module.exports = {
146
- getChatResponce,
180
+ getChatResponce,
147
181
  };
@@ -1,33 +0,0 @@
1
- # This workflow will run tests using node and then publish a package to GitHub Packages when a release is created
2
- # For more information see: https://docs.github.com/en/actions/publishing-packages/publishing-nodejs-packages
3
-
4
- name: Node.js Package
5
-
6
- on:
7
- release:
8
- types: [created]
9
-
10
- jobs:
11
- build:
12
- runs-on: ubuntu-latest
13
- steps:
14
- - uses: actions/checkout@v3
15
- - uses: actions/setup-node@v3
16
- with:
17
- node-version: 16
18
- - run: npm ci
19
- - run: npm test
20
-
21
- publish-npm:
22
- needs: build
23
- runs-on: ubuntu-latest
24
- steps:
25
- - uses: actions/checkout@v3
26
- - uses: actions/setup-node@v3
27
- with:
28
- node-version: 16
29
- registry-url: https://registry.npmjs.org/
30
- - run: npm ci
31
- - run: npm publish
32
- env:
33
- NODE_AUTH_TOKEN: ${{secrets.npm_token}}