viho-llm 0.1.3 → 0.1.4
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 +44 -16
- package/package.json +3 -2
- package/src/gemini.js +47 -17
package/index.js
CHANGED
|
@@ -1,6 +1,7 @@
|
|
|
1
1
|
'use strict';
|
|
2
2
|
|
|
3
3
|
var genai = require('@google/genai');
|
|
4
|
+
var mime = require('mime-types');
|
|
4
5
|
var qiao_log_js = require('qiao.log.js');
|
|
5
6
|
|
|
6
7
|
// gemini
|
|
@@ -16,15 +17,15 @@ const Gemini = (options) => {
|
|
|
16
17
|
|
|
17
18
|
// check
|
|
18
19
|
if (!options) {
|
|
19
|
-
logger.
|
|
20
|
+
logger.error(methodName, 'need options');
|
|
20
21
|
return;
|
|
21
22
|
}
|
|
22
23
|
if (!options.apiKey) {
|
|
23
|
-
logger.
|
|
24
|
+
logger.error(methodName, 'need options.apiKey');
|
|
24
25
|
return;
|
|
25
26
|
}
|
|
26
27
|
if (!options.modelName) {
|
|
27
|
-
logger.
|
|
28
|
+
logger.error(methodName, 'need options.modelName');
|
|
28
29
|
return;
|
|
29
30
|
}
|
|
30
31
|
|
|
@@ -44,8 +45,8 @@ const Gemini = (options) => {
|
|
|
44
45
|
};
|
|
45
46
|
|
|
46
47
|
// cache
|
|
47
|
-
gemini.cacheAdd = async (
|
|
48
|
-
return await cacheAdd(gemini.client, options.modelName,
|
|
48
|
+
gemini.cacheAdd = async (cacheOptions) => {
|
|
49
|
+
return await cacheAdd(gemini.client, options.modelName, cacheOptions);
|
|
49
50
|
};
|
|
50
51
|
|
|
51
52
|
// r
|
|
@@ -58,11 +59,11 @@ async function chat(client, modelName, chatOptions) {
|
|
|
58
59
|
|
|
59
60
|
// check
|
|
60
61
|
if (!chatOptions) {
|
|
61
|
-
logger.
|
|
62
|
+
logger.error(methodName, 'need chatOptions');
|
|
62
63
|
return;
|
|
63
64
|
}
|
|
64
65
|
if (!chatOptions.contents) {
|
|
65
|
-
logger.
|
|
66
|
+
logger.error(methodName, 'need chatOptions.contents');
|
|
66
67
|
return;
|
|
67
68
|
}
|
|
68
69
|
|
|
@@ -87,11 +88,11 @@ async function chatWithStreaming(client, modelName, chatOptions, callbackOptions
|
|
|
87
88
|
|
|
88
89
|
// check
|
|
89
90
|
if (!chatOptions) {
|
|
90
|
-
logger.
|
|
91
|
+
logger.error(methodName, 'need chatOptions');
|
|
91
92
|
return;
|
|
92
93
|
}
|
|
93
94
|
if (!chatOptions.contents) {
|
|
94
|
-
logger.
|
|
95
|
+
logger.error(methodName, 'need chatOptions.contents');
|
|
95
96
|
return;
|
|
96
97
|
}
|
|
97
98
|
|
|
@@ -132,25 +133,52 @@ async function chatWithStreaming(client, modelName, chatOptions, callbackOptions
|
|
|
132
133
|
}
|
|
133
134
|
|
|
134
135
|
// cache add
|
|
135
|
-
async function cacheAdd(client, modelName,
|
|
136
|
+
async function cacheAdd(client, modelName, cacheOptions) {
|
|
136
137
|
const methodName = 'Gemini - cacheAdd';
|
|
137
138
|
|
|
138
139
|
// check
|
|
139
|
-
if (!
|
|
140
|
-
logger.
|
|
140
|
+
if (!cacheOptions) {
|
|
141
|
+
logger.error(methodName, 'need cacheOptions');
|
|
141
142
|
return;
|
|
142
143
|
}
|
|
143
|
-
if (!
|
|
144
|
-
logger.
|
|
144
|
+
if (!cacheOptions.filePath) {
|
|
145
|
+
logger.error(methodName, 'need cacheOptions.filePath');
|
|
145
146
|
return;
|
|
146
147
|
}
|
|
148
|
+
if (!cacheOptions.systemPrompt) {
|
|
149
|
+
logger.error(methodName, 'need cacheOptions.systemPrompt');
|
|
150
|
+
return;
|
|
151
|
+
}
|
|
152
|
+
if (!cacheOptions.cacheName) {
|
|
153
|
+
logger.error(methodName, 'need cacheOptions.cacheName');
|
|
154
|
+
return;
|
|
155
|
+
}
|
|
156
|
+
if (!cacheOptions.cacheTTL) {
|
|
157
|
+
logger.error(methodName, 'need cacheOptions.cacheTTL');
|
|
158
|
+
return;
|
|
159
|
+
}
|
|
160
|
+
|
|
161
|
+
// const
|
|
162
|
+
const mimeType = mime.lookup(cacheOptions.filePath);
|
|
163
|
+
logger.info(methodName, 'cacheOptions', cacheOptions);
|
|
164
|
+
logger.info(methodName, 'mimeType', mimeType);
|
|
147
165
|
|
|
148
166
|
try {
|
|
167
|
+
// upload doc
|
|
168
|
+
const doc = await client.files.upload({
|
|
169
|
+
file: cacheOptions.filePath,
|
|
170
|
+
config: { mimeType: mimeType },
|
|
171
|
+
});
|
|
172
|
+
logger.info(methodName, 'doc.name', doc.name);
|
|
173
|
+
|
|
174
|
+
// cache add
|
|
149
175
|
const cache = await client.caches.create({
|
|
150
176
|
model: modelName,
|
|
151
177
|
config: {
|
|
152
|
-
|
|
153
|
-
|
|
178
|
+
contents: genai.createUserContent(genai.createPartFromUri(doc.uri, doc.mimeType)),
|
|
179
|
+
systemInstruction: cacheOptions.systemPrompt,
|
|
180
|
+
displayName: cacheOptions.cacheName,
|
|
181
|
+
ttl: cacheOptions.cacheTTL,
|
|
154
182
|
},
|
|
155
183
|
});
|
|
156
184
|
|
package/package.json
CHANGED
|
@@ -1,6 +1,6 @@
|
|
|
1
1
|
{
|
|
2
2
|
"name": "viho-llm",
|
|
3
|
-
"version": "0.1.
|
|
3
|
+
"version": "0.1.4",
|
|
4
4
|
"description": "Utility library for working with Google Gemini AI, providing common tools and helpers for AI interactions",
|
|
5
5
|
"keywords": [
|
|
6
6
|
"llm",
|
|
@@ -41,6 +41,7 @@
|
|
|
41
41
|
},
|
|
42
42
|
"dependencies": {
|
|
43
43
|
"@google/genai": "^1.34.0",
|
|
44
|
+
"mime-types": "^2.1.35",
|
|
44
45
|
"qiao.log.js": "^3.7.5"
|
|
45
46
|
},
|
|
46
47
|
"nx": {
|
|
@@ -60,5 +61,5 @@
|
|
|
60
61
|
}
|
|
61
62
|
}
|
|
62
63
|
},
|
|
63
|
-
"gitHead": "
|
|
64
|
+
"gitHead": "38e28266271d8b64e56c5f6d27deca78ce8cdb1b"
|
|
64
65
|
}
|
package/src/gemini.js
CHANGED
|
@@ -1,5 +1,8 @@
|
|
|
1
1
|
// gemini
|
|
2
|
-
import { GoogleGenAI, createUserContent } from '@google/genai';
|
|
2
|
+
import { GoogleGenAI, createUserContent, createPartFromUri } from '@google/genai';
|
|
3
|
+
|
|
4
|
+
// mime
|
|
5
|
+
import mime from 'mime-types';
|
|
3
6
|
|
|
4
7
|
// Logger
|
|
5
8
|
import { Logger } from 'qiao.log.js';
|
|
@@ -15,15 +18,15 @@ export const Gemini = (options) => {
|
|
|
15
18
|
|
|
16
19
|
// check
|
|
17
20
|
if (!options) {
|
|
18
|
-
logger.
|
|
21
|
+
logger.error(methodName, 'need options');
|
|
19
22
|
return;
|
|
20
23
|
}
|
|
21
24
|
if (!options.apiKey) {
|
|
22
|
-
logger.
|
|
25
|
+
logger.error(methodName, 'need options.apiKey');
|
|
23
26
|
return;
|
|
24
27
|
}
|
|
25
28
|
if (!options.modelName) {
|
|
26
|
-
logger.
|
|
29
|
+
logger.error(methodName, 'need options.modelName');
|
|
27
30
|
return;
|
|
28
31
|
}
|
|
29
32
|
|
|
@@ -43,8 +46,8 @@ export const Gemini = (options) => {
|
|
|
43
46
|
};
|
|
44
47
|
|
|
45
48
|
// cache
|
|
46
|
-
gemini.cacheAdd = async (
|
|
47
|
-
return await cacheAdd(gemini.client, options.modelName,
|
|
49
|
+
gemini.cacheAdd = async (cacheOptions) => {
|
|
50
|
+
return await cacheAdd(gemini.client, options.modelName, cacheOptions);
|
|
48
51
|
};
|
|
49
52
|
|
|
50
53
|
// r
|
|
@@ -57,11 +60,11 @@ async function chat(client, modelName, chatOptions) {
|
|
|
57
60
|
|
|
58
61
|
// check
|
|
59
62
|
if (!chatOptions) {
|
|
60
|
-
logger.
|
|
63
|
+
logger.error(methodName, 'need chatOptions');
|
|
61
64
|
return;
|
|
62
65
|
}
|
|
63
66
|
if (!chatOptions.contents) {
|
|
64
|
-
logger.
|
|
67
|
+
logger.error(methodName, 'need chatOptions.contents');
|
|
65
68
|
return;
|
|
66
69
|
}
|
|
67
70
|
|
|
@@ -86,11 +89,11 @@ async function chatWithStreaming(client, modelName, chatOptions, callbackOptions
|
|
|
86
89
|
|
|
87
90
|
// check
|
|
88
91
|
if (!chatOptions) {
|
|
89
|
-
logger.
|
|
92
|
+
logger.error(methodName, 'need chatOptions');
|
|
90
93
|
return;
|
|
91
94
|
}
|
|
92
95
|
if (!chatOptions.contents) {
|
|
93
|
-
logger.
|
|
96
|
+
logger.error(methodName, 'need chatOptions.contents');
|
|
94
97
|
return;
|
|
95
98
|
}
|
|
96
99
|
|
|
@@ -131,25 +134,52 @@ async function chatWithStreaming(client, modelName, chatOptions, callbackOptions
|
|
|
131
134
|
}
|
|
132
135
|
|
|
133
136
|
// cache add
|
|
134
|
-
async function cacheAdd(client, modelName,
|
|
137
|
+
async function cacheAdd(client, modelName, cacheOptions) {
|
|
135
138
|
const methodName = 'Gemini - cacheAdd';
|
|
136
139
|
|
|
137
140
|
// check
|
|
138
|
-
if (!
|
|
139
|
-
logger.
|
|
141
|
+
if (!cacheOptions) {
|
|
142
|
+
logger.error(methodName, 'need cacheOptions');
|
|
143
|
+
return;
|
|
144
|
+
}
|
|
145
|
+
if (!cacheOptions.filePath) {
|
|
146
|
+
logger.error(methodName, 'need cacheOptions.filePath');
|
|
147
|
+
return;
|
|
148
|
+
}
|
|
149
|
+
if (!cacheOptions.systemPrompt) {
|
|
150
|
+
logger.error(methodName, 'need cacheOptions.systemPrompt');
|
|
140
151
|
return;
|
|
141
152
|
}
|
|
142
|
-
if (!
|
|
143
|
-
logger.
|
|
153
|
+
if (!cacheOptions.cacheName) {
|
|
154
|
+
logger.error(methodName, 'need cacheOptions.cacheName');
|
|
144
155
|
return;
|
|
145
156
|
}
|
|
157
|
+
if (!cacheOptions.cacheTTL) {
|
|
158
|
+
logger.error(methodName, 'need cacheOptions.cacheTTL');
|
|
159
|
+
return;
|
|
160
|
+
}
|
|
161
|
+
|
|
162
|
+
// const
|
|
163
|
+
const mimeType = mime.lookup(cacheOptions.filePath);
|
|
164
|
+
logger.info(methodName, 'cacheOptions', cacheOptions);
|
|
165
|
+
logger.info(methodName, 'mimeType', mimeType);
|
|
146
166
|
|
|
147
167
|
try {
|
|
168
|
+
// upload doc
|
|
169
|
+
const doc = await client.files.upload({
|
|
170
|
+
file: cacheOptions.filePath,
|
|
171
|
+
config: { mimeType: mimeType },
|
|
172
|
+
});
|
|
173
|
+
logger.info(methodName, 'doc.name', doc.name);
|
|
174
|
+
|
|
175
|
+
// cache add
|
|
148
176
|
const cache = await client.caches.create({
|
|
149
177
|
model: modelName,
|
|
150
178
|
config: {
|
|
151
|
-
|
|
152
|
-
|
|
179
|
+
contents: createUserContent(createPartFromUri(doc.uri, doc.mimeType)),
|
|
180
|
+
systemInstruction: cacheOptions.systemPrompt,
|
|
181
|
+
displayName: cacheOptions.cacheName,
|
|
182
|
+
ttl: cacheOptions.cacheTTL,
|
|
153
183
|
},
|
|
154
184
|
});
|
|
155
185
|
|