openai-api-mock 0.1.8 → 0.1.10

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.8",
3
+ "version": "0.1.10",
4
4
  "description": "A Node.js module for mocking OpenAI API responses in a development environment",
5
5
  "repository": {
6
6
  "type": "git",
package/readme.md CHANGED
@@ -49,7 +49,7 @@ const response = await openai.chat.completions.create({
49
49
  ```
50
50
  In this example, the `response` constant will contain mock data, simulating a response from the OpenAI API:
51
51
 
52
- ```json
52
+ ```javascript
53
53
  {
54
54
  choices: [
55
55
  {
package/src/chat.js CHANGED
@@ -1,147 +1,167 @@
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 {
116
+ return faker.lorem.words(5);
117
+ }
118
+ case "number":
119
+ return faker.number.int({ max: 100 });
120
+ case "array":
121
+ return generateFakeArray(properties);
122
+ case "object":
123
+ return generateFakeObject(properties);
124
+ case "boolean":
125
+ return faker.datatype.boolean();
126
+ default:
127
+ return faker.lorem.words(1);
128
+ }
112
129
  }
113
130
 
114
131
  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
- });
132
+ const arrayItemsType = properties.items.type;
133
+ return Array.from({ length: 5 }, () => {
134
+ if (arrayItemsType === "string") {
135
+ return faker.lorem.words(1);
136
+ } else if (arrayItemsType === "object") {
137
+ const itemProperties = properties.items.properties;
138
+ return generateFakeData("object", { properties: itemProperties });
139
+ }
140
+ });
124
141
  }
125
142
 
126
143
  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;
144
+ const itemObject = {};
145
+ Object.entries(properties.properties).forEach(([itemName, itemDetails]) => {
146
+ itemObject[itemName] = generateFakeData(itemDetails.type, itemDetails, itemName);
147
+ });
148
+ return itemObject;
132
149
  }
133
150
 
134
151
  function generateFunctionCallArguments(requestBody) {
135
- const { parameters } = requestBody.functions[0];
136
- const argumentsObject = {};
152
+ const { parameters } = requestBody.functions[0];
153
+ const argumentsObject = {};
137
154
 
138
- Object.entries(parameters.properties).forEach(([paramName, paramDetails]) => {
139
- argumentsObject[paramName] = generateFakeData(paramDetails.type, paramDetails);
140
- });
155
+ Object.entries(parameters.properties).forEach(([paramName, paramDetails]) => {
156
+ argumentsObject[paramName] = generateFakeData(
157
+ paramDetails.type,
158
+ paramDetails
159
+ );
160
+ });
141
161
 
142
- return JSON.stringify(argumentsObject, null, 2);
162
+ return JSON.stringify(argumentsObject, null, 2);
143
163
  }
144
164
 
145
165
  module.exports = {
146
- getChatResponce,
147
- };
166
+ getChatResponce,
167
+ };
@@ -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}}