mm_llm 1.1.0 → 1.3.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.
@@ -0,0 +1,489 @@
1
+ const { llmAdmin } = require('../../index.js');
2
+ /**
3
+ * LLM 调用器
4
+ * 基于 OpenAI SDK 直接调用 LLM 接口
5
+ * 单次调用返回原始响应(含 tool_calls)
6
+ * 如需工具执行和多轮对话,请使用 ai 模块
7
+ */
8
+ const { configAdmin } = require('mm_config');
9
+
10
+ const { OpenAI } = require('openai');
11
+
12
+ const api = {
13
+ /**
14
+ * 调用 LLM 接口
15
+ * @param {object} ctx 上下文对象,结果写入 ctx.output
16
+ * @param {object} params 步骤参数
17
+ * - system_prompt {string} 系统提示词
18
+ * - user_prompt {string} 用户提示词(含上下文)
19
+ * - messages {Array} 消息数组,优先级高于 system_prompt/user_prompt
20
+ * - llm {string} LLM 模块名称,默认 default
21
+ * - api_key {string} API 密钥
22
+ * - base_url {string} API 基础地址
23
+ * - model {string} 模型名称
24
+ * - temperature {number} 生成温度
25
+ * - max_tokens {number} 最大 token 数
26
+ * - response_format {object} 响应格式
27
+ * - tools {Array} 工具定义数组,用于 function calling
28
+ * - tool_names {Array} 工具名称数组,通过 kernel.getTools 获取工具定义
29
+ * - tool_choice {string|object} 工具选择策略
30
+ * @returns {object} { msg, content, tool_calls, model, usage }
31
+ */
32
+ async main(params, ctx, options) {
33
+ const self = this || api;
34
+ const admin = llmAdmin((params && params.scope) || 'os');
35
+ var p = params || {};
36
+ if (p.action && p.action !== 'chat') {
37
+ return api._runAction.call(self, p.action, p, ctx);
38
+ }
39
+ var opts = await (self._getOptions || api._getOptions).call(self, params);
40
+
41
+ if (!opts.api_key) {
42
+ ctx.output = {
43
+ error: {
44
+ code: 'EXEC_MISSING_API_KEY',
45
+ message: '缺少 API Key,请在 options 或 params 中配置 api_key'
46
+ }
47
+ };
48
+ return ctx.output;
49
+ }
50
+
51
+ var messages = (self._buildMessages || api._buildMessages).call(self, params);
52
+ if (messages.length === 0) {
53
+ ctx.output = {
54
+ error: {
55
+ code: 'EXEC_MISSING_PROMPT',
56
+ message: '缺少提示词,请提供 system_prompt 和 user_prompt,或 messages 数组'
57
+ }
58
+ };
59
+ return ctx.output;
60
+ }
61
+
62
+ // tool_names: 从 kernel 获取工具定义并转换为 OpenAI 格式
63
+ var req_params = params;
64
+ if (!params.tools && params.tool_names && Array.isArray(params.tool_names)) {
65
+ var kernel = typeof self.getParent === 'function' ? self.getParent() : null;
66
+ var resolved_tools = (self._resolveTools || api._resolveTools).call(self, kernel, params.tool_names);
67
+ if (resolved_tools) {
68
+ req_params = Object.assign({}, params, { tools: resolved_tools });
69
+ }
70
+ }
71
+
72
+ var result = await (self._sendRequest || api._sendRequest).call(self, opts, messages, req_params);
73
+
74
+ ctx.output = {
75
+ msg: 'LLM 调用成功',
76
+ content: result.content || null,
77
+ tool_calls: result.tool_calls || null,
78
+ model: result.model,
79
+ usage: result.usage
80
+ };
81
+ return ctx.output;
82
+ },
83
+
84
+ /**
85
+ * 合并 options 和 params 获取最终配置
86
+ * @param {object} params 步骤参数
87
+ * @returns {object} 合并后的配置
88
+ */
89
+ async _getOptions(params) {
90
+ var cfg = null;
91
+ try {
92
+ var llm_cfg = await configAdmin('os').get('llm');
93
+ cfg = (llm_cfg && llm_cfg[params.llm || 'default']) || null;
94
+ } catch (e) {
95
+ void e;
96
+ }
97
+ if (!cfg && typeof this.getOptions === 'function') {
98
+ cfg = this.getOptions();
99
+ }
100
+ cfg = cfg || {};
101
+
102
+ return {
103
+ api_key: params.api_key || cfg.api_key || '',
104
+ base_url: params.base_url || cfg.base_url || 'https://api.openai.com/v1',
105
+ model: params.model || cfg.model || 'gpt-4o',
106
+ temperature:
107
+ params.temperature !== undefined
108
+ ? params.temperature
109
+ : cfg.temperature !== undefined
110
+ ? cfg.temperature
111
+ : 0.2,
112
+ max_tokens: params.max_tokens || cfg.max_tokens || 4096,
113
+ timeout: cfg.timeout || 60000
114
+ };
115
+ },
116
+
117
+ /**
118
+ * 构建消息数组
119
+ * @param {object} params 步骤参数
120
+ * @returns {Array} 消息数组 [{role, content}]
121
+ */
122
+ _buildMessages(params) {
123
+ if (params.messages && Array.isArray(params.messages) && params.messages.length > 0) {
124
+ return params.messages;
125
+ }
126
+
127
+ var list = [];
128
+ if (params.system_prompt) {
129
+ list.push({ role: 'system', content: params.system_prompt });
130
+ }
131
+ if (params.user_prompt) {
132
+ list.push({ role: 'user', content: params.user_prompt });
133
+ }
134
+ return list;
135
+ },
136
+
137
+ /**
138
+ * 通过 kernel.getTools 获取工具并转为 OpenAI function calling 格式
139
+ * @param {object} kernel Kernel 实例
140
+ * @param {Array} tool_names 工具名称数组
141
+ * @returns {Array|null} OpenAI 工具定义数组
142
+ */
143
+ _resolveTools(kernel, tool_names) {
144
+ if (!kernel || typeof kernel.getTools !== 'function') {
145
+ return null;
146
+ }
147
+
148
+ var kernel_tools;
149
+ try {
150
+ kernel_tools = kernel.getTools(tool_names);
151
+ } catch (e) {
152
+ void e;
153
+ return null;
154
+ }
155
+
156
+ if (!kernel_tools || kernel_tools.length === 0) {
157
+ return null;
158
+ }
159
+
160
+ var openai_tools = [];
161
+ for (var i = 0; i < kernel_tools.length; i++) {
162
+ var t = kernel_tools[i];
163
+ openai_tools.push({
164
+ type: 'function',
165
+ function: {
166
+ name: t.name,
167
+ description: t.description,
168
+ parameters: t.params || {}
169
+ }
170
+ });
171
+ }
172
+ return openai_tools;
173
+ },
174
+
175
+ /**
176
+ * 通过 OpenAI SDK 发送请求
177
+ * @param {object} opts 配置项
178
+ * @param {Array} messages 消息数组
179
+ * @param {object} params 步骤参数
180
+ * @returns {Promise<object>} { content, tool_calls, model, usage }
181
+ */
182
+ async _sendRequest(opts, messages, params) {
183
+ var client = new OpenAI({
184
+ apiKey: opts.api_key,
185
+ baseURL: opts.base_url,
186
+ timeout: opts.timeout
187
+ });
188
+
189
+ var req = {
190
+ model: opts.model,
191
+ messages: messages,
192
+ temperature: opts.temperature,
193
+ max_tokens: opts.max_tokens
194
+ };
195
+
196
+ if (params.response_format) {
197
+ req.response_format = params.response_format;
198
+ }
199
+
200
+ if (params.tools && Array.isArray(params.tools) && params.tools.length > 0) {
201
+ req.tools = params.tools;
202
+ }
203
+
204
+ if (params.tool_choice !== undefined) {
205
+ req.tool_choice = params.tool_choice;
206
+ }
207
+
208
+ var completion;
209
+ try {
210
+ completion = await client.chat.completions.create(req);
211
+ } catch (e) {
212
+ throw new Error('LLM 请求失败: ' + e.message, { cause: e });
213
+ }
214
+
215
+ var choice = completion.choices && completion.choices[0];
216
+ if (!choice || !choice.message) {
217
+ throw new Error('LLM 返回内容为空');
218
+ }
219
+
220
+ var response = {
221
+ model: completion.model || opts.model,
222
+ usage: completion.usage || null
223
+ };
224
+
225
+ if (choice.message.content) {
226
+ response.content = choice.message.content;
227
+ }
228
+
229
+ if (choice.message.tool_calls && choice.message.tool_calls.length > 0) {
230
+ response.tool_calls = choice.message.tool_calls;
231
+ }
232
+
233
+ if (!response.content && !response.tool_calls) {
234
+ throw new Error('LLM 返回内容为空');
235
+ }
236
+
237
+ return response;
238
+ },
239
+
240
+ /**
241
+ * 执行 LLM 实例管理动作(发送、会话、选项、配额、工具注册)
242
+ * @param {string} action 动作名称
243
+ * @param {object} p 步骤参数
244
+ * @param {object} ctx 上下文对象,结果写入 ctx.output
245
+ * @returns {Promise<object>} 操作结果
246
+ */
247
+ async _runAction(action, p, ctx) {
248
+ var self = this || api;
249
+ var done = function (data) {
250
+ ctx.output = data;
251
+ return data;
252
+ };
253
+
254
+ try {
255
+ var llm = api._resolveLLM.call(self, p);
256
+
257
+ if (action === 'send') {
258
+ _assertSession(p);
259
+ var response = await llm.send(p.user_id, p.agent_id, p.session_id);
260
+ return done({
261
+ code: 0,
262
+ msg: '发送完成',
263
+ user_id: p.user_id,
264
+ agent_id: p.agent_id,
265
+ session_id: p.session_id,
266
+ result: response
267
+ });
268
+ }
269
+
270
+ if (action === 'reset_session') {
271
+ _assertSession(p);
272
+ var reset = await llm.resetSession(p.user_id, p.agent_id, p.session_id);
273
+ return done({
274
+ code: 0,
275
+ msg: reset ? '会话已重置' : '会话不存在',
276
+ user_id: p.user_id,
277
+ agent_id: p.agent_id,
278
+ session_id: p.session_id,
279
+ reset: !!reset
280
+ });
281
+ }
282
+
283
+ if (action === 'history') {
284
+ _assertSession(p);
285
+ var messages = await llm.getMessages(p.user_id, p.agent_id, p.session_id);
286
+ return done({
287
+ code: 0,
288
+ msg: '成功',
289
+ user_id: p.user_id,
290
+ agent_id: p.agent_id,
291
+ session_id: p.session_id,
292
+ count: messages.length,
293
+ messages: messages
294
+ });
295
+ }
296
+
297
+ if (action === 'options') {
298
+ _assertPair(p);
299
+ var options = await llm.getOptions(p.user_id, p.agent_id);
300
+ if (options && options.api_key) {
301
+ options = Object.assign({}, options, { api_key: '***' });
302
+ }
303
+ return done({ code: 0, msg: '成功', user_id: p.user_id, agent_id: p.agent_id, options: options });
304
+ }
305
+
306
+ if (action === 'quota') {
307
+ if (p.user_id === undefined || p.user_id === null) throw new TypeError('user_id 不能为空');
308
+ var store = llm.getStore();
309
+ var quota = store && typeof store.getQuota === 'function' ? store.getQuota(p.user_id) : undefined;
310
+ var allowed = await llm.checkQuota(p.user_id, p.agent_id);
311
+ return done({
312
+ code: 0,
313
+ msg: '成功',
314
+ user_id: p.user_id,
315
+ agent_id: p.agent_id,
316
+ quota: quota === undefined ? null : quota,
317
+ allowed: !!allowed
318
+ });
319
+ }
320
+
321
+ if (action === 'list_tools') {
322
+ var tools = llm.listTools();
323
+ var store_tools = llm.getStore();
324
+ var schemas = store_tools && typeof store_tools.listToolSchemas === 'function' ? store_tools.listToolSchemas() : [];
325
+ var tool_result = { code: 0, msg: '成功', count: tools.length, tools: tools, schemas: schemas };
326
+ if (p.agent_id !== undefined && p.agent_id !== null) {
327
+ tool_result.agent_tools = llm.getTools(p.agent_id);
328
+ }
329
+ return done(tool_result);
330
+ }
331
+
332
+ if (action === 'set_tool') {
333
+ var tool_name = p.name || p.tool;
334
+ if (!tool_name) throw new TypeError('name 不能为空');
335
+ var target = p.tool || tool_name;
336
+ var kernel = typeof self.getParent === 'function' ? self.getParent() : null;
337
+ var kernel_mod = api._findKernelMod.call(self, kernel, target);
338
+ if (!kernel_mod || typeof kernel_mod.main !== 'function') {
339
+ throw new Error('内核中未找到工具: ' + target);
340
+ }
341
+ llm.setTool(tool_name, async function (args) {
342
+ var temp_ctx = { output: null };
343
+ await kernel_mod.main.call(kernel_mod, temp_ctx, args || {});
344
+ return temp_ctx.output;
345
+ });
346
+ var schema = api._buildToolSchema.call(self, tool_name, p);
347
+ var schema_store = llm.getStore();
348
+ var schema_saved = false;
349
+ if (schema && schema_store && typeof schema_store.setToolSchema === 'function') {
350
+ schema_store.setToolSchema(tool_name, schema);
351
+ schema_saved = true;
352
+ }
353
+ return done({
354
+ code: 0,
355
+ msg: '工具注册完成',
356
+ tool: tool_name,
357
+ kernel_tool: target,
358
+ schema_saved: schema_saved
359
+ });
360
+ }
361
+
362
+ if (action === 'del_tool') {
363
+ if (!p.name) throw new TypeError('name 不能为空');
364
+ var removed = llm.delTool(p.name);
365
+ var del_store = llm.getStore();
366
+ var removed_schema = del_store && typeof del_store.delToolSchema === 'function' ? del_store.delToolSchema(p.name) : false;
367
+ return done({
368
+ code: 0,
369
+ msg: removed || removed_schema ? '工具已删除' : '工具不存在',
370
+ tool: p.name,
371
+ removed: !!removed,
372
+ removed_schema: !!removed_schema
373
+ });
374
+ }
375
+
376
+ if (action === 'clear_tools') {
377
+ llm.clearTools();
378
+ var clear_store = llm.getStore();
379
+ var cleared_schema = false;
380
+ if (clear_store && typeof clear_store.clearToolSchemas === 'function') {
381
+ clear_store.clearToolSchemas();
382
+ cleared_schema = true;
383
+ }
384
+ return done({ code: 0, msg: '工具已清空', cleared_schema: cleared_schema });
385
+ }
386
+
387
+ return done({ code: 10001, msg: '不支持的操作: ' + action });
388
+ } catch (error) {
389
+ return done({
390
+ code: 10000,
391
+ msg: '执行失败:' + error.message,
392
+ error: { name: error.name, message: error.message }
393
+ });
394
+ }
395
+ },
396
+
397
+ /**
398
+ * 获取指定名称的 LLM 实例
399
+ * @param {object} p 步骤参数
400
+ * @returns {object} LLM 实例
401
+ */
402
+ _resolveLLM(p) {
403
+ var mgr = llmAdmin(p.scope || 'os');
404
+ var name = p.llm || 'default';
405
+ var llm = mgr.get(name);
406
+ if (!llm) {
407
+ var rows = typeof mgr.list === 'function' ? mgr.list() : [];
408
+ var names = [];
409
+ for (var i = 0; i < rows.length; i++) {
410
+ if (rows[i] && rows[i].config) names.push(rows[i].config.name);
411
+ }
412
+ throw new Error('LLM 实例不存在: ' + name + (names.length ? ',可用实例: ' + names.join('、') : ''));
413
+ }
414
+ return llm;
415
+ },
416
+
417
+ /**
418
+ * 在内核中按名称查找工具模块
419
+ * @param {object} kernel Kernel 实例
420
+ * @param {string} name 工具名称
421
+ * @returns {object|null} 工具模块
422
+ */
423
+ _findKernelMod(kernel, name) {
424
+ if (!kernel || !kernel.operators) return null;
425
+ for (var op_name in kernel.operators) {
426
+ var mod = kernel.operators[op_name][name];
427
+ if (mod) return mod;
428
+ }
429
+ return null;
430
+ },
431
+
432
+ /**
433
+ * 构建工具定义(优先取显式 schema,其次按 description/parameters 组装)
434
+ * @param {string} name 工具名称
435
+ * @param {object} p 步骤参数
436
+ * @returns {object|null} 工具定义
437
+ */
438
+ _buildToolSchema(name, p) {
439
+ if (p.schema) return p.schema;
440
+ if (!p.description && !p.parameters) return null;
441
+ return {
442
+ type: 'function',
443
+ function: {
444
+ name: name,
445
+ description: p.description || '',
446
+ parameters: p.parameters || { type: 'object', properties: {} }
447
+ }
448
+ };
449
+ }
450
+ };
451
+
452
+ module.exports = {
453
+ main(params, ctx, options) {
454
+ return api.main.call(this || api, params, ctx, options);
455
+ },
456
+ _buildMessages(params) {
457
+ return api._buildMessages.call(this || api, params);
458
+ },
459
+ _getOptions(params) {
460
+ return api._getOptions.call(this || api, params);
461
+ },
462
+ _resolveTools(kernel, tool_names) {
463
+ return api._resolveTools.call(this || api, kernel, tool_names);
464
+ },
465
+ _sendRequest(opts, messages, params) {
466
+ return api._sendRequest.call(this || api, opts, messages, params);
467
+ }
468
+ };
469
+
470
+ /**
471
+ * 校验会话三元组参数
472
+ * @param {object} p 步骤参数
473
+ * @returns {void}
474
+ */
475
+ function _assertSession(p) {
476
+ if (p.user_id === undefined || p.user_id === null) throw new TypeError('user_id 不能为空');
477
+ if (p.agent_id === undefined || p.agent_id === null) throw new TypeError('agent_id 不能为空');
478
+ if (p.session_id === undefined || p.session_id === null) throw new TypeError('session_id 不能为空');
479
+ }
480
+
481
+ /**
482
+ * 校验用户与智能体参数
483
+ * @param {object} p 步骤参数
484
+ * @returns {void}
485
+ */
486
+ function _assertPair(p) {
487
+ if (p.user_id === undefined || p.user_id === null) throw new TypeError('user_id 不能为空');
488
+ if (p.agent_id === undefined || p.agent_id === null) throw new TypeError('agent_id 不能为空');
489
+ }