mm_llm 1.0.0 → 1.1.0
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/README.md +26 -0
- package/index.js +33 -30
- package/lib/config.tpl.json +36 -0
- package/lib/llm.js +31 -7
- package/lib/manager.js +73 -0
- package/lib/script.tpl.js +12 -0
- package/package.json +44 -42
package/README.md
CHANGED
|
@@ -25,6 +25,7 @@ npm install mm_llm
|
|
|
25
25
|
const { LLM } = require('mm_llm');
|
|
26
26
|
|
|
27
27
|
const llm = new LLM({
|
|
28
|
+
name: 'gpt', // 实例名称,必填
|
|
28
29
|
api_key: 'sk-xxx',
|
|
29
30
|
base_url: 'https://api.deepseek.com/v1',
|
|
30
31
|
model: 'deepseek-chat'
|
|
@@ -88,6 +89,7 @@ getQuota(user_id) -> Number // 未设置时返回 undefined,视为无配额
|
|
|
88
89
|
const { LLM, Sqlite } = require('mm_llm');
|
|
89
90
|
|
|
90
91
|
const llm = new LLM({
|
|
92
|
+
name: 'gpt',
|
|
91
93
|
api_key: 'sk-xxx',
|
|
92
94
|
store: new Sqlite('llm.db') // 缺省为 ':memory:',进程退出即丢失
|
|
93
95
|
});
|
|
@@ -99,6 +101,7 @@ const llm = new LLM({
|
|
|
99
101
|
|
|
100
102
|
| 字段 | 类型 | 默认值 | 说明 |
|
|
101
103
|
| --- | --- | --- | --- |
|
|
104
|
+
| `name` | String | `''` | 实例名称,必填,缺失时构造抛异常 |
|
|
102
105
|
| `api_key` | String | `''` | 服务密钥,缺失时调用抛异常 |
|
|
103
106
|
| `base_url` | String | `https://api.openai.com/v1` | 兼容接口地址 |
|
|
104
107
|
| `model` | String | `gpt-4o` | 模型名 |
|
|
@@ -174,6 +177,29 @@ llm.getStore().setAgent(2, { system_prompt: '你会按需调用工具', tools: [
|
|
|
174
177
|
2. **覆写方法**:子类覆写 `getHistory` / `getOptions` / `getSystemPrompt` / `getTools` / `useTool` / `post` / `checkAuth` / `checkQuota`,即可自定义任意一步的数据来源与行为;
|
|
175
178
|
3. **替换工具实现**:通过 `setTool` 注册的 `handler` 按名覆盖。
|
|
176
179
|
|
|
180
|
+
## 管理器与模板
|
|
181
|
+
|
|
182
|
+
`LLM` 继承 [mm_machine](https://www.npmjs.com/package/mm_machine) 的 `Drive`,可由 `LLMManager`(继承 `mm_machine.Manager`)托管:
|
|
183
|
+
|
|
184
|
+
```js
|
|
185
|
+
const { llmAdmin } = require('mm_llm');
|
|
186
|
+
|
|
187
|
+
const manager = llmAdmin('os', { dir: './llm' });
|
|
188
|
+
|
|
189
|
+
// 按 lib/config.tpl.json 模板生成 llm.json 并登记
|
|
190
|
+
manager.create({ name: 'gpt_main', title: '主力模型', description: '对话', api_key: 'sk-xxx' });
|
|
191
|
+
|
|
192
|
+
// 扫描目录,登记全部 llm.json 托管实例
|
|
193
|
+
await manager.load('./llm');
|
|
194
|
+
|
|
195
|
+
// 调度:load 生命周期加载主脚本后执行
|
|
196
|
+
const reply = await manager.exec('gpt_main', 'main', ctx, params, options);
|
|
197
|
+
```
|
|
198
|
+
|
|
199
|
+
- 检索文件名为 `llm.json`,模板位于 `lib/config.tpl.json`;
|
|
200
|
+
- 配置声明 `main` 指向脚本时,脚本的 `func_name`(默认 `main`)函数覆写内置对话入口,脚本缺失时按 `lib/script.tpl.js` 生成;`main` 缺省为空,保持内置逻辑;
|
|
201
|
+
- 目录监听与热更新沿用 mm_machine 基类机制。
|
|
202
|
+
|
|
177
203
|
## 示例
|
|
178
204
|
|
|
179
205
|
`example` 目录提供九个可独立运行的示例(`demo1.js` ~ `demo9.js`),由 `example/demo.js` 依次调用,均请求真实接口:
|
package/index.js
CHANGED
|
@@ -1,30 +1,33 @@
|
|
|
1
|
-
'use strict';
|
|
2
|
-
|
|
3
|
-
const { LLM } = require('./lib/llm');
|
|
4
|
-
const { Memory } = require('./lib/memory');
|
|
5
|
-
const { Sqlite } = require('./lib/sqlite');
|
|
6
|
-
const
|
|
7
|
-
|
|
8
|
-
|
|
9
|
-
|
|
10
|
-
|
|
11
|
-
|
|
12
|
-
|
|
13
|
-
|
|
14
|
-
|
|
15
|
-
|
|
16
|
-
|
|
17
|
-
|
|
18
|
-
|
|
19
|
-
|
|
20
|
-
|
|
21
|
-
|
|
22
|
-
|
|
23
|
-
|
|
24
|
-
|
|
25
|
-
|
|
26
|
-
|
|
27
|
-
|
|
28
|
-
|
|
29
|
-
|
|
30
|
-
|
|
1
|
+
'use strict';
|
|
2
|
+
|
|
3
|
+
const { LLM } = require('./lib/llm');
|
|
4
|
+
const { Memory } = require('./lib/memory');
|
|
5
|
+
const { Sqlite } = require('./lib/sqlite');
|
|
6
|
+
const Manager = require('./lib/manager');
|
|
7
|
+
const { reg } = require('mm_admin');
|
|
8
|
+
|
|
9
|
+
/**
|
|
10
|
+
* 大模型管理器实例(mm_admin 注册实例)
|
|
11
|
+
* 支持:require('mm_llm').llmAdmin(scope, config) -> manager.get('name')
|
|
12
|
+
*/
|
|
13
|
+
const admin = reg('mm_llm');
|
|
14
|
+
admin.setClass(Manager);
|
|
15
|
+
|
|
16
|
+
/**
|
|
17
|
+
* 获取指定作用域的大模型管理器
|
|
18
|
+
* @param {string} scope 作用域
|
|
19
|
+
* @param {Object} config 管理器配置
|
|
20
|
+
* @returns {LLMManager} 大模型管理器实例
|
|
21
|
+
*/
|
|
22
|
+
function llmAdmin(scope, config) {
|
|
23
|
+
return admin.get(scope || 'os', config);
|
|
24
|
+
}
|
|
25
|
+
|
|
26
|
+
module.exports = {
|
|
27
|
+
LLM,
|
|
28
|
+
Memory,
|
|
29
|
+
Sqlite,
|
|
30
|
+
Manager,
|
|
31
|
+
admin,
|
|
32
|
+
llmAdmin
|
|
33
|
+
};
|
|
@@ -0,0 +1,36 @@
|
|
|
1
|
+
{
|
|
2
|
+
// 注册域, 即本实例注册到 require('mm_llm').admin 的哪个域
|
|
3
|
+
"scope": "${scope || 'os'}",
|
|
4
|
+
// 实例名称, 用于动态增删改查
|
|
5
|
+
"name": "${name || 'demo'}",
|
|
6
|
+
// 实例标题
|
|
7
|
+
"title": "${title || '示例大模型'}",
|
|
8
|
+
// 实例描述
|
|
9
|
+
"description": "${description || '暂无描述'}",
|
|
10
|
+
// 服务密钥, 建议运行时注入, 勿硬编码
|
|
11
|
+
"api_key": "${api_key || ''}",
|
|
12
|
+
// OpenAI 兼容接口地址
|
|
13
|
+
"base_url": "${base_url || 'https://api.openai.com/v1'}",
|
|
14
|
+
// 模型名
|
|
15
|
+
"model": "${model || 'gpt-4o'}",
|
|
16
|
+
// 采样温度
|
|
17
|
+
"temperature": ${temperature || 0.7},
|
|
18
|
+
// 单次最大生成 token
|
|
19
|
+
"max_tokens": ${max_tokens || 8192},
|
|
20
|
+
// 是否流式返回
|
|
21
|
+
"stream": ${stream || false},
|
|
22
|
+
// 兜底系统提示词
|
|
23
|
+
"system_prompt": "${system_prompt || ''}",
|
|
24
|
+
// 工具调用最大轮数
|
|
25
|
+
"max_rounds": ${max_rounds || 8},
|
|
26
|
+
// 智能体未限定 users 时的默认准入策略: allow / deny
|
|
27
|
+
"auth_default": "${auth_default || 'allow'}",
|
|
28
|
+
// 主脚本文件, 相对 llm.json 所在目录解析; 为空表示使用内置对话逻辑
|
|
29
|
+
"main": "${main || ''}",
|
|
30
|
+
// 回调函数名, 用于决定调用脚本的哪个函数
|
|
31
|
+
"func_name": "${func_name || 'main'}",
|
|
32
|
+
// 状态 0未启用, 1启用
|
|
33
|
+
"state": ${state || 1},
|
|
34
|
+
// 排序, 越小越靠前
|
|
35
|
+
"sort": ${sort || 10}
|
|
36
|
+
}
|
package/lib/llm.js
CHANGED
|
@@ -2,6 +2,7 @@
|
|
|
2
2
|
|
|
3
3
|
const { AsyncLocalStorage } = require('async_hooks');
|
|
4
4
|
const { OpenAI } = require('openai');
|
|
5
|
+
const { Drive } = require('mm_machine');
|
|
5
6
|
const { Memory } = require('./memory');
|
|
6
7
|
|
|
7
8
|
/**
|
|
@@ -19,7 +20,7 @@ const CALL_OPTIONS_STORAGE = new AsyncLocalStorage();
|
|
|
19
20
|
* 经 OpenAI 兼容接口完成对话,并把助手回复写回存储。
|
|
20
21
|
*
|
|
21
22
|
* 用法:
|
|
22
|
-
* const llm = new LLM({ api_key: 'sk-xxx' });
|
|
23
|
+
* const llm = new LLM({ name: 'gpt', api_key: 'sk-xxx' });
|
|
23
24
|
* llm.setTool('get_time', () => ({ time: '12:00' }));
|
|
24
25
|
* llm.getStore().setToolSchema('get_time', { type: 'function', function: { name: 'get_time' } });
|
|
25
26
|
* llm.getStore().setAgent(1, { system_prompt: '你是助手', tools: ['get_time'], users: [9] });
|
|
@@ -51,12 +52,31 @@ const CALL_OPTIONS_STORAGE = new AsyncLocalStorage();
|
|
|
51
52
|
* getUser(user_id) -> { name, profile }
|
|
52
53
|
* getOptions(user_id, agent_id) -> Object
|
|
53
54
|
* getQuota(user_id) -> Number(未设置时返回 undefined,视为无配额)
|
|
55
|
+
*
|
|
56
|
+
* mm_machine 集成:
|
|
57
|
+
* 继承 Drive,可由 LLMManager 以 llm.json 配置文件登记为托管实例;
|
|
58
|
+
* 配置声明 main 指向脚本时,脚本的 func_name 函数会覆写内置 main
|
|
59
|
+
* (脚本不存在时按 lib/script.tpl.js 模板生成);缺省 main 为空,
|
|
60
|
+
* 不加载脚本,保持内置对话逻辑。
|
|
54
61
|
*/
|
|
55
|
-
class LLM {
|
|
62
|
+
class LLM extends Drive {
|
|
56
63
|
/**
|
|
57
64
|
* 默认配置
|
|
58
65
|
*/
|
|
59
66
|
static config = {
|
|
67
|
+
name: '',
|
|
68
|
+
title: '',
|
|
69
|
+
description: '',
|
|
70
|
+
// 主脚本文件,缺省为空表示不加载脚本、保持内置对话逻辑
|
|
71
|
+
main: '',
|
|
72
|
+
// 脚本回调函数名
|
|
73
|
+
func_name: 'main',
|
|
74
|
+
// 作用域
|
|
75
|
+
scope: 'os',
|
|
76
|
+
// 状态 0未启用 1启用
|
|
77
|
+
state: 1,
|
|
78
|
+
// 排序
|
|
79
|
+
sort: 10,
|
|
60
80
|
api_key: '',
|
|
61
81
|
base_url: 'https://api.openai.com/v1',
|
|
62
82
|
model: 'gpt-4o',
|
|
@@ -86,10 +106,14 @@ class LLM {
|
|
|
86
106
|
|
|
87
107
|
/**
|
|
88
108
|
* 构造函数
|
|
89
|
-
* @param {Object} config 配置,字段同 LLM.config
|
|
109
|
+
* @param {Object} config 配置,字段同 LLM.config,name 必填
|
|
110
|
+
* @param {Object} parent 父级管理器(可选)
|
|
90
111
|
*/
|
|
91
|
-
constructor(config = {}) {
|
|
92
|
-
|
|
112
|
+
constructor(config = {}, parent) {
|
|
113
|
+
if (!config.name) {
|
|
114
|
+
throw new TypeError('[LLM] 配置缺少 name,实例名称必填');
|
|
115
|
+
}
|
|
116
|
+
super({ ...LLM.config, ...config }, parent);
|
|
93
117
|
this.setStore(this.config.store);
|
|
94
118
|
}
|
|
95
119
|
|
|
@@ -188,7 +212,7 @@ class LLM {
|
|
|
188
212
|
* @returns {Promise<Object>} 助手消息;stream 为真时返回流对象
|
|
189
213
|
*/
|
|
190
214
|
async main(ctx = {}, params = {}, options = {}) {
|
|
191
|
-
return CALL_OPTIONS_STORAGE.run(options, () => this._converse(ctx,
|
|
215
|
+
return CALL_OPTIONS_STORAGE.run(options, () => this._converse(params, ctx, options));
|
|
192
216
|
}
|
|
193
217
|
|
|
194
218
|
/**
|
|
@@ -200,7 +224,7 @@ class LLM {
|
|
|
200
224
|
* @param {Object} params 调用参数,含 agent_id / session_id / content
|
|
201
225
|
* @returns {Promise<Object>} 助手消息;stream 为真时返回流对象
|
|
202
226
|
*/
|
|
203
|
-
async _converse(ctx,
|
|
227
|
+
async _converse(params, ctx, options) {
|
|
204
228
|
const authed = await this.checkAuth(ctx.user_id, params.agent_id);
|
|
205
229
|
if (!authed) {
|
|
206
230
|
throw new Error(`[LLM] 用户 ${ctx.user_id} 无权调用智能体 ${params.agent_id}`);
|
package/lib/manager.js
ADDED
|
@@ -0,0 +1,73 @@
|
|
|
1
|
+
'use strict';
|
|
2
|
+
|
|
3
|
+
const path = require('path');
|
|
4
|
+
const { Manager } = require('mm_machine');
|
|
5
|
+
const { LLM } = require('./llm');
|
|
6
|
+
|
|
7
|
+
/**
|
|
8
|
+
* 大模型管理器
|
|
9
|
+
* 继承 mm_machine.Manager:以 llm.json 为检索文件名扫描目录登记托管实例(update/load),
|
|
10
|
+
* create(config) 按 lib/config.tpl.json 模板生成配置文件、按 lib/script.tpl.js 生成主脚本,
|
|
11
|
+
* 目录监听与热更新沿用基类机制。
|
|
12
|
+
* @augments {Manager}
|
|
13
|
+
*/
|
|
14
|
+
class LLMManager extends Manager {
|
|
15
|
+
/**
|
|
16
|
+
* 配置参数
|
|
17
|
+
* @type {object}
|
|
18
|
+
*/
|
|
19
|
+
static config = {
|
|
20
|
+
// 名称
|
|
21
|
+
name: 'llm',
|
|
22
|
+
// 标题
|
|
23
|
+
title: '大模型管理',
|
|
24
|
+
// 描述
|
|
25
|
+
description: '这是大模型管理器',
|
|
26
|
+
// 托管实例配置文件名
|
|
27
|
+
filename: 'llm.json',
|
|
28
|
+
// 模板目录(config.tpl.json / script.tpl.js 所在目录)
|
|
29
|
+
tpl_dir: __dirname,
|
|
30
|
+
// 托管实例根目录,缺省为模块根目录
|
|
31
|
+
dir: ''
|
|
32
|
+
};
|
|
33
|
+
|
|
34
|
+
/**
|
|
35
|
+
* 构造函数
|
|
36
|
+
* @param {object} config 配置参数
|
|
37
|
+
* @param {object} parent 父级模块
|
|
38
|
+
*/
|
|
39
|
+
constructor(config, parent) {
|
|
40
|
+
super({ ...LLMManager.config, ...config }, parent);
|
|
41
|
+
// 受管驱动类
|
|
42
|
+
this.setClass(LLM);
|
|
43
|
+
}
|
|
44
|
+
}
|
|
45
|
+
|
|
46
|
+
/**
|
|
47
|
+
* 受管驱动类(基类 newMod / createFile 按此实例化)
|
|
48
|
+
* @type {LLM}
|
|
49
|
+
*/
|
|
50
|
+
LLMManager.prototype.Drive = LLM;
|
|
51
|
+
|
|
52
|
+
/**
|
|
53
|
+
* 获取托管实例根目录
|
|
54
|
+
* @returns {string} 根目录绝对路径
|
|
55
|
+
*/
|
|
56
|
+
LLMManager.prototype.getDir = function () {
|
|
57
|
+
if (this.config.dir) {
|
|
58
|
+
return this.config.dir;
|
|
59
|
+
}
|
|
60
|
+
return path.join(__dirname, '..');
|
|
61
|
+
};
|
|
62
|
+
|
|
63
|
+
/**
|
|
64
|
+
* 加载目录下的全部托管实例
|
|
65
|
+
* @param {string} dir 根目录(可选,缺省使用配置目录)
|
|
66
|
+
* @returns {Promise<Array<object>>} 实例列表
|
|
67
|
+
*/
|
|
68
|
+
LLMManager.prototype.load = async function (dir) {
|
|
69
|
+
await this.update(dir || this.getDir());
|
|
70
|
+
return this.list();
|
|
71
|
+
};
|
|
72
|
+
|
|
73
|
+
module.exports = LLMManager;
|
|
@@ -0,0 +1,12 @@
|
|
|
1
|
+
module.exports = {
|
|
2
|
+
/**
|
|
3
|
+
* 主要逻辑(覆写 LLM 内置对话入口)
|
|
4
|
+
* @param {object} params 调用参数,含 agent_id / session_id / content
|
|
5
|
+
* @param {object} ctx 调用上下文,含 user_id
|
|
6
|
+
* @param {object} options 本次调用选项
|
|
7
|
+
* @returns {Promise<object>} 助手消息
|
|
8
|
+
*/
|
|
9
|
+
async main(params, ctx, options) {
|
|
10
|
+
// 主要代码写在这
|
|
11
|
+
},
|
|
12
|
+
};
|
package/package.json
CHANGED
|
@@ -1,44 +1,46 @@
|
|
|
1
1
|
{
|
|
2
|
-
|
|
3
|
-
|
|
4
|
-
|
|
5
|
-
|
|
6
|
-
|
|
7
|
-
|
|
8
|
-
|
|
9
|
-
|
|
10
|
-
|
|
11
|
-
|
|
12
|
-
|
|
13
|
-
|
|
14
|
-
|
|
15
|
-
|
|
16
|
-
|
|
17
|
-
|
|
18
|
-
|
|
19
|
-
|
|
20
|
-
|
|
21
|
-
|
|
22
|
-
|
|
23
|
-
|
|
24
|
-
|
|
25
|
-
|
|
26
|
-
|
|
27
|
-
|
|
28
|
-
|
|
29
|
-
|
|
30
|
-
|
|
31
|
-
|
|
32
|
-
|
|
33
|
-
|
|
34
|
-
|
|
35
|
-
|
|
36
|
-
|
|
37
|
-
|
|
38
|
-
|
|
39
|
-
|
|
40
|
-
|
|
41
|
-
|
|
42
|
-
|
|
43
|
-
|
|
2
|
+
"name": "mm_llm",
|
|
3
|
+
"version": "1.1.0",
|
|
4
|
+
"description": "这是一个适用于多用户、多智能体、多技能、多工具的模块,方便打造多智能体系统。",
|
|
5
|
+
"keywords": [
|
|
6
|
+
"mm_mod",
|
|
7
|
+
"mm_port",
|
|
8
|
+
"mm_out_port",
|
|
9
|
+
"llm",
|
|
10
|
+
"openai",
|
|
11
|
+
"chat",
|
|
12
|
+
"agent",
|
|
13
|
+
"multi-agent",
|
|
14
|
+
"tool-calling"
|
|
15
|
+
],
|
|
16
|
+
"layer": "infrastructure",
|
|
17
|
+
"group": "adapters",
|
|
18
|
+
"license": "ISC",
|
|
19
|
+
"author": "qww",
|
|
20
|
+
"type": "commonjs",
|
|
21
|
+
"main": "index.js",
|
|
22
|
+
"files": [
|
|
23
|
+
"index.js",
|
|
24
|
+
"lib"
|
|
25
|
+
],
|
|
26
|
+
"engines": {
|
|
27
|
+
"node": ">=22.5.0"
|
|
28
|
+
},
|
|
29
|
+
"repository": {
|
|
30
|
+
"type": "git",
|
|
31
|
+
"url": "git+https://gitee.com/qiuwenwu91/mm_llm.git"
|
|
32
|
+
},
|
|
33
|
+
"homepage": "https://gitee.com/qiuwenwu91/mm_llm",
|
|
34
|
+
"bugs": {
|
|
35
|
+
"url": "https://gitee.com/qiuwenwu91/mm_llm/issues"
|
|
36
|
+
},
|
|
37
|
+
"scripts": {
|
|
38
|
+
"demo": "node example/demo.js",
|
|
39
|
+
"test": "node test.js"
|
|
40
|
+
},
|
|
41
|
+
"dependencies": {
|
|
42
|
+
"mm_admin": "^1.1.0",
|
|
43
|
+
"mm_machine": "^6.0.0",
|
|
44
|
+
"openai": "^7.23.0"
|
|
45
|
+
}
|
|
44
46
|
}
|