You can not select more than 25 topics Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.

147 lines
6.6 KiB

2 months ago
package com.dsideal.Res.Test;
import cn.hutool.core.io.FileUtil;
import cn.hutool.http.HttpRequest;
import cn.hutool.http.HttpResponse;
import cn.hutool.json.JSONObject;
import cn.hutool.json.JSONUtil;
import com.alibaba.fastjson.JSONArray;
import com.jfinal.kit.PathKit;
2 months ago
import java.io.BufferedReader;
2 months ago
import java.io.File;
2 months ago
import java.io.InputStreamReader;
2 months ago
public class CallDeepSeek {
private static final String API_KEY = "sk-44ae895eeb614aa1a9c6460579e322f1"; // 请替换为您的API KEY
private static final String API_URL = "https://api.deepseek.com/v1/chat/completions";
public static void callDeepSeekStream(String prompt, SSEListener listener) {
new Thread(() -> {
StringBuilder fullResponse = new StringBuilder();
try {
// 修改提示词确保只返回HTML
2 months ago
String htmlPrompt = prompt + "请严格按照以下HTML模板格式输出SQL血缘关系图只返回HTML代码\n" +
"<!DOCTYPE html>\n" +
"<html>\n" +
"<head>\n" +
" <title>SQL血缘关系图</title>\n" +
" <script src='https://cdn.jsdelivr.net/npm/mermaid/dist/mermaid.min.js'></script>\n" +
" <style>\n" +
" .mermaid { margin: 20px auto; }\n" +
" body { font-family: Arial; text-align: center; }\n" +
" </style>\n" +
"</head>\n" +
"<body>\n" +
" <h2>SQL血缘关系图</h2>\n" +
" <div class='mermaid'>\n" +
" [请在此处插入mermaid格式的血缘关系图]\n" +
" </div>\n" +
" <script>mermaid.initialize({startOnLoad:true});</script>\n" +
"</body>\n" +
"</html>";
2 months ago
JSONObject json = new JSONObject();
json.set("model", "deepseek-chat");
// 修改为对象形式构建messages
JSONObject message = new JSONObject();
message.set("role", "user");
message.set("content", htmlPrompt);
JSONArray messages = new JSONArray();
messages.add(message);
json.set("messages", messages);
json.set("stream", true);
System.out.println(json); // 打印最终请求体
HttpRequest request = HttpRequest.post(API_URL)
.header("Content-Type", "application/json")
.header("Authorization", "Bearer " + API_KEY)
.header("Accept", "text/event-stream")
.body(json.toString());
HttpResponse response = request.execute();
if (response.isOk()) {
2 months ago
try (BufferedReader reader = new BufferedReader(
new InputStreamReader(response.bodyStream(), "UTF-8"))) {
String line;
while ((line = reader.readLine()) != null) {
if (line.startsWith("data:")) {
String data = line.substring(5).trim();
if (!data.equals("[DONE]")) {
try {
JSONObject jsonData = JSONUtil.parseObj(data);
if (jsonData.containsKey("choices")) {
String content = jsonData.getJSONArray("choices")
.getJSONObject(0)
.getJSONObject("delta")
.getStr("content", "");
if (content != null && !content.isEmpty()) {
fullResponse.append(content);
// 实时输出到控制台
System.out.print(content);
System.out.flush();
listener.onData(content);
}
}
} catch (Exception e) {
System.err.println("解析SSE数据错误: " + e.getMessage());
2 months ago
}
}
}
}
}
2 months ago
}
// 保存HTML到文件
String htmlContent = fullResponse.toString();
if (htmlContent.contains("<!DOCTYPE html>") && htmlContent.contains("</html>")) {
FileUtil.writeString(htmlContent, new File("C:\\1.html"), "UTF-8");
listener.onComplete("HTML已成功保存到C:\\1.html");
2 months ago
} else {
2 months ago
listener.onError("返回内容不是有效的HTML");
2 months ago
}
} catch (Exception e) {
listener.onError(e.getMessage());
}
}).start();
}
public static void main(String[] args) {
String sql = FileUtil.readUtf8String(new File(PathKit.getRootClassPath()+"/XueYuan.sql"));
2 months ago
String prompt = "你是一个数据库SQL的血缘关系分析专家我将提供SQL语句给你帮我整理数据血缘关系并且绘制HTML页面以展示最终的可视化形式展现。";
2 months ago
callDeepSeekStream(prompt + "\n" + sql, new SSEListener() {
@Override
public void onData(String data) {
System.out.print(data);
}
@Override
public void onComplete(String fullResponse) {
System.out.println("\n\n完整回复: " + fullResponse);
}
@Override
public void onError(String error) {
System.err.println("错误: " + error);
}
});
try {
Thread.sleep(30000);
} catch (InterruptedException e) {
e.printStackTrace();
}
}
public interface SSEListener {
void onData(String data);
void onComplete(String fullResponse);
void onError(String error);
}
}