@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,307 @@
|
|
|
1
|
+
import { Logger } from '@mate-academy/core';
|
|
2
|
+
import { v4 as uuidv4 } from 'uuid';
|
|
3
|
+
import {
|
|
4
|
+
Chat,
|
|
5
|
+
Content,
|
|
6
|
+
CachedContent,
|
|
7
|
+
GoogleGenAI,
|
|
8
|
+
} from '@google/genai';
|
|
9
|
+
import {
|
|
10
|
+
LLMAssistanceOptions,
|
|
11
|
+
LLMAssistanceResult,
|
|
12
|
+
LLMCreateAssistantResult,
|
|
13
|
+
LLMCreateChatOptions,
|
|
14
|
+
LLMCreateChatResult,
|
|
15
|
+
LLMCreateFileStorageOptions,
|
|
16
|
+
LLMCreateFileStorageResult,
|
|
17
|
+
LLMInstanceOptions,
|
|
18
|
+
LLMProviders,
|
|
19
|
+
LLMUploadedFile,
|
|
20
|
+
LLMUploadFile,
|
|
21
|
+
LLMUploadFileResult,
|
|
22
|
+
} from '../../../LLMService.typedefs';
|
|
23
|
+
import { LLMAssistanceService } from '../../../services';
|
|
24
|
+
import { GoogleGenerativeAIRoles } from '../GoogleGenerativeAI.typedefs';
|
|
25
|
+
import { GoogleGenerativeAIEntity } from '../GoogleGenerativeAI.entity';
|
|
26
|
+
import { MIN_CACHE_CONTENT_LENGTH } from '../GoogleGenerativeAI.constants';
|
|
27
|
+
|
|
28
|
+
export class GoogleGenerativeAIAssistanceService
|
|
29
|
+
extends LLMAssistanceService<LLMProviders.GoogleGenerativeAI> {
|
|
30
|
+
#instance: GoogleGenAI | null = null;
|
|
31
|
+
|
|
32
|
+
private cachedContent: Map<string, CachedContent> = new Map();
|
|
33
|
+
|
|
34
|
+
private chatSessions: Map<string, Chat> = new Map();
|
|
35
|
+
|
|
36
|
+
private uploadedFiles: Map<string, LLMUploadedFile> = new Map();
|
|
37
|
+
|
|
38
|
+
constructor(
|
|
39
|
+
logger: Logger,
|
|
40
|
+
options: LLMInstanceOptions[LLMProviders.GoogleGenerativeAI],
|
|
41
|
+
) {
|
|
42
|
+
super(LLMProviders.GoogleGenerativeAI, logger, options);
|
|
43
|
+
}
|
|
44
|
+
|
|
45
|
+
protected get instance(): GoogleGenAI {
|
|
46
|
+
if (!this.#instance) {
|
|
47
|
+
this.#instance = new GoogleGenAI({
|
|
48
|
+
apiKey: this.options.apiKey,
|
|
49
|
+
});
|
|
50
|
+
}
|
|
51
|
+
|
|
52
|
+
return this.#instance;
|
|
53
|
+
}
|
|
54
|
+
|
|
55
|
+
async uploadFile(file: LLMUploadFile): Promise<LLMUploadFileResult> {
|
|
56
|
+
try {
|
|
57
|
+
const {
|
|
58
|
+
path,
|
|
59
|
+
mimeType,
|
|
60
|
+
name,
|
|
61
|
+
} = file;
|
|
62
|
+
|
|
63
|
+
const existingFile = this.uploadedFiles.get(path);
|
|
64
|
+
|
|
65
|
+
if (existingFile) {
|
|
66
|
+
return existingFile;
|
|
67
|
+
}
|
|
68
|
+
|
|
69
|
+
const uploadResult = await this.instance.files.upload({
|
|
70
|
+
file: path,
|
|
71
|
+
config: {
|
|
72
|
+
mimeType,
|
|
73
|
+
displayName: name,
|
|
74
|
+
},
|
|
75
|
+
});
|
|
76
|
+
|
|
77
|
+
if (!uploadResult.name) {
|
|
78
|
+
throw new Error('Upload result did not return a name');
|
|
79
|
+
}
|
|
80
|
+
|
|
81
|
+
if (!uploadResult.uri) {
|
|
82
|
+
throw new Error('Upload result did not return a uri');
|
|
83
|
+
}
|
|
84
|
+
|
|
85
|
+
if (uploadResult.error) {
|
|
86
|
+
throw uploadResult.error;
|
|
87
|
+
}
|
|
88
|
+
|
|
89
|
+
const uploadedFile = {
|
|
90
|
+
...file,
|
|
91
|
+
fileId: uploadResult.name,
|
|
92
|
+
path: uploadResult.uri,
|
|
93
|
+
};
|
|
94
|
+
|
|
95
|
+
this.uploadedFiles.set(path, uploadedFile);
|
|
96
|
+
|
|
97
|
+
return uploadedFile;
|
|
98
|
+
} catch (error: any) {
|
|
99
|
+
this.logger
|
|
100
|
+
.child('uploadFile')
|
|
101
|
+
.error('Error uploading file', {
|
|
102
|
+
error,
|
|
103
|
+
file,
|
|
104
|
+
});
|
|
105
|
+
|
|
106
|
+
return { error };
|
|
107
|
+
}
|
|
108
|
+
}
|
|
109
|
+
|
|
110
|
+
async deleteFile(
|
|
111
|
+
fileId: string,
|
|
112
|
+
): Promise<void> {
|
|
113
|
+
this.uploadedFiles.delete(fileId);
|
|
114
|
+
|
|
115
|
+
await this.instance.files.delete({
|
|
116
|
+
name: fileId,
|
|
117
|
+
});
|
|
118
|
+
}
|
|
119
|
+
|
|
120
|
+
async createFileStorage(
|
|
121
|
+
options: LLMCreateFileStorageOptions<LLMProviders.GoogleGenerativeAI>,
|
|
122
|
+
): Promise<LLMCreateFileStorageResult> {
|
|
123
|
+
const {
|
|
124
|
+
uploadedFiles,
|
|
125
|
+
model,
|
|
126
|
+
instructions,
|
|
127
|
+
} = options;
|
|
128
|
+
|
|
129
|
+
try {
|
|
130
|
+
const parts = GoogleGenerativeAIEntity.getFileDataParts(uploadedFiles);
|
|
131
|
+
|
|
132
|
+
const countTokensFn = GoogleGenerativeAIEntity
|
|
133
|
+
.getCountTokensFn(this.instance);
|
|
134
|
+
|
|
135
|
+
const tokenCount = await countTokensFn(parts, model);
|
|
136
|
+
|
|
137
|
+
if (tokenCount < MIN_CACHE_CONTENT_LENGTH) {
|
|
138
|
+
return {
|
|
139
|
+
storageId: '',
|
|
140
|
+
};
|
|
141
|
+
}
|
|
142
|
+
|
|
143
|
+
const cacheResult = await this.instance.caches.create({
|
|
144
|
+
model: model.name,
|
|
145
|
+
config: {
|
|
146
|
+
contents: [
|
|
147
|
+
{
|
|
148
|
+
role: GoogleGenerativeAIRoles.User,
|
|
149
|
+
parts,
|
|
150
|
+
},
|
|
151
|
+
],
|
|
152
|
+
systemInstruction: instructions,
|
|
153
|
+
ttl: '1h',
|
|
154
|
+
},
|
|
155
|
+
});
|
|
156
|
+
|
|
157
|
+
if (!cacheResult.name) {
|
|
158
|
+
throw new Error('Cache result did not return a name');
|
|
159
|
+
}
|
|
160
|
+
|
|
161
|
+
this.cachedContent.set(cacheResult.name, cacheResult);
|
|
162
|
+
|
|
163
|
+
return {
|
|
164
|
+
storageId: cacheResult.name,
|
|
165
|
+
};
|
|
166
|
+
} catch (error: any) {
|
|
167
|
+
this.logger
|
|
168
|
+
.child('createFileStorage')
|
|
169
|
+
.error('Error creating file storage', {
|
|
170
|
+
error,
|
|
171
|
+
uploadedFiles,
|
|
172
|
+
model,
|
|
173
|
+
});
|
|
174
|
+
|
|
175
|
+
return { error };
|
|
176
|
+
}
|
|
177
|
+
}
|
|
178
|
+
|
|
179
|
+
async deleteFileStorage(
|
|
180
|
+
fileStorageId: string,
|
|
181
|
+
): Promise<void> {
|
|
182
|
+
if (fileStorageId === '') {
|
|
183
|
+
return;
|
|
184
|
+
}
|
|
185
|
+
|
|
186
|
+
this.cachedContent.delete(fileStorageId);
|
|
187
|
+
|
|
188
|
+
await this.instance.caches.delete({
|
|
189
|
+
name: fileStorageId,
|
|
190
|
+
});
|
|
191
|
+
}
|
|
192
|
+
|
|
193
|
+
createChat(
|
|
194
|
+
options: LLMCreateChatOptions<LLMProviders.GoogleGenerativeAI>,
|
|
195
|
+
): Promise<LLMCreateChatResult> {
|
|
196
|
+
try {
|
|
197
|
+
const {
|
|
198
|
+
storageId,
|
|
199
|
+
model,
|
|
200
|
+
instructions,
|
|
201
|
+
history: providedHistory,
|
|
202
|
+
files,
|
|
203
|
+
} = options;
|
|
204
|
+
|
|
205
|
+
const cachedContent = storageId
|
|
206
|
+
? this.cachedContent.get(storageId)
|
|
207
|
+
: undefined;
|
|
208
|
+
|
|
209
|
+
const history: Content[] = providedHistory
|
|
210
|
+
? [
|
|
211
|
+
{
|
|
212
|
+
role: GoogleGenerativeAIRoles.User,
|
|
213
|
+
parts: GoogleGenerativeAIEntity.getTextParts(providedHistory),
|
|
214
|
+
},
|
|
215
|
+
]
|
|
216
|
+
: [];
|
|
217
|
+
|
|
218
|
+
if (!cachedContent && files) {
|
|
219
|
+
const fileParts = GoogleGenerativeAIEntity.getFileDataParts(files);
|
|
220
|
+
|
|
221
|
+
history.push({
|
|
222
|
+
role: GoogleGenerativeAIRoles.User,
|
|
223
|
+
parts: fileParts,
|
|
224
|
+
});
|
|
225
|
+
}
|
|
226
|
+
|
|
227
|
+
const chatSession = this.instance.chats.create({
|
|
228
|
+
model: model.name,
|
|
229
|
+
history,
|
|
230
|
+
config: {
|
|
231
|
+
systemInstruction: instructions,
|
|
232
|
+
temperature: model.config.temperature,
|
|
233
|
+
...(cachedContent && { cachedContent: cachedContent.name }),
|
|
234
|
+
},
|
|
235
|
+
});
|
|
236
|
+
|
|
237
|
+
const chatId = uuidv4();
|
|
238
|
+
|
|
239
|
+
this.chatSessions.set(chatId, chatSession);
|
|
240
|
+
|
|
241
|
+
return Promise.resolve({ chatId });
|
|
242
|
+
} catch (error: any) {
|
|
243
|
+
this.logger
|
|
244
|
+
.child('createChat')
|
|
245
|
+
.error('Error creating chat', {
|
|
246
|
+
error,
|
|
247
|
+
options,
|
|
248
|
+
});
|
|
249
|
+
|
|
250
|
+
return Promise.resolve({ error });
|
|
251
|
+
}
|
|
252
|
+
}
|
|
253
|
+
|
|
254
|
+
deleteChat(
|
|
255
|
+
chatId: string,
|
|
256
|
+
): Promise<void> {
|
|
257
|
+
this.chatSessions.delete(chatId);
|
|
258
|
+
|
|
259
|
+
return Promise.resolve();
|
|
260
|
+
}
|
|
261
|
+
|
|
262
|
+
createAssistant(): Promise<LLMCreateAssistantResult> {
|
|
263
|
+
// Google Generative AI does not have an assistant concept at the moment
|
|
264
|
+
return Promise.resolve({
|
|
265
|
+
assistantId: '',
|
|
266
|
+
});
|
|
267
|
+
}
|
|
268
|
+
|
|
269
|
+
deleteAssistant(): Promise<void> {
|
|
270
|
+
// Google Generative AI does not have an assistant concept at the moment
|
|
271
|
+
return Promise.resolve();
|
|
272
|
+
}
|
|
273
|
+
|
|
274
|
+
async assistInChat(
|
|
275
|
+
options: LLMAssistanceOptions<LLMProviders.GoogleGenerativeAI>,
|
|
276
|
+
): Promise<LLMAssistanceResult> {
|
|
277
|
+
try {
|
|
278
|
+
const {
|
|
279
|
+
message,
|
|
280
|
+
chatId,
|
|
281
|
+
} = options;
|
|
282
|
+
|
|
283
|
+
const chatSession = this.chatSessions.get(chatId);
|
|
284
|
+
|
|
285
|
+
if (!chatSession) {
|
|
286
|
+
throw new Error('Chat session not found');
|
|
287
|
+
}
|
|
288
|
+
|
|
289
|
+
const chatResponse = await chatSession.sendMessage({
|
|
290
|
+
message: message.content,
|
|
291
|
+
});
|
|
292
|
+
|
|
293
|
+
return {
|
|
294
|
+
text: chatResponse.text ?? '',
|
|
295
|
+
};
|
|
296
|
+
} catch (error: any) {
|
|
297
|
+
this.logger
|
|
298
|
+
.child('assistInChat')
|
|
299
|
+
.error('Error assisting in chat', {
|
|
300
|
+
error,
|
|
301
|
+
options,
|
|
302
|
+
});
|
|
303
|
+
|
|
304
|
+
return { error };
|
|
305
|
+
}
|
|
306
|
+
}
|
|
307
|
+
}
|
|
@@ -0,0 +1,79 @@
|
|
|
1
|
+
import { Logger } from '@mate-academy/core';
|
|
2
|
+
import { GoogleGenAI } from '@google/genai';
|
|
3
|
+
import {
|
|
4
|
+
LLMCompletionResult,
|
|
5
|
+
LLMInstanceOptions,
|
|
6
|
+
LLMProviders,
|
|
7
|
+
LLMSendMessageOptions,
|
|
8
|
+
} from '../../../LLMService.typedefs';
|
|
9
|
+
import {
|
|
10
|
+
LLMCompletionService,
|
|
11
|
+
} from '../../../services';
|
|
12
|
+
import { LLMServiceEntity } from '../../../LLMService.entity';
|
|
13
|
+
import { GoogleGenerativeAIEntity } from '../GoogleGenerativeAI.entity';
|
|
14
|
+
|
|
15
|
+
export class GoogleGenerativeAICompletionService
|
|
16
|
+
extends LLMCompletionService<LLMProviders.GoogleGenerativeAI> {
|
|
17
|
+
#instance: GoogleGenAI | null = null;
|
|
18
|
+
|
|
19
|
+
constructor(
|
|
20
|
+
logger: Logger,
|
|
21
|
+
options: LLMInstanceOptions[LLMProviders.GoogleGenerativeAI],
|
|
22
|
+
) {
|
|
23
|
+
super(LLMProviders.GoogleGenerativeAI, logger, options);
|
|
24
|
+
}
|
|
25
|
+
|
|
26
|
+
protected get instance(): GoogleGenAI {
|
|
27
|
+
if (!this.#instance) {
|
|
28
|
+
this.#instance = new GoogleGenAI({
|
|
29
|
+
apiKey: this.options.apiKey,
|
|
30
|
+
});
|
|
31
|
+
}
|
|
32
|
+
|
|
33
|
+
return this.#instance;
|
|
34
|
+
}
|
|
35
|
+
|
|
36
|
+
async sendMessage(
|
|
37
|
+
options: LLMSendMessageOptions<typeof this.provider>,
|
|
38
|
+
): Promise<LLMCompletionResult> {
|
|
39
|
+
try {
|
|
40
|
+
const {
|
|
41
|
+
model,
|
|
42
|
+
message,
|
|
43
|
+
history = [],
|
|
44
|
+
instructions,
|
|
45
|
+
} = options;
|
|
46
|
+
|
|
47
|
+
const countTokensFn = GoogleGenerativeAIEntity
|
|
48
|
+
.getCountTokensFn(this.instance);
|
|
49
|
+
|
|
50
|
+
const combinedMessages = await LLMServiceEntity.combineMessagesWithLimit({
|
|
51
|
+
history,
|
|
52
|
+
message: message.content,
|
|
53
|
+
model,
|
|
54
|
+
countTokensFn,
|
|
55
|
+
});
|
|
56
|
+
|
|
57
|
+
const result = await this.instance.models.generateContent(
|
|
58
|
+
GoogleGenerativeAIEntity.getContentParameters(
|
|
59
|
+
model,
|
|
60
|
+
combinedMessages,
|
|
61
|
+
instructions,
|
|
62
|
+
),
|
|
63
|
+
);
|
|
64
|
+
|
|
65
|
+
return {
|
|
66
|
+
text: result.text ?? '',
|
|
67
|
+
};
|
|
68
|
+
} catch (error: any) {
|
|
69
|
+
this.logger
|
|
70
|
+
.child('sendMessage')
|
|
71
|
+
.error('Error sending message', {
|
|
72
|
+
error,
|
|
73
|
+
options,
|
|
74
|
+
});
|
|
75
|
+
|
|
76
|
+
return { error };
|
|
77
|
+
}
|
|
78
|
+
}
|
|
79
|
+
}
|
|
@@ -0,0 +1,71 @@
|
|
|
1
|
+
import {
|
|
2
|
+
LLMProviderAvailableModels,
|
|
3
|
+
LLMProviderModelsByPurpose,
|
|
4
|
+
LLMProviders,
|
|
5
|
+
LLMPurposes,
|
|
6
|
+
LLMServiceBuilder,
|
|
7
|
+
} from '../../LLMService.typedefs';
|
|
8
|
+
import { OpenAIModelNames } from './OpenAI.typedefs';
|
|
9
|
+
import {
|
|
10
|
+
OpenAIAssistanceService,
|
|
11
|
+
OpenAICompletionService,
|
|
12
|
+
} from './services';
|
|
13
|
+
import { pick } from '../../functional.utils';
|
|
14
|
+
|
|
15
|
+
const OPEN_AI_AVAILABLE_MODELS: LLMProviderAvailableModels<
|
|
16
|
+
LLMProviders.OpenAI
|
|
17
|
+
> = {
|
|
18
|
+
[OpenAIModelNames.GPT_4_OMNI]: {
|
|
19
|
+
name: OpenAIModelNames.GPT_4_OMNI,
|
|
20
|
+
limits: {
|
|
21
|
+
maxInputTokens: 128_000,
|
|
22
|
+
maxOutputTokens: 16_384,
|
|
23
|
+
},
|
|
24
|
+
config: {
|
|
25
|
+
temperature: 0.2,
|
|
26
|
+
},
|
|
27
|
+
},
|
|
28
|
+
[OpenAIModelNames.GPT_4_1]: {
|
|
29
|
+
name: OpenAIModelNames.GPT_4_1,
|
|
30
|
+
limits: {
|
|
31
|
+
maxInputTokens: 1_047_576,
|
|
32
|
+
maxOutputTokens: 32_768,
|
|
33
|
+
},
|
|
34
|
+
config: {
|
|
35
|
+
temperature: 0.2,
|
|
36
|
+
},
|
|
37
|
+
},
|
|
38
|
+
};
|
|
39
|
+
|
|
40
|
+
export const OPEN_AI_MODELS: LLMProviderModelsByPurpose<
|
|
41
|
+
LLMPurposes,
|
|
42
|
+
LLMProviders.OpenAI
|
|
43
|
+
> = {
|
|
44
|
+
[LLMPurposes.Completion]: pick(
|
|
45
|
+
OPEN_AI_AVAILABLE_MODELS,
|
|
46
|
+
[
|
|
47
|
+
OpenAIModelNames.GPT_4_OMNI,
|
|
48
|
+
OpenAIModelNames.GPT_4_1,
|
|
49
|
+
],
|
|
50
|
+
),
|
|
51
|
+
[LLMPurposes.Assistance]: pick(
|
|
52
|
+
OPEN_AI_AVAILABLE_MODELS,
|
|
53
|
+
[
|
|
54
|
+
OpenAIModelNames.GPT_4_OMNI,
|
|
55
|
+
OpenAIModelNames.GPT_4_1,
|
|
56
|
+
],
|
|
57
|
+
),
|
|
58
|
+
};
|
|
59
|
+
|
|
60
|
+
export const OPEN_AI_SERVICE_BUILDERS: {
|
|
61
|
+
[purpose in LLMPurposes]: (
|
|
62
|
+
LLMServiceBuilder<LLMProviders.OpenAI, purpose> | null
|
|
63
|
+
)
|
|
64
|
+
} = {
|
|
65
|
+
[LLMPurposes.Completion]: (logger, options) => (
|
|
66
|
+
new OpenAICompletionService(logger, options)
|
|
67
|
+
),
|
|
68
|
+
[LLMPurposes.Assistance]: (logger, options) => (
|
|
69
|
+
new OpenAIAssistanceService(logger, options)
|
|
70
|
+
),
|
|
71
|
+
};
|
|
@@ -0,0 +1,17 @@
|
|
|
1
|
+
import { getEncoding } from 'js-tiktoken';
|
|
2
|
+
|
|
3
|
+
export class OpenAIEntity {
|
|
4
|
+
static getCountTokensFn() {
|
|
5
|
+
return (
|
|
6
|
+
messages: string[],
|
|
7
|
+
): Promise<number> => {
|
|
8
|
+
const encoder = getEncoding('o200k_base');
|
|
9
|
+
const tokens = messages.reduce(
|
|
10
|
+
(acc, message) => acc + encoder.encode(message).length,
|
|
11
|
+
0,
|
|
12
|
+
);
|
|
13
|
+
|
|
14
|
+
return Promise.resolve(tokens);
|
|
15
|
+
};
|
|
16
|
+
}
|
|
17
|
+
}
|
|
@@ -0,0 +1,29 @@
|
|
|
1
|
+
import { Logger } from '@mate-academy/core';
|
|
2
|
+
import {
|
|
3
|
+
LLMInstanceOptions,
|
|
4
|
+
LLMProviders,
|
|
5
|
+
LLMPurposes,
|
|
6
|
+
LLMServiceByPurpose,
|
|
7
|
+
} from '../../LLMService.typedefs';
|
|
8
|
+
import { LLMServicePurposeFactory } from '../../services';
|
|
9
|
+
import { OPEN_AI_SERVICE_BUILDERS } from './OpenAI.constants';
|
|
10
|
+
|
|
11
|
+
export class OpenAIServiceFactory extends LLMServicePurposeFactory<
|
|
12
|
+
LLMProviders.OpenAI
|
|
13
|
+
> {
|
|
14
|
+
createService<
|
|
15
|
+
Purpose extends LLMPurposes
|
|
16
|
+
>(
|
|
17
|
+
purpose: Purpose,
|
|
18
|
+
logger: Logger,
|
|
19
|
+
options: LLMInstanceOptions[LLMProviders.OpenAI],
|
|
20
|
+
): LLMServiceByPurpose<LLMProviders.OpenAI>[Purpose] {
|
|
21
|
+
const serviceBuilder = OPEN_AI_SERVICE_BUILDERS[purpose];
|
|
22
|
+
|
|
23
|
+
if (!serviceBuilder) {
|
|
24
|
+
throw new Error(`Purpose [${purpose}] is not supported for [${LLMProviders.OpenAI}] service`);
|
|
25
|
+
}
|
|
26
|
+
|
|
27
|
+
return serviceBuilder(logger, options);
|
|
28
|
+
}
|
|
29
|
+
}
|