@drax/ai-back 3.32.0 → 3.35.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/dist/config/GoogleAiConfig.js +8 -0
- package/dist/config/OllamaAiConfig.js +9 -0
- package/dist/factory/AiProviderFactory.js +12 -3
- package/dist/factory/GoogleAiProviderFactory.js +14 -0
- package/dist/factory/OllamaAiProviderFactory.js +14 -0
- package/dist/index.js +7 -1
- package/dist/providers/GoogleAiProvider.js +367 -0
- package/dist/providers/OllamaAiProvider.js +342 -0
- package/dist/tools/BuilderTool.js +9 -0
- package/package.json +4 -2
- package/src/config/GoogleAiConfig.ts +13 -0
- package/src/config/OllamaAiConfig.ts +14 -0
- package/src/factory/AiProviderFactory.ts +12 -5
- package/src/factory/GoogleAiProviderFactory.ts +26 -0
- package/src/factory/OllamaAiProviderFactory.ts +27 -0
- package/src/index.ts +12 -0
- package/src/providers/GoogleAiProvider.ts +489 -0
- package/src/providers/OllamaAiProvider.ts +469 -0
- package/src/tools/BuilderTool.ts +12 -0
- package/test/GoogleAiProvider.test.ts +211 -0
- package/test/ToolBuilder.test.ts +48 -0
- package/tsconfig.tsbuildinfo +1 -1
- package/types/config/GoogleAiConfig.d.ts +8 -0
- package/types/config/GoogleAiConfig.d.ts.map +1 -0
- package/types/config/OllamaAiConfig.d.ts +9 -0
- package/types/config/OllamaAiConfig.d.ts.map +1 -0
- package/types/factory/AiProviderFactory.d.ts.map +1 -1
- package/types/factory/GoogleAiProviderFactory.d.ts +8 -0
- package/types/factory/GoogleAiProviderFactory.d.ts.map +1 -0
- package/types/factory/OllamaAiProviderFactory.d.ts +8 -0
- package/types/factory/OllamaAiProviderFactory.d.ts.map +1 -0
- package/types/index.d.ts.map +1 -1
- package/types/providers/GoogleAiProvider.d.ts +63 -0
- package/types/providers/GoogleAiProvider.d.ts.map +1 -0
- package/types/providers/OllamaAiProvider.d.ts +78 -0
- package/types/providers/OllamaAiProvider.d.ts.map +1 -0
- package/types/tools/BuilderTool.d.ts.map +1 -1
- package/.env +0 -4
- package/dist/agents/ChatbotTaskService.js +0 -143
- package/dist/agents/ChatbotTaskTools.js +0 -756
- package/dist/controllers/AIController.js +0 -150
- package/dist/interfaces/IAILog.js +0 -1
- package/dist/routes/ChatbotTaskRoutes.js +0 -8
- package/dist/tools/ToolBuilder.js +0 -243
- package/dist/vectors/ChromaVector.js +0 -65
- package/types/agents/ChatbotTaskService.d.ts +0 -42
- package/types/agents/ChatbotTaskService.d.ts.map +0 -1
- package/types/agents/ChatbotTaskTools.d.ts +0 -54
- package/types/agents/ChatbotTaskTools.d.ts.map +0 -1
- package/types/controllers/AIController.d.ts +0 -25
- package/types/controllers/AIController.d.ts.map +0 -1
- package/types/interfaces/IAILog.d.ts +0 -77
- package/types/interfaces/IAILog.d.ts.map +0 -1
- package/types/routes/ChatbotTaskRoutes.d.ts +0 -4
- package/types/routes/ChatbotTaskRoutes.d.ts.map +0 -1
- package/types/tools/ToolBuilder.d.ts +0 -47
- package/types/tools/ToolBuilder.d.ts.map +0 -1
- package/types/vectors/ChromaVector.d.ts +0 -21
- package/types/vectors/ChromaVector.d.ts.map +0 -1
|
@@ -0,0 +1,342 @@
|
|
|
1
|
+
import { toJSONSchema } from "zod";
|
|
2
|
+
class OllamaAiProvider {
|
|
3
|
+
constructor(baseUrl, model, visionModel, embeddingModel, aiLogService) {
|
|
4
|
+
if (!baseUrl) {
|
|
5
|
+
throw new Error("Ollama AI baseUrl required");
|
|
6
|
+
}
|
|
7
|
+
if (!model) {
|
|
8
|
+
throw new Error("Ollama AI model required");
|
|
9
|
+
}
|
|
10
|
+
this._baseUrl = baseUrl.replace(/\/+$/, "");
|
|
11
|
+
this._model = model;
|
|
12
|
+
this._visionModel = visionModel;
|
|
13
|
+
this._embeddingModel = embeddingModel;
|
|
14
|
+
this._aiLogService = aiLogService;
|
|
15
|
+
}
|
|
16
|
+
get model() {
|
|
17
|
+
if (!this._model) {
|
|
18
|
+
throw new Error("Ollama AI model not found");
|
|
19
|
+
}
|
|
20
|
+
return this._model;
|
|
21
|
+
}
|
|
22
|
+
get visionModel() {
|
|
23
|
+
return this._visionModel;
|
|
24
|
+
}
|
|
25
|
+
get embeddingModel() {
|
|
26
|
+
return this._embeddingModel ?? this.model;
|
|
27
|
+
}
|
|
28
|
+
async post(path, body) {
|
|
29
|
+
const response = await fetch(`${this._baseUrl}${path}`, {
|
|
30
|
+
method: "POST",
|
|
31
|
+
headers: {
|
|
32
|
+
"Content-Type": "application/json",
|
|
33
|
+
},
|
|
34
|
+
body: JSON.stringify(body),
|
|
35
|
+
});
|
|
36
|
+
if (!response.ok) {
|
|
37
|
+
const errorText = await response.text();
|
|
38
|
+
throw new Error(`Ollama AI request failed (${response.status}): ${errorText}`);
|
|
39
|
+
}
|
|
40
|
+
return await response.json();
|
|
41
|
+
}
|
|
42
|
+
async buildUserMessage(input) {
|
|
43
|
+
if (input.userContent && input.userContent.length > 0) {
|
|
44
|
+
return await this.mapContentPartsToMessage(input.userContent);
|
|
45
|
+
}
|
|
46
|
+
if (input.userImages && input.userImages.length > 0) {
|
|
47
|
+
return {
|
|
48
|
+
role: "user",
|
|
49
|
+
content: input.userInput ?? "",
|
|
50
|
+
images: await Promise.all(input.userImages.map(image => this.imageUrlToBase64(image.url))),
|
|
51
|
+
};
|
|
52
|
+
}
|
|
53
|
+
return {
|
|
54
|
+
role: "user",
|
|
55
|
+
content: input.userInput ?? "",
|
|
56
|
+
};
|
|
57
|
+
}
|
|
58
|
+
async mapContentPartsToMessage(content, role = "user") {
|
|
59
|
+
const text = [];
|
|
60
|
+
const images = [];
|
|
61
|
+
for (const part of content) {
|
|
62
|
+
if (part.type === "text") {
|
|
63
|
+
text.push(part.text);
|
|
64
|
+
continue;
|
|
65
|
+
}
|
|
66
|
+
images.push(await this.imageUrlToBase64(part.imageUrl));
|
|
67
|
+
}
|
|
68
|
+
return {
|
|
69
|
+
role,
|
|
70
|
+
content: text.join("\n"),
|
|
71
|
+
...(images.length > 0 ? { images } : {}),
|
|
72
|
+
};
|
|
73
|
+
}
|
|
74
|
+
async imageUrlToBase64(url) {
|
|
75
|
+
const dataUrlMatch = url.match(/^data:[^;,]+;base64,(.+)$/);
|
|
76
|
+
if (dataUrlMatch) {
|
|
77
|
+
return dataUrlMatch[1];
|
|
78
|
+
}
|
|
79
|
+
const response = await fetch(url);
|
|
80
|
+
if (!response.ok) {
|
|
81
|
+
throw new Error(`Ollama AI image request failed (${response.status}): ${url}`);
|
|
82
|
+
}
|
|
83
|
+
const buffer = Buffer.from(await response.arrayBuffer());
|
|
84
|
+
return buffer.toString("base64");
|
|
85
|
+
}
|
|
86
|
+
async mapHistory(history = []) {
|
|
87
|
+
const messages = [];
|
|
88
|
+
for (const message of history) {
|
|
89
|
+
if (typeof message.content === "string") {
|
|
90
|
+
messages.push({
|
|
91
|
+
role: message.role,
|
|
92
|
+
content: message.content,
|
|
93
|
+
});
|
|
94
|
+
continue;
|
|
95
|
+
}
|
|
96
|
+
messages.push(await this.mapContentPartsToMessage(message.content, message.role));
|
|
97
|
+
}
|
|
98
|
+
return messages;
|
|
99
|
+
}
|
|
100
|
+
hasImageInput(input) {
|
|
101
|
+
if (input.userImages && input.userImages.length > 0) {
|
|
102
|
+
return true;
|
|
103
|
+
}
|
|
104
|
+
if (input.userContent?.some(part => part.type === 'image')) {
|
|
105
|
+
return true;
|
|
106
|
+
}
|
|
107
|
+
return input.history?.some(message => Array.isArray(message.content) && message.content.some(part => part.type === 'image')) ?? false;
|
|
108
|
+
}
|
|
109
|
+
serializePromptInput(input, systemPrompt) {
|
|
110
|
+
return JSON.stringify({
|
|
111
|
+
systemPrompt,
|
|
112
|
+
history: input.history,
|
|
113
|
+
userInput: input.userInput,
|
|
114
|
+
userContent: input.userContent,
|
|
115
|
+
memory: input.memory,
|
|
116
|
+
knowledgeBase: input.knowledgeBase,
|
|
117
|
+
tools: input.tools?.map(tool => ({
|
|
118
|
+
name: tool.name,
|
|
119
|
+
description: tool.description,
|
|
120
|
+
parameters: tool.parameters,
|
|
121
|
+
})),
|
|
122
|
+
});
|
|
123
|
+
}
|
|
124
|
+
serializePromptOutput(output) {
|
|
125
|
+
if (typeof output === "string") {
|
|
126
|
+
return output;
|
|
127
|
+
}
|
|
128
|
+
if (output === null || output === undefined) {
|
|
129
|
+
return undefined;
|
|
130
|
+
}
|
|
131
|
+
return JSON.stringify(output);
|
|
132
|
+
}
|
|
133
|
+
buildLogPayload(input, params) {
|
|
134
|
+
return {
|
|
135
|
+
provider: "ollamaai",
|
|
136
|
+
model: params.model,
|
|
137
|
+
operationTitle: input.operationTitle,
|
|
138
|
+
operationGroup: input.operationGroup,
|
|
139
|
+
ip: input.ip,
|
|
140
|
+
userAgent: input.userAgent,
|
|
141
|
+
input: this.serializePromptInput(input, params.systemPrompt),
|
|
142
|
+
inputImages: input.userImages?.map(image => ({
|
|
143
|
+
url: image.url,
|
|
144
|
+
})) ?? input.userContent
|
|
145
|
+
?.filter(part => part.type === "image")
|
|
146
|
+
.map(part => ({
|
|
147
|
+
url: part.imageUrl,
|
|
148
|
+
})),
|
|
149
|
+
inputFiles: input.inputFiles,
|
|
150
|
+
inputTokens: params.inputTokens,
|
|
151
|
+
outputTokens: params.outputTokens,
|
|
152
|
+
tokens: params.tokens,
|
|
153
|
+
startedAt: params.startedAt,
|
|
154
|
+
endedAt: params.endedAt,
|
|
155
|
+
responseTime: params.endedAt ? `${params.endedAt.getTime() - params.startedAt.getTime()}ms` : undefined,
|
|
156
|
+
output: this.serializePromptOutput(params.output),
|
|
157
|
+
success: params.success,
|
|
158
|
+
errorMessage: params.errorMessage,
|
|
159
|
+
tenant: input.tenant,
|
|
160
|
+
user: input.user,
|
|
161
|
+
};
|
|
162
|
+
}
|
|
163
|
+
async registerPromptLog(input, params) {
|
|
164
|
+
if (!this._aiLogService) {
|
|
165
|
+
return;
|
|
166
|
+
}
|
|
167
|
+
try {
|
|
168
|
+
await this._aiLogService.create(this.buildLogPayload(input, params));
|
|
169
|
+
}
|
|
170
|
+
catch (e) {
|
|
171
|
+
console.error("Error registerPromptLog", {
|
|
172
|
+
name: e?.name,
|
|
173
|
+
message: e?.message,
|
|
174
|
+
stack: e?.stack,
|
|
175
|
+
});
|
|
176
|
+
}
|
|
177
|
+
}
|
|
178
|
+
async generateEmbedding({ text, model }) {
|
|
179
|
+
const response = await this.post("/api/embed", {
|
|
180
|
+
model: model ?? this.embeddingModel,
|
|
181
|
+
input: text,
|
|
182
|
+
});
|
|
183
|
+
return response.embeddings?.[0] ?? response.embedding ?? [];
|
|
184
|
+
}
|
|
185
|
+
mapTools(tools = []) {
|
|
186
|
+
return tools.map(tool => ({
|
|
187
|
+
type: "function",
|
|
188
|
+
function: {
|
|
189
|
+
name: tool.name,
|
|
190
|
+
description: tool.description,
|
|
191
|
+
parameters: tool.parameters ?? {
|
|
192
|
+
type: "object",
|
|
193
|
+
properties: {},
|
|
194
|
+
additionalProperties: false,
|
|
195
|
+
},
|
|
196
|
+
},
|
|
197
|
+
}));
|
|
198
|
+
}
|
|
199
|
+
normalizeResponseFormat(input) {
|
|
200
|
+
if (input.zodSchema) {
|
|
201
|
+
return toJSONSchema(input.zodSchema, {
|
|
202
|
+
target: "draft-7",
|
|
203
|
+
});
|
|
204
|
+
}
|
|
205
|
+
if (!input.jsonSchema) {
|
|
206
|
+
return undefined;
|
|
207
|
+
}
|
|
208
|
+
const jsonSchema = input.jsonSchema;
|
|
209
|
+
if (jsonSchema.type === "json_schema" && jsonSchema.json_schema?.schema) {
|
|
210
|
+
return jsonSchema.json_schema.schema;
|
|
211
|
+
}
|
|
212
|
+
return jsonSchema;
|
|
213
|
+
}
|
|
214
|
+
parseToolArguments(args) {
|
|
215
|
+
if (!args) {
|
|
216
|
+
return {};
|
|
217
|
+
}
|
|
218
|
+
if (typeof args === "object") {
|
|
219
|
+
return args;
|
|
220
|
+
}
|
|
221
|
+
try {
|
|
222
|
+
return JSON.parse(args);
|
|
223
|
+
}
|
|
224
|
+
catch (e) {
|
|
225
|
+
throw new Error(`Invalid tool arguments: ${args}`);
|
|
226
|
+
}
|
|
227
|
+
}
|
|
228
|
+
serializeToolOutput(output) {
|
|
229
|
+
if (typeof output === "string") {
|
|
230
|
+
return output;
|
|
231
|
+
}
|
|
232
|
+
if (output === undefined) {
|
|
233
|
+
return "";
|
|
234
|
+
}
|
|
235
|
+
return JSON.stringify(output);
|
|
236
|
+
}
|
|
237
|
+
async buildToolMessages(toolCalls = [], tools = []) {
|
|
238
|
+
const toolMessages = [];
|
|
239
|
+
for (const toolCall of toolCalls) {
|
|
240
|
+
const toolName = toolCall.function?.name;
|
|
241
|
+
const tool = tools.find(t => t.name === toolName);
|
|
242
|
+
if (!tool) {
|
|
243
|
+
throw new Error(`Tool not found: ${toolName}`);
|
|
244
|
+
}
|
|
245
|
+
const args = this.parseToolArguments(toolCall.function?.arguments);
|
|
246
|
+
const output = await tool.execute(args);
|
|
247
|
+
toolMessages.push({
|
|
248
|
+
role: "tool",
|
|
249
|
+
name: toolName,
|
|
250
|
+
content: this.serializeToolOutput(output),
|
|
251
|
+
});
|
|
252
|
+
}
|
|
253
|
+
return toolMessages;
|
|
254
|
+
}
|
|
255
|
+
async prompt(input) {
|
|
256
|
+
if (!input.systemPrompt) {
|
|
257
|
+
throw new Error("systemPrompt required");
|
|
258
|
+
}
|
|
259
|
+
let systemPrompt = input.systemPrompt;
|
|
260
|
+
if (input.memory && input.memory.length > 0) {
|
|
261
|
+
systemPrompt += `\n\n ${input.memoryHeader ?? '[MEMORIA]'}\n ${input.memory.map(m => `${m.key}: ${m.value}`).join('\n')}`;
|
|
262
|
+
}
|
|
263
|
+
if (input.knowledgeBase && input.knowledgeBase.length > 0) {
|
|
264
|
+
systemPrompt += `\n\n${input.knowledgeBaseHeader ?? '[BASE DE CONOCIMIENTO]'}\n ${input.knowledgeBase.join('\n')}`;
|
|
265
|
+
}
|
|
266
|
+
const model = input.model ?? (this.hasImageInput(input) ? this.visionModel ?? this.model : this.model);
|
|
267
|
+
const startedAt = new Date();
|
|
268
|
+
const startTime = performance.now();
|
|
269
|
+
let tokens = 0;
|
|
270
|
+
let inputTokens = 0;
|
|
271
|
+
let outputTokens = 0;
|
|
272
|
+
try {
|
|
273
|
+
const messages = [
|
|
274
|
+
{ role: 'system', content: systemPrompt },
|
|
275
|
+
...await this.mapHistory(input.history),
|
|
276
|
+
await this.buildUserMessage(input),
|
|
277
|
+
];
|
|
278
|
+
const tools = input.tools ?? [];
|
|
279
|
+
const maxIterations = input.toolMaxIterations ?? 5;
|
|
280
|
+
const responseFormat = this.normalizeResponseFormat(input);
|
|
281
|
+
let output;
|
|
282
|
+
for (let iteration = 0; iteration < maxIterations; iteration++) {
|
|
283
|
+
const response = await this.post("/api/chat", {
|
|
284
|
+
model,
|
|
285
|
+
messages,
|
|
286
|
+
stream: false,
|
|
287
|
+
...(responseFormat ? { format: responseFormat } : {}),
|
|
288
|
+
...(tools.length > 0 ? { tools: this.mapTools(tools) } : {}),
|
|
289
|
+
});
|
|
290
|
+
inputTokens += response.prompt_eval_count ?? 0;
|
|
291
|
+
outputTokens += response.eval_count ?? 0;
|
|
292
|
+
tokens += (response.prompt_eval_count ?? 0) + (response.eval_count ?? 0);
|
|
293
|
+
const message = response.message ?? {};
|
|
294
|
+
const toolCalls = message.tool_calls ?? [];
|
|
295
|
+
if (toolCalls.length === 0) {
|
|
296
|
+
output = message.content ?? response.response ?? "";
|
|
297
|
+
break;
|
|
298
|
+
}
|
|
299
|
+
messages.push(message);
|
|
300
|
+
messages.push(...await this.buildToolMessages(toolCalls, tools));
|
|
301
|
+
}
|
|
302
|
+
if (output === undefined) {
|
|
303
|
+
throw new Error(`Tool max iterations reached: ${maxIterations}`);
|
|
304
|
+
}
|
|
305
|
+
const endTime = performance.now();
|
|
306
|
+
const time = endTime - startTime;
|
|
307
|
+
const endedAt = new Date();
|
|
308
|
+
await this.registerPromptLog(input, {
|
|
309
|
+
model,
|
|
310
|
+
systemPrompt,
|
|
311
|
+
startedAt,
|
|
312
|
+
endedAt,
|
|
313
|
+
inputTokens,
|
|
314
|
+
outputTokens,
|
|
315
|
+
tokens,
|
|
316
|
+
output,
|
|
317
|
+
success: true,
|
|
318
|
+
});
|
|
319
|
+
return {
|
|
320
|
+
output,
|
|
321
|
+
tokens,
|
|
322
|
+
inputTokens,
|
|
323
|
+
outputTokens,
|
|
324
|
+
time
|
|
325
|
+
};
|
|
326
|
+
}
|
|
327
|
+
catch (e) {
|
|
328
|
+
const endedAt = new Date();
|
|
329
|
+
await this.registerPromptLog(input, {
|
|
330
|
+
model,
|
|
331
|
+
systemPrompt,
|
|
332
|
+
startedAt,
|
|
333
|
+
endedAt,
|
|
334
|
+
success: false,
|
|
335
|
+
errorMessage: e?.message,
|
|
336
|
+
});
|
|
337
|
+
throw e;
|
|
338
|
+
}
|
|
339
|
+
}
|
|
340
|
+
}
|
|
341
|
+
export default OllamaAiProvider;
|
|
342
|
+
export { OllamaAiProvider };
|
|
@@ -224,6 +224,15 @@ class BuilderTool {
|
|
|
224
224
|
if (typeof f?.unwrap === "function" && typeName === "ZodNullable") {
|
|
225
225
|
return this.fieldAdapter(f.unwrap()).nullable();
|
|
226
226
|
}
|
|
227
|
+
if (typeof f?.unwrap === "function" && typeName === "ZodDefault") {
|
|
228
|
+
return this.fieldAdapter(f.unwrap()).default(f.def.defaultValue);
|
|
229
|
+
}
|
|
230
|
+
if (typeof f?.unwrap === "function" && typeName === "ZodCatch") {
|
|
231
|
+
return this.fieldAdapter(f.unwrap()).catch(f.def.catchValue);
|
|
232
|
+
}
|
|
233
|
+
if (typeof f?.unwrap === "function" && typeName === "ZodReadonly") {
|
|
234
|
+
return this.fieldAdapter(f.unwrap()).readonly();
|
|
235
|
+
}
|
|
227
236
|
if (typeName === "ZodArray" && f?.element) {
|
|
228
237
|
return z.array(this.fieldAdapter(f.element));
|
|
229
238
|
}
|
package/package.json
CHANGED
|
@@ -3,7 +3,7 @@
|
|
|
3
3
|
"publishConfig": {
|
|
4
4
|
"access": "public"
|
|
5
5
|
},
|
|
6
|
-
"version": "3.
|
|
6
|
+
"version": "3.35.1",
|
|
7
7
|
"description": "Ai utils",
|
|
8
8
|
"main": "dist/index.js",
|
|
9
9
|
"types": "types/index.d.ts",
|
|
@@ -24,6 +24,7 @@
|
|
|
24
24
|
"mongoose-paginate-v2": "^1.8.3"
|
|
25
25
|
},
|
|
26
26
|
"peerDependencies": {
|
|
27
|
+
"@google/genai": "^1.52.0",
|
|
27
28
|
"jsdom": "^26.0.0",
|
|
28
29
|
"office-text-extractor": "^3.0.3",
|
|
29
30
|
"openai": "^6.0.0",
|
|
@@ -35,6 +36,7 @@
|
|
|
35
36
|
}
|
|
36
37
|
},
|
|
37
38
|
"devDependencies": {
|
|
39
|
+
"@google/genai": "^1.52.0",
|
|
38
40
|
"@types/node": "^25.2.3",
|
|
39
41
|
"jsdom": "^26.0.0",
|
|
40
42
|
"office-text-extractor": "^3.0.3",
|
|
@@ -44,5 +46,5 @@
|
|
|
44
46
|
"typescript": "^5.9.3",
|
|
45
47
|
"vitest": "^3.0.8"
|
|
46
48
|
},
|
|
47
|
-
"gitHead": "
|
|
49
|
+
"gitHead": "5c5d65ae83b6da98c044cc868150e5e1780e18ca"
|
|
48
50
|
}
|
|
@@ -0,0 +1,14 @@
|
|
|
1
|
+
|
|
2
|
+
enum OllamaAiConfig {
|
|
3
|
+
|
|
4
|
+
OllamaAiBaseUrl = "OLLAMA_AI_BASE_URL",
|
|
5
|
+
OllamaAiModel = "OLLAMA_AI_MODEL",
|
|
6
|
+
OllamaAiVisionModel = "OLLAMA_AI_VISION_MODEL",
|
|
7
|
+
OllamaAiEmbeddingModel = "OLLAMA_AI_EMBEDDING_MODEL",
|
|
8
|
+
|
|
9
|
+
}
|
|
10
|
+
|
|
11
|
+
|
|
12
|
+
export default OllamaAiConfig;
|
|
13
|
+
|
|
14
|
+
export {OllamaAiConfig}
|
|
@@ -1,20 +1,28 @@
|
|
|
1
1
|
import type {IAIProvider} from "../interfaces/IAIProvider"
|
|
2
2
|
import OpenAiProviderFactory from "./OpenAiProviderFactory.js";
|
|
3
|
+
import GoogleAiProviderFactory from "./GoogleAiProviderFactory.js";
|
|
4
|
+
import OllamaAiProviderFactory from "./OllamaAiProviderFactory.js";
|
|
3
5
|
|
|
4
6
|
class AiProviderFactory {
|
|
5
|
-
private static
|
|
7
|
+
private static singletons: Record<string, IAIProvider> = {};
|
|
6
8
|
|
|
7
9
|
public static instance(provider: string = 'OpenAi'): IAIProvider {
|
|
8
|
-
if (!AiProviderFactory.
|
|
10
|
+
if (!AiProviderFactory.singletons[provider]) {
|
|
9
11
|
switch (provider) {
|
|
10
12
|
case 'OpenAi':
|
|
11
|
-
AiProviderFactory.
|
|
13
|
+
AiProviderFactory.singletons[provider] = OpenAiProviderFactory.instance()
|
|
14
|
+
break;
|
|
15
|
+
case 'GoogleAi':
|
|
16
|
+
AiProviderFactory.singletons[provider] = GoogleAiProviderFactory.instance()
|
|
17
|
+
break;
|
|
18
|
+
case 'OllamaAi':
|
|
19
|
+
AiProviderFactory.singletons[provider] = OllamaAiProviderFactory.instance()
|
|
12
20
|
break;
|
|
13
21
|
default:
|
|
14
22
|
throw new Error(`Unsupported AI provider: ${provider}`);
|
|
15
23
|
}
|
|
16
24
|
}
|
|
17
|
-
return AiProviderFactory.
|
|
25
|
+
return AiProviderFactory.singletons[provider];
|
|
18
26
|
}
|
|
19
27
|
}
|
|
20
28
|
|
|
@@ -22,4 +30,3 @@ export default AiProviderFactory
|
|
|
22
30
|
export {
|
|
23
31
|
AiProviderFactory
|
|
24
32
|
}
|
|
25
|
-
|
|
@@ -0,0 +1,26 @@
|
|
|
1
|
+
import {DraxConfig} from "@drax/common-back";
|
|
2
|
+
import GoogleAiConfig from "../config/GoogleAiConfig.js";
|
|
3
|
+
import type {IAIProvider} from "../interfaces/IAIProvider"
|
|
4
|
+
import GoogleAiProvider from "../providers/GoogleAiProvider.js";
|
|
5
|
+
import AILogServiceFactory from "./services/AILogServiceFactory.js";
|
|
6
|
+
|
|
7
|
+
class GoogleAiProviderFactory {
|
|
8
|
+
private static singleton: IAIProvider;
|
|
9
|
+
|
|
10
|
+
public static instance(): IAIProvider {
|
|
11
|
+
if (!GoogleAiProviderFactory.singleton) {
|
|
12
|
+
GoogleAiProviderFactory.singleton = new GoogleAiProvider(
|
|
13
|
+
DraxConfig.getOrLoad(GoogleAiConfig.GoogleAiApiKey),
|
|
14
|
+
DraxConfig.getOrLoad(GoogleAiConfig.GoogleAiModel),
|
|
15
|
+
DraxConfig.getOrLoad(GoogleAiConfig.GoogleAiVisionModel),
|
|
16
|
+
AILogServiceFactory.instance
|
|
17
|
+
);
|
|
18
|
+
}
|
|
19
|
+
return GoogleAiProviderFactory.singleton;
|
|
20
|
+
}
|
|
21
|
+
}
|
|
22
|
+
|
|
23
|
+
export default GoogleAiProviderFactory
|
|
24
|
+
export {
|
|
25
|
+
GoogleAiProviderFactory
|
|
26
|
+
}
|
|
@@ -0,0 +1,27 @@
|
|
|
1
|
+
import {DraxConfig} from "@drax/common-back";
|
|
2
|
+
import OllamaAiConfig from "../config/OllamaAiConfig.js";
|
|
3
|
+
import type {IAIProvider} from "../interfaces/IAIProvider"
|
|
4
|
+
import OllamaAiProvider from "../providers/OllamaAiProvider.js";
|
|
5
|
+
import AILogServiceFactory from "./services/AILogServiceFactory.js";
|
|
6
|
+
|
|
7
|
+
class OllamaAiProviderFactory {
|
|
8
|
+
private static singleton: IAIProvider;
|
|
9
|
+
|
|
10
|
+
public static instance(): IAIProvider {
|
|
11
|
+
if (!OllamaAiProviderFactory.singleton) {
|
|
12
|
+
OllamaAiProviderFactory.singleton = new OllamaAiProvider(
|
|
13
|
+
DraxConfig.getOrLoad(OllamaAiConfig.OllamaAiBaseUrl, "string", "http://localhost:11434"),
|
|
14
|
+
DraxConfig.getOrLoad(OllamaAiConfig.OllamaAiModel),
|
|
15
|
+
DraxConfig.getOrLoad(OllamaAiConfig.OllamaAiVisionModel),
|
|
16
|
+
DraxConfig.getOrLoad(OllamaAiConfig.OllamaAiEmbeddingModel),
|
|
17
|
+
AILogServiceFactory.instance
|
|
18
|
+
);
|
|
19
|
+
}
|
|
20
|
+
return OllamaAiProviderFactory.singleton;
|
|
21
|
+
}
|
|
22
|
+
}
|
|
23
|
+
|
|
24
|
+
export default OllamaAiProviderFactory
|
|
25
|
+
export {
|
|
26
|
+
OllamaAiProviderFactory
|
|
27
|
+
}
|
package/src/index.ts
CHANGED
|
@@ -1,12 +1,18 @@
|
|
|
1
1
|
import {OpenAiConfig} from "./config/OpenAiConfig.js";
|
|
2
|
+
import {GoogleAiConfig} from "./config/GoogleAiConfig.js";
|
|
3
|
+
import {OllamaAiConfig} from "./config/OllamaAiConfig.js";
|
|
2
4
|
import {AILogSchema, AILogBaseSchema} from "./schemas/AILogSchema.js";
|
|
3
5
|
import AILogModel from "./models/AILogModel.js";
|
|
4
6
|
import AILogMongoRepository from "./repository/mongo/AILogMongoRepository.js";
|
|
5
7
|
import AILogSqliteRepository from "./repository/sqlite/AILogSqliteRepository.js";
|
|
6
8
|
import {OpenAiProviderFactory} from "./factory/OpenAiProviderFactory.js";
|
|
9
|
+
import {GoogleAiProviderFactory} from "./factory/GoogleAiProviderFactory.js";
|
|
10
|
+
import {OllamaAiProviderFactory} from "./factory/OllamaAiProviderFactory.js";
|
|
7
11
|
import {AiProviderFactory} from "./factory/AiProviderFactory.js";
|
|
8
12
|
import AILogServiceFactory from "./factory/services/AILogServiceFactory.js";
|
|
9
13
|
import {OpenAiProvider} from "./providers/OpenAiProvider.js";
|
|
14
|
+
import {GoogleAiProvider} from "./providers/GoogleAiProvider.js";
|
|
15
|
+
import {OllamaAiProvider} from "./providers/OllamaAiProvider.js";
|
|
10
16
|
import {BuilderTool} from "./tools/BuilderTool.js";
|
|
11
17
|
import {KnowledgeService} from "./services/KnowledgeService.js";
|
|
12
18
|
import {AILogService} from "./services/AILogService.js";
|
|
@@ -95,15 +101,21 @@ export type {
|
|
|
95
101
|
|
|
96
102
|
export {
|
|
97
103
|
OpenAiConfig,
|
|
104
|
+
GoogleAiConfig,
|
|
105
|
+
OllamaAiConfig,
|
|
98
106
|
AILogSchema,
|
|
99
107
|
AILogBaseSchema,
|
|
100
108
|
AILogModel,
|
|
101
109
|
AILogMongoRepository,
|
|
102
110
|
AILogSqliteRepository,
|
|
103
111
|
OpenAiProviderFactory,
|
|
112
|
+
GoogleAiProviderFactory,
|
|
113
|
+
OllamaAiProviderFactory,
|
|
104
114
|
AiProviderFactory,
|
|
105
115
|
AILogServiceFactory,
|
|
106
116
|
OpenAiProvider,
|
|
117
|
+
GoogleAiProvider,
|
|
118
|
+
OllamaAiProvider,
|
|
107
119
|
BuilderTool,
|
|
108
120
|
//Service
|
|
109
121
|
KnowledgeService,
|