mm_llm 1.3.0 → 1.3.2
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 +6 -33
- package/lib/admin.js +23 -0
- package/lib/llm.js +228 -639
- package/lib/llm_execute.js +216 -0
- package/lib/llm_message.js +224 -0
- package/lib/manager.js +1 -1
- package/lib/memory.js +1 -1
- package/lib/sqlite.js +9 -475
- package/lib/sqlite_config.js +197 -0
- package/lib/sqlite_quota.js +123 -0
- package/lib/sqlite_session.js +189 -0
- package/package.json +7 -7
- package/tool/llm/index.js +487 -5
- package/tool/llm/tool.json +135 -3
- package/tool/llm_admin/index.js +36 -0
- package/tool/llm_admin/tool.json +11 -0
- package/lib/llm_tool_logic.js +0 -231
- package/lib/tools/index.js +0 -16
- package/lib/tools/llm/index.js +0 -7
package/tool/llm/index.js
CHANGED
|
@@ -1,7 +1,489 @@
|
|
|
1
|
-
|
|
1
|
+
const { llmAdmin } = require('../../index.js');
|
|
2
2
|
/**
|
|
3
|
-
*
|
|
4
|
-
*
|
|
5
|
-
*
|
|
3
|
+
* LLM 调用器
|
|
4
|
+
* 基于 OpenAI SDK 直接调用 LLM 接口
|
|
5
|
+
* 单次调用返回原始响应(含 tool_calls)
|
|
6
|
+
* 如需工具执行和多轮对话,请使用 ai 模块
|
|
6
7
|
*/
|
|
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
|
+
}
|
package/tool/llm/tool.json
CHANGED
|
@@ -2,11 +2,64 @@
|
|
|
2
2
|
"name": "llm",
|
|
3
3
|
"handler": "executor",
|
|
4
4
|
"title": "LLM 调用器",
|
|
5
|
-
"description": "
|
|
5
|
+
"description": "LLM 网关:不传 action(或 action=chat)时单次调用 OpenAI 兼容接口并支持 function calling;传入 action 时操作 mm_llm 管理器实例,支持按会话发送、重置会话、查询会话消息/调用选项/用户配额,以及注册、删除、清空与列举已注册工具。适用场景:文本生成或 JSON、多轮会话维护、LLM 工具注册与配额核查。",
|
|
6
6
|
"type": "data",
|
|
7
7
|
"params": {
|
|
8
8
|
"type": "object",
|
|
9
9
|
"properties": {
|
|
10
|
+
"action": {
|
|
11
|
+
"type": "string",
|
|
12
|
+
"description": "操作类型: chat(单次直连对话,缺省)/send(按会话发送)/reset_session(重置会话)/history(查询会话消息)/options(查询调用选项)/quota(查询配额)/list_tools(列举已注册工具)/set_tool(注册工具)/del_tool(删除工具)/clear_tools(清空工具)",
|
|
13
|
+
"enum": [
|
|
14
|
+
"chat",
|
|
15
|
+
"send",
|
|
16
|
+
"reset_session",
|
|
17
|
+
"history",
|
|
18
|
+
"options",
|
|
19
|
+
"quota",
|
|
20
|
+
"list_tools",
|
|
21
|
+
"set_tool",
|
|
22
|
+
"del_tool",
|
|
23
|
+
"clear_tools"
|
|
24
|
+
]
|
|
25
|
+
},
|
|
26
|
+
"scope": {
|
|
27
|
+
"type": "string",
|
|
28
|
+
"description": "llm 管理器实例作用域,缺省 os",
|
|
29
|
+
"default": "os"
|
|
30
|
+
},
|
|
31
|
+
"user_id": {
|
|
32
|
+
"type": "integer",
|
|
33
|
+
"description": "用户ID(send/reset_session/history/options/quota 必填)"
|
|
34
|
+
},
|
|
35
|
+
"agent_id": {
|
|
36
|
+
"type": "integer",
|
|
37
|
+
"description": "智能体ID(send/reset_session/history/options 必填;quota 可选;list_tools 可选)"
|
|
38
|
+
},
|
|
39
|
+
"session_id": {
|
|
40
|
+
"type": "integer",
|
|
41
|
+
"description": "会话ID(send/reset_session/history 必填)"
|
|
42
|
+
},
|
|
43
|
+
"name": {
|
|
44
|
+
"type": "string",
|
|
45
|
+
"description": "LLM 工具名称(set_tool/del_tool 必填)"
|
|
46
|
+
},
|
|
47
|
+
"tool": {
|
|
48
|
+
"type": "string",
|
|
49
|
+
"description": "内核工具名称(set_tool 可选,缺省与 name 相同;注册的执行函数转调该内核工具)"
|
|
50
|
+
},
|
|
51
|
+
"description": {
|
|
52
|
+
"type": "string",
|
|
53
|
+
"description": "工具描述(set_tool 可选,用于生成工具定义)"
|
|
54
|
+
},
|
|
55
|
+
"parameters": {
|
|
56
|
+
"type": "object",
|
|
57
|
+
"description": "工具参数 JSON Schema(set_tool 可选)"
|
|
58
|
+
},
|
|
59
|
+
"schema": {
|
|
60
|
+
"type": "object",
|
|
61
|
+
"description": "完整工具定义(set_tool 可选,优先于 description/parameters)"
|
|
62
|
+
},
|
|
10
63
|
"system_prompt": {
|
|
11
64
|
"type": "string",
|
|
12
65
|
"description": "系统提示词,定义 AI 的角色和行为规则"
|
|
@@ -66,11 +119,90 @@
|
|
|
66
119
|
},
|
|
67
120
|
"returns": {
|
|
68
121
|
"type": "object",
|
|
69
|
-
"description": "
|
|
122
|
+
"description": "对话模式返回响应内容、模型和用量;管理动作模式返回 code/msg 及动作结果,失败时包含 error",
|
|
70
123
|
"properties": {
|
|
124
|
+
"code": {
|
|
125
|
+
"type": "number",
|
|
126
|
+
"description": "0 表示成功,1xxxx 表示业务/执行异常(管理动作模式)"
|
|
127
|
+
},
|
|
71
128
|
"msg": {
|
|
72
129
|
"type": "string",
|
|
73
|
-
"description": "LLM
|
|
130
|
+
"description": "LLM 调用成功/失败或动作结果描述"
|
|
131
|
+
},
|
|
132
|
+
"user_id": {
|
|
133
|
+
"type": "integer",
|
|
134
|
+
"description": "用户ID"
|
|
135
|
+
},
|
|
136
|
+
"agent_id": {
|
|
137
|
+
"type": "integer",
|
|
138
|
+
"description": "智能体ID"
|
|
139
|
+
},
|
|
140
|
+
"session_id": {
|
|
141
|
+
"type": "integer",
|
|
142
|
+
"description": "会话ID"
|
|
143
|
+
},
|
|
144
|
+
"result": {
|
|
145
|
+
"description": "send 动作的助手消息或流对象"
|
|
146
|
+
},
|
|
147
|
+
"reset": {
|
|
148
|
+
"type": "boolean",
|
|
149
|
+
"description": "是否重置了已存在的会话"
|
|
150
|
+
},
|
|
151
|
+
"count": {
|
|
152
|
+
"type": "number",
|
|
153
|
+
"description": "消息或已注册工具数量"
|
|
154
|
+
},
|
|
155
|
+
"messages": {
|
|
156
|
+
"type": "array",
|
|
157
|
+
"description": "会话消息上下文(history)"
|
|
158
|
+
},
|
|
159
|
+
"options": {
|
|
160
|
+
"type": "object",
|
|
161
|
+
"description": "调用选项(options)"
|
|
162
|
+
},
|
|
163
|
+
"quota": {
|
|
164
|
+
"type": "number",
|
|
165
|
+
"description": "剩余配额,未设置时为 null(quota)"
|
|
166
|
+
},
|
|
167
|
+
"allowed": {
|
|
168
|
+
"type": "boolean",
|
|
169
|
+
"description": "是否通过配额准入(quota)"
|
|
170
|
+
},
|
|
171
|
+
"tools": {
|
|
172
|
+
"type": "array",
|
|
173
|
+
"description": "已注册工具实现名称(list_tools)"
|
|
174
|
+
},
|
|
175
|
+
"schemas": {
|
|
176
|
+
"type": "array",
|
|
177
|
+
"description": "已定义工具名(list_tools)"
|
|
178
|
+
},
|
|
179
|
+
"agent_tools": {
|
|
180
|
+
"type": "array",
|
|
181
|
+
"description": "指定智能体引用的工具定义(list_tools)"
|
|
182
|
+
},
|
|
183
|
+
"tool": {
|
|
184
|
+
"type": "string",
|
|
185
|
+
"description": "工具名称(set_tool/del_tool)"
|
|
186
|
+
},
|
|
187
|
+
"kernel_tool": {
|
|
188
|
+
"type": "string",
|
|
189
|
+
"description": "绑定的内核工具名称(set_tool)"
|
|
190
|
+
},
|
|
191
|
+
"schema_saved": {
|
|
192
|
+
"type": "boolean",
|
|
193
|
+
"description": "是否写入工具定义(set_tool)"
|
|
194
|
+
},
|
|
195
|
+
"removed": {
|
|
196
|
+
"type": "boolean",
|
|
197
|
+
"description": "是否删除工具实现(del_tool)"
|
|
198
|
+
},
|
|
199
|
+
"removed_schema": {
|
|
200
|
+
"type": "boolean",
|
|
201
|
+
"description": "是否删除工具定义(del_tool)"
|
|
202
|
+
},
|
|
203
|
+
"cleared_schema": {
|
|
204
|
+
"type": "boolean",
|
|
205
|
+
"description": "是否清空工具定义(clear_tools)"
|
|
74
206
|
},
|
|
75
207
|
"content": {
|
|
76
208
|
"type": "string",
|
|
@@ -0,0 +1,36 @@
|
|
|
1
|
+
'use strict';
|
|
2
|
+
const { admin } = require('../../index.js');
|
|
3
|
+
|
|
4
|
+
/**
|
|
5
|
+
* 管理 LLM 实例池
|
|
6
|
+
* @param {object} params 参数:action/scope
|
|
7
|
+
* @param {object} ctx 执行上下文
|
|
8
|
+
* @returns {Promise<object>} 执行结果
|
|
9
|
+
*/
|
|
10
|
+
async function main(params, ctx) {
|
|
11
|
+
const p = params || {};
|
|
12
|
+
const action = p.action || 'list';
|
|
13
|
+
const scope = p.scope || 'os';
|
|
14
|
+
const done = (data) => {
|
|
15
|
+
if (ctx) ctx.output = data;
|
|
16
|
+
return data;
|
|
17
|
+
};
|
|
18
|
+
try {
|
|
19
|
+
if (action === 'list') {
|
|
20
|
+
const scopes = [...admin.keys()];
|
|
21
|
+
return done({ code: 0, msg: '获取成功', action, count: scopes.length, scopes });
|
|
22
|
+
}
|
|
23
|
+
if (action === 'inspect') {
|
|
24
|
+
const exists = admin.has(scope);
|
|
25
|
+
const instance = exists ? admin.get(scope) : null;
|
|
26
|
+
return done({ code: 0, msg: exists ? '获取成功' : '实例不存在', action, scope, exists, config: instance && instance.config ? instance.config : null });
|
|
27
|
+
}
|
|
28
|
+
if (action === 'del') return done({ code: 0, msg: '删除成功', action, scope, ok: admin.del(scope) });
|
|
29
|
+
if (action === 'clear') { admin.clear(); return done({ code: 0, msg: '实例池已清空', action }); }
|
|
30
|
+
return done({ code: 10001, msg: '不支持的操作: ' + action });
|
|
31
|
+
} catch (error) {
|
|
32
|
+
return done({ code: 10000, msg: '执行失败:' + error.message, error: { name: error.name, message: error.message } });
|
|
33
|
+
}
|
|
34
|
+
}
|
|
35
|
+
|
|
36
|
+
module.exports = { main };
|
|
@@ -0,0 +1,11 @@
|
|
|
1
|
+
{
|
|
2
|
+
"name": "llm_admin",
|
|
3
|
+
"title": "LLM 实例管理",
|
|
4
|
+
"description": "管理 mm_llm 的实例池:list 列出实例、inspect 查看配置、del 删除实例、clear 清空实例池。",
|
|
5
|
+
"category": "AI 能力",
|
|
6
|
+
"group": "ai",
|
|
7
|
+
"type": "function",
|
|
8
|
+
"main": "./index.js",
|
|
9
|
+
"params": { "type": "object", "properties": { "action": { "type": "string", "description": "操作类型: list/inspect/del/clear", "default": "list" }, "scope": { "type": "string", "description": "实例作用域,缺省 os", "default": "os" } }, "required": ["action"] },
|
|
10
|
+
"handler": "manager"
|
|
11
|
+
}
|