mm_llm 1.1.0 → 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/index.js CHANGED
@@ -31,3 +31,10 @@ module.exports = {
31
31
  admin,
32
32
  llmAdmin
33
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,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
+ };
@@ -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.1.0",
3
+ "version": "1.3.0",
4
4
  "description": "这是一个适用于多用户、多智能体、多技能、多工具的模块,方便打造多智能体系统。",
5
5
  "keywords": [
6
6
  "mm_mod",
@@ -21,7 +21,8 @@
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"
@@ -41,6 +42,7 @@
41
42
  "dependencies": {
42
43
  "mm_admin": "^1.1.0",
43
44
  "mm_machine": "^6.0.0",
45
+ "mm_config": "^2.6.0",
44
46
  "openai": "^7.23.0"
45
47
  }
46
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
+ }