mm_llm 1.0.1 → 1.3.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 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,40 @@
1
- 'use strict';
2
-
3
- const { LLM } = require('./lib/llm');
4
- const { Memory } = require('./lib/memory');
5
- const { Sqlite } = require('./lib/sqlite');
6
- const Admin = require('mm_corejs').Admin;
7
-
8
- let admin = new Admin();
9
- admin.setClass(LLM);
10
-
11
- /**
12
- * 获取大模型实例
13
- * @param {string} scope 作用域
14
- * @param {Object} config 配置参数
15
- * @returns {LLM} 大模型实例
16
- */
17
- function llmAdmin(scope, config) {
18
- if (!scope) {
19
- scope = 'os';
20
- }
21
- return admin.get(scope, config);
22
- }
23
-
24
- module.exports = {
25
- LLM,
26
- Memory,
27
- Sqlite,
28
- admin,
29
- llmAdmin
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
+ };
34
+
35
+ /* mm-tool-logic-export 工具能力层:其它模块可 require("<pkg>").tools.<name> 直接引用 */
36
+ Object.defineProperty(module.exports, 'tools', { enumerable: true, configurable: true,
37
+ get: function () { return require('./lib/tools'); } });
38
+
39
+ /* S2 能力下沉:业务实现可在模块之间直接引用(不经过工具注册面) */
40
+ Object.defineProperty(module.exports, 'llmToolLogic', { enumerable: true, value: require('./lib/llm_tool_logic.js') });
@@ -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
- this.config = Object.assign({}, LLM.config, config);
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, params));
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, params) {
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}`);
@@ -0,0 +1,231 @@
1
+ /**
2
+ * LLM 调用器
3
+ * 基于 OpenAI SDK 直接调用 LLM 接口
4
+ * 单次调用返回原始响应(含 tool_calls)
5
+ * 如需工具执行和多轮对话,请使用 ai 模块
6
+ */
7
+ const { configAdmin } = require('mm_config');
8
+
9
+ const { OpenAI } = require('openai');
10
+
11
+ module.exports = {
12
+ /**
13
+ * 调用 LLM 接口
14
+ * @param {object} ctx 上下文对象,结果写入 ctx.output
15
+ * @param {object} params 步骤参数
16
+ * - system_prompt {string} 系统提示词
17
+ * - user_prompt {string} 用户提示词(含上下文)
18
+ * - messages {Array} 消息数组,优先级高于 system_prompt/user_prompt
19
+ * - llm {string} LLM 模块名称,默认 default
20
+ * - api_key {string} API 密钥
21
+ * - base_url {string} API 基础地址
22
+ * - model {string} 模型名称
23
+ * - temperature {number} 生成温度
24
+ * - max_tokens {number} 最大 token 数
25
+ * - response_format {object} 响应格式
26
+ * - tools {Array} 工具定义数组,用于 function calling
27
+ * - tool_names {Array} 工具名称数组,通过 kernel.getTools 获取工具定义
28
+ * - tool_choice {string|object} 工具选择策略
29
+ * @returns {object} { msg, content, tool_calls, model, usage }
30
+ */
31
+ async main(params, ctx, options) {
32
+ var opts = await this._getOptions(params);
33
+
34
+ if (!opts.api_key) {
35
+ ctx.output = {
36
+ error: {
37
+ code: 'EXEC_MISSING_API_KEY',
38
+ message: '缺少 API Key,请在 options 或 params 中配置 api_key'
39
+ }
40
+ };
41
+ return ctx.output;
42
+ }
43
+
44
+ var messages = this._buildMessages(params);
45
+ if (messages.length === 0) {
46
+ ctx.output = {
47
+ error: {
48
+ code: 'EXEC_MISSING_PROMPT',
49
+ message: '缺少提示词,请提供 system_prompt 和 user_prompt,或 messages 数组'
50
+ }
51
+ };
52
+ return ctx.output;
53
+ }
54
+
55
+ // tool_names: 从 kernel 获取工具定义并转换为 OpenAI 格式
56
+ var req_params = params;
57
+ if (!params.tools && params.tool_names && Array.isArray(params.tool_names)) {
58
+ var resolved_tools = this._resolveTools(this.getParent(), params.tool_names);
59
+ if (resolved_tools) {
60
+ req_params = Object.assign({}, params, { tools: resolved_tools });
61
+ }
62
+ }
63
+
64
+ var result = await this._sendRequest(opts, messages, req_params);
65
+
66
+ ctx.output = {
67
+ msg: 'LLM 调用成功',
68
+ content: result.content || null,
69
+ tool_calls: result.tool_calls || null,
70
+ model: result.model,
71
+ usage: result.usage
72
+ };
73
+ return ctx.output;
74
+ },
75
+
76
+ /**
77
+ * 合并 options 和 params 获取最终配置
78
+ * @param {object} params 步骤参数
79
+ * @returns {object} 合并后的配置
80
+ */
81
+ async _getOptions(params) {
82
+ var cfg = null;
83
+ try {
84
+ var llm_cfg = await configAdmin('os').get('llm');
85
+ cfg = (llm_cfg && llm_cfg[params.llm || 'default']) || null;
86
+ } catch (e) {
87
+ void e;
88
+ }
89
+ if (!cfg && typeof this.getOptions === 'function') {
90
+ cfg = this.getOptions();
91
+ }
92
+ cfg = cfg || {};
93
+
94
+ return {
95
+ api_key: params.api_key || cfg.api_key || '',
96
+ base_url: params.base_url || cfg.base_url || 'https://api.openai.com/v1',
97
+ model: params.model || cfg.model || 'gpt-4o',
98
+ temperature:
99
+ params.temperature !== undefined
100
+ ? params.temperature
101
+ : cfg.temperature !== undefined
102
+ ? cfg.temperature
103
+ : 0.2,
104
+ max_tokens: params.max_tokens || cfg.max_tokens || 4096,
105
+ timeout: cfg.timeout || 60000
106
+ };
107
+ },
108
+
109
+ /**
110
+ * 构建消息数组
111
+ * @param {object} params 步骤参数
112
+ * @returns {Array} 消息数组 [{role, content}]
113
+ */
114
+ _buildMessages(params) {
115
+ if (params.messages && Array.isArray(params.messages) && params.messages.length > 0) {
116
+ return params.messages;
117
+ }
118
+
119
+ var list = [];
120
+ if (params.system_prompt) {
121
+ list.push({ role: 'system', content: params.system_prompt });
122
+ }
123
+ if (params.user_prompt) {
124
+ list.push({ role: 'user', content: params.user_prompt });
125
+ }
126
+ return list;
127
+ },
128
+
129
+ /**
130
+ * 通过 kernel.getTools 获取工具并转为 OpenAI function calling 格式
131
+ * @param {object} kernel Kernel 实例
132
+ * @param {Array} tool_names 工具名称数组
133
+ * @returns {Array|null} OpenAI 工具定义数组
134
+ */
135
+ _resolveTools(kernel, tool_names) {
136
+ if (!kernel || typeof kernel.getTools !== 'function') {
137
+ return null;
138
+ }
139
+
140
+ var kernel_tools;
141
+ try {
142
+ kernel_tools = kernel.getTools(tool_names);
143
+ } catch (e) {
144
+ void e;
145
+ return null;
146
+ }
147
+
148
+ if (!kernel_tools || kernel_tools.length === 0) {
149
+ return null;
150
+ }
151
+
152
+ var openai_tools = [];
153
+ for (var i = 0; i < kernel_tools.length; i++) {
154
+ var t = kernel_tools[i];
155
+ openai_tools.push({
156
+ type: 'function',
157
+ function: {
158
+ name: t.name,
159
+ description: t.description,
160
+ parameters: t.params || {}
161
+ }
162
+ });
163
+ }
164
+ return openai_tools;
165
+ },
166
+
167
+ /**
168
+ * 通过 OpenAI SDK 发送请求
169
+ * @param {object} opts 配置项
170
+ * @param {Array} messages 消息数组
171
+ * @param {object} params 步骤参数
172
+ * @returns {Promise<object>} { content, tool_calls, model, usage }
173
+ */
174
+ async _sendRequest(opts, messages, params) {
175
+ var client = new OpenAI({
176
+ apiKey: opts.api_key,
177
+ baseURL: opts.base_url,
178
+ timeout: opts.timeout
179
+ });
180
+
181
+ var req = {
182
+ model: opts.model,
183
+ messages: messages,
184
+ temperature: opts.temperature,
185
+ max_tokens: opts.max_tokens
186
+ };
187
+
188
+ if (params.response_format) {
189
+ req.response_format = params.response_format;
190
+ }
191
+
192
+ if (params.tools && Array.isArray(params.tools) && params.tools.length > 0) {
193
+ req.tools = params.tools;
194
+ }
195
+
196
+ if (params.tool_choice !== undefined) {
197
+ req.tool_choice = params.tool_choice;
198
+ }
199
+
200
+ var completion;
201
+ try {
202
+ completion = await client.chat.completions.create(req);
203
+ } catch (e) {
204
+ throw new Error('LLM 请求失败: ' + e.message, { cause: e });
205
+ }
206
+
207
+ var choice = completion.choices && completion.choices[0];
208
+ if (!choice || !choice.message) {
209
+ throw new Error('LLM 返回内容为空');
210
+ }
211
+
212
+ var response = {
213
+ model: completion.model || opts.model,
214
+ usage: completion.usage || null
215
+ };
216
+
217
+ if (choice.message.content) {
218
+ response.content = choice.message.content;
219
+ }
220
+
221
+ if (choice.message.tool_calls && choice.message.tool_calls.length > 0) {
222
+ response.tool_calls = choice.message.tool_calls;
223
+ }
224
+
225
+ if (!response.content && !response.tool_calls) {
226
+ throw new Error('LLM 返回内容为空');
227
+ }
228
+
229
+ return response;
230
+ }
231
+ };
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
+ };
@@ -0,0 +1,16 @@
1
+ 'use strict';
2
+ const fs = require('fs');
3
+ const path = require('path');
4
+ /** 模块能力层:懒加载工具实现,避免模块入口被 require 时加载全部实现 */
5
+ module.exports = new Proxy({}, {
6
+ get(_t, key) {
7
+ if (typeof key !== 'string') return undefined;
8
+ if (!fs.existsSync(path.join(__dirname, key, 'index.js'))) return undefined;
9
+ return require('./' + key);
10
+ },
11
+ has(_t, key) {
12
+ return typeof key === 'string' && fs.existsSync(path.join(__dirname, key, 'index.js'));
13
+ },
14
+ ownKeys() { return fs.readdirSync(__dirname, { withFileTypes: true }).filter(e => e.isDirectory()).map(e => e.name); },
15
+ getOwnPropertyDescriptor(_t, key) { return this.has(_t, key) ? { enumerable: true, configurable: true } : undefined; }
16
+ });
@@ -0,0 +1,7 @@
1
+ 'use strict';
2
+ /**
3
+ * llm —— 工具契约适配层
4
+ * 业务实现归位于模块 lib/llm_tool_logic.js,本文件只把模块能力暴露给工具加载器。
5
+ * 其它模块可直接 require('mm_llm').llmToolLogic 复用,无需经过工具注册面。
6
+ */
7
+ module.exports = require('../../llm_tool_logic.js');
package/package.json CHANGED
@@ -1,6 +1,6 @@
1
1
  {
2
2
  "name": "mm_llm",
3
- "version": "1.0.1",
3
+ "version": "1.3.0",
4
4
  "description": "这是一个适用于多用户、多智能体、多技能、多工具的模块,方便打造多智能体系统。",
5
5
  "keywords": [
6
6
  "mm_mod",
@@ -13,15 +13,16 @@
13
13
  "multi-agent",
14
14
  "tool-calling"
15
15
  ],
16
- "layer": "adapter",
17
- "group": "out_port",
16
+ "layer": "infrastructure",
17
+ "group": "adapters",
18
18
  "license": "ISC",
19
19
  "author": "qww",
20
20
  "type": "commonjs",
21
21
  "main": "index.js",
22
22
  "files": [
23
23
  "index.js",
24
- "lib"
24
+ "lib",
25
+ "tool"
25
26
  ],
26
27
  "engines": {
27
28
  "node": ">=22.5.0"
@@ -39,7 +40,9 @@
39
40
  "test": "node test.js"
40
41
  },
41
42
  "dependencies": {
42
- "mm_corejs": "^1.1.6",
43
+ "mm_admin": "^1.1.0",
44
+ "mm_machine": "^6.0.0",
45
+ "mm_config": "^2.6.0",
43
46
  "openai": "^7.23.0"
44
47
  }
45
48
  }
@@ -0,0 +1,7 @@
1
+ 'use strict';
2
+ /**
3
+ * llm —— 工具契约适配层
4
+ * 业务实现归位于模块 lib/tools/llm/,本文件只把模块能力暴露给加载器。
5
+ * 其它模块可直接 require('mm_llm').tools.llm 复用。
6
+ */
7
+ module.exports = require('../../lib/tools/llm');
@@ -0,0 +1,153 @@
1
+ {
2
+ "name": "llm",
3
+ "handler": "executor",
4
+ "title": "LLM 调用器",
5
+ "description": "单次调用 OpenAI 兼容 LLM 支持 function calling。适用场景:文本生成或 JSON。工作流提示:system_prompt/messages 传提示词,model 指定模型,response_format 强制 JSON,结果在 content/tool_calls,多轮编排用 ai 模块。",
6
+ "type": "data",
7
+ "params": {
8
+ "type": "object",
9
+ "properties": {
10
+ "system_prompt": {
11
+ "type": "string",
12
+ "description": "系统提示词,定义 AI 的角色和行为规则"
13
+ },
14
+ "user_prompt": {
15
+ "type": "string",
16
+ "description": "用户提示词,包含具体问题和上下文"
17
+ },
18
+ "messages": {
19
+ "type": "array",
20
+ "description": "消息数组 [{role:'system'|'user'|'assistant', content:'...'}],优先级高于 system_prompt/user_prompt"
21
+ },
22
+ "llm": {
23
+ "type": "string",
24
+ "description": "LLM 模块名称,默认 default",
25
+ "default": "default"
26
+ },
27
+ "api_key": {
28
+ "type": "string",
29
+ "description": "API 密钥,优先使用此值,否则从 options 读取"
30
+ },
31
+ "base_url": {
32
+ "type": "string",
33
+ "description": "API 基础地址,如 https://api.openai.com/v1"
34
+ },
35
+ "model": {
36
+ "type": "string",
37
+ "description": "模型名称,如 gpt-4o"
38
+ },
39
+ "temperature": {
40
+ "type": "number",
41
+ "description": "生成温度 (0-2),值越低越确定",
42
+ "min": 0,
43
+ "max": 2
44
+ },
45
+ "max_tokens": {
46
+ "type": "integer",
47
+ "description": "最大生成 token 数"
48
+ },
49
+ "response_format": {
50
+ "type": "object",
51
+ "description": "响应格式,如 {type:'json_object'} 强制 JSON 输出"
52
+ },
53
+ "tools": {
54
+ "type": "array",
55
+ "description": "OpenAI 工具定义数组,格式 [{type:'function', function:{name, description, parameters}}]"
56
+ },
57
+ "tool_names": {
58
+ "type": "array",
59
+ "description": "工具名称数组,通过 kernel.getTools 自动获取工具定义并转换为 OpenAI 格式"
60
+ },
61
+ "tool_choice": {
62
+ "description": "工具选择策略: 'auto' | 'none' | 'required' | {type:'function', function:{name}}"
63
+ }
64
+ },
65
+ "required": []
66
+ },
67
+ "returns": {
68
+ "type": "object",
69
+ "description": "返回 LLM 调用结果,成功时包含响应内容、模型和用量,失败时包含 error",
70
+ "properties": {
71
+ "msg": {
72
+ "type": "string",
73
+ "description": "LLM 调用成功/LLM 调用失败"
74
+ },
75
+ "content": {
76
+ "type": "string",
77
+ "description": "LLM 响应文本内容"
78
+ },
79
+ "tool_calls": {
80
+ "type": "array",
81
+ "description": "LLM 返回的工具调用列表,每个元素含 id、type、function(name, arguments)"
82
+ },
83
+ "usage": {
84
+ "type": "object",
85
+ "description": "Token 用量统计",
86
+ "properties": {
87
+ "prompt_tokens": {
88
+ "type": "integer",
89
+ "description": "输入 token 数"
90
+ },
91
+ "completion_tokens": {
92
+ "type": "integer",
93
+ "description": "输出 token 数"
94
+ },
95
+ "total_tokens": {
96
+ "type": "integer",
97
+ "description": "总 token 数"
98
+ }
99
+ }
100
+ },
101
+ "error": {
102
+ "type": "object",
103
+ "description": "错误信息,仅调用出错时返回",
104
+ "properties": {
105
+ "code": {
106
+ "type": "string",
107
+ "description": "错误码 (EXEC_ 前缀)"
108
+ },
109
+ "message": {
110
+ "type": "string",
111
+ "description": "错误描述"
112
+ }
113
+ }
114
+ }
115
+ }
116
+ },
117
+ "options": {
118
+ "api_key": {
119
+ "type": "string",
120
+ "description": "API 密钥"
121
+ },
122
+ "base_url": {
123
+ "type": "string",
124
+ "description": "API 基础地址",
125
+ "default": "https://api.openai.com/v1"
126
+ },
127
+ "model": {
128
+ "type": "string",
129
+ "description": "模型名称",
130
+ "default": "gpt-4o"
131
+ },
132
+ "temperature": {
133
+ "type": "number",
134
+ "description": "生成温度 (0-2)",
135
+ "default": 0.2,
136
+ "min": 0,
137
+ "max": 2
138
+ },
139
+ "max_tokens": {
140
+ "type": "integer",
141
+ "description": "最大生成 token 数",
142
+ "default": 4096
143
+ },
144
+ "timeout": {
145
+ "type": "integer",
146
+ "description": "请求超时(毫秒)",
147
+ "default": 60000,
148
+ "min": 1000,
149
+ "max": 300000
150
+ }
151
+ },
152
+ "group": "ai"
153
+ }