@mate-academy/llm-gateway 0.0.1-canary.1
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/.lintstagedrc +5 -0
- package/.nvmrc +1 -0
- package/dist/LLMService.constants.d.ts +8 -0
- package/dist/LLMService.constants.js +16 -0
- package/dist/LLMService.constants.js.map +1 -0
- package/dist/LLMService.entity.d.ts +13 -0
- package/dist/LLMService.entity.js +38 -0
- package/dist/LLMService.entity.js.map +1 -0
- package/dist/LLMService.factory.d.ts +8 -0
- package/dist/LLMService.factory.js +28 -0
- package/dist/LLMService.factory.js.map +1 -0
- package/dist/LLMService.typedefs.d.ts +126 -0
- package/dist/LLMService.typedefs.js +24 -0
- package/dist/LLMService.typedefs.js.map +1 -0
- package/dist/functional.utils.d.ts +1 -0
- package/dist/functional.utils.js +9 -0
- package/dist/functional.utils.js.map +1 -0
- package/dist/index.d.ts +6 -0
- package/dist/index.js +23 -0
- package/dist/index.js.map +1 -0
- package/dist/providers/GoogleGenerativeAI/GoogleGenerativeAI.constants.d.ts +6 -0
- package/dist/providers/GoogleGenerativeAI/GoogleGenerativeAI.constants.js +98 -0
- package/dist/providers/GoogleGenerativeAI/GoogleGenerativeAI.constants.js.map +1 -0
- package/dist/providers/GoogleGenerativeAI/GoogleGenerativeAI.entity.d.ts +8 -0
- package/dist/providers/GoogleGenerativeAI/GoogleGenerativeAI.entity.js +42 -0
- package/dist/providers/GoogleGenerativeAI/GoogleGenerativeAI.entity.js.map +1 -0
- package/dist/providers/GoogleGenerativeAI/GoogleGenerativeAI.typedefs.d.ts +13 -0
- package/dist/providers/GoogleGenerativeAI/GoogleGenerativeAI.typedefs.js +19 -0
- package/dist/providers/GoogleGenerativeAI/GoogleGenerativeAI.typedefs.js.map +1 -0
- package/dist/providers/GoogleGenerativeAI/GoogleGenerativeAIService.factory.d.ts +6 -0
- package/dist/providers/GoogleGenerativeAI/GoogleGenerativeAIService.factory.js +17 -0
- package/dist/providers/GoogleGenerativeAI/GoogleGenerativeAIService.factory.js.map +1 -0
- package/dist/providers/GoogleGenerativeAI/services/GoogleGenerativeAIAssistance.service.d.ts +21 -0
- package/dist/providers/GoogleGenerativeAI/services/GoogleGenerativeAIAssistance.service.js +209 -0
- package/dist/providers/GoogleGenerativeAI/services/GoogleGenerativeAIAssistance.service.js.map +1 -0
- package/dist/providers/GoogleGenerativeAI/services/GoogleGenerativeAICompletion.service.d.ts +10 -0
- package/dist/providers/GoogleGenerativeAI/services/GoogleGenerativeAICompletion.service.js +50 -0
- package/dist/providers/GoogleGenerativeAI/services/GoogleGenerativeAICompletion.service.js.map +1 -0
- package/dist/providers/GoogleGenerativeAI/services/index.d.ts +2 -0
- package/dist/providers/GoogleGenerativeAI/services/index.js +19 -0
- package/dist/providers/GoogleGenerativeAI/services/index.js.map +1 -0
- package/dist/providers/OpenAI/OpenAI.constants.d.ts +5 -0
- package/dist/providers/OpenAI/OpenAI.constants.js +44 -0
- package/dist/providers/OpenAI/OpenAI.constants.js.map +1 -0
- package/dist/providers/OpenAI/OpenAI.entity.d.ts +3 -0
- package/dist/providers/OpenAI/OpenAI.entity.js +15 -0
- package/dist/providers/OpenAI/OpenAI.entity.js.map +1 -0
- package/dist/providers/OpenAI/OpenAI.typedefs.d.ts +8 -0
- package/dist/providers/OpenAI/OpenAI.typedefs.js +14 -0
- package/dist/providers/OpenAI/OpenAI.typedefs.js.map +1 -0
- package/dist/providers/OpenAI/OpenAIService.factory.d.ts +6 -0
- package/dist/providers/OpenAI/OpenAIService.factory.js +17 -0
- package/dist/providers/OpenAI/OpenAIService.factory.js.map +1 -0
- package/dist/providers/OpenAI/services/OpenAIAssistance.service.d.ts +19 -0
- package/dist/providers/OpenAI/services/OpenAIAssistance.service.js +200 -0
- package/dist/providers/OpenAI/services/OpenAIAssistance.service.js.map +1 -0
- package/dist/providers/OpenAI/services/OpenAICompletion.service.d.ts +10 -0
- package/dist/providers/OpenAI/services/OpenAICompletion.service.js +52 -0
- package/dist/providers/OpenAI/services/OpenAICompletion.service.js.map +1 -0
- package/dist/providers/OpenAI/services/index.d.ts +2 -0
- package/dist/providers/OpenAI/services/index.js +19 -0
- package/dist/providers/OpenAI/services/index.js.map +1 -0
- package/dist/providers/index.d.ts +2 -0
- package/dist/providers/index.js +19 -0
- package/dist/providers/index.js.map +1 -0
- package/dist/services/LLMAssistanceService.abstract.d.ts +15 -0
- package/dist/services/LLMAssistanceService.abstract.js +12 -0
- package/dist/services/LLMAssistanceService.abstract.js.map +1 -0
- package/dist/services/LLMBaseService.abstract.d.ts +13 -0
- package/dist/services/LLMBaseService.abstract.js +27 -0
- package/dist/services/LLMBaseService.abstract.js.map +1 -0
- package/dist/services/LLMCompletionService.abstract.d.ts +7 -0
- package/dist/services/LLMCompletionService.abstract.js +12 -0
- package/dist/services/LLMCompletionService.abstract.js.map +1 -0
- package/dist/services/LLMServicePurposeFactory.abstract.d.ts +5 -0
- package/dist/services/LLMServicePurposeFactory.abstract.js +7 -0
- package/dist/services/LLMServicePurposeFactory.abstract.js.map +1 -0
- package/dist/services/index.d.ts +4 -0
- package/dist/services/index.js +21 -0
- package/dist/services/index.js.map +1 -0
- package/eslint.config.mjs +39 -0
- package/package.json +35 -0
- package/src/LLMService.constants.ts +19 -0
- package/src/LLMService.entity.ts +71 -0
- package/src/LLMService.factory.ts +57 -0
- package/src/LLMService.typedefs.ts +174 -0
- package/src/functional.utils.ts +7 -0
- package/src/index.ts +6 -0
- package/src/providers/GoogleGenerativeAI/GoogleGenerativeAI.constants.ts +127 -0
- package/src/providers/GoogleGenerativeAI/GoogleGenerativeAI.entity.ts +67 -0
- package/src/providers/GoogleGenerativeAI/GoogleGenerativeAI.typedefs.ts +14 -0
- package/src/providers/GoogleGenerativeAI/GoogleGenerativeAIService.factory.ts +29 -0
- package/src/providers/GoogleGenerativeAI/services/GoogleGenerativeAIAssistance.service.ts +307 -0
- package/src/providers/GoogleGenerativeAI/services/GoogleGenerativeAICompletion.service.ts +79 -0
- package/src/providers/GoogleGenerativeAI/services/index.ts +2 -0
- package/src/providers/OpenAI/OpenAI.constants.ts +71 -0
- package/src/providers/OpenAI/OpenAI.entity.ts +17 -0
- package/src/providers/OpenAI/OpenAI.typedefs.ts +9 -0
- package/src/providers/OpenAI/OpenAIService.factory.ts +29 -0
- package/src/providers/OpenAI/services/OpenAIAssistance.service.ts +304 -0
- package/src/providers/OpenAI/services/OpenAICompletion.service.ts +74 -0
- package/src/providers/OpenAI/services/index.ts +2 -0
- package/src/providers/index.ts +2 -0
- package/src/services/LLMAssistanceService.abstract.ts +73 -0
- package/src/services/LLMBaseService.abstract.ts +34 -0
- package/src/services/LLMCompletionService.abstract.ts +32 -0
- package/src/services/LLMServicePurposeFactory.abstract.ts +15 -0
- package/src/services/index.ts +4 -0
- package/tsconfig.json +23 -0
|
@@ -0,0 +1,304 @@
|
|
|
1
|
+
import { createReadStream } from 'fs';
|
|
2
|
+
import { Logger } from '@mate-academy/core';
|
|
3
|
+
import { OpenAI } from 'openai';
|
|
4
|
+
import {
|
|
5
|
+
LLMAssistanceOptions,
|
|
6
|
+
LLMAssistanceResult,
|
|
7
|
+
LLMCreateAssistantOptions,
|
|
8
|
+
LLMCreateAssistantResult,
|
|
9
|
+
LLMCreateChatOptions,
|
|
10
|
+
LLMCreateChatResult,
|
|
11
|
+
LLMCreateFileStorageOptions,
|
|
12
|
+
LLMCreateFileStorageResult,
|
|
13
|
+
LLMInstanceOptions,
|
|
14
|
+
LLMProviders,
|
|
15
|
+
LLMRoles,
|
|
16
|
+
LLMUploadFile,
|
|
17
|
+
LLMUploadFileResult,
|
|
18
|
+
} from '../../../LLMService.typedefs';
|
|
19
|
+
import { LLMAssistanceService } from '../../../services';
|
|
20
|
+
import { OpenAIRoles } from '../OpenAI.typedefs';
|
|
21
|
+
|
|
22
|
+
export class OpenAIAssistanceService
|
|
23
|
+
extends LLMAssistanceService<LLMProviders.OpenAI> {
|
|
24
|
+
#instance: OpenAI | null = null;
|
|
25
|
+
|
|
26
|
+
private uploadedFiles: Map<string, string> = new Map();
|
|
27
|
+
|
|
28
|
+
constructor(
|
|
29
|
+
logger: Logger,
|
|
30
|
+
options: LLMInstanceOptions[LLMProviders.OpenAI],
|
|
31
|
+
) {
|
|
32
|
+
super(LLMProviders.OpenAI, logger, options);
|
|
33
|
+
}
|
|
34
|
+
|
|
35
|
+
protected get instance(): OpenAI {
|
|
36
|
+
if (!this.#instance) {
|
|
37
|
+
this.#instance = new OpenAI(this.options);
|
|
38
|
+
}
|
|
39
|
+
|
|
40
|
+
return this.#instance;
|
|
41
|
+
}
|
|
42
|
+
|
|
43
|
+
async uploadFile(file: LLMUploadFile): Promise<LLMUploadFileResult> {
|
|
44
|
+
try {
|
|
45
|
+
const { path } = file;
|
|
46
|
+
|
|
47
|
+
const exstingFileId = this.uploadedFiles.get(path);
|
|
48
|
+
|
|
49
|
+
if (exstingFileId) {
|
|
50
|
+
const storedFile = await this.instance.files.retrieve(exstingFileId);
|
|
51
|
+
|
|
52
|
+
if (storedFile.id) {
|
|
53
|
+
return {
|
|
54
|
+
...file,
|
|
55
|
+
fileId: exstingFileId,
|
|
56
|
+
};
|
|
57
|
+
}
|
|
58
|
+
}
|
|
59
|
+
|
|
60
|
+
const readableStream = createReadStream(path);
|
|
61
|
+
|
|
62
|
+
const uploadedFile = await this.instance.files.create({
|
|
63
|
+
purpose: 'assistants',
|
|
64
|
+
file: readableStream,
|
|
65
|
+
});
|
|
66
|
+
|
|
67
|
+
await this.instance.files.waitForProcessing(uploadedFile.id);
|
|
68
|
+
|
|
69
|
+
this.uploadedFiles.set(path, uploadedFile.id);
|
|
70
|
+
|
|
71
|
+
return {
|
|
72
|
+
...file,
|
|
73
|
+
fileId: uploadedFile.id,
|
|
74
|
+
};
|
|
75
|
+
} catch (error: any) {
|
|
76
|
+
this.logger
|
|
77
|
+
.child('uploadFile')
|
|
78
|
+
.error('Error uploading file', {
|
|
79
|
+
error,
|
|
80
|
+
file,
|
|
81
|
+
});
|
|
82
|
+
|
|
83
|
+
return { error };
|
|
84
|
+
}
|
|
85
|
+
}
|
|
86
|
+
|
|
87
|
+
async deleteFile(
|
|
88
|
+
fileId: string,
|
|
89
|
+
): Promise<void> {
|
|
90
|
+
await this.instance.files.del(fileId);
|
|
91
|
+
|
|
92
|
+
this.uploadedFiles.delete(fileId);
|
|
93
|
+
}
|
|
94
|
+
|
|
95
|
+
async createFileStorage(
|
|
96
|
+
options: LLMCreateFileStorageOptions<LLMProviders.OpenAI>,
|
|
97
|
+
): Promise<LLMCreateFileStorageResult> {
|
|
98
|
+
const { uploadedFiles } = options;
|
|
99
|
+
|
|
100
|
+
try {
|
|
101
|
+
const vectorStore = await this.instance.vectorStores.create({
|
|
102
|
+
expires_after: {
|
|
103
|
+
anchor: 'last_active_at',
|
|
104
|
+
days: 1,
|
|
105
|
+
},
|
|
106
|
+
});
|
|
107
|
+
|
|
108
|
+
await this.instance.vectorStores.fileBatches
|
|
109
|
+
.createAndPoll(
|
|
110
|
+
vectorStore.id,
|
|
111
|
+
{ file_ids: uploadedFiles.map(({ fileId }) => fileId) },
|
|
112
|
+
);
|
|
113
|
+
|
|
114
|
+
return {
|
|
115
|
+
storageId: vectorStore.id,
|
|
116
|
+
};
|
|
117
|
+
} catch (error: any) {
|
|
118
|
+
this.logger
|
|
119
|
+
.child('createFileStorage')
|
|
120
|
+
.error('Error creating file storage', {
|
|
121
|
+
error,
|
|
122
|
+
uploadedFiles,
|
|
123
|
+
});
|
|
124
|
+
|
|
125
|
+
return { error };
|
|
126
|
+
}
|
|
127
|
+
}
|
|
128
|
+
|
|
129
|
+
async deleteFileStorage(
|
|
130
|
+
fileStorageId: string,
|
|
131
|
+
): Promise<void> {
|
|
132
|
+
await this.instance.vectorStores.del(fileStorageId);
|
|
133
|
+
}
|
|
134
|
+
|
|
135
|
+
async createChat(
|
|
136
|
+
options: LLMCreateChatOptions<LLMProviders.OpenAI>,
|
|
137
|
+
): Promise<LLMCreateChatResult> {
|
|
138
|
+
try {
|
|
139
|
+
const {
|
|
140
|
+
storageId,
|
|
141
|
+
} = options;
|
|
142
|
+
|
|
143
|
+
const fileSearchOptions = storageId
|
|
144
|
+
? {
|
|
145
|
+
tool_resources: {
|
|
146
|
+
file_search: {
|
|
147
|
+
vector_store_ids: [storageId],
|
|
148
|
+
},
|
|
149
|
+
},
|
|
150
|
+
}
|
|
151
|
+
: {};
|
|
152
|
+
|
|
153
|
+
const thread = await this.instance.beta.threads.create({
|
|
154
|
+
...fileSearchOptions,
|
|
155
|
+
});
|
|
156
|
+
|
|
157
|
+
return {
|
|
158
|
+
chatId: thread.id,
|
|
159
|
+
};
|
|
160
|
+
} catch (error: any) {
|
|
161
|
+
this.logger
|
|
162
|
+
.child('createChat')
|
|
163
|
+
.error('Error creating chat', {
|
|
164
|
+
error,
|
|
165
|
+
options,
|
|
166
|
+
});
|
|
167
|
+
|
|
168
|
+
return { error };
|
|
169
|
+
}
|
|
170
|
+
}
|
|
171
|
+
|
|
172
|
+
async deleteChat(
|
|
173
|
+
chatId: string,
|
|
174
|
+
): Promise<void> {
|
|
175
|
+
await this.instance.beta.threads.del(chatId);
|
|
176
|
+
}
|
|
177
|
+
|
|
178
|
+
async createAssistant(
|
|
179
|
+
options: LLMCreateAssistantOptions<LLMProviders.OpenAI>,
|
|
180
|
+
): Promise<LLMCreateAssistantResult> {
|
|
181
|
+
try {
|
|
182
|
+
const {
|
|
183
|
+
model,
|
|
184
|
+
instructions,
|
|
185
|
+
storageIds,
|
|
186
|
+
} = options;
|
|
187
|
+
|
|
188
|
+
const fileSearchOptions: Pick<
|
|
189
|
+
OpenAI.Beta.AssistantCreateParams,
|
|
190
|
+
'tools' | 'tool_resources'
|
|
191
|
+
> = storageIds?.length
|
|
192
|
+
? {
|
|
193
|
+
tools: [
|
|
194
|
+
{
|
|
195
|
+
type: 'file_search',
|
|
196
|
+
},
|
|
197
|
+
],
|
|
198
|
+
tool_resources: {
|
|
199
|
+
file_search: {
|
|
200
|
+
vector_store_ids: storageIds,
|
|
201
|
+
},
|
|
202
|
+
},
|
|
203
|
+
}
|
|
204
|
+
: {};
|
|
205
|
+
|
|
206
|
+
const assistant = await this.instance.beta.assistants.create({
|
|
207
|
+
model: model.name,
|
|
208
|
+
instructions,
|
|
209
|
+
temperature: model.config.temperature,
|
|
210
|
+
top_p: model.config.top_p,
|
|
211
|
+
...fileSearchOptions,
|
|
212
|
+
});
|
|
213
|
+
|
|
214
|
+
return {
|
|
215
|
+
assistantId: assistant.id,
|
|
216
|
+
};
|
|
217
|
+
} catch (error: any) {
|
|
218
|
+
this.logger
|
|
219
|
+
.child('createAssistant')
|
|
220
|
+
.error('Error creating assistant', {
|
|
221
|
+
error,
|
|
222
|
+
options,
|
|
223
|
+
});
|
|
224
|
+
|
|
225
|
+
return { error };
|
|
226
|
+
}
|
|
227
|
+
}
|
|
228
|
+
|
|
229
|
+
async deleteAssistant(
|
|
230
|
+
assistantId: string,
|
|
231
|
+
): Promise<void> {
|
|
232
|
+
await this.instance.beta.assistants.del(assistantId);
|
|
233
|
+
}
|
|
234
|
+
|
|
235
|
+
async assistInChat(
|
|
236
|
+
options: LLMAssistanceOptions<LLMProviders.OpenAI>,
|
|
237
|
+
): Promise<LLMAssistanceResult> {
|
|
238
|
+
try {
|
|
239
|
+
const {
|
|
240
|
+
message,
|
|
241
|
+
chatId,
|
|
242
|
+
assistantId,
|
|
243
|
+
} = options;
|
|
244
|
+
|
|
245
|
+
const threadMessageData = {
|
|
246
|
+
content: message.content,
|
|
247
|
+
role: (
|
|
248
|
+
message.role === LLMRoles.User
|
|
249
|
+
? OpenAIRoles.User
|
|
250
|
+
: OpenAIRoles.Assistant
|
|
251
|
+
),
|
|
252
|
+
} as const;
|
|
253
|
+
|
|
254
|
+
await this.instance.beta.threads.messages.create(
|
|
255
|
+
chatId,
|
|
256
|
+
threadMessageData,
|
|
257
|
+
);
|
|
258
|
+
|
|
259
|
+
const threadRun = await this.instance.beta.threads.runs.createAndPoll(
|
|
260
|
+
chatId,
|
|
261
|
+
{
|
|
262
|
+
assistant_id: assistantId,
|
|
263
|
+
},
|
|
264
|
+
);
|
|
265
|
+
|
|
266
|
+
const { status } = threadRun;
|
|
267
|
+
|
|
268
|
+
if (status !== 'completed') {
|
|
269
|
+
throw new Error(`Thread run did not complete for message, status: ${status}`);
|
|
270
|
+
}
|
|
271
|
+
|
|
272
|
+
const threadRunMessages = await this.instance.beta.threads.messages.list(
|
|
273
|
+
chatId,
|
|
274
|
+
{
|
|
275
|
+
run_id: threadRun.id,
|
|
276
|
+
},
|
|
277
|
+
);
|
|
278
|
+
|
|
279
|
+
const assistantResponse = threadRunMessages.data.find(
|
|
280
|
+
(message) => message.role === OpenAIRoles.Assistant,
|
|
281
|
+
);
|
|
282
|
+
|
|
283
|
+
const textContent = assistantResponse?.content
|
|
284
|
+
.find(
|
|
285
|
+
(content): content is OpenAI.Beta.Threads.TextContentBlock => (
|
|
286
|
+
content.type === 'text' && 'text' in content
|
|
287
|
+
),
|
|
288
|
+
);
|
|
289
|
+
|
|
290
|
+
return {
|
|
291
|
+
text: textContent?.text?.value ?? '',
|
|
292
|
+
};
|
|
293
|
+
} catch (error: any) {
|
|
294
|
+
this.logger
|
|
295
|
+
.child('assistInChat')
|
|
296
|
+
.error('Error assisting in chat', {
|
|
297
|
+
error,
|
|
298
|
+
options,
|
|
299
|
+
});
|
|
300
|
+
|
|
301
|
+
return { error };
|
|
302
|
+
}
|
|
303
|
+
}
|
|
304
|
+
}
|
|
@@ -0,0 +1,74 @@
|
|
|
1
|
+
import { Logger } from '@mate-academy/core';
|
|
2
|
+
import { OpenAI } from 'openai';
|
|
3
|
+
import {
|
|
4
|
+
LLMCompletionResult,
|
|
5
|
+
LLMInstanceOptions,
|
|
6
|
+
LLMProviders,
|
|
7
|
+
LLMSendMessageOptions,
|
|
8
|
+
} from '../../../LLMService.typedefs';
|
|
9
|
+
import { LLMServiceEntity } from '../../../LLMService.entity';
|
|
10
|
+
import {
|
|
11
|
+
LLMCompletionService,
|
|
12
|
+
} from '../../../services';
|
|
13
|
+
import { OpenAIEntity } from '../OpenAI.entity';
|
|
14
|
+
|
|
15
|
+
export class OpenAICompletionService
|
|
16
|
+
extends LLMCompletionService<LLMProviders.OpenAI> {
|
|
17
|
+
#instance: OpenAI | null = null;
|
|
18
|
+
|
|
19
|
+
constructor(
|
|
20
|
+
logger: Logger,
|
|
21
|
+
options: LLMInstanceOptions[LLMProviders.OpenAI],
|
|
22
|
+
) {
|
|
23
|
+
super(LLMProviders.OpenAI, logger, options);
|
|
24
|
+
}
|
|
25
|
+
|
|
26
|
+
protected get instance(): OpenAI {
|
|
27
|
+
if (!this.#instance) {
|
|
28
|
+
this.#instance = new OpenAI(this.options);
|
|
29
|
+
}
|
|
30
|
+
|
|
31
|
+
return this.#instance;
|
|
32
|
+
}
|
|
33
|
+
|
|
34
|
+
async sendMessage(
|
|
35
|
+
options: LLMSendMessageOptions<typeof this.provider>,
|
|
36
|
+
): Promise<LLMCompletionResult> {
|
|
37
|
+
try {
|
|
38
|
+
const {
|
|
39
|
+
model,
|
|
40
|
+
message,
|
|
41
|
+
history = [],
|
|
42
|
+
} = options;
|
|
43
|
+
|
|
44
|
+
const countTokensFn = OpenAIEntity.getCountTokensFn();
|
|
45
|
+
|
|
46
|
+
const combinedMessages = await LLMServiceEntity.combineMessagesWithLimit({
|
|
47
|
+
history,
|
|
48
|
+
message: message.content,
|
|
49
|
+
model,
|
|
50
|
+
countTokensFn,
|
|
51
|
+
});
|
|
52
|
+
|
|
53
|
+
const completion = await this.instance.chat.completions.create({
|
|
54
|
+
model: model.name,
|
|
55
|
+
messages: combinedMessages,
|
|
56
|
+
n: 1,
|
|
57
|
+
...model.config,
|
|
58
|
+
});
|
|
59
|
+
|
|
60
|
+
return {
|
|
61
|
+
text: completion.choices[0]?.message.content ?? '',
|
|
62
|
+
};
|
|
63
|
+
} catch (error: any) {
|
|
64
|
+
this.logger
|
|
65
|
+
.child('sendMessage')
|
|
66
|
+
.error('Error sending message', {
|
|
67
|
+
error,
|
|
68
|
+
options,
|
|
69
|
+
});
|
|
70
|
+
|
|
71
|
+
return { error };
|
|
72
|
+
}
|
|
73
|
+
}
|
|
74
|
+
}
|
|
@@ -0,0 +1,73 @@
|
|
|
1
|
+
import { Logger } from '@mate-academy/core';
|
|
2
|
+
import {
|
|
3
|
+
LLMAssistanceOptions,
|
|
4
|
+
LLMAssistanceResult,
|
|
5
|
+
LLMCreateAssistantOptions,
|
|
6
|
+
LLMCreateAssistantResult,
|
|
7
|
+
LLMCreateChatOptions,
|
|
8
|
+
LLMCreateChatResult,
|
|
9
|
+
LLMCreateFileStorageOptions,
|
|
10
|
+
LLMCreateFileStorageResult,
|
|
11
|
+
LLMInstanceOptions,
|
|
12
|
+
LLMProviders,
|
|
13
|
+
LLMPurposes,
|
|
14
|
+
LLMUploadFile,
|
|
15
|
+
LLMUploadFileResult,
|
|
16
|
+
} from '../LLMService.typedefs';
|
|
17
|
+
import { LLMBaseService } from '../services';
|
|
18
|
+
|
|
19
|
+
export abstract class LLMAssistanceService<Provider extends LLMProviders>
|
|
20
|
+
extends LLMBaseService<
|
|
21
|
+
LLMPurposes.Assistance,
|
|
22
|
+
Provider
|
|
23
|
+
> {
|
|
24
|
+
constructor(
|
|
25
|
+
provider: Provider,
|
|
26
|
+
logger: Logger,
|
|
27
|
+
options: LLMInstanceOptions[Provider],
|
|
28
|
+
) {
|
|
29
|
+
super(
|
|
30
|
+
provider,
|
|
31
|
+
'AssistanceService',
|
|
32
|
+
LLMPurposes.Assistance,
|
|
33
|
+
logger,
|
|
34
|
+
options,
|
|
35
|
+
);
|
|
36
|
+
}
|
|
37
|
+
|
|
38
|
+
abstract uploadFile(
|
|
39
|
+
file: LLMUploadFile,
|
|
40
|
+
): Promise<LLMUploadFileResult>;
|
|
41
|
+
|
|
42
|
+
abstract deleteFile(
|
|
43
|
+
fileId: string,
|
|
44
|
+
): Promise<void>;
|
|
45
|
+
|
|
46
|
+
abstract createFileStorage(
|
|
47
|
+
options: LLMCreateFileStorageOptions<Provider>,
|
|
48
|
+
): Promise<LLMCreateFileStorageResult>;
|
|
49
|
+
|
|
50
|
+
abstract deleteFileStorage(
|
|
51
|
+
fileStorageId: string,
|
|
52
|
+
): Promise<void>;
|
|
53
|
+
|
|
54
|
+
abstract createAssistant(
|
|
55
|
+
options: LLMCreateAssistantOptions<Provider>,
|
|
56
|
+
): Promise<LLMCreateAssistantResult>;
|
|
57
|
+
|
|
58
|
+
abstract deleteAssistant(
|
|
59
|
+
assistantId: string,
|
|
60
|
+
): Promise<void>;
|
|
61
|
+
|
|
62
|
+
abstract createChat(
|
|
63
|
+
options: LLMCreateChatOptions<Provider>,
|
|
64
|
+
): Promise<LLMCreateChatResult>;
|
|
65
|
+
|
|
66
|
+
abstract deleteChat(
|
|
67
|
+
chatId: string,
|
|
68
|
+
): Promise<void>;
|
|
69
|
+
|
|
70
|
+
abstract assistInChat(
|
|
71
|
+
options: LLMAssistanceOptions<Provider>,
|
|
72
|
+
): Promise<LLMAssistanceResult>;
|
|
73
|
+
}
|
|
@@ -0,0 +1,34 @@
|
|
|
1
|
+
import { Logger } from '@mate-academy/core';
|
|
2
|
+
import {
|
|
3
|
+
LLMInstanceOptions,
|
|
4
|
+
LLMInstances,
|
|
5
|
+
LLMProviderAvailableModels,
|
|
6
|
+
LLMProviders,
|
|
7
|
+
LLMPurposes,
|
|
8
|
+
} from '../LLMService.typedefs';
|
|
9
|
+
import { LLM_SERVICE_MODELS } from '../LLMService.constants';
|
|
10
|
+
|
|
11
|
+
export abstract class LLMBaseService<
|
|
12
|
+
Purpose extends LLMPurposes,
|
|
13
|
+
Provider extends LLMProviders
|
|
14
|
+
> {
|
|
15
|
+
constructor(
|
|
16
|
+
public readonly provider: Provider,
|
|
17
|
+
public readonly serviceName: string,
|
|
18
|
+
public readonly purpose: Purpose,
|
|
19
|
+
public logger: Logger,
|
|
20
|
+
public options: LLMInstanceOptions[Provider],
|
|
21
|
+
) {
|
|
22
|
+
this.initLogger();
|
|
23
|
+
}
|
|
24
|
+
|
|
25
|
+
protected abstract get instance(): LLMInstances[Provider];
|
|
26
|
+
|
|
27
|
+
public get models(): Partial<LLMProviderAvailableModels<Provider>> {
|
|
28
|
+
return LLM_SERVICE_MODELS[this.provider][this.purpose];
|
|
29
|
+
}
|
|
30
|
+
|
|
31
|
+
protected initLogger(): void {
|
|
32
|
+
this.logger = this.logger.child(`${this.provider}:${this.serviceName}`);
|
|
33
|
+
}
|
|
34
|
+
}
|
|
@@ -0,0 +1,32 @@
|
|
|
1
|
+
import { Logger } from '@mate-academy/core';
|
|
2
|
+
import {
|
|
3
|
+
LLMPurposes,
|
|
4
|
+
LLMProviders,
|
|
5
|
+
LLMSendMessageOptions,
|
|
6
|
+
LLMCompletionResult,
|
|
7
|
+
LLMInstanceOptions,
|
|
8
|
+
} from '../LLMService.typedefs';
|
|
9
|
+
import { LLMBaseService } from '../services';
|
|
10
|
+
|
|
11
|
+
export abstract class LLMCompletionService<
|
|
12
|
+
Provider extends LLMProviders
|
|
13
|
+
>
|
|
14
|
+
extends LLMBaseService<LLMPurposes.Completion, Provider> {
|
|
15
|
+
constructor(
|
|
16
|
+
provider: Provider,
|
|
17
|
+
logger: Logger,
|
|
18
|
+
options: LLMInstanceOptions[Provider],
|
|
19
|
+
) {
|
|
20
|
+
super(
|
|
21
|
+
provider,
|
|
22
|
+
'CompletionService',
|
|
23
|
+
LLMPurposes.Completion,
|
|
24
|
+
logger,
|
|
25
|
+
options,
|
|
26
|
+
);
|
|
27
|
+
}
|
|
28
|
+
|
|
29
|
+
abstract sendMessage(
|
|
30
|
+
options: LLMSendMessageOptions<Provider>,
|
|
31
|
+
): Promise<LLMCompletionResult>;
|
|
32
|
+
}
|
|
@@ -0,0 +1,15 @@
|
|
|
1
|
+
import { Logger } from '@mate-academy/core';
|
|
2
|
+
import {
|
|
3
|
+
LLMInstanceOptions,
|
|
4
|
+
LLMProviders,
|
|
5
|
+
LLMPurposes,
|
|
6
|
+
LLMServiceByPurpose,
|
|
7
|
+
} from '../LLMService.typedefs';
|
|
8
|
+
|
|
9
|
+
export abstract class LLMServicePurposeFactory<Provider extends LLMProviders> {
|
|
10
|
+
abstract createService<Purpose extends LLMPurposes>(
|
|
11
|
+
purpose: Purpose,
|
|
12
|
+
logger: Logger,
|
|
13
|
+
options: LLMInstanceOptions[Provider],
|
|
14
|
+
): LLMServiceByPurpose<Provider>[Purpose];
|
|
15
|
+
}
|
package/tsconfig.json
ADDED
|
@@ -0,0 +1,23 @@
|
|
|
1
|
+
{
|
|
2
|
+
"extends": "@mate-academy/ts-config",
|
|
3
|
+
"compilerOptions": {
|
|
4
|
+
"target": "ESNext",
|
|
5
|
+
"module": "CommonJS",
|
|
6
|
+
"declaration": true,
|
|
7
|
+
"declarationDir": "./dist",
|
|
8
|
+
"baseUrl": "./src",
|
|
9
|
+
"outDir": "./dist",
|
|
10
|
+
"strict": true,
|
|
11
|
+
"types": [
|
|
12
|
+
"node",
|
|
13
|
+
]
|
|
14
|
+
},
|
|
15
|
+
"include": [
|
|
16
|
+
"src/**/*",
|
|
17
|
+
],
|
|
18
|
+
"exclude": [
|
|
19
|
+
"node_modules",
|
|
20
|
+
"dist"
|
|
21
|
+
]
|
|
22
|
+
}
|
|
23
|
+
|