goblin-agents 0.1.1 → 0.1.2
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/lib/llm/aiAgent.js +36 -26
- package/package.json +1 -1
package/lib/llm/aiAgent.js
CHANGED
|
@@ -238,6 +238,9 @@ class AiAgent extends Elf {
|
|
|
238
238
|
logic = Elf.getLogic(AiAgentLogic);
|
|
239
239
|
state = new AiAgentState();
|
|
240
240
|
|
|
241
|
+
_provider;
|
|
242
|
+
_conf;
|
|
243
|
+
|
|
241
244
|
/**
|
|
242
245
|
* @param {string} id
|
|
243
246
|
* @param {string} desktopId
|
|
@@ -270,6 +273,23 @@ class AiAgent extends Elf {
|
|
|
270
273
|
this.logic.patch(prev);
|
|
271
274
|
}
|
|
272
275
|
|
|
276
|
+
async _loadProvider() {
|
|
277
|
+
const {state} = this;
|
|
278
|
+
const {provider, host, headers} = state;
|
|
279
|
+
if (
|
|
280
|
+
this._provider &&
|
|
281
|
+
this._conf.provider === provider &&
|
|
282
|
+
this._conf.host === host &&
|
|
283
|
+
this._conf.headers === headers
|
|
284
|
+
) {
|
|
285
|
+
return;
|
|
286
|
+
}
|
|
287
|
+
|
|
288
|
+
const Instance = LLMProvider.get(this.state.provider);
|
|
289
|
+
this._provider = new Instance(this.state.host, this.state.headers);
|
|
290
|
+
this._conf = {provider, host, headers};
|
|
291
|
+
}
|
|
292
|
+
|
|
273
293
|
/**
|
|
274
294
|
* @param {Partial<AiAgentState>} [agentState]
|
|
275
295
|
*/
|
|
@@ -292,14 +312,13 @@ class AiAgent extends Elf {
|
|
|
292
312
|
}
|
|
293
313
|
|
|
294
314
|
async gen(prompt) {
|
|
295
|
-
|
|
296
|
-
const provider = new Instance(this.state.host, this.state.headers);
|
|
315
|
+
await this._loadProvider();
|
|
297
316
|
|
|
298
317
|
let options;
|
|
299
318
|
if (this.state.options) {
|
|
300
319
|
options = this.state.options.toJS();
|
|
301
320
|
}
|
|
302
|
-
return await
|
|
321
|
+
return await this._provider.gen({
|
|
303
322
|
model: this.state.model,
|
|
304
323
|
prompt,
|
|
305
324
|
system: this.state.prompt,
|
|
@@ -323,8 +342,7 @@ class AiAgent extends Elf {
|
|
|
323
342
|
}
|
|
324
343
|
|
|
325
344
|
async readPDFpages(pdfPath) {
|
|
326
|
-
|
|
327
|
-
const provider = new Instance(this.state.host, this.state.headers);
|
|
345
|
+
await this._loadProvider();
|
|
328
346
|
|
|
329
347
|
const images = await getPDFImages(pdfPath);
|
|
330
348
|
const descriptions = [];
|
|
@@ -346,7 +364,7 @@ class AiAgent extends Elf {
|
|
|
346
364
|
],
|
|
347
365
|
},
|
|
348
366
|
];
|
|
349
|
-
const result = await
|
|
367
|
+
const result = await this._provider.chat({
|
|
350
368
|
model: this.state.model,
|
|
351
369
|
messages,
|
|
352
370
|
});
|
|
@@ -359,9 +377,8 @@ class AiAgent extends Elf {
|
|
|
359
377
|
if (!rawText) {
|
|
360
378
|
return null;
|
|
361
379
|
}
|
|
362
|
-
|
|
363
|
-
|
|
364
|
-
return await provider.embed({
|
|
380
|
+
await this._loadProvider();
|
|
381
|
+
return await this._provider.embed({
|
|
365
382
|
model: this.state.model,
|
|
366
383
|
input: rawText,
|
|
367
384
|
});
|
|
@@ -371,9 +388,8 @@ class AiAgent extends Elf {
|
|
|
371
388
|
if (rawTexts.length === 0) {
|
|
372
389
|
return [];
|
|
373
390
|
}
|
|
374
|
-
|
|
375
|
-
|
|
376
|
-
return await provider.embedInBatch({
|
|
391
|
+
await this._loadProvider();
|
|
392
|
+
return await this._provider.embedInBatch({
|
|
377
393
|
model: this.state.model,
|
|
378
394
|
input: rawTexts,
|
|
379
395
|
});
|
|
@@ -384,8 +400,7 @@ class AiAgent extends Elf {
|
|
|
384
400
|
}
|
|
385
401
|
|
|
386
402
|
async chat(contextId, question, userDesktopId, sessionId = null) {
|
|
387
|
-
|
|
388
|
-
const provider = new Instance(this.state.host, this.state.headers);
|
|
403
|
+
await this._loadProvider();
|
|
389
404
|
const contextMessages = this.state.messages[contextId] || [];
|
|
390
405
|
const messages = [
|
|
391
406
|
{role: 'system', content: this.state.prompt},
|
|
@@ -406,7 +421,7 @@ class AiAgent extends Elf {
|
|
|
406
421
|
this.log.dbg(`🐈 Chat session started : ${sessionId}`);
|
|
407
422
|
}
|
|
408
423
|
|
|
409
|
-
const result = await
|
|
424
|
+
const result = await this._provider.chat({
|
|
410
425
|
model: this.state.model,
|
|
411
426
|
messages,
|
|
412
427
|
options,
|
|
@@ -474,9 +489,7 @@ class AiAgent extends Elf {
|
|
|
474
489
|
if (this.state.provider !== 'ollama') {
|
|
475
490
|
throw new Error('Streaming API not impl.');
|
|
476
491
|
}
|
|
477
|
-
|
|
478
|
-
const provider = new Instance(this.state.host, this.state.headers);
|
|
479
|
-
const feedId = await this.newQuestFeed();
|
|
492
|
+
await this._loadProvider();
|
|
480
493
|
|
|
481
494
|
const contextMessages = this.state.messages[contextId] || [];
|
|
482
495
|
const messages = [
|
|
@@ -491,7 +504,7 @@ class AiAgent extends Elf {
|
|
|
491
504
|
const pState = this.state._state.toJS();
|
|
492
505
|
options = pState?.options;
|
|
493
506
|
}
|
|
494
|
-
const response = await
|
|
507
|
+
const response = await this._provider.chat({
|
|
495
508
|
model: this.state.model,
|
|
496
509
|
messages,
|
|
497
510
|
options,
|
|
@@ -509,9 +522,7 @@ class AiAgent extends Elf {
|
|
|
509
522
|
}
|
|
510
523
|
|
|
511
524
|
async resumeExchange(resumePrompt, contextId) {
|
|
512
|
-
|
|
513
|
-
const provider = new Instance(this.state.host, this.state.headers);
|
|
514
|
-
const feedId = await this.newQuestFeed();
|
|
525
|
+
await this._loadProvider();
|
|
515
526
|
const contextMessages = this.state.messages[contextId] || [];
|
|
516
527
|
|
|
517
528
|
let options;
|
|
@@ -524,7 +535,7 @@ class AiAgent extends Elf {
|
|
|
524
535
|
.map((m) => `${m.role}: ${m.content}`)
|
|
525
536
|
.join('\n');
|
|
526
537
|
|
|
527
|
-
return await
|
|
538
|
+
return await this._provider.gen({
|
|
528
539
|
model: this.state.model,
|
|
529
540
|
prompt,
|
|
530
541
|
system: resumePrompt,
|
|
@@ -534,8 +545,7 @@ class AiAgent extends Elf {
|
|
|
534
545
|
}
|
|
535
546
|
|
|
536
547
|
async react(reactPrompt, contextId, question) {
|
|
537
|
-
|
|
538
|
-
const provider = new Instance(this.state.host, this.state.headers);
|
|
548
|
+
await this._loadProvider();
|
|
539
549
|
const feedId = await this.newQuestFeed();
|
|
540
550
|
const contextMessages = this.state.messages[contextId] || [];
|
|
541
551
|
const messages = [
|
|
@@ -550,7 +560,7 @@ class AiAgent extends Elf {
|
|
|
550
560
|
options = this.state.options.toJS();
|
|
551
561
|
}
|
|
552
562
|
|
|
553
|
-
const answer = await
|
|
563
|
+
const answer = await this._provider.chat({
|
|
554
564
|
model: this.state.model,
|
|
555
565
|
messages,
|
|
556
566
|
options,
|