更新提示词逻辑,以XML格式
This commit is contained in:
parent
53994476c4
commit
eaf75ac17f
@ -4,16 +4,18 @@ import { COMMANDS } from './commands.js';
|
|||||||
export async function translateToCommand(userInput) {
|
export async function translateToCommand(userInput) {
|
||||||
const availableKeys = COMMANDS.map(c => c.key).join(', ');
|
const availableKeys = COMMANDS.map(c => c.key).join(', ');
|
||||||
|
|
||||||
// 优化提示词:增加参数提取规则和别名映射
|
|
||||||
const systemPrompt = `你是一个工业自动化系统助手。
|
const systemPrompt = `你是一个工业自动化系统助手。
|
||||||
标准指令列表:[${availableKeys}]
|
标准指令列表:[${availableKeys}]
|
||||||
|
|
||||||
任务规则:
|
任务规则:
|
||||||
1. 如果用户指令包含设备编号、序列号或验证码(如“20260114”),请按格式输出:“标准指令 参数”。
|
1. 识别用户意图并按以下 XML 格式输出:
|
||||||
例如:“帮我添加一台编号为20260114的设备” -> 输出 “添加设备 20260114”。
|
- 基础格式:<cmd>标准指令</cmd>
|
||||||
2. 如果只是单纯跳转,只输出“标准指令”。
|
- 带参数格式(如设备号、序列号):<cmd>标准指令</cmd><arg>参数内容</arg>
|
||||||
|
2. 示例:
|
||||||
|
- “帮我添加一台编号为20260114的设备” -> 输出 “<cmd>添加设备</cmd><arg>20260114</arg>”
|
||||||
|
- “打开监控中心” -> 输出 “<cmd>监控中心</cmd>”
|
||||||
3. “主界面”、“主页”映射为“首页”。
|
3. “主界面”、“主页”映射为“首页”。
|
||||||
4. 只输出结果,不要输出任何解释说明或标点。
|
4. 只输出 XML 结果,不要输出任何解释说明或标点。
|
||||||
5. 无法匹配请输出 UNKNOWN。`;
|
5. 无法匹配请输出 UNKNOWN。`;
|
||||||
|
|
||||||
return new Promise((resolve) => {
|
return new Promise((resolve) => {
|
||||||
@ -25,10 +27,9 @@ export async function translateToCommand(userInput) {
|
|||||||
try {
|
try {
|
||||||
if (response.data.choices && response.data.choices.length > 0) {
|
if (response.data.choices && response.data.choices.length > 0) {
|
||||||
const content = response.data.choices[0].message.content.trim();
|
const content = response.data.choices[0].message.content.trim();
|
||||||
console.log("📥 AI 映射结果:", content);
|
console.log("📥 AI 原始响应:", content);
|
||||||
resolve(content === "UNKNOWN" ? null : content);
|
resolve(content === "UNKNOWN" ? null : content);
|
||||||
} else if (response.data.error || response.data.errors) {
|
} else {
|
||||||
console.error("AI服务报错:", response.data.error || response.data.errors);
|
|
||||||
resolve(null);
|
resolve(null);
|
||||||
}
|
}
|
||||||
} catch (e) {
|
} catch (e) {
|
||||||
@ -41,7 +42,6 @@ export async function translateToCommand(userInput) {
|
|||||||
};
|
};
|
||||||
|
|
||||||
window.addEventListener("AI_RESULT", handler);
|
window.addEventListener("AI_RESULT", handler);
|
||||||
|
|
||||||
window.dispatchEvent(new CustomEvent("DO_AI_REQUEST", {
|
window.dispatchEvent(new CustomEvent("DO_AI_REQUEST", {
|
||||||
detail: { userInput, systemPrompt }
|
detail: { userInput, systemPrompt }
|
||||||
}));
|
}));
|
||||||
|
|||||||
@ -89,12 +89,22 @@ export function expandParentMenu(span) {
|
|||||||
}
|
}
|
||||||
|
|
||||||
export function handleCommand(aiResult) {
|
export function handleCommand(aiResult) {
|
||||||
if (!aiResult) return;
|
if (!aiResult || aiResult === "UNKNOWN") return;
|
||||||
console.log("🚀 处理自动化指令:", aiResult);
|
console.log("🚀 收到结构化指令:", aiResult);
|
||||||
|
|
||||||
// 解析格式:"指令 参数" (例如 "添加设备 20260114")
|
// 使用正则解析 XML 标签内容
|
||||||
const [key, arg] = aiResult.split(" ");
|
const cmdMatch = aiResult.match(/<cmd>([\s\S]*?)<\/cmd>/);
|
||||||
|
const argMatch = aiResult.match(/<arg>([\s\S]*?)<\/arg>/);
|
||||||
|
|
||||||
|
const key = cmdMatch ? cmdMatch[1].trim() : null;
|
||||||
|
const arg = argMatch ? argMatch[1].trim() : null;
|
||||||
|
|
||||||
|
if (!key) {
|
||||||
|
console.warn("未能解析出有效的 <cmd> 标签");
|
||||||
|
return;
|
||||||
|
}
|
||||||
|
|
||||||
|
// 在命令库中寻找匹配项
|
||||||
const command = COMMANDS.find(c => c.key === key) ||
|
const command = COMMANDS.find(c => c.key === key) ||
|
||||||
COMMANDS.find(c => key.includes(c.key));
|
COMMANDS.find(c => key.includes(c.key));
|
||||||
|
|
||||||
@ -108,13 +118,18 @@ export function handleCommand(aiResult) {
|
|||||||
span.click();
|
span.click();
|
||||||
window.location.hash = command.route;
|
window.location.hash = command.route;
|
||||||
|
|
||||||
// 第二步:如果有编号参数,启动后续填充流程
|
// 第二步:如果有参数(如设备号),启动后续填充流程
|
||||||
if (arg) {
|
if (arg) {
|
||||||
console.log(`⏳ 页面跳转中,准备填充参数: ${arg}`);
|
console.log(`⏳ 检测到参数,准备自动填充: ${arg}`);
|
||||||
autoFillAndSubmit(arg);
|
autoFillAndSubmit(arg);
|
||||||
}
|
}
|
||||||
|
} else {
|
||||||
|
// 容错:如果没找到菜单元素,依然尝试直接跳转路由
|
||||||
|
console.log("未发现菜单 DOM,尝试直接通过路由跳转");
|
||||||
|
window.location.hash = command.route;
|
||||||
|
if (arg) autoFillAndSubmit(arg);
|
||||||
}
|
}
|
||||||
} else {
|
} else {
|
||||||
console.warn("未匹配到标准指令:", key);
|
console.warn("未匹配到标准库指令:", key);
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
Loading…
x
Reference in New Issue
Block a user