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-lock.json +3956 -0
- package/package.json +1 -1
- package/src/chat.js +142 -108
- package/.github/workflows/npm-publish.yml +0 -33
package/package.json
CHANGED
package/src/chat.js
CHANGED
|
@@ -1,147 +1,181 @@
|
|
|
1
|
-
const { faker } = require(
|
|
1
|
+
const { faker } = require("@faker-js/faker");
|
|
2
2
|
|
|
3
3
|
function getChatResponce(requestBody) {
|
|
4
|
-
|
|
4
|
+
const created = Math.floor(Date.now() / 1000);
|
|
5
5
|
|
|
6
|
-
|
|
7
|
-
|
|
8
|
-
|
|
6
|
+
if (!requestBody.functions && !requestBody.tools) {
|
|
7
|
+
return createDefaultResponse(created);
|
|
8
|
+
}
|
|
9
9
|
|
|
10
|
-
|
|
10
|
+
return createFunctionCallingResponse(requestBody, created);
|
|
11
11
|
}
|
|
12
12
|
|
|
13
13
|
function createDefaultResponse(created) {
|
|
14
|
-
|
|
15
|
-
|
|
16
|
-
|
|
17
|
-
|
|
18
|
-
|
|
19
|
-
|
|
20
|
-
|
|
21
|
-
|
|
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
|
-
|
|
40
|
-
|
|
41
|
-
|
|
42
|
-
|
|
43
|
-
|
|
44
|
-
|
|
45
|
-
|
|
46
|
-
|
|
47
|
-
|
|
48
|
-
|
|
49
|
-
|
|
50
|
-
|
|
51
|
-
|
|
52
|
-
|
|
53
|
-
|
|
54
|
-
|
|
55
|
-
|
|
56
|
-
|
|
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
|
-
|
|
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
|
-
|
|
71
|
-
|
|
72
|
-
|
|
73
|
-
|
|
74
|
-
|
|
75
|
-
|
|
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
|
-
|
|
81
|
-
|
|
82
|
-
|
|
83
|
-
|
|
82
|
+
return {
|
|
83
|
+
name: `${requestBody.functions[0].name}`,
|
|
84
|
+
arguments: `${generateFunctionCallArguments(requestBody)}`,
|
|
85
|
+
};
|
|
84
86
|
}
|
|
85
87
|
|
|
86
88
|
function generateToolCallArguments(requestBody) {
|
|
87
|
-
|
|
88
|
-
|
|
89
|
+
const { parameters } = requestBody.tools[0].function;
|
|
90
|
+
const argumentsObject = {};
|
|
89
91
|
|
|
90
|
-
|
|
91
|
-
|
|
92
|
-
|
|
92
|
+
Object.entries(parameters.properties).forEach(([paramName, paramDetails]) => {
|
|
93
|
+
argumentsObject[paramName] = generateFakeData(
|
|
94
|
+
paramDetails.type,
|
|
95
|
+
paramDetails
|
|
96
|
+
);
|
|
97
|
+
});
|
|
93
98
|
|
|
94
|
-
|
|
99
|
+
return JSON.stringify(argumentsObject, null, 2);
|
|
95
100
|
}
|
|
96
101
|
|
|
97
|
-
function generateFakeData(type, properties) {
|
|
98
|
-
|
|
99
|
-
|
|
100
|
-
|
|
101
|
-
|
|
102
|
-
|
|
103
|
-
|
|
104
|
-
|
|
105
|
-
|
|
106
|
-
|
|
107
|
-
|
|
108
|
-
|
|
109
|
-
|
|
110
|
-
|
|
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
|
-
|
|
116
|
-
|
|
117
|
-
|
|
118
|
-
|
|
119
|
-
|
|
120
|
-
|
|
121
|
-
|
|
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
|
-
|
|
128
|
-
|
|
129
|
-
|
|
130
|
-
|
|
131
|
-
|
|
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
|
-
|
|
136
|
-
|
|
166
|
+
const { parameters } = requestBody.functions[0];
|
|
167
|
+
const argumentsObject = {};
|
|
137
168
|
|
|
138
|
-
|
|
139
|
-
|
|
140
|
-
|
|
169
|
+
Object.entries(parameters.properties).forEach(([paramName, paramDetails]) => {
|
|
170
|
+
argumentsObject[paramName] = generateFakeData(
|
|
171
|
+
paramDetails.type,
|
|
172
|
+
paramDetails
|
|
173
|
+
);
|
|
174
|
+
});
|
|
141
175
|
|
|
142
|
-
|
|
176
|
+
return JSON.stringify(argumentsObject, null, 2);
|
|
143
177
|
}
|
|
144
178
|
|
|
145
179
|
module.exports = {
|
|
146
|
-
|
|
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}}
|