viho-llm 1.0.0 → 1.0.5
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 +70 -0
- package/package.json +5 -5
- package/src/agents/agent-util.js +46 -0
- package/src/agents/agent.js +22 -0
- package/src/index.js +1 -0
package/index.js
CHANGED
|
@@ -514,6 +514,76 @@ 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
|
+
|
|
579
|
+
// go
|
|
580
|
+
agentStartCallback();
|
|
581
|
+
const agentResponse = await callLLM(agentRequestOptions);
|
|
582
|
+
agentEndCallback(agentResponse);
|
|
583
|
+
}
|
|
584
|
+
};
|
|
585
|
+
|
|
517
586
|
exports.GeminiAPI = GeminiAPI;
|
|
518
587
|
exports.GeminiVertex = GeminiVertex;
|
|
519
588
|
exports.OpenAIAPI = OpenAIAPI;
|
|
589
|
+
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.5",
|
|
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",
|
|
@@ -46,9 +46,9 @@
|
|
|
46
46
|
"build": "qpro rollup ./rollup.config.js"
|
|
47
47
|
},
|
|
48
48
|
"dependencies": {
|
|
49
|
-
"@google/genai": "^1.
|
|
50
|
-
"mime-types": "^
|
|
51
|
-
"openai": "^
|
|
49
|
+
"@google/genai": "^1.43.0",
|
|
50
|
+
"mime-types": "^3.0.2",
|
|
51
|
+
"openai": "^6.25.0",
|
|
52
52
|
"qiao.log.js": "^3.7.5"
|
|
53
53
|
},
|
|
54
54
|
"nx": {
|
|
@@ -68,5 +68,5 @@
|
|
|
68
68
|
}
|
|
69
69
|
}
|
|
70
70
|
},
|
|
71
|
-
"gitHead": "
|
|
71
|
+
"gitHead": "7b7a9c1c3c14b967a21a13a72728eebb6a339335"
|
|
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,22 @@
|
|
|
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
|
+
|
|
17
|
+
// go
|
|
18
|
+
agentStartCallback();
|
|
19
|
+
const agentResponse = await callLLM(agentRequestOptions);
|
|
20
|
+
agentEndCallback(agentResponse);
|
|
21
|
+
}
|
|
22
|
+
};
|
package/src/index.js
CHANGED