viho-llm 1.0.1 → 1.0.6
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/index.js +77 -0
- package/package.json +2 -2
- package/src/agents/agent-util.js +46 -0
- package/src/agents/agent.js +29 -0
- package/src/index.js +1 -0
package/index.js
CHANGED
|
@@ -514,6 +514,83 @@ const OpenAIAPI = (options) => {
|
|
|
514
514
|
return openai;
|
|
515
515
|
};
|
|
516
516
|
|
|
517
|
+
/**
|
|
518
|
+
* callLLM
|
|
519
|
+
* @param {*} options
|
|
520
|
+
* @returns
|
|
521
|
+
*/
|
|
522
|
+
const callLLM = async (options) => {
|
|
523
|
+
const response = await options.llm.chat({
|
|
524
|
+
model: options.modelName,
|
|
525
|
+
messages: options.messages,
|
|
526
|
+
});
|
|
527
|
+
const fullContent = response.content || '';
|
|
528
|
+
|
|
529
|
+
// r
|
|
530
|
+
return options.isJson ? extractJSON(fullContent) : fullContent;
|
|
531
|
+
};
|
|
532
|
+
|
|
533
|
+
/**
|
|
534
|
+
* extractJSON
|
|
535
|
+
* @param {*} text
|
|
536
|
+
* @returns
|
|
537
|
+
*/
|
|
538
|
+
const extractJSON = (text) => {
|
|
539
|
+
const cleaned = text.replace(/\{\{/g, '{').replace(/\}\}/g, '}');
|
|
540
|
+
try {
|
|
541
|
+
return JSON.parse(cleaned);
|
|
542
|
+
} catch (e) {
|
|
543
|
+
// try next
|
|
544
|
+
}
|
|
545
|
+
const codeBlock = cleaned.match(/```(?:json)?\s*([\s\S]*?)```/);
|
|
546
|
+
if (codeBlock) {
|
|
547
|
+
try {
|
|
548
|
+
return JSON.parse(codeBlock[1].trim());
|
|
549
|
+
} catch (e) {
|
|
550
|
+
// try next
|
|
551
|
+
}
|
|
552
|
+
}
|
|
553
|
+
const match = cleaned.match(/(\{[\s\S]*\}|\[[\s\S]*\])/);
|
|
554
|
+
if (match) {
|
|
555
|
+
try {
|
|
556
|
+
return JSON.parse(match[1]);
|
|
557
|
+
} catch (e) {
|
|
558
|
+
// try next
|
|
559
|
+
}
|
|
560
|
+
}
|
|
561
|
+
throw new Error(`Cannot extract JSON from: ${text.slice(0, 200)}`);
|
|
562
|
+
};
|
|
563
|
+
|
|
564
|
+
// util
|
|
565
|
+
/**
|
|
566
|
+
* runAgents
|
|
567
|
+
* @param {*} agents
|
|
568
|
+
*/
|
|
569
|
+
const runAgents = async (agents) => {
|
|
570
|
+
// for
|
|
571
|
+
for (let i = 0; i < agents.length; i++) {
|
|
572
|
+
const agent = agents[i];
|
|
573
|
+
|
|
574
|
+
// const
|
|
575
|
+
const agentStartCallback = agent.agentStartCallback;
|
|
576
|
+
const agentRequestOptions = agent.agentRequestOptions;
|
|
577
|
+
const agentEndCallback = agent.agentEndCallback;
|
|
578
|
+
const agentBreakCallback = agent.agentBreakCallback;
|
|
579
|
+
|
|
580
|
+
// go
|
|
581
|
+
agentStartCallback();
|
|
582
|
+
const agentResponse = await callLLM(agentRequestOptions);
|
|
583
|
+
|
|
584
|
+
// check
|
|
585
|
+
const breakAgent = agentEndCallback(agentResponse);
|
|
586
|
+
if (breakAgent) {
|
|
587
|
+
if (agentBreakCallback) agentBreakCallback();
|
|
588
|
+
break;
|
|
589
|
+
}
|
|
590
|
+
}
|
|
591
|
+
};
|
|
592
|
+
|
|
517
593
|
exports.GeminiAPI = GeminiAPI;
|
|
518
594
|
exports.GeminiVertex = GeminiVertex;
|
|
519
595
|
exports.OpenAIAPI = OpenAIAPI;
|
|
596
|
+
exports.runAgents = runAgents;
|
package/package.json
CHANGED
|
@@ -1,6 +1,6 @@
|
|
|
1
1
|
{
|
|
2
2
|
"name": "viho-llm",
|
|
3
|
-
"version": "1.0.
|
|
3
|
+
"version": "1.0.6",
|
|
4
4
|
"description": "Utility library for working with multiple LLM providers (Google Gemini and OpenAI), providing common tools and helpers for AI interactions",
|
|
5
5
|
"keywords": [
|
|
6
6
|
"llm",
|
|
@@ -68,5 +68,5 @@
|
|
|
68
68
|
}
|
|
69
69
|
}
|
|
70
70
|
},
|
|
71
|
-
"gitHead": "
|
|
71
|
+
"gitHead": "1c8edc1b39d4b2617ec3b6c6ed555e3c8a27a91d"
|
|
72
72
|
}
|
|
@@ -0,0 +1,46 @@
|
|
|
1
|
+
/**
|
|
2
|
+
* callLLM
|
|
3
|
+
* @param {*} options
|
|
4
|
+
* @returns
|
|
5
|
+
*/
|
|
6
|
+
export const callLLM = async (options) => {
|
|
7
|
+
const response = await options.llm.chat({
|
|
8
|
+
model: options.modelName,
|
|
9
|
+
messages: options.messages,
|
|
10
|
+
});
|
|
11
|
+
const fullContent = response.content || '';
|
|
12
|
+
|
|
13
|
+
// r
|
|
14
|
+
return options.isJson ? extractJSON(fullContent) : fullContent;
|
|
15
|
+
};
|
|
16
|
+
|
|
17
|
+
/**
|
|
18
|
+
* extractJSON
|
|
19
|
+
* @param {*} text
|
|
20
|
+
* @returns
|
|
21
|
+
*/
|
|
22
|
+
export const extractJSON = (text) => {
|
|
23
|
+
const cleaned = text.replace(/\{\{/g, '{').replace(/\}\}/g, '}');
|
|
24
|
+
try {
|
|
25
|
+
return JSON.parse(cleaned);
|
|
26
|
+
} catch (e) {
|
|
27
|
+
// try next
|
|
28
|
+
}
|
|
29
|
+
const codeBlock = cleaned.match(/```(?:json)?\s*([\s\S]*?)```/);
|
|
30
|
+
if (codeBlock) {
|
|
31
|
+
try {
|
|
32
|
+
return JSON.parse(codeBlock[1].trim());
|
|
33
|
+
} catch (e) {
|
|
34
|
+
// try next
|
|
35
|
+
}
|
|
36
|
+
}
|
|
37
|
+
const match = cleaned.match(/(\{[\s\S]*\}|\[[\s\S]*\])/);
|
|
38
|
+
if (match) {
|
|
39
|
+
try {
|
|
40
|
+
return JSON.parse(match[1]);
|
|
41
|
+
} catch (e) {
|
|
42
|
+
// try next
|
|
43
|
+
}
|
|
44
|
+
}
|
|
45
|
+
throw new Error(`Cannot extract JSON from: ${text.slice(0, 200)}`);
|
|
46
|
+
};
|
|
@@ -0,0 +1,29 @@
|
|
|
1
|
+
// util
|
|
2
|
+
import { callLLM } from './agent-util.js';
|
|
3
|
+
/**
|
|
4
|
+
* runAgents
|
|
5
|
+
* @param {*} agents
|
|
6
|
+
*/
|
|
7
|
+
export const runAgents = async (agents) => {
|
|
8
|
+
// for
|
|
9
|
+
for (let i = 0; i < agents.length; i++) {
|
|
10
|
+
const agent = agents[i];
|
|
11
|
+
|
|
12
|
+
// const
|
|
13
|
+
const agentStartCallback = agent.agentStartCallback;
|
|
14
|
+
const agentRequestOptions = agent.agentRequestOptions;
|
|
15
|
+
const agentEndCallback = agent.agentEndCallback;
|
|
16
|
+
const agentBreakCallback = agent.agentBreakCallback;
|
|
17
|
+
|
|
18
|
+
// go
|
|
19
|
+
agentStartCallback();
|
|
20
|
+
const agentResponse = await callLLM(agentRequestOptions);
|
|
21
|
+
|
|
22
|
+
// check
|
|
23
|
+
const breakAgent = agentEndCallback(agentResponse);
|
|
24
|
+
if (breakAgent) {
|
|
25
|
+
if (agentBreakCallback) agentBreakCallback();
|
|
26
|
+
break;
|
|
27
|
+
}
|
|
28
|
+
}
|
|
29
|
+
};
|
package/src/index.js
CHANGED