用户 23:18 明令:搜到的信息无效就换通道再搜一次;3 次都没搜到老实交代; DS 接口失败计数按 key 形态——填了 sk-xxx 记 1 次,空白/内置记 0 次, 不能让没配置 DS 的情况下白白浪费一次机会。 WebSearchTool: - search() 转调新 searchEx(),返回 SearchOutcome(result, attempted) - searchEx:结果有效性判定 isValidResult(>20 字,空壳「搜索结果(共0条…」头无效), 无效自动换下一公开通道重搜,最多 3 通道(事不过三),全部无效才返回 null - 公开后端轮询同步走有效性判定,首个有效即返回 AIChatManager agent loop: - 新增 searchFailCount 累计失败;SEARCH 走 searchEx,FETCH 失败直接 +1 - DS 失败计数:searchKey 非空(填了 sk-xxx 或内置 deepseek 模型)→ 记 1 次; 空(未配置 DS 走公开后端)→ 记 0 次;公开失败数 = attempted - DS通道 - 累计达 3 → 老实告知玩家「换了好几个搜索通道都没查到靠谱的信息」,不硬凑答案 - 未到上限:去掉 TOOL 标记后 break,让模型基于已有信息回答(旧逻辑保留)
This commit is contained in:
@@ -1099,7 +1099,13 @@ public class AIChatManager {
|
|||||||
// 模型回复含 "TOOL:SEARCH:关键词" 标记 → 联网搜索 → 追加结果再请求一次
|
// 模型回复含 "TOOL:SEARCH:关键词" 标记 → 联网搜索 → 追加结果再请求一次
|
||||||
// 模型回复含 "TOOL:FETCH:URL" 标记 → 抓取该页面 → 追加内容再请求一次
|
// 模型回复含 "TOOL:FETCH:URL" 标记 → 抓取该页面 → 追加内容再请求一次
|
||||||
// 最多循环 2 轮,防止死循环
|
// 最多循环 2 轮,防止死循环
|
||||||
|
// ★ 第121批续16-遗留5(事不过三):
|
||||||
|
// ① WebSearchTool.searchEx 结果无效(≤20字空壳)自动换通道重搜,最多 3 通道;
|
||||||
|
// ② DS 接口失败计数按 key 形态:填了 sk-xxx → 记 1 次;空白/内置 → 记 0 次
|
||||||
|
// (没配置 DS 时不能白白浪费一次机会);
|
||||||
|
// ③ 累计失败达 3 次 → 老实告知玩家没搜到,不硬凑答案。
|
||||||
int agentRound = 0;
|
int agentRound = 0;
|
||||||
|
int searchFailCount = 0; // 累计搜索失败次数(事不过三上限 3)
|
||||||
while (agentRound < 2) {
|
while (agentRound < 2) {
|
||||||
ToolCall call = extractToolCall(reply);
|
ToolCall call = extractToolCall(reply);
|
||||||
if (call == null) break;
|
if (call == null) break;
|
||||||
@@ -1110,7 +1116,24 @@ public class AIChatManager {
|
|||||||
if (call.type == ToolCall.Type.FETCH) {
|
if (call.type == ToolCall.Type.FETCH) {
|
||||||
toolResults = WebSearchTool.fetchUrl(call.arg, searchKey);
|
toolResults = WebSearchTool.fetchUrl(call.arg, searchKey);
|
||||||
} else {
|
} else {
|
||||||
toolResults = WebSearchTool.search(call.arg, searchKey);
|
WebSearchTool.SearchOutcome outcome = WebSearchTool.searchEx(call.arg, searchKey);
|
||||||
|
toolResults = outcome.result;
|
||||||
|
if (toolResults == null) {
|
||||||
|
// ★ DS 失败计数:key 非空(填了 sk-xxx 或内置 deepseek 模型)→ 记 1 次;
|
||||||
|
// key 空(未配置 DS,走公开后端)→ 记 0 次(不浪费机会)
|
||||||
|
boolean dsKeyConfigured = searchKey != null && !searchKey.isEmpty();
|
||||||
|
int dsFail = (dsKeyConfigured && outcome.attempted > 0) ? 1 : 0;
|
||||||
|
// 公开后端失败数 = 尝试通道数(attempted 已含 DS 通道则减 1)
|
||||||
|
int publicFail = Math.max(0, outcome.attempted - (dsKeyConfigured ? 1 : 0));
|
||||||
|
searchFailCount += dsFail + publicFail;
|
||||||
|
plugin.getLogger().info("[AI聊天-Agent] 搜索无效/失败: 累计 " + searchFailCount
|
||||||
|
+ " 次(DS " + dsFail + " + 公开 " + publicFail + "),上限 3");
|
||||||
|
if (searchFailCount >= 3) {
|
||||||
|
plugin.getLogger().info("[AI聊天-Agent] 事不过三:3 次搜索均无有效结果,老实告知玩家");
|
||||||
|
reply = "我换了好几个搜索通道都没查到靠谱的信息,这个我确实帮不上忙~你说个别的游戏里的事,我肯定能帮上!";
|
||||||
|
break;
|
||||||
|
}
|
||||||
|
}
|
||||||
}
|
}
|
||||||
if (toolResults == null) {
|
if (toolResults == null) {
|
||||||
plugin.getLogger().info("[AI聊天-Agent] 工具调用无结果,告知模型无法获取");
|
plugin.getLogger().info("[AI聊天-Agent] 工具调用无结果,告知模型无法获取");
|
||||||
|
|||||||
@@ -72,22 +72,66 @@ public class WebSearchTool {
|
|||||||
* @return 搜索结果摘要文本;全部失败返回 null
|
* @return 搜索结果摘要文本;全部失败返回 null
|
||||||
*/
|
*/
|
||||||
public static String search(String query, String apiKey) {
|
public static String search(String query, String apiKey) {
|
||||||
if (query == null || query.trim().isEmpty()) return null;
|
return searchEx(query, apiKey).result;
|
||||||
|
}
|
||||||
|
|
||||||
|
/**
|
||||||
|
* ★ 第121批续16-遗留5:search 扩展版,附带「有效结果」判定与尝试通道数。
|
||||||
|
* <p>
|
||||||
|
* 旧逻辑把「后端返回了东西」当成功,但好搜常返回 5 条全无关视频/垃圾页,
|
||||||
|
* 模型拿到手只能说「没查到」(用户反馈:工具返回了结果他却说没找到信息)。
|
||||||
|
* 本方法:结果文本 ≤20 字(只有「搜索结果(共0条…」头)或解析 0 条视为无效,
|
||||||
|
* 自动换下一通道再搜;最多 3 个通道(事不过三),全部无效才返回 null。
|
||||||
|
*
|
||||||
|
* @return SearchOutcome(result=有效结果文本或null, attempted=尝试过的通道数)
|
||||||
|
*/
|
||||||
|
public static SearchOutcome searchEx(String query, String apiKey) {
|
||||||
|
if (query == null || query.trim().isEmpty()) return new SearchOutcome(null, 0);
|
||||||
|
|
||||||
|
int attempted = 0;
|
||||||
|
|
||||||
// 优先:DeepSeek Search API
|
// 优先:DeepSeek Search API
|
||||||
if (apiKey != null && !apiKey.isEmpty()) {
|
if (apiKey != null && !apiKey.isEmpty()) {
|
||||||
|
attempted++;
|
||||||
String dsResult = deepseekSearch(query, apiKey);
|
String dsResult = deepseekSearch(query, apiKey);
|
||||||
if (dsResult != null) return dsResult;
|
if (dsResult != null && isValidResult(dsResult)) return new SearchOutcome(dsResult, attempted);
|
||||||
|
System.out.println("[WebSearchTool] DeepSeek Search 结果无效,换通道重试, query=" + query);
|
||||||
}
|
}
|
||||||
|
|
||||||
// 兜底:公开搜索后端轮询(首个有结果即返回)
|
// 兜底:公开搜索后端轮询(有效结果即返回;无效换下一通道;最多 3 通道)
|
||||||
for (String backend : FALLBACK_BACKENDS) {
|
for (String backend : FALLBACK_BACKENDS) {
|
||||||
|
if (attempted >= 3) {
|
||||||
|
System.out.println("[WebSearchTool] 已试满3通道仍无有效结果,放弃, query=" + query);
|
||||||
|
break;
|
||||||
|
}
|
||||||
|
attempted++;
|
||||||
String result = publicSearch(backend, query);
|
String result = publicSearch(backend, query);
|
||||||
if (result != null) return result;
|
if (result != null && isValidResult(result)) return new SearchOutcome(result, attempted);
|
||||||
|
System.out.println("[WebSearchTool] " + backend + " 结果无效,换下一通道, query=" + query);
|
||||||
|
}
|
||||||
|
System.out.println("[WebSearchTool] 搜索无有效结果: 已试 " + attempted + " 通道("
|
||||||
|
+ String.join("/", FALLBACK_BACKENDS) + "), query=" + query);
|
||||||
|
return new SearchOutcome(null, attempted);
|
||||||
|
}
|
||||||
|
|
||||||
|
/**
|
||||||
|
* ★ 第121批续16-遗留5:结果有效性判定。
|
||||||
|
* 好搜/必应等后端即使没匹配到内容也会拼出「搜索结果(共0条,来源:XX):」头,
|
||||||
|
* 旧逻辑把这种空壳当成功返回 → 模型拿到手只能说「没查到」。
|
||||||
|
* 判定:去掉头尾空白后长度 > 20 字(真正 5 条结果至少上百字)才算有效。
|
||||||
|
*/
|
||||||
|
private static boolean isValidResult(String result) {
|
||||||
|
return result != null && result.trim().length() > 20;
|
||||||
|
}
|
||||||
|
|
||||||
|
/** searchEx 的返回载体:有效结果文本 + 尝试过的通道数 */
|
||||||
|
public static final class SearchOutcome {
|
||||||
|
public final String result;
|
||||||
|
public final int attempted;
|
||||||
|
SearchOutcome(String result, int attempted) {
|
||||||
|
this.result = result;
|
||||||
|
this.attempted = attempted;
|
||||||
}
|
}
|
||||||
System.out.println("[WebSearchTool] 搜索无结果: DeepSeek 与全部公开后端("
|
|
||||||
+ String.join("/", FALLBACK_BACKENDS) + ") 均失败, query=" + query);
|
|
||||||
return null;
|
|
||||||
}
|
}
|
||||||
|
|
||||||
/** 公开搜索后端路由 */
|
/** 公开搜索后端路由 */
|
||||||
|
|||||||
Reference in New Issue
Block a user