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.
@@ -1,231 +0,0 @@
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
- };
@@ -1,16 +0,0 @@
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
- });
@@ -1,7 +0,0 @@
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');