-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathindex.ts
178 lines (145 loc) · 6.3 KB
/
index.ts
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
import { IAdminForth, IHttpServer, AdminForthPlugin, AdminForthResource, AdminForthDataTypes, RateLimiter } from "adminforth";
import { PluginOptions } from './types.js';
export default class TextCompletePlugin extends AdminForthPlugin {
options: PluginOptions;
resourceConfig!: AdminForthResource;
columnType!: AdminForthDataTypes;
adminforth!: IAdminForth;
constructor(options: PluginOptions) {
super(options, import.meta.url);
this.options = options;
}
instanceUniqueRepresentation(pluginOptions: any) : string {
return `${pluginOptions.fieldName}`;
}
validateConfigAfterDiscover(adminforth: IAdminForth, resourceConfig: AdminForthResource) {
this.adminforth = adminforth;
if (this.options.adapter) {
this.options.adapter.validate();
}
const column = this.resourceConfig.columns.find(f => f.name === this.options.fieldName);
if (![AdminForthDataTypes.STRING, AdminForthDataTypes.TEXT].includes(column!.type!)) {
throw new Error(`Field ${this.options.fieldName} should be string or text type, but it is ${column!.type}`);
}
if (!this.options.adapter) {
throw new Error('adapter is required');
}
}
generateRecordContext(record: any, maxFields: number, inputFieldLimit: number, partsCount: number): string {
// stringify each field
let fields: [string, string][] = Object.entries(record).map(([key, value]) => {
return [key, JSON.stringify(value)];
});
// sort fields by length, higher first
fields = fields.sort((a, b) => b[1].length - a[1].length);
// select longest maxFields fields
fields = fields.slice(0, maxFields);
const minLimit = '...'.length * partsCount;
if (inputFieldLimit < minLimit) {
throw new Error(`inputFieldLimit should be at least ${minLimit}`);
}
// for each field, if it is longer than inputFieldLimit, truncate it using next way:
// split into 5 parts, divide inputFieldLimit by 5, crop each part to this length, join parts using '...'
fields = fields.map(([key, value]) => {
if (value.length > inputFieldLimit) {
const partLength = Math.floor(inputFieldLimit / partsCount) - '...'.length;
const parts = [];
for (let i = 0; i < partsCount; i++) {
parts.push(value.slice(i * partLength, (i + 1) * partLength));
}
value = parts.join('...');
return [key, value];
}
return [key, value];
});
return JSON.stringify(Object.fromEntries(fields));
}
setupEndpoints(server: IHttpServer) {
server.endpoint({
method: 'POST',
path: `/plugin/${this.pluginInstanceId}/doComplete`,
handler: async ({ body, headers }) => {
if (this.options.rateLimit?.limit) {
// rate limit
const { error } = RateLimiter.checkRateLimit(
this.pluginInstanceId,
this.options.rateLimit?.limit,
this.adminforth.auth.getClientIp(headers),
);
if (error) {
return {
completion: [],
}
}
}
const { record } = body;
const recordNoField = {...record};
delete recordNoField[this.options.fieldName];
let currentVal = record[this.options.fieldName];
const promptLimit = this.options.expert?.promptInputLimit || 500;
const inputContext = this.generateRecordContext(
recordNoField,
this.options.expert?.recordContext?.maxFields || 5,
this.options.expert?.recordContext?.maxFieldLength || 300,
this.options.expert?.recordContext?.splitParts || 5,
);
if (currentVal && currentVal.length > promptLimit) {
currentVal = currentVal.slice(-promptLimit);
}
const resLabel = this.resourceConfig.label;
let content;
if (currentVal) {
content = `Continue writing for text/string field "${this.options.fieldName}" in the table "${resLabel}"\n` +
(Object.keys(recordNoField).length > 0 ? `Record has values for the context: ${inputContext}\n` : '') +
`Current field value: ${currentVal}\n` +
"Don't talk to me. Just write text. No quotes. Don't repeat current field value, just write completion\n";
} else {
content = `Fill text/string field "${this.options.fieldName}" in the table "${resLabel}"\n` +
(Object.keys(recordNoField).length > 0 ? `Record has values for the context: ${inputContext}\n` : '') +
"Be short, clear and precise. No quotes. Don't talk to me. Just write text\n";
}
process.env.HEAVY_DEBUG && console.log('🪲 OpenAI Prompt 🧠', content);
const { content: respContent, finishReason } = await this.options.adapter.complete(content, this.options.expert?.stop, this.options.expert?.maxTokens);
const stop = this.options.expert?.stop || ['.'];
let suggestion = respContent + (
finishReason === 'stop' ? (
stop[0] === '.' && stop.length === 1 && this.columnType === AdminForthDataTypes.TEXT ? '. ' : ''
) : ''
);
if (suggestion.startsWith(currentVal)) {
suggestion = suggestion.slice(currentVal.length);
}
const wordsList = suggestion.split(' ').map((w, i) => {
return (i === suggestion.split(' ').length - 1) ? w : w + ' ';
});
// remove quotes from start and end
return {
completion: wordsList
};
}
});
}
async modifyResourceConfig(adminforth: IAdminForth, resourceConfig: AdminForthResource) {
super.modifyResourceConfig(adminforth, resourceConfig);
this.resourceConfig = resourceConfig;
// ensure that column exists
const column = this.resourceConfig.columns.find(f => f.name === this.options.fieldName);
if (!column) {
throw new Error(`Field ${this.options.fieldName} not found in resource ${this.resourceConfig.label}`);
}
if (!column.components) {
column.components = {};
}
const filed = {
file: this.componentPath('completionInput.vue'),
meta: {
pluginInstanceId: this.pluginInstanceId,
fieldName: this.options.fieldName,
debounceTime: this.options.expert?.debounceTime || 300,
}
}
column.components.create = filed;
column.components.edit = filed;
this.columnType = column.type!;
}
}