mm_llm 1.3.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.
- package/index.js +5 -30
- package/lib/admin.js +22 -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 +3 -3
- 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
|
@@ -0,0 +1,216 @@
|
|
|
1
|
+
'use strict';
|
|
2
|
+
|
|
3
|
+
/**
|
|
4
|
+
* 对话执行分区
|
|
5
|
+
* @description 装配 LLM 的准入控制与对话执行方法:鉴权、配额、会话入口、发送与工具调用循环
|
|
6
|
+
*/
|
|
7
|
+
|
|
8
|
+
/**
|
|
9
|
+
* 装配准入控制与对话执行方法到 LLM 原型
|
|
10
|
+
* @param {class} LLM 大模型调用类
|
|
11
|
+
*/
|
|
12
|
+
function install(LLM) {
|
|
13
|
+
/**
|
|
14
|
+
* 在本次调用选项的上下文中完成一轮对话(私有方法)
|
|
15
|
+
*
|
|
16
|
+
* 先做鉴权与准入判定,通过后把用户输入写入存储,委托 send 完成调用,
|
|
17
|
+
* 再把助手回复写回存储。判定不通过则抛出异常,既不发送请求也不写入消息。
|
|
18
|
+
* @param {Object} ctx 调用上下文,含 user_id
|
|
19
|
+
* @param {Object} params 调用参数,含 agent_id / session_id / content
|
|
20
|
+
* @returns {Promise<Object>} 助手消息;stream 为真时返回流对象
|
|
21
|
+
*/
|
|
22
|
+
LLM.prototype._converse = async function (params, ctx, options) {
|
|
23
|
+
let authed = await this.checkAuth(ctx.user_id, params.agent_id);
|
|
24
|
+
if (!authed) {
|
|
25
|
+
throw new Error(`[LLM] 用户 ${ctx.user_id} 无权调用智能体 ${params.agent_id}`);
|
|
26
|
+
}
|
|
27
|
+
let allowed = await this.checkQuota(ctx.user_id, params.agent_id);
|
|
28
|
+
if (!allowed) {
|
|
29
|
+
throw new Error(`[LLM] 用户 ${ctx.user_id} 调用智能体 ${params.agent_id} 被拒绝`);
|
|
30
|
+
}
|
|
31
|
+
if (params.content) {
|
|
32
|
+
this.getStore().addMessage(ctx.user_id, params.agent_id, params.session_id, {
|
|
33
|
+
role: 'user',
|
|
34
|
+
content: params.content
|
|
35
|
+
});
|
|
36
|
+
}
|
|
37
|
+
let response = await this.send(ctx.user_id, params.agent_id, params.session_id);
|
|
38
|
+
this._saveResponse(ctx.user_id, params.agent_id, params.session_id, response);
|
|
39
|
+
return response;
|
|
40
|
+
};
|
|
41
|
+
|
|
42
|
+
/**
|
|
43
|
+
* 发送消息
|
|
44
|
+
*
|
|
45
|
+
* 依次装配消息上下文、工具定义与调用选项,交由 post 完成调用。
|
|
46
|
+
* @param {Number} user_id 用户ID
|
|
47
|
+
* @param {Number} agent_id 智能体ID
|
|
48
|
+
* @param {Number} session_id 会话ID
|
|
49
|
+
* @returns {Promise<Object>} 助手消息;stream 为真时返回流对象
|
|
50
|
+
*/
|
|
51
|
+
LLM.prototype.send = async function (user_id, agent_id, session_id) {
|
|
52
|
+
let messages = await this.getMessages(user_id, agent_id, session_id);
|
|
53
|
+
let tools = this.getTools(agent_id);
|
|
54
|
+
let options = this.getOptions(user_id, agent_id);
|
|
55
|
+
return this.post(messages, tools, options);
|
|
56
|
+
};
|
|
57
|
+
|
|
58
|
+
/**
|
|
59
|
+
* 调用前鉴权
|
|
60
|
+
*
|
|
61
|
+
* 默认按智能体配置判定:智能体的 users 存在时,仅列表内的用户可调用,
|
|
62
|
+
* 空数组表示谁都不能用;users 缺省时由 config.auth_default 决定,
|
|
63
|
+
* 'allow' 放行、'deny' 拒绝。
|
|
64
|
+
* 子类可覆写以实现角色、黑名单等其它鉴权:返回假值即拒绝本次调用,
|
|
65
|
+
* main 会抛出异常,不发送请求也不写入消息;需要自定义拒绝原因时,
|
|
66
|
+
* 可直接在覆写实现中抛出异常。
|
|
67
|
+
* @param {Number} user_id 用户ID
|
|
68
|
+
* @param {Number} agent_id 智能体ID
|
|
69
|
+
* @returns {Promise<Boolean>} 是否允许调用
|
|
70
|
+
*/
|
|
71
|
+
LLM.prototype.checkAuth = async function (user_id, agent_id) {
|
|
72
|
+
let agent = this.getStore().getAgent(agent_id);
|
|
73
|
+
return this._canUseAgent(agent, user_id);
|
|
74
|
+
};
|
|
75
|
+
|
|
76
|
+
/**
|
|
77
|
+
* 调用前准入判定
|
|
78
|
+
*
|
|
79
|
+
* 默认按用户配额判定:从存储读取该用户的剩余配额,大于 0 才放行;
|
|
80
|
+
* 未设置配额(返回 undefined)视为无配额,一并拒绝。
|
|
81
|
+
* 子类可覆写以实现权限、限流等其它控制:返回假值即拒绝本次调用,
|
|
82
|
+
* main 会抛出异常,不发送请求也不写入消息;需要自定义拒绝原因时,
|
|
83
|
+
* 可直接在覆写实现中抛出异常。配额扣减等结算动作由二次开发者自行处理。
|
|
84
|
+
* @param {Number} user_id 用户ID
|
|
85
|
+
* @param {Number} agent_id 智能体ID
|
|
86
|
+
* @returns {Promise<Boolean>} 是否允许调用
|
|
87
|
+
*/
|
|
88
|
+
LLM.prototype.checkQuota = async function (user_id, agent_id) {
|
|
89
|
+
let quota = this.getStore().getQuota(user_id);
|
|
90
|
+
return typeof quota === 'number' && quota > 0;
|
|
91
|
+
};
|
|
92
|
+
|
|
93
|
+
/**
|
|
94
|
+
* 使用工具函数
|
|
95
|
+
*
|
|
96
|
+
* 默认从实例上的工具注册表按名称查找执行函数。
|
|
97
|
+
* @param {String} tool_name 工具函数名称
|
|
98
|
+
* @param {Object} params 工具函数参数
|
|
99
|
+
* @returns {Promise<*>} 工具函数执行结果
|
|
100
|
+
*/
|
|
101
|
+
LLM.prototype.useTool = async function (tool_name, params) {
|
|
102
|
+
let handler = this.getTool(tool_name);
|
|
103
|
+
if (typeof handler !== 'function') {
|
|
104
|
+
throw new Error(`[LLM] 未找到工具 ${tool_name} 的实现`);
|
|
105
|
+
}
|
|
106
|
+
return handler(params);
|
|
107
|
+
};
|
|
108
|
+
|
|
109
|
+
/**
|
|
110
|
+
* 发送消息
|
|
111
|
+
*
|
|
112
|
+
* 默认走 OpenAI 兼容接口,并在模型请求工具时执行工具、回填结果后继续对话,
|
|
113
|
+
* 直到模型不再请求工具或达到 config.max_rounds 上限。
|
|
114
|
+
* @param {Array} messages 消息上下文数组
|
|
115
|
+
* @param {Array} tools 工具函数数组
|
|
116
|
+
* @param {Object} options 选项对象
|
|
117
|
+
* @returns {Promise<Object>} 助手消息;stream 为真时返回流对象
|
|
118
|
+
*/
|
|
119
|
+
LLM.prototype.post = async function (messages, tools, options) {
|
|
120
|
+
let client = this._getClient(options);
|
|
121
|
+
let round = 0;
|
|
122
|
+
for (;;) {
|
|
123
|
+
let request = this._buildRequest(messages, tools, options);
|
|
124
|
+
let response = await client.chat.completions.create(request);
|
|
125
|
+
if (options.stream) return response;
|
|
126
|
+
|
|
127
|
+
let message = response.choices[0].message;
|
|
128
|
+
let tool_calls = message.tool_calls || [];
|
|
129
|
+
if (tool_calls.length === 0 || round >= this.config.max_rounds) {
|
|
130
|
+
return message;
|
|
131
|
+
}
|
|
132
|
+
|
|
133
|
+
await this._appendToolMessages(messages, message, tool_calls);
|
|
134
|
+
round += 1;
|
|
135
|
+
}
|
|
136
|
+
};
|
|
137
|
+
|
|
138
|
+
/**
|
|
139
|
+
* 把助手消息与工具执行结果追加到上下文(私有方法)
|
|
140
|
+
* @param {Array} messages 消息上下文数组
|
|
141
|
+
* @param {Object} message 助手消息
|
|
142
|
+
* @param {Array} tool_calls 工具调用数组
|
|
143
|
+
*/
|
|
144
|
+
LLM.prototype._appendToolMessages = async function (messages, message, tool_calls) {
|
|
145
|
+
messages.push({
|
|
146
|
+
role: 'assistant',
|
|
147
|
+
content: message.content || '',
|
|
148
|
+
tool_calls
|
|
149
|
+
});
|
|
150
|
+
for (let tool_call of tool_calls) {
|
|
151
|
+
let params = this._parseToolParams(tool_call);
|
|
152
|
+
let result = await this.useTool(tool_call.function.name, params);
|
|
153
|
+
messages.push({
|
|
154
|
+
role: 'tool',
|
|
155
|
+
tool_call_id: tool_call.id,
|
|
156
|
+
content: this._stringifyResult(result)
|
|
157
|
+
});
|
|
158
|
+
}
|
|
159
|
+
};
|
|
160
|
+
|
|
161
|
+
/**
|
|
162
|
+
* 解析工具调用参数(私有方法)
|
|
163
|
+
* @param {Object} tool_call 工具调用对象
|
|
164
|
+
* @returns {Object} 参数对象
|
|
165
|
+
* @throws {SyntaxError} 参数 JSON 非法时抛出
|
|
166
|
+
*/
|
|
167
|
+
LLM.prototype._parseToolParams = function (tool_call) {
|
|
168
|
+
return JSON.parse(tool_call.function.arguments || '{}');
|
|
169
|
+
};
|
|
170
|
+
|
|
171
|
+
/**
|
|
172
|
+
* 序列化工具执行结果(私有方法)
|
|
173
|
+
* @param {*} result 工具执行结果
|
|
174
|
+
* @returns {String} 字符串结果
|
|
175
|
+
*/
|
|
176
|
+
LLM.prototype._stringifyResult = function (result) {
|
|
177
|
+
if (typeof result === 'string') return result;
|
|
178
|
+
return JSON.stringify(result === undefined ? null : result);
|
|
179
|
+
};
|
|
180
|
+
|
|
181
|
+
/**
|
|
182
|
+
* 组装请求参数(私有方法)
|
|
183
|
+
* @param {Array} messages 消息上下文数组
|
|
184
|
+
* @param {Array} tools 工具函数数组
|
|
185
|
+
* @param {Object} options 选项对象
|
|
186
|
+
* @returns {Object} 请求参数
|
|
187
|
+
*/
|
|
188
|
+
LLM.prototype._buildRequest = function (messages, tools, options) {
|
|
189
|
+
let request = {
|
|
190
|
+
model: options.model,
|
|
191
|
+
messages,
|
|
192
|
+
temperature: options.temperature,
|
|
193
|
+
max_tokens: options.max_tokens,
|
|
194
|
+
stream: options.stream
|
|
195
|
+
};
|
|
196
|
+
if (tools && tools.length > 0) request.tools = tools;
|
|
197
|
+
return request;
|
|
198
|
+
};
|
|
199
|
+
|
|
200
|
+
/**
|
|
201
|
+
* 把助手回复写回存储(私有方法,流式响应时跳过)
|
|
202
|
+
* @param {Number} user_id 用户ID
|
|
203
|
+
* @param {Number} agent_id 智能体ID
|
|
204
|
+
* @param {Number} session_id 会话ID
|
|
205
|
+
* @param {Object} response 助手消息或流对象
|
|
206
|
+
*/
|
|
207
|
+
LLM.prototype._saveResponse = function (user_id, agent_id, session_id, response) {
|
|
208
|
+
if (!session_id || !response || !response.role) return;
|
|
209
|
+
this.getStore().addMessage(user_id, agent_id, session_id, {
|
|
210
|
+
role: response.role,
|
|
211
|
+
content: response.content || ''
|
|
212
|
+
});
|
|
213
|
+
};
|
|
214
|
+
}
|
|
215
|
+
|
|
216
|
+
module.exports = { install };
|
|
@@ -0,0 +1,224 @@
|
|
|
1
|
+
'use strict';
|
|
2
|
+
|
|
3
|
+
const Memory = require('./memory');
|
|
4
|
+
|
|
5
|
+
/**
|
|
6
|
+
* 消息装配分区
|
|
7
|
+
* @description 装配 LLM 的存储访问与提示词、消息上下文装配方法
|
|
8
|
+
*/
|
|
9
|
+
|
|
10
|
+
/**
|
|
11
|
+
* 装配存储访问与消息装配方法到 LLM 原型
|
|
12
|
+
* @param {class} LLM 大模型调用类
|
|
13
|
+
*/
|
|
14
|
+
function install(LLM) {
|
|
15
|
+
/**
|
|
16
|
+
* 设置存储实例
|
|
17
|
+
* @param {Object} store 存储实例,缺省时使用内存存储
|
|
18
|
+
* @returns {LLM} 当前实例
|
|
19
|
+
*/
|
|
20
|
+
LLM.prototype.setStore = function (store) {
|
|
21
|
+
this.store = store || new Memory();
|
|
22
|
+
return this;
|
|
23
|
+
};
|
|
24
|
+
|
|
25
|
+
/**
|
|
26
|
+
* 获取存储实例
|
|
27
|
+
* @returns {Object} 存储实例
|
|
28
|
+
*/
|
|
29
|
+
LLM.prototype.getStore = function () {
|
|
30
|
+
return this.store;
|
|
31
|
+
};
|
|
32
|
+
|
|
33
|
+
/**
|
|
34
|
+
* 重置会话
|
|
35
|
+
*
|
|
36
|
+
* 清空该会话的全部消息(含已固化的 system 消息),
|
|
37
|
+
* 下次对话会按最新的提示词与用户信息重新生成 system 消息。
|
|
38
|
+
* @param {Number} user_id 用户ID
|
|
39
|
+
* @param {Number} agent_id 智能体ID
|
|
40
|
+
* @param {Number} session_id 会话ID
|
|
41
|
+
* @returns {Boolean} 是否重置了已存在的会话
|
|
42
|
+
*/
|
|
43
|
+
LLM.prototype.resetSession = function (user_id, agent_id, session_id) {
|
|
44
|
+
return this.getStore().delSession(user_id, agent_id, session_id);
|
|
45
|
+
};
|
|
46
|
+
|
|
47
|
+
/**
|
|
48
|
+
* 获取系统提示
|
|
49
|
+
*
|
|
50
|
+
* 由两段拼成:基础提示 + 用户信息段。
|
|
51
|
+
* 基础提示优先取 (user_id, agent_id) 级提示词,其次智能体配置,最后实例配置。
|
|
52
|
+
* 任一段为空则忽略,都为空时返回空串表示不添加系统消息。
|
|
53
|
+
* @param {Number} user_id 用户ID
|
|
54
|
+
* @param {Number} agent_id 智能体ID
|
|
55
|
+
* @param {Number} session_id 会话ID
|
|
56
|
+
* @returns {String} 系统提示
|
|
57
|
+
*/
|
|
58
|
+
LLM.prototype.getSystemPrompt = function (user_id, agent_id, session_id) {
|
|
59
|
+
let base_prompt = this.getBasePrompt(user_id, agent_id);
|
|
60
|
+
let user_prompt = this._buildUserPrompt(user_id);
|
|
61
|
+
return this._joinPrompts([base_prompt, user_prompt]);
|
|
62
|
+
};
|
|
63
|
+
|
|
64
|
+
/**
|
|
65
|
+
* 获取基础提示词
|
|
66
|
+
*
|
|
67
|
+
* 优先取该用户对该智能体的提示词(由 setOptions 设置),
|
|
68
|
+
* 存在时即代替智能体自身的系统提示词;否则依次回落到智能体配置、实例配置。
|
|
69
|
+
* @param {Number} user_id 用户ID
|
|
70
|
+
* @param {Number} agent_id 智能体ID
|
|
71
|
+
* @returns {String} 基础提示词,均未配置时返回空串
|
|
72
|
+
*/
|
|
73
|
+
LLM.prototype.getBasePrompt = function (user_id, agent_id) {
|
|
74
|
+
let options = this.getStore().getOptions(user_id, agent_id);
|
|
75
|
+
if (options.system_prompt) return options.system_prompt;
|
|
76
|
+
let agent = this.getStore().getAgent(agent_id);
|
|
77
|
+
return agent.system_prompt || this.config.system_prompt || '';
|
|
78
|
+
};
|
|
79
|
+
|
|
80
|
+
/**
|
|
81
|
+
* 获取历史消息
|
|
82
|
+
*
|
|
83
|
+
* 默认从存储中按 (user_id, agent_id, session_id) 三元组读取;替换存储即改从 SQLite 等处读取。
|
|
84
|
+
* @param {Number} user_id 用户ID
|
|
85
|
+
* @param {Number} agent_id 智能体ID
|
|
86
|
+
* @param {Number} session_id 会话ID
|
|
87
|
+
* @returns {Promise<Array>} 历史消息数组,元素形如 { role, content }
|
|
88
|
+
*/
|
|
89
|
+
LLM.prototype.getHistory = function (user_id, agent_id, session_id) {
|
|
90
|
+
return this.getStore().getMessages(user_id, agent_id, session_id);
|
|
91
|
+
};
|
|
92
|
+
|
|
93
|
+
/**
|
|
94
|
+
* 获取消息上下文
|
|
95
|
+
*
|
|
96
|
+
* 会话首次调用时把系统提示固化为一条 system 消息写入存储(置于最前),
|
|
97
|
+
* 之后每轮直接从历史读取,不再重新拼装,使历史前缀保持稳定以命中缓存。
|
|
98
|
+
* 若历史中已有 system 消息,或本就无需系统提示,则原样返回历史。
|
|
99
|
+
* @param {Number} user_id 用户ID
|
|
100
|
+
* @param {Number} agent_id 智能体ID
|
|
101
|
+
* @param {Number} session_id 会话ID
|
|
102
|
+
* @returns {Promise<Array>} 消息上下文数组
|
|
103
|
+
*/
|
|
104
|
+
LLM.prototype.getMessages = async function (user_id, agent_id, session_id) {
|
|
105
|
+
let history = await this.getHistory(user_id, agent_id, session_id);
|
|
106
|
+
if (this._hasSystemMessage(history)) return history;
|
|
107
|
+
|
|
108
|
+
let system_message = this._buildSystemMessage(user_id, agent_id, session_id);
|
|
109
|
+
if (!system_message) return history;
|
|
110
|
+
|
|
111
|
+
let messages = [system_message].concat(history);
|
|
112
|
+
this.getStore().setMessages(user_id, agent_id, session_id, messages);
|
|
113
|
+
return messages;
|
|
114
|
+
};
|
|
115
|
+
|
|
116
|
+
/**
|
|
117
|
+
* 获取工具定义
|
|
118
|
+
*
|
|
119
|
+
* 默认取智能体配置中的工具名集合(agent.tools),逐个到存储读取工具定义
|
|
120
|
+
* (即提交给模型的函数声明)后拼成数组;引用的工具名未在存储中定义时抛出异常。
|
|
121
|
+
* 工具的执行函数由实例上的工具注册表提供,见 setTool。
|
|
122
|
+
* @param {Number} agent_id 智能体ID
|
|
123
|
+
* @returns {Array} 工具函数数组,元素为模型的函数定义结构
|
|
124
|
+
*/
|
|
125
|
+
LLM.prototype.getTools = function (agent_id) {
|
|
126
|
+
let agent = this.getStore().getAgent(agent_id);
|
|
127
|
+
let tool_names = agent.tools || [];
|
|
128
|
+
return tool_names.map((tool_name) => this._getToolSchema(tool_name));
|
|
129
|
+
};
|
|
130
|
+
|
|
131
|
+
/**
|
|
132
|
+
* 判断用户能否调用指定智能体(私有方法)
|
|
133
|
+
*
|
|
134
|
+
* 以 users 字段是否存在区分「已限定」与「未限定」:存在时仅列表内用户可通过,
|
|
135
|
+
* 空数组即谁都不能用;缺省时回落到 config.auth_default。
|
|
136
|
+
* @param {Object} agent 智能体配置
|
|
137
|
+
* @param {Number} user_id 用户ID
|
|
138
|
+
* @returns {Boolean} 是否允许调用
|
|
139
|
+
*/
|
|
140
|
+
LLM.prototype._canUseAgent = function (agent, user_id) {
|
|
141
|
+
if (agent.users === undefined) return this.config.auth_default !== 'deny';
|
|
142
|
+
return agent.users.includes(user_id);
|
|
143
|
+
};
|
|
144
|
+
|
|
145
|
+
/**
|
|
146
|
+
* 从存储读取用户信息并组装成提示段(私有方法)
|
|
147
|
+
* @param {Number} user_id 用户ID
|
|
148
|
+
* @returns {String} 用户信息提示段,无用户信息时返回空串
|
|
149
|
+
*/
|
|
150
|
+
LLM.prototype._buildUserPrompt = function (user_id) {
|
|
151
|
+
let user = this.getStore().getUser(user_id);
|
|
152
|
+
let lines = [];
|
|
153
|
+
if (user.name) lines.push(`用户称呼:${user.name}`);
|
|
154
|
+
if (user.profile) lines.push(`用户偏好:${user.profile}`);
|
|
155
|
+
return lines.join('\n');
|
|
156
|
+
};
|
|
157
|
+
|
|
158
|
+
/**
|
|
159
|
+
* 拼接非空提示段(私有方法)
|
|
160
|
+
* @param {Array} sections 提示段数组,空值会被忽略
|
|
161
|
+
* @returns {String} 拼接结果,各段以空行分隔
|
|
162
|
+
*/
|
|
163
|
+
LLM.prototype._joinPrompts = function (sections) {
|
|
164
|
+
let valid = [];
|
|
165
|
+
for (let section of sections) {
|
|
166
|
+
if (section) valid.push(section);
|
|
167
|
+
}
|
|
168
|
+
return valid.join('\n\n');
|
|
169
|
+
};
|
|
170
|
+
|
|
171
|
+
/**
|
|
172
|
+
* 组装系统消息(私有方法)
|
|
173
|
+
* @param {Number} user_id 用户ID
|
|
174
|
+
* @param {Number} agent_id 智能体ID
|
|
175
|
+
* @param {Number} session_id 会话ID
|
|
176
|
+
* @returns {Object|null} 系统消息,提示为空时返回 null
|
|
177
|
+
*/
|
|
178
|
+
LLM.prototype._buildSystemMessage = function (user_id, agent_id, session_id) {
|
|
179
|
+
let system_prompt = this.getSystemPrompt(user_id, agent_id, session_id);
|
|
180
|
+
if (!system_prompt) return null;
|
|
181
|
+
return { role: 'system', content: system_prompt };
|
|
182
|
+
};
|
|
183
|
+
|
|
184
|
+
/**
|
|
185
|
+
* 判断消息列表中是否已有 system 消息(私有方法)
|
|
186
|
+
* @param {Array} messages 消息数组
|
|
187
|
+
* @returns {Boolean} 是否已含 system 消息
|
|
188
|
+
*/
|
|
189
|
+
LLM.prototype._hasSystemMessage = function (messages) {
|
|
190
|
+
for (let message of messages) {
|
|
191
|
+
if (message.role === 'system') return true;
|
|
192
|
+
}
|
|
193
|
+
return false;
|
|
194
|
+
};
|
|
195
|
+
|
|
196
|
+
/**
|
|
197
|
+
* 按工具名读取工具定义(私有方法)
|
|
198
|
+
* @param {String} tool_name 工具函数名称
|
|
199
|
+
* @returns {Object} 工具定义
|
|
200
|
+
*/
|
|
201
|
+
LLM.prototype._getToolSchema = function (tool_name) {
|
|
202
|
+
let schema = this.getStore().getToolSchema(tool_name);
|
|
203
|
+
if (!schema) {
|
|
204
|
+
throw new Error(`[LLM] 智能体引用的工具 ${tool_name} 未在存储中定义`);
|
|
205
|
+
}
|
|
206
|
+
return schema;
|
|
207
|
+
};
|
|
208
|
+
|
|
209
|
+
/**
|
|
210
|
+
* 从配置中挑选模型调用相关字段(私有方法)
|
|
211
|
+
* @param {Object} source 配置来源
|
|
212
|
+
* @returns {Object} 选项对象
|
|
213
|
+
*/
|
|
214
|
+
LLM.prototype._pickOptions = function (source) {
|
|
215
|
+
let keys = ['api_key', 'base_url', 'model', 'temperature', 'max_tokens', 'stream'];
|
|
216
|
+
let options = {};
|
|
217
|
+
for (let key of keys) {
|
|
218
|
+
if (source[key] !== undefined) options[key] = source[key];
|
|
219
|
+
}
|
|
220
|
+
return options;
|
|
221
|
+
};
|
|
222
|
+
}
|
|
223
|
+
|
|
224
|
+
module.exports = { install };
|
package/lib/manager.js
CHANGED
package/lib/memory.js
CHANGED