feat(ai): 切换到DeepSeek推理模型并优化API调用流程
- 将API模型从 deepseek-chat 切换为 deepseek-reasoner - 添加推理过程提示信息显示 - 实现实时显示思维链推理过程 - 优化响应内容收集逻辑 - 更新JSON对象属性设置方法支持多种数据类型 - 修改预设项目SVN地址配置
This commit is contained in:
@@ -95,6 +95,8 @@ public class DeepSeekLogProcessor {
|
|||||||
|
|
||||||
System.out.println();
|
System.out.println();
|
||||||
System.out.println("正在调用DeepSeek API分析日志...");
|
System.out.println("正在调用DeepSeek API分析日志...");
|
||||||
|
System.out.println("(使用 deepseek-reasoner 模型,推理阶段可能需要数十秒,请耐心等待)");
|
||||||
|
System.out.println("--- 推理过程 ---");
|
||||||
|
|
||||||
// 调用DeepSeek API处理日志
|
// 调用DeepSeek API处理日志
|
||||||
String prompt = buildPrompt(combinedContent, period);
|
String prompt = buildPrompt(combinedContent, period);
|
||||||
@@ -216,7 +218,7 @@ public class DeepSeekLogProcessor {
|
|||||||
*/
|
*/
|
||||||
private static String callDeepSeekAPI(String apiKey, String prompt) throws IOException {
|
private static String callDeepSeekAPI(String apiKey, String prompt) throws IOException {
|
||||||
JSONObject requestBody = new JSONObject();
|
JSONObject requestBody = new JSONObject();
|
||||||
requestBody.put("model", "deepseek-chat");
|
requestBody.put("model", "deepseek-reasoner");
|
||||||
|
|
||||||
// 创建消息对象,包含 role 和 content 字段
|
// 创建消息对象,包含 role 和 content 字段
|
||||||
JSONObject messageObj = new JSONObject();
|
JSONObject messageObj = new JSONObject();
|
||||||
@@ -228,9 +230,8 @@ public class DeepSeekLogProcessor {
|
|||||||
messagesArray.add(messageObj.jsonObject);
|
messagesArray.add(messageObj.jsonObject);
|
||||||
requestBody.put("messages", messagesArray);
|
requestBody.put("messages", messagesArray);
|
||||||
|
|
||||||
requestBody.put("temperature", 0.7);
|
|
||||||
requestBody.put("max_tokens", 4000);
|
requestBody.put("max_tokens", 4000);
|
||||||
requestBody.put("stream", Optional.of(true)); // 启用流式输出
|
requestBody.put("stream", true); // 启用流式输出
|
||||||
|
|
||||||
Request request = new Request.Builder()
|
Request request = new Request.Builder()
|
||||||
.url(DEEPSEEK_API_URL)
|
.url(DEEPSEEK_API_URL)
|
||||||
@@ -266,16 +267,29 @@ public class DeepSeekLogProcessor {
|
|||||||
JSONObject choice = chunk.getJSONArray("choices").get(0);
|
JSONObject choice = chunk.getJSONArray("choices").get(0);
|
||||||
if (choice.has("delta")) {
|
if (choice.has("delta")) {
|
||||||
JSONObject delta = choice.getJSONObject("delta");
|
JSONObject delta = choice.getJSONObject("delta");
|
||||||
|
// 打印思维链推理过程(不计入最终结果)
|
||||||
|
if (delta.has("reasoning_content")) {
|
||||||
|
String reasoning = delta.optString("reasoning_content", "");
|
||||||
|
if (!reasoning.isEmpty()) {
|
||||||
|
System.out.print(reasoning);
|
||||||
|
System.out.flush();
|
||||||
|
}
|
||||||
|
}
|
||||||
|
// 收集最终回答内容
|
||||||
if (delta.has("content")) {
|
if (delta.has("content")) {
|
||||||
String content = delta.optString("content", "");
|
String content = delta.optString("content", "");
|
||||||
|
if (!content.isEmpty()) {
|
||||||
|
if (chunkCount == 0) {
|
||||||
|
System.out.println("\n--- 最终结果 ---");
|
||||||
|
}
|
||||||
fullResponse.append(content);
|
fullResponse.append(content);
|
||||||
chunkCount++;
|
chunkCount++;
|
||||||
// 实时打印处理进度
|
|
||||||
System.out.print(content);
|
System.out.print(content);
|
||||||
System.out.flush();
|
System.out.flush();
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
}
|
||||||
} catch (Exception e) {
|
} catch (Exception e) {
|
||||||
// 忽略解析错误,继续处理下一行
|
// 忽略解析错误,继续处理下一行
|
||||||
}
|
}
|
||||||
@@ -513,11 +527,15 @@ public class DeepSeekLogProcessor {
|
|||||||
}
|
}
|
||||||
|
|
||||||
public void put(String key, int value) {
|
public void put(String key, int value) {
|
||||||
jsonObject.addProperty(key, String.valueOf(value));
|
jsonObject.addProperty(key, value);
|
||||||
}
|
}
|
||||||
|
|
||||||
public void put(String key, double value) {
|
public void put(String key, double value) {
|
||||||
jsonObject.addProperty(key, String.valueOf(value));
|
jsonObject.addProperty(key, value);
|
||||||
|
}
|
||||||
|
|
||||||
|
public void put(String key, boolean value) {
|
||||||
|
jsonObject.addProperty(key, value);
|
||||||
}
|
}
|
||||||
|
|
||||||
public void put(String key, Object value) {
|
public void put(String key, Object value) {
|
||||||
|
|||||||
@@ -12,9 +12,9 @@ public class Main {
|
|||||||
|
|
||||||
// 预设项目列表
|
// 预设项目列表
|
||||||
private static final Project[] PRESET_PROJECTS = {
|
private static final Project[] PRESET_PROJECTS = {
|
||||||
new Project("PRS-7050场站智慧管控", "https://10.6.220.216:48080/svn/houtai/001_后台软件/PRS-7050场站智慧管控/01_开发库/V1.00/src_java"),
|
new Project("PRS-7050场站智慧管控", "https://10.6.221.149:48080/svn/houtai/001_后台软件/PRS-7050场站智慧管控/01_开发库/V1.00/src_java"),
|
||||||
new Project("PRS-7950在线巡视", "https://10.6.220.216:48080/svn/houtai/001_后台软件/PRS-7950在线巡视/01_开发库/V2.00/src_java"),
|
new Project("PRS-7950在线巡视", "https://10.6.221.149:48080/svn/houtai/001_后台软件/PRS-7950在线巡视/01_开发库/V2.00/src_java"),
|
||||||
new Project("PRS-7950在线巡视电科院测试版", "https://10.6.220.216:48080/svn/houtai/001_后台软件/PRS-7950在线巡视/01_开发库/V1.00_2024/src_java")
|
new Project("PRS-7950在线巡视电科院测试版", "https://10.6.221.149:48080/svn/houtai/001_后台软件/PRS-7950在线巡视/01_开发库/V1.00_2024/src_java")
|
||||||
};
|
};
|
||||||
|
|
||||||
public static void main(String[] args) {
|
public static void main(String[] args) {
|
||||||
|
|||||||
Reference in New Issue
Block a user