viho-llm 0.1.4 → 0.1.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 CHANGED
@@ -5,65 +5,33 @@ var mime = require('mime-types');
5
5
  var qiao_log_js = require('qiao.log.js');
6
6
 
7
7
  // gemini
8
- const logger = qiao_log_js.Logger('viho-llm');
8
+ const logger$2 = qiao_log_js.Logger('gemini-util.js');
9
9
 
10
10
  /**
11
- * Gemini
12
- * @param {*} options
11
+ * chat
12
+ * @param {*} client
13
+ * @param {*} modelName
14
+ * @param {*} chatOptions
13
15
  * @returns
14
16
  */
15
- const Gemini = (options) => {
16
- const methodName = 'Gemini';
17
+ const chat = async (client, modelName, chatOptions) => {
18
+ const methodName = 'chat';
17
19
 
18
20
  // check
19
- if (!options) {
20
- logger.error(methodName, 'need options');
21
+ if (!client) {
22
+ logger$2.error(methodName, 'need client');
21
23
  return;
22
24
  }
23
- if (!options.apiKey) {
24
- logger.error(methodName, 'need options.apiKey');
25
- return;
26
- }
27
- if (!options.modelName) {
28
- logger.error(methodName, 'need options.modelName');
25
+ if (!modelName) {
26
+ logger$2.error(methodName, 'need modelName');
29
27
  return;
30
28
  }
31
-
32
- // gemini
33
- const gemini = {};
34
- gemini.client = new genai.GoogleGenAI({
35
- vertexai: true,
36
- apiKey: options.apiKey,
37
- });
38
-
39
- // chat
40
- gemini.chat = async (chatOptions) => {
41
- return await chat(gemini.client, options.modelName, chatOptions);
42
- };
43
- gemini.chatWithStreaming = async (chatOptions, callbackOptions) => {
44
- return await chatWithStreaming(gemini.client, options.modelName, chatOptions, callbackOptions);
45
- };
46
-
47
- // cache
48
- gemini.cacheAdd = async (cacheOptions) => {
49
- return await cacheAdd(gemini.client, options.modelName, cacheOptions);
50
- };
51
-
52
- // r
53
- return gemini;
54
- };
55
-
56
- // chat
57
- async function chat(client, modelName, chatOptions) {
58
- const methodName = 'Gemini - chat';
59
-
60
- // check
61
29
  if (!chatOptions) {
62
- logger.error(methodName, 'need chatOptions');
30
+ logger$2.error(methodName, 'need chatOptions');
63
31
  return;
64
32
  }
65
33
  if (!chatOptions.contents) {
66
- logger.error(methodName, 'need chatOptions.contents');
34
+ logger$2.error(methodName, 'need chatOptions.contents');
67
35
  return;
68
36
  }
69
37
 
@@ -73,26 +41,46 @@ async function chat(client, modelName, chatOptions) {
73
41
  contents: chatOptions.contents,
74
42
  });
75
43
  if (!response || !response.text) {
76
- logger.error(methodName, 'invalid response');
44
+ logger$2.error(methodName, 'invalid response');
77
45
  return;
78
46
  }
79
47
 
80
48
  return response.text;
81
49
  } catch (error) {
82
- logger.error(methodName, 'error', error);
50
+ logger$2.error(methodName, 'error', error);
83
51
  }
84
- }
52
+ };
85
53
 
86
- async function chatWithStreaming(client, modelName, chatOptions, callbackOptions) {
87
- const methodName = 'Gemini - chatWithStreaming';
54
+ /**
55
+ * chatWithStreaming
56
+ * @param {*} client
57
+ * @param {*} modelName
58
+ * @param {*} chatOptions
59
+ * @param {*} callbackOptions
60
+ * @returns
61
+ */
62
+ const chatWithStreaming = async (client, modelName, chatOptions, callbackOptions) => {
63
+ const methodName = 'chatWithStreaming';
88
64
 
89
65
  // check
66
+ if (!client) {
67
+ logger$2.error(methodName, 'need client');
68
+ return;
69
+ }
70
+ if (!modelName) {
71
+ logger$2.error(methodName, 'need modelName');
72
+ return;
73
+ }
90
74
  if (!chatOptions) {
91
- logger.error(methodName, 'need chatOptions');
75
+ logger$2.error(methodName, 'need chatOptions');
92
76
  return;
93
77
  }
94
78
  if (!chatOptions.contents) {
95
- logger.error(methodName, 'need chatOptions.contents');
79
+ logger$2.error(methodName, 'need chatOptions.contents');
80
+ return;
81
+ }
82
+ if (!callbackOptions) {
83
+ logger$2.error(methodName, 'need callbackOptions');
96
84
  return;
97
85
  }
98
86
 
@@ -130,52 +118,51 @@ async function chatWithStreaming(client, modelName, chatOptions, callbackOptions
130
118
  } catch (error) {
131
119
  if (errorCallback) errorCallback(error);
132
120
  }
133
- }
121
+ };
134
122
 
135
- // cache add
136
- async function cacheAdd(client, modelName, cacheOptions) {
137
- const methodName = 'Gemini - cacheAdd';
123
+ /**
124
+ * cacheAdd
125
+ * @param {*} client
126
+ * @param {*} modelName
127
+ * @param {*} cacheOptions
128
+ * @returns
129
+ */
130
+ const cacheAdd = async (client, modelName, cacheOptions) => {
131
+ const methodName = 'cacheAdd';
138
132
 
139
133
  // check
140
134
  if (!cacheOptions) {
141
- logger.error(methodName, 'need cacheOptions');
135
+ logger$2.error(methodName, 'need cacheOptions');
142
136
  return;
143
137
  }
144
- if (!cacheOptions.filePath) {
145
- logger.error(methodName, 'need cacheOptions.filePath');
138
+ if (!cacheOptions.gsPath) {
139
+ logger$2.error(methodName, 'need cacheOptions.gsPath');
146
140
  return;
147
141
  }
148
142
  if (!cacheOptions.systemPrompt) {
149
- logger.error(methodName, 'need cacheOptions.systemPrompt');
143
+ logger$2.error(methodName, 'need cacheOptions.systemPrompt');
150
144
  return;
151
145
  }
152
146
  if (!cacheOptions.cacheName) {
153
- logger.error(methodName, 'need cacheOptions.cacheName');
147
+ logger$2.error(methodName, 'need cacheOptions.cacheName');
154
148
  return;
155
149
  }
156
150
  if (!cacheOptions.cacheTTL) {
157
- logger.error(methodName, 'need cacheOptions.cacheTTL');
151
+ logger$2.error(methodName, 'need cacheOptions.cacheTTL');
158
152
  return;
159
153
  }
160
154
 
161
155
  // const
162
- const mimeType = mime.lookup(cacheOptions.filePath);
163
- logger.info(methodName, 'cacheOptions', cacheOptions);
164
- logger.info(methodName, 'mimeType', mimeType);
156
+ const mimeType = mime.lookup(cacheOptions.gsPath);
157
+ logger$2.info(methodName, 'cacheOptions', cacheOptions);
158
+ logger$2.info(methodName, 'mimeType', mimeType);
165
159
 
166
160
  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
161
  // cache add
175
162
  const cache = await client.caches.create({
176
163
  model: modelName,
177
164
  config: {
178
- contents: genai.createUserContent(genai.createPartFromUri(doc.uri, doc.mimeType)),
165
+ contents: genai.createUserContent(genai.createPartFromUri(cacheOptions.gsPath, mimeType)),
179
166
  systemInstruction: cacheOptions.systemPrompt,
180
167
  displayName: cacheOptions.cacheName,
181
168
  ttl: cacheOptions.cacheTTL,
@@ -184,8 +171,106 @@ async function cacheAdd(client, modelName, cacheOptions) {
184
171
 
185
172
  return cache;
186
173
  } catch (error) {
187
- logger.error(methodName, 'error', error);
174
+ logger$2.error(methodName, 'error', error);
175
+ }
176
+ };
177
+
178
+ // gemini
179
+ const logger$1 = qiao_log_js.Logger('gemini-api.js');
180
+
181
+ /**
182
+ * GeminiAPI
183
+ * @param {*} options
184
+ * @returns
185
+ */
186
+ const GeminiAPI = (options) => {
187
+ const methodName = 'GeminiAPI';
188
+
189
+ // check
190
+ if (!options) {
191
+ logger$1.error(methodName, 'need options');
192
+ return;
193
+ }
194
+ if (!options.apiKey) {
195
+ logger$1.error(methodName, 'need options.apiKey');
196
+ return;
188
197
  }
189
- }
198
+ if (!options.modelName) {
199
+ logger$1.error(methodName, 'need options.modelName');
200
+ return;
201
+ }
202
+
203
+ // gemini
204
+ const gemini = {};
205
+ gemini.client = new genai.GoogleGenAI({
206
+ apiKey: options.apiKey,
207
+ });
208
+
209
+ // chat
210
+ gemini.chat = async (chatOptions) => {
211
+ return await chat(gemini.client, options.modelName, chatOptions);
212
+ };
213
+ gemini.chatWithStreaming = async (chatOptions, callbackOptions) => {
214
+ return await chatWithStreaming(gemini.client, options.modelName, chatOptions, callbackOptions);
215
+ };
216
+
217
+ // r
218
+ return gemini;
219
+ };
220
+
221
+ // gemini
222
+ const logger = qiao_log_js.Logger('viho-llm');
223
+
224
+ /**
225
+ * GeminiVertex
226
+ * @param {*} options
227
+ * @returns
228
+ */
229
+ const GeminiVertex = (options) => {
230
+ const methodName = 'GeminiVertex';
231
+
232
+ // check
233
+ if (!options) {
234
+ logger.error(methodName, 'need options');
235
+ return;
236
+ }
237
+ if (!options.projectId) {
238
+ logger.error(methodName, 'need options.projectId');
239
+ return;
240
+ }
241
+ if (!options.location) {
242
+ logger.error(methodName, 'need options.location');
243
+ return;
244
+ }
245
+ if (!options.modelName) {
246
+ logger.error(methodName, 'need options.modelName');
247
+ return;
248
+ }
249
+
250
+ // gemini
251
+ const gemini = {};
252
+ gemini.client = new genai.GoogleGenAI({
253
+ vertexai: true,
254
+ project: options.projectId,
255
+ location: options.location,
256
+ });
257
+
258
+ // chat
259
+ gemini.chat = async (chatOptions) => {
260
+ return await chat(gemini.client, options.modelName, chatOptions);
261
+ };
262
+ gemini.chatWithStreaming = async (chatOptions, callbackOptions) => {
263
+ return await chatWithStreaming(gemini.client, options.modelName, chatOptions, callbackOptions);
264
+ };
265
+
266
+ // cache
267
+ gemini.cacheAdd = async (cacheOptions) => {
268
+ return await cacheAdd(gemini.client, options.modelName, cacheOptions);
269
+ };
270
+
271
+ // r
272
+ return gemini;
273
+ };
190
274
 
191
- exports.Gemini = Gemini;
275
+ exports.GeminiAPI = GeminiAPI;
276
+ exports.GeminiVertex = GeminiVertex;
package/package.json CHANGED
@@ -1,6 +1,6 @@
1
1
  {
2
2
  "name": "viho-llm",
3
- "version": "0.1.4",
3
+ "version": "0.1.5",
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",
@@ -61,5 +61,5 @@
61
61
  }
62
62
  }
63
63
  },
64
- "gitHead": "38e28266271d8b64e56c5f6d27deca78ce8cdb1b"
64
+ "gitHead": "b37b523239fe354f991017387e95a6314e994981"
65
65
  }
package/src/index.js CHANGED
@@ -1 +1,2 @@
1
- export * from './gemini.js';
1
+ export * from './models/gemini-api.js';
2
+ export * from './models/gemini-vertex.js';
@@ -0,0 +1,49 @@
1
+ // gemini
2
+ import { GoogleGenAI } from '@google/genai';
3
+
4
+ // util
5
+ import { chat, chatWithStreaming } from './gemini-util.js';
6
+
7
+ // Logger
8
+ import { Logger } from 'qiao.log.js';
9
+ const logger = Logger('gemini-api.js');
10
+
11
+ /**
12
+ * GeminiAPI
13
+ * @param {*} options
14
+ * @returns
15
+ */
16
+ export const GeminiAPI = (options) => {
17
+ const methodName = 'GeminiAPI';
18
+
19
+ // check
20
+ if (!options) {
21
+ logger.error(methodName, 'need options');
22
+ return;
23
+ }
24
+ if (!options.apiKey) {
25
+ logger.error(methodName, 'need options.apiKey');
26
+ return;
27
+ }
28
+ if (!options.modelName) {
29
+ logger.error(methodName, 'need options.modelName');
30
+ return;
31
+ }
32
+
33
+ // gemini
34
+ const gemini = {};
35
+ gemini.client = new GoogleGenAI({
36
+ apiKey: options.apiKey,
37
+ });
38
+
39
+ // chat
40
+ gemini.chat = async (chatOptions) => {
41
+ return await chat(gemini.client, options.modelName, chatOptions);
42
+ };
43
+ gemini.chatWithStreaming = async (chatOptions, callbackOptions) => {
44
+ return await chatWithStreaming(gemini.client, options.modelName, chatOptions, callbackOptions);
45
+ };
46
+
47
+ // r
48
+ return gemini;
49
+ };
@@ -1,64 +1,32 @@
1
1
  // gemini
2
- import { GoogleGenAI, createUserContent, createPartFromUri } from '@google/genai';
2
+ import { createUserContent, createPartFromUri } from '@google/genai';
3
3
 
4
4
  // mime
5
5
  import mime from 'mime-types';
6
6
 
7
7
  // Logger
8
8
  import { Logger } from 'qiao.log.js';
9
- const logger = Logger('viho-llm');
9
+ const logger = Logger('gemini-util.js');
10
10
 
11
11
  /**
12
- * Gemini
13
- * @param {*} options
12
+ * chat
13
+ * @param {*} client
14
+ * @param {*} modelName
15
+ * @param {*} chatOptions
14
16
  * @returns
15
17
  */
16
- export const Gemini = (options) => {
17
- const methodName = 'Gemini';
18
+ export const chat = async (client, modelName, chatOptions) => {
19
+ const methodName = 'chat';
18
20
 
19
21
  // check
20
- if (!options) {
21
- logger.error(methodName, 'need options');
22
+ if (!client) {
23
+ logger.error(methodName, 'need client');
22
24
  return;
23
25
  }
24
- if (!options.apiKey) {
25
- logger.error(methodName, 'need options.apiKey');
26
+ if (!modelName) {
27
+ logger.error(methodName, 'need modelName');
26
28
  return;
27
29
  }
28
- if (!options.modelName) {
29
- logger.error(methodName, 'need options.modelName');
30
- return;
31
- }
32
-
33
- // gemini
34
- const gemini = {};
35
- gemini.client = new GoogleGenAI({
36
- vertexai: true,
37
- apiKey: options.apiKey,
38
- });
39
-
40
- // chat
41
- gemini.chat = async (chatOptions) => {
42
- return await chat(gemini.client, options.modelName, chatOptions);
43
- };
44
- gemini.chatWithStreaming = async (chatOptions, callbackOptions) => {
45
- return await chatWithStreaming(gemini.client, options.modelName, chatOptions, callbackOptions);
46
- };
47
-
48
- // cache
49
- gemini.cacheAdd = async (cacheOptions) => {
50
- return await cacheAdd(gemini.client, options.modelName, cacheOptions);
51
- };
52
-
53
- // r
54
- return gemini;
55
- };
56
-
57
- // chat
58
- async function chat(client, modelName, chatOptions) {
59
- const methodName = 'Gemini - chat';
60
-
61
- // check
62
30
  if (!chatOptions) {
63
31
  logger.error(methodName, 'need chatOptions');
64
32
  return;
@@ -82,12 +50,28 @@ async function chat(client, modelName, chatOptions) {
82
50
  } catch (error) {
83
51
  logger.error(methodName, 'error', error);
84
52
  }
85
- }
53
+ };
86
54
 
87
- async function chatWithStreaming(client, modelName, chatOptions, callbackOptions) {
88
- const methodName = 'Gemini - chatWithStreaming';
55
+ /**
56
+ * chatWithStreaming
57
+ * @param {*} client
58
+ * @param {*} modelName
59
+ * @param {*} chatOptions
60
+ * @param {*} callbackOptions
61
+ * @returns
62
+ */
63
+ export const chatWithStreaming = async (client, modelName, chatOptions, callbackOptions) => {
64
+ const methodName = 'chatWithStreaming';
89
65
 
90
66
  // check
67
+ if (!client) {
68
+ logger.error(methodName, 'need client');
69
+ return;
70
+ }
71
+ if (!modelName) {
72
+ logger.error(methodName, 'need modelName');
73
+ return;
74
+ }
91
75
  if (!chatOptions) {
92
76
  logger.error(methodName, 'need chatOptions');
93
77
  return;
@@ -96,6 +80,10 @@ async function chatWithStreaming(client, modelName, chatOptions, callbackOptions
96
80
  logger.error(methodName, 'need chatOptions.contents');
97
81
  return;
98
82
  }
83
+ if (!callbackOptions) {
84
+ logger.error(methodName, 'need callbackOptions');
85
+ return;
86
+ }
99
87
 
100
88
  // callback
101
89
  const beginCallback = callbackOptions.beginCallback;
@@ -131,19 +119,25 @@ async function chatWithStreaming(client, modelName, chatOptions, callbackOptions
131
119
  } catch (error) {
132
120
  if (errorCallback) errorCallback(error);
133
121
  }
134
- }
122
+ };
135
123
 
136
- // cache add
137
- async function cacheAdd(client, modelName, cacheOptions) {
138
- const methodName = 'Gemini - cacheAdd';
124
+ /**
125
+ * cacheAdd
126
+ * @param {*} client
127
+ * @param {*} modelName
128
+ * @param {*} cacheOptions
129
+ * @returns
130
+ */
131
+ export const cacheAdd = async (client, modelName, cacheOptions) => {
132
+ const methodName = 'cacheAdd';
139
133
 
140
134
  // check
141
135
  if (!cacheOptions) {
142
136
  logger.error(methodName, 'need cacheOptions');
143
137
  return;
144
138
  }
145
- if (!cacheOptions.filePath) {
146
- logger.error(methodName, 'need cacheOptions.filePath');
139
+ if (!cacheOptions.gsPath) {
140
+ logger.error(methodName, 'need cacheOptions.gsPath');
147
141
  return;
148
142
  }
149
143
  if (!cacheOptions.systemPrompt) {
@@ -160,23 +154,16 @@ async function cacheAdd(client, modelName, cacheOptions) {
160
154
  }
161
155
 
162
156
  // const
163
- const mimeType = mime.lookup(cacheOptions.filePath);
157
+ const mimeType = mime.lookup(cacheOptions.gsPath);
164
158
  logger.info(methodName, 'cacheOptions', cacheOptions);
165
159
  logger.info(methodName, 'mimeType', mimeType);
166
160
 
167
161
  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
162
  // cache add
176
163
  const cache = await client.caches.create({
177
164
  model: modelName,
178
165
  config: {
179
- contents: createUserContent(createPartFromUri(doc.uri, doc.mimeType)),
166
+ contents: createUserContent(createPartFromUri(cacheOptions.gsPath, mimeType)),
180
167
  systemInstruction: cacheOptions.systemPrompt,
181
168
  displayName: cacheOptions.cacheName,
182
169
  ttl: cacheOptions.cacheTTL,
@@ -187,4 +174,4 @@ async function cacheAdd(client, modelName, cacheOptions) {
187
174
  } catch (error) {
188
175
  logger.error(methodName, 'error', error);
189
176
  }
190
- }
177
+ };
@@ -0,0 +1,60 @@
1
+ // gemini
2
+ import { GoogleGenAI } from '@google/genai';
3
+
4
+ // util
5
+ import { chat, chatWithStreaming, cacheAdd } from './gemini-util.js';
6
+
7
+ // Logger
8
+ import { Logger } from 'qiao.log.js';
9
+ const logger = Logger('viho-llm');
10
+
11
+ /**
12
+ * GeminiVertex
13
+ * @param {*} options
14
+ * @returns
15
+ */
16
+ export const GeminiVertex = (options) => {
17
+ const methodName = 'GeminiVertex';
18
+
19
+ // check
20
+ if (!options) {
21
+ logger.error(methodName, 'need options');
22
+ return;
23
+ }
24
+ if (!options.projectId) {
25
+ logger.error(methodName, 'need options.projectId');
26
+ return;
27
+ }
28
+ if (!options.location) {
29
+ logger.error(methodName, 'need options.location');
30
+ return;
31
+ }
32
+ if (!options.modelName) {
33
+ logger.error(methodName, 'need options.modelName');
34
+ return;
35
+ }
36
+
37
+ // gemini
38
+ const gemini = {};
39
+ gemini.client = new GoogleGenAI({
40
+ vertexai: true,
41
+ project: options.projectId,
42
+ location: options.location,
43
+ });
44
+
45
+ // chat
46
+ gemini.chat = async (chatOptions) => {
47
+ return await chat(gemini.client, options.modelName, chatOptions);
48
+ };
49
+ gemini.chatWithStreaming = async (chatOptions, callbackOptions) => {
50
+ return await chatWithStreaming(gemini.client, options.modelName, chatOptions, callbackOptions);
51
+ };
52
+
53
+ // cache
54
+ gemini.cacheAdd = async (cacheOptions) => {
55
+ return await cacheAdd(gemini.client, options.modelName, cacheOptions);
56
+ };
57
+
58
+ // r
59
+ return gemini;
60
+ };