goblin-agents 2.2.3 → 2.3.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/lib/llm/aiAgent.js +39 -9
- package/lib/llm/utils.js +2 -2
- package/package.json +1 -1
package/lib/llm/aiAgent.js
CHANGED
|
@@ -134,10 +134,7 @@ function cleanText(text) {
|
|
|
134
134
|
if (typeof text !== 'string') {
|
|
135
135
|
return text;
|
|
136
136
|
}
|
|
137
|
-
const {
|
|
138
|
-
for (const think of thinks) {
|
|
139
|
-
this.log.dbg(`🧐 ${think}`);
|
|
140
|
-
}
|
|
137
|
+
const {cleanText} = splitThinkTags(text);
|
|
141
138
|
return cleanText;
|
|
142
139
|
}
|
|
143
140
|
|
|
@@ -183,6 +180,8 @@ const defaultValues = {
|
|
|
183
180
|
|
|
184
181
|
class AiAgentLogic extends Elf.Archetype {
|
|
185
182
|
static db = 'agents';
|
|
183
|
+
static indices = ['role'];
|
|
184
|
+
|
|
186
185
|
state = new AiAgentState({
|
|
187
186
|
id: undefined,
|
|
188
187
|
version: 1,
|
|
@@ -433,13 +432,13 @@ class AiAgent extends Elf {
|
|
|
433
432
|
}
|
|
434
433
|
}
|
|
435
434
|
|
|
436
|
-
async readPDFpages(pdfPath) {
|
|
435
|
+
async readPDFpages(pdfPath, pages) {
|
|
437
436
|
await this._mutex.lock(this.id);
|
|
438
437
|
this.quest.defer(() => this._mutex.unlock(this.id));
|
|
439
438
|
|
|
440
439
|
await this._loadProvider();
|
|
441
440
|
|
|
442
|
-
const images = await getPDFImages(pdfPath);
|
|
441
|
+
const images = await getPDFImages(pdfPath, pages);
|
|
443
442
|
const descriptions = [];
|
|
444
443
|
for (const image of images) {
|
|
445
444
|
const messages = [
|
|
@@ -448,7 +447,7 @@ class AiAgent extends Elf {
|
|
|
448
447
|
content: [
|
|
449
448
|
{
|
|
450
449
|
type: 'text',
|
|
451
|
-
text: OCR_SYSTEM_PROMPT,
|
|
450
|
+
text: this.state.prompt || OCR_SYSTEM_PROMPT,
|
|
452
451
|
},
|
|
453
452
|
{
|
|
454
453
|
type: 'image_url',
|
|
@@ -469,6 +468,37 @@ class AiAgent extends Elf {
|
|
|
469
468
|
return descriptions;
|
|
470
469
|
}
|
|
471
470
|
|
|
471
|
+
async vision(context, image) {
|
|
472
|
+
await this._mutex.lock(this.id);
|
|
473
|
+
this.quest.defer(() => this._mutex.unlock(this.id));
|
|
474
|
+
|
|
475
|
+
await this._loadProvider();
|
|
476
|
+
const messages = [
|
|
477
|
+
{role: 'system', content: this.state.prompt},
|
|
478
|
+
{
|
|
479
|
+
role: 'user',
|
|
480
|
+
content: [
|
|
481
|
+
{
|
|
482
|
+
type: 'image_url',
|
|
483
|
+
image_url: {
|
|
484
|
+
url: image,
|
|
485
|
+
},
|
|
486
|
+
},
|
|
487
|
+
{
|
|
488
|
+
type: 'text',
|
|
489
|
+
text: context,
|
|
490
|
+
},
|
|
491
|
+
],
|
|
492
|
+
},
|
|
493
|
+
];
|
|
494
|
+
const result = await this._provider.chat({
|
|
495
|
+
model: this.state.model,
|
|
496
|
+
reasoning: this.state.reasoning,
|
|
497
|
+
messages,
|
|
498
|
+
});
|
|
499
|
+
return result.message.content;
|
|
500
|
+
}
|
|
501
|
+
|
|
472
502
|
async embed(rawText) {
|
|
473
503
|
if (!rawText) {
|
|
474
504
|
return null;
|
|
@@ -522,7 +552,7 @@ class AiAgent extends Elf {
|
|
|
522
552
|
await this._loadProvider();
|
|
523
553
|
const contextMessages = this.state.messages[contextId] || [];
|
|
524
554
|
const messages = [
|
|
525
|
-
{role: 'system', content: this.state.prompt},
|
|
555
|
+
{role: 'system', content: this.state.prompt || ''},
|
|
526
556
|
...Array.from(contextMessages).map((m) => m._state.toJS()), //history
|
|
527
557
|
];
|
|
528
558
|
|
|
@@ -579,7 +609,7 @@ class AiAgent extends Elf {
|
|
|
579
609
|
const res = cleanText(result.message.content);
|
|
580
610
|
if (typeof res === 'string') {
|
|
581
611
|
//keep agent response
|
|
582
|
-
this.logic.chat(contextId, 'assistant',
|
|
612
|
+
this.logic.chat(contextId, 'assistant', res);
|
|
583
613
|
}
|
|
584
614
|
|
|
585
615
|
this.log.dbg(`🐈 Chat session ended : ${contextId}`);
|
package/lib/llm/utils.js
CHANGED
|
@@ -24,7 +24,7 @@ Ignore les artefacts visuels inutiles.
|
|
|
24
24
|
Corrige les erreurs évidentes et restitue un texte propre et fidèle à l'original.
|
|
25
25
|
Ta réponse contient uniquement le texte lisible.`;
|
|
26
26
|
|
|
27
|
-
const getPDFImages = async (pdfPath) => {
|
|
27
|
+
const getPDFImages = async (pdfPath, pagesToProcess) => {
|
|
28
28
|
const {pdfToPng} = loadPdfToPngConverter();
|
|
29
29
|
// The function accepts PDF file path or a Buffer
|
|
30
30
|
const pages = await pdfToPng(pdfPath, {
|
|
@@ -35,7 +35,7 @@ const getPDFImages = async (pdfPath) => {
|
|
|
35
35
|
//outputFolder: 'output/folder', // Folder to write output PNG files. If not specified, PNG output will be available only as a Buffer content, without saving to a file.
|
|
36
36
|
//outputFileMaskFunc: (pageNumber) => `page_${pageNumber}.png`, // Output filename mask function. Example: (pageNumber) => `page_${pageNumber}.png`
|
|
37
37
|
//pdfFilePassword: password, // Password for encrypted PDF.
|
|
38
|
-
|
|
38
|
+
pagesToProcess, // Subset of pages to convert (first page = 1), other pages will be skipped if specified.
|
|
39
39
|
//strictPagesToProcess: false, // When `true`, will throw an error if specified page number in pagesToProcess is invalid, otherwise will skip invalid page. Default value is false.
|
|
40
40
|
verbosityLevel: 0, // Verbosity level. ERRORS: 0, WARNINGS: 1, INFOS: 5. Default value is 0.
|
|
41
41
|
});
|