viho-llm 0.1.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/LICENSE +21 -0
- package/README.md +100 -0
- package/index.js +77 -0
- package/package.json +64 -0
- package/src/gemini.js +74 -0
- package/src/index.js +1 -0
package/LICENSE
ADDED
|
@@ -0,0 +1,21 @@
|
|
|
1
|
+
MIT License
|
|
2
|
+
|
|
3
|
+
Copyright (c) 2023 qiaowenbin<uikoo9@qq.com>
|
|
4
|
+
|
|
5
|
+
Permission is hereby granted, free of charge, to any person obtaining a copy
|
|
6
|
+
of this software and associated documentation files (the "Software"), to deal
|
|
7
|
+
in the Software without restriction, including without limitation the rights
|
|
8
|
+
to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
|
|
9
|
+
copies of the Software, and to permit persons to whom the Software is
|
|
10
|
+
furnished to do so, subject to the following conditions:
|
|
11
|
+
|
|
12
|
+
The above copyright notice and this permission notice shall be included in all
|
|
13
|
+
copies or substantial portions of the Software.
|
|
14
|
+
|
|
15
|
+
THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
|
|
16
|
+
IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
|
|
17
|
+
FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
|
|
18
|
+
AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
|
|
19
|
+
LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
|
|
20
|
+
OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE
|
|
21
|
+
SOFTWARE.
|
package/README.md
ADDED
|
@@ -0,0 +1,100 @@
|
|
|
1
|
+
# viho-llm
|
|
2
|
+
|
|
3
|
+
Utility library for working with Google Gemini AI, providing common tools and helpers for AI interactions.
|
|
4
|
+
|
|
5
|
+
## Installation
|
|
6
|
+
|
|
7
|
+
```bash
|
|
8
|
+
npm install viho-llm
|
|
9
|
+
```
|
|
10
|
+
|
|
11
|
+
## Prerequisites
|
|
12
|
+
|
|
13
|
+
You need to have a Google AI API key. Get one from [Google AI Studio](https://makersuite.google.com/app/apikey).
|
|
14
|
+
|
|
15
|
+
## Usage
|
|
16
|
+
|
|
17
|
+
### Basic Example
|
|
18
|
+
|
|
19
|
+
```javascript
|
|
20
|
+
import { Gemini } from 'viho-llm';
|
|
21
|
+
|
|
22
|
+
// Initialize Gemini client
|
|
23
|
+
const gemini = Gemini({
|
|
24
|
+
apiKey: 'your-google-api-key',
|
|
25
|
+
modelName: 'gemini-pro',
|
|
26
|
+
});
|
|
27
|
+
|
|
28
|
+
// Send a chat message
|
|
29
|
+
const response = await gemini.chat({
|
|
30
|
+
contents: [
|
|
31
|
+
{
|
|
32
|
+
role: 'user',
|
|
33
|
+
parts: [{ text: 'Hello, how are you?' }],
|
|
34
|
+
},
|
|
35
|
+
],
|
|
36
|
+
});
|
|
37
|
+
|
|
38
|
+
console.log(response);
|
|
39
|
+
```
|
|
40
|
+
|
|
41
|
+
## API Reference
|
|
42
|
+
|
|
43
|
+
### `Gemini(options)`
|
|
44
|
+
|
|
45
|
+
Creates a new Gemini client instance.
|
|
46
|
+
|
|
47
|
+
#### Parameters
|
|
48
|
+
|
|
49
|
+
- `options` (Object) - Configuration options
|
|
50
|
+
- `apiKey` (string) **required** - Your Google AI API key
|
|
51
|
+
- `modelName` (string) **required** - Model name (e.g., 'gemini-pro', 'gemini-pro-vision')
|
|
52
|
+
|
|
53
|
+
#### Returns
|
|
54
|
+
|
|
55
|
+
Returns a Gemini client object with the following methods:
|
|
56
|
+
|
|
57
|
+
##### `client.chat(chatOptions)`
|
|
58
|
+
|
|
59
|
+
Sends a chat request to the Gemini API.
|
|
60
|
+
|
|
61
|
+
**Parameters:**
|
|
62
|
+
|
|
63
|
+
- `chatOptions` (Object)
|
|
64
|
+
- `contents` (Array) **required** - Array of message objects
|
|
65
|
+
- `role` (string) - Either 'user' or 'model'
|
|
66
|
+
- `parts` (Array) - Array of content parts
|
|
67
|
+
- `text` (string) - The text content
|
|
68
|
+
|
|
69
|
+
**Returns:**
|
|
70
|
+
|
|
71
|
+
- (Promise\<string\>) - The generated text response
|
|
72
|
+
|
|
73
|
+
**Example:**
|
|
74
|
+
|
|
75
|
+
```javascript
|
|
76
|
+
const response = await gemini.chat({
|
|
77
|
+
contents: [
|
|
78
|
+
{
|
|
79
|
+
role: 'user',
|
|
80
|
+
parts: [{ text: 'Write a haiku about coding' }],
|
|
81
|
+
},
|
|
82
|
+
],
|
|
83
|
+
});
|
|
84
|
+
```
|
|
85
|
+
|
|
86
|
+
## License
|
|
87
|
+
|
|
88
|
+
MIT
|
|
89
|
+
|
|
90
|
+
## Author
|
|
91
|
+
|
|
92
|
+
uikoo9 <uikoo9@qq.com>
|
|
93
|
+
|
|
94
|
+
## Related Projects
|
|
95
|
+
|
|
96
|
+
- [viho](https://github.com/uikoo9/viho) - Main CLI tool for managing AI models
|
|
97
|
+
|
|
98
|
+
## Contributing
|
|
99
|
+
|
|
100
|
+
Issues and pull requests are welcome on [GitHub](https://github.com/uikoo9/viho/issues).
|
package/index.js
ADDED
|
@@ -0,0 +1,77 @@
|
|
|
1
|
+
'use strict';
|
|
2
|
+
|
|
3
|
+
var genai = require('@google/genai');
|
|
4
|
+
var qiao_log_js = require('qiao.log.js');
|
|
5
|
+
|
|
6
|
+
// gemini
|
|
7
|
+
const logger = qiao_log_js.Logger('viho-llm');
|
|
8
|
+
|
|
9
|
+
/**
|
|
10
|
+
* Gemini
|
|
11
|
+
* @param {*} options
|
|
12
|
+
* @returns
|
|
13
|
+
*/
|
|
14
|
+
const Gemini = (options) => {
|
|
15
|
+
const methodName = 'Gemini';
|
|
16
|
+
|
|
17
|
+
// check
|
|
18
|
+
if (!options) {
|
|
19
|
+
logger.info(methodName, 'need options');
|
|
20
|
+
return;
|
|
21
|
+
}
|
|
22
|
+
if (!options.apiKey) {
|
|
23
|
+
logger.info(methodName, 'need options.apiKey');
|
|
24
|
+
return;
|
|
25
|
+
}
|
|
26
|
+
if (!options.modelName) {
|
|
27
|
+
logger.info(methodName, 'need options.modelName');
|
|
28
|
+
return;
|
|
29
|
+
}
|
|
30
|
+
|
|
31
|
+
// gemini
|
|
32
|
+
const gemini = {};
|
|
33
|
+
gemini.client = new genai.GoogleGenAI({
|
|
34
|
+
vertexai: true,
|
|
35
|
+
apiKey: options.apiKey,
|
|
36
|
+
});
|
|
37
|
+
|
|
38
|
+
// chat
|
|
39
|
+
gemini.chat = async (chatOptions) => {
|
|
40
|
+
return await chat(gemini.client, options.modelName, chatOptions);
|
|
41
|
+
};
|
|
42
|
+
|
|
43
|
+
// r
|
|
44
|
+
return gemini;
|
|
45
|
+
};
|
|
46
|
+
|
|
47
|
+
// chat
|
|
48
|
+
async function chat(client, modelName, chatOptions) {
|
|
49
|
+
const methodName = 'Gemini - chat';
|
|
50
|
+
|
|
51
|
+
// check
|
|
52
|
+
if (!chatOptions) {
|
|
53
|
+
logger.info(methodName, 'need chatOptions');
|
|
54
|
+
return;
|
|
55
|
+
}
|
|
56
|
+
if (!chatOptions.contents) {
|
|
57
|
+
logger.info(methodName, 'need chatOptions.contents');
|
|
58
|
+
return;
|
|
59
|
+
}
|
|
60
|
+
|
|
61
|
+
try {
|
|
62
|
+
const response = await client.models.generateContent({
|
|
63
|
+
model: modelName,
|
|
64
|
+
contents: chatOptions.contents,
|
|
65
|
+
});
|
|
66
|
+
if (!response || !response.text) {
|
|
67
|
+
logger.error(methodName, 'invalid response');
|
|
68
|
+
return;
|
|
69
|
+
}
|
|
70
|
+
|
|
71
|
+
return response.text;
|
|
72
|
+
} catch (error) {
|
|
73
|
+
logger.error(methodName, 'error', error);
|
|
74
|
+
}
|
|
75
|
+
}
|
|
76
|
+
|
|
77
|
+
exports.Gemini = Gemini;
|
package/package.json
ADDED
|
@@ -0,0 +1,64 @@
|
|
|
1
|
+
{
|
|
2
|
+
"name": "viho-llm",
|
|
3
|
+
"version": "0.1.1",
|
|
4
|
+
"description": "Utility library for working with Google Gemini AI, providing common tools and helpers for AI interactions",
|
|
5
|
+
"keywords": [
|
|
6
|
+
"llm",
|
|
7
|
+
"ai",
|
|
8
|
+
"gemini",
|
|
9
|
+
"google-ai",
|
|
10
|
+
"google-gemini",
|
|
11
|
+
"genai",
|
|
12
|
+
"ai-tools",
|
|
13
|
+
"language-model",
|
|
14
|
+
"ai-utilities",
|
|
15
|
+
"viho"
|
|
16
|
+
],
|
|
17
|
+
"author": "uikoo9 <uikoo9@qq.com>",
|
|
18
|
+
"license": "MIT",
|
|
19
|
+
"homepage": "https://github.com/uikoo9/viho#readme",
|
|
20
|
+
"repository": {
|
|
21
|
+
"type": "git",
|
|
22
|
+
"url": "git+https://github.com/uikoo9/viho.git"
|
|
23
|
+
},
|
|
24
|
+
"bugs": {
|
|
25
|
+
"url": "https://github.com/uikoo9/viho/issues"
|
|
26
|
+
},
|
|
27
|
+
"main": "index.js",
|
|
28
|
+
"module": "src/index.js",
|
|
29
|
+
"sideEffects": false,
|
|
30
|
+
"files": [
|
|
31
|
+
"src",
|
|
32
|
+
"index.js",
|
|
33
|
+
"LICENSE",
|
|
34
|
+
"README.md"
|
|
35
|
+
],
|
|
36
|
+
"engines": {
|
|
37
|
+
"node": ">=18.0.0"
|
|
38
|
+
},
|
|
39
|
+
"scripts": {
|
|
40
|
+
"build": "qpro rollup ./rollup.config.js"
|
|
41
|
+
},
|
|
42
|
+
"dependencies": {
|
|
43
|
+
"@google/genai": "^1.34.0",
|
|
44
|
+
"qiao.log.js": "^3.7.5"
|
|
45
|
+
},
|
|
46
|
+
"nx": {
|
|
47
|
+
"namedInputs": {
|
|
48
|
+
"default": [
|
|
49
|
+
"{projectRoot}/src/**/*"
|
|
50
|
+
]
|
|
51
|
+
},
|
|
52
|
+
"targets": {
|
|
53
|
+
"build": {
|
|
54
|
+
"inputs": [
|
|
55
|
+
"default"
|
|
56
|
+
],
|
|
57
|
+
"outputs": [
|
|
58
|
+
"{projectRoot}/index.js"
|
|
59
|
+
]
|
|
60
|
+
}
|
|
61
|
+
}
|
|
62
|
+
},
|
|
63
|
+
"gitHead": "30ba092c8b2ab0e52894506eb93fdd77a26e4e36"
|
|
64
|
+
}
|
package/src/gemini.js
ADDED
|
@@ -0,0 +1,74 @@
|
|
|
1
|
+
// gemini
|
|
2
|
+
import { GoogleGenAI } from '@google/genai';
|
|
3
|
+
|
|
4
|
+
// Logger
|
|
5
|
+
import { Logger } from 'qiao.log.js';
|
|
6
|
+
const logger = Logger('viho-llm');
|
|
7
|
+
|
|
8
|
+
/**
|
|
9
|
+
* Gemini
|
|
10
|
+
* @param {*} options
|
|
11
|
+
* @returns
|
|
12
|
+
*/
|
|
13
|
+
export const Gemini = (options) => {
|
|
14
|
+
const methodName = 'Gemini';
|
|
15
|
+
|
|
16
|
+
// check
|
|
17
|
+
if (!options) {
|
|
18
|
+
logger.info(methodName, 'need options');
|
|
19
|
+
return;
|
|
20
|
+
}
|
|
21
|
+
if (!options.apiKey) {
|
|
22
|
+
logger.info(methodName, 'need options.apiKey');
|
|
23
|
+
return;
|
|
24
|
+
}
|
|
25
|
+
if (!options.modelName) {
|
|
26
|
+
logger.info(methodName, 'need options.modelName');
|
|
27
|
+
return;
|
|
28
|
+
}
|
|
29
|
+
|
|
30
|
+
// gemini
|
|
31
|
+
const gemini = {};
|
|
32
|
+
gemini.client = new GoogleGenAI({
|
|
33
|
+
vertexai: true,
|
|
34
|
+
apiKey: options.apiKey,
|
|
35
|
+
});
|
|
36
|
+
|
|
37
|
+
// chat
|
|
38
|
+
gemini.chat = async (chatOptions) => {
|
|
39
|
+
return await chat(gemini.client, options.modelName, chatOptions);
|
|
40
|
+
};
|
|
41
|
+
|
|
42
|
+
// r
|
|
43
|
+
return gemini;
|
|
44
|
+
};
|
|
45
|
+
|
|
46
|
+
// chat
|
|
47
|
+
async function chat(client, modelName, chatOptions) {
|
|
48
|
+
const methodName = 'Gemini - chat';
|
|
49
|
+
|
|
50
|
+
// check
|
|
51
|
+
if (!chatOptions) {
|
|
52
|
+
logger.info(methodName, 'need chatOptions');
|
|
53
|
+
return;
|
|
54
|
+
}
|
|
55
|
+
if (!chatOptions.contents) {
|
|
56
|
+
logger.info(methodName, 'need chatOptions.contents');
|
|
57
|
+
return;
|
|
58
|
+
}
|
|
59
|
+
|
|
60
|
+
try {
|
|
61
|
+
const response = await client.models.generateContent({
|
|
62
|
+
model: modelName,
|
|
63
|
+
contents: chatOptions.contents,
|
|
64
|
+
});
|
|
65
|
+
if (!response || !response.text) {
|
|
66
|
+
logger.error(methodName, 'invalid response');
|
|
67
|
+
return;
|
|
68
|
+
}
|
|
69
|
+
|
|
70
|
+
return response.text;
|
|
71
|
+
} catch (error) {
|
|
72
|
+
logger.error(methodName, 'error', error);
|
|
73
|
+
}
|
|
74
|
+
}
|
package/src/index.js
ADDED
|
@@ -0,0 +1 @@
|
|
|
1
|
+
export * from './gemini.js';
|