1、优化工具调用相关提示词,拆分静态system-prompt和动态上下文。
2、新增few-shot示例,使用"调用→结果→回复"流程闭环,显著提高多次会话后工具调用准确性。
3、去除对话标题总结和记忆的模型思考模式。
This commit is contained in:
DaGou12138
2026-04-27 11:08:25 +08:00
parent 8c0899415a
commit 151a0c1b99
4 changed files with 129 additions and 73 deletions
@@ -1,6 +1,7 @@
package xiaozhi.modules.llm.service.impl;
import java.util.HashMap;
import java.util.LinkedHashMap;
import java.util.List;
import java.util.Map;
@@ -30,11 +31,34 @@ import xiaozhi.modules.model.service.ModelConfigService;
@Service
public class OpenAIStyleLLMServiceImpl implements LLMService {
// 需要禁用思考模式的平台域名及其对应参数
private static final Map<String, Map<String, Object>> THINKING_DISABLED_DOMAINS = new LinkedHashMap<>();
static {
THINKING_DISABLED_DOMAINS.put("aliyuncs.com", Map.of("enable_thinking", false));
Map<String, Object> thinkingDisabled = Map.of("thinking", Map.of("type", "disabled"));
THINKING_DISABLED_DOMAINS.put("bigmodel.cn", thinkingDisabled);
THINKING_DISABLED_DOMAINS.put("moonshot.cn", thinkingDisabled);
THINKING_DISABLED_DOMAINS.put("volces.com", thinkingDisabled);
}
@Autowired
private ModelConfigService modelConfigService;
private final RestTemplate restTemplate = new RestTemplate();
/**
* 根据域名自动禁用思考模式
*/
private void applyThinkingDisabled(String baseUrl, Map<String, Object> requestBody) {
for (Map.Entry<String, Map<String, Object>> entry : THINKING_DISABLED_DOMAINS.entrySet()) {
if (baseUrl.contains(entry.getKey())) {
requestBody.putAll(entry.getValue());
log.info("为域名 {} 禁用思考模式,参数: {}", baseUrl, entry.getValue());
break;
}
}
}
private static final String DEFAULT_SUMMARY_PROMPT = "你是一个经验丰富的记忆总结者,擅长将对话内容进行总结摘要,遵循以下规则:\n1、总结用户的重要信息,以便在未来的对话中提供更个性化的服务\n2、不要重复总结,不要遗忘之前记忆,除非原来的记忆超过了1800字,否则不要遗忘、不要压缩用户的历史记忆\n3、用户操控的设备音量、播放音乐、天气、退出、不想对话等和用户本身无关的内容,这些信息不需要加入到总结中\n4、聊天内容中的今天的日期时间、今天的天气情况与用户事件无关的数据,这些信息如果当成记忆存储会影响后续对话,这些信息不需要加入到总结中\n5、不要把设备操控的成果结果和失败结果加入到总结中,也不要把用户的一些废话加入到总结中\n6、不要为了总结而总结,如果用户的聊天没有意义,请返回原来的历史记录也是可以的\n7、只需要返回总结摘要,严格控制在1800字内\n8、不要包含代码、xml,不需要解释、注释和说明,保存记忆时仅从对话提取信息,不要混入示例内容\n9、如果提供了历史记忆,请将新对话内容与历史记忆进行智能合并,保留有价值的历史信息,同时添加新的重要信息\n\n历史记忆:\n{history_memory}\n\n新对话内容:\n{conversation}";
private static final String DEFAULT_TITLE_PROMPT = "请根据以下对话内容,生成一个简洁的会话标题(约15字以内),只返回标题,不要包含任何解释或标点符号:\n{conversation}";
@@ -102,6 +126,9 @@ public class OpenAIStyleLLMServiceImpl implements LLMService {
requestBody.put("temperature", temperature != null ? temperature : 0.7);
requestBody.put("max_tokens", maxTokens != null ? maxTokens : 2000);
// 禁用思考模式
applyThinkingDisabled(baseUrl, requestBody);
// 发送HTTP请求
HttpHeaders headers = new HttpHeaders();
headers.setContentType(MediaType.APPLICATION_JSON);
@@ -156,10 +183,8 @@ public class OpenAIStyleLLMServiceImpl implements LLMService {
// 从智控台获取LLM模型配置
ModelConfigEntity llmConfig;
if (modelId != null && !modelId.trim().isEmpty()) {
// 通过具体模型ID获取配置
llmConfig = modelConfigService.getModelByIdFromCache(modelId);
} else {
// 保持向后兼容,使用默认配置
llmConfig = getDefaultLLMConfig();
}
@@ -197,6 +222,9 @@ public class OpenAIStyleLLMServiceImpl implements LLMService {
requestBody.put("temperature", 0.2);
requestBody.put("max_tokens", 2000);
// 禁用思考模式
applyThinkingDisabled(baseUrl, requestBody);
// 发送HTTP请求
HttpHeaders headers = new HttpHeaders();
headers.setContentType(MediaType.APPLICATION_JSON);
@@ -350,6 +378,9 @@ public class OpenAIStyleLLMServiceImpl implements LLMService {
requestBody.put("temperature", 0.3);
requestBody.put("max_tokens", 50);
// 禁用思考模式
applyThinkingDisabled(baseUrl, requestBody);
HttpHeaders headers = new HttpHeaders();
headers.setContentType(MediaType.APPLICATION_JSON);
headers.set("Authorization", "Bearer " + apiKey);