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 +1 -1
- package/readme.md +1 -1
- package/src/chat.js +129 -109
- package/.github/workflows/npm-publish.yml +0 -33
package/package.json
CHANGED
package/readme.md
CHANGED
package/src/chat.js
CHANGED
|
@@ -1,147 +1,167 @@
|
|
|
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 {
|
|
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
|
-
|
|
116
|
-
|
|
117
|
-
|
|
118
|
-
|
|
119
|
-
|
|
120
|
-
|
|
121
|
-
|
|
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
|
-
|
|
128
|
-
|
|
129
|
-
|
|
130
|
-
|
|
131
|
-
|
|
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
|
-
|
|
136
|
-
|
|
152
|
+
const { parameters } = requestBody.functions[0];
|
|
153
|
+
const argumentsObject = {};
|
|
137
154
|
|
|
138
|
-
|
|
139
|
-
|
|
140
|
-
|
|
155
|
+
Object.entries(parameters.properties).forEach(([paramName, paramDetails]) => {
|
|
156
|
+
argumentsObject[paramName] = generateFakeData(
|
|
157
|
+
paramDetails.type,
|
|
158
|
+
paramDetails
|
|
159
|
+
);
|
|
160
|
+
});
|
|
141
161
|
|
|
142
|
-
|
|
162
|
+
return JSON.stringify(argumentsObject, null, 2);
|
|
143
163
|
}
|
|
144
164
|
|
|
145
165
|
module.exports = {
|
|
146
|
-
|
|
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}}
|