diff --git a/dsAi/src/main/java/com/dsideal/Res/Test/1.html b/dsAi/src/main/java/com/dsideal/Res/Test/1.html new file mode 100644 index 00000000..5a397e30 --- /dev/null +++ b/dsAi/src/main/java/com/dsideal/Res/Test/1.html @@ -0,0 +1,176 @@ + + + + SQL Data Lineage Visualization + + + .node text { + font-size: 12px; + } + .link { + stroke: #999; + stroke-opacity: 0.6; + stroke-width: 2px; + } + .arrow { + fill: #999; + stroke-opacity: 0.6; + } + .title { + font-size: 18px; + font-weight: bold; + margin-bottom: 20px; + } + + + +
SQL Data Lineage Visualization
+
+ + + + \ No newline at end of file diff --git a/dsAi/src/main/java/com/dsideal/Res/Test/CallDeepSeek.java b/dsAi/src/main/java/com/dsideal/Res/Test/CallDeepSeek.java index f1197528..fd7b5488 100644 --- a/dsAi/src/main/java/com/dsideal/Res/Test/CallDeepSeek.java +++ b/dsAi/src/main/java/com/dsideal/Res/Test/CallDeepSeek.java @@ -8,7 +8,9 @@ import cn.hutool.json.JSONUtil; import com.alibaba.fastjson.JSONArray; import com.jfinal.kit.PathKit; +import java.io.BufferedReader; import java.io.File; +import java.io.InputStreamReader; public class CallDeepSeek { private static final String API_KEY = "sk-44ae895eeb614aa1a9c6460579e322f1"; // 请替换为您的API KEY @@ -19,8 +21,25 @@ public class CallDeepSeek { StringBuilder fullResponse = new StringBuilder(); try { // 修改提示词确保只返回HTML - String htmlPrompt = prompt + "请只返回HTML代码,不要包含任何解释或Markdown格式。" + - "确保HTML是完整的,包含和标签。"; + String htmlPrompt = prompt + "请严格按照以下HTML模板格式输出SQL血缘关系图,只返回HTML代码:\n" + + "\n" + + "\n" + + "\n" + + " SQL血缘关系图\n" + + " \n" + + " \n" + + "\n" + + "\n" + + "

SQL血缘关系图

\n" + + "
\n" + + " [请在此处插入mermaid格式的血缘关系图]\n" + + "
\n" + + " \n" + + "\n" + + ""; JSONObject json = new JSONObject(); json.set("model", "deepseek-chat"); @@ -42,36 +61,45 @@ public class CallDeepSeek { HttpResponse response = request.execute(); if (response.isOk()) { - String[] lines = response.body().split("\\r?\\n"); - for (String line : lines) { - if (line.startsWith("data:")) { - String data = line.substring(5).trim(); - if (!data.equals("[DONE]")) { - 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); - listener.onData(content); + 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()); } } } } } - - // 保存HTML到文件 - String htmlContent = fullResponse.toString(); - if (htmlContent.contains("") && htmlContent.contains("")) { - FileUtil.writeString(htmlContent, new File("C:\\1.html"), "UTF-8"); - listener.onComplete("HTML已成功保存到C:\\1.html"); - } else { - listener.onError("返回内容不是有效的HTML"); - } + } + + // 保存HTML到文件 + String htmlContent = fullResponse.toString(); + if (htmlContent.contains("") && htmlContent.contains("")) { + FileUtil.writeString(htmlContent, new File("C:\\1.html"), "UTF-8"); + listener.onComplete("HTML已成功保存到C:\\1.html"); } else { - listener.onError("API请求失败: " + response.getStatus()); + listener.onError("返回内容不是有效的HTML"); } } catch (Exception e) { listener.onError(e.getMessage()); @@ -81,7 +109,7 @@ public class CallDeepSeek { public static void main(String[] args) { String sql = FileUtil.readUtf8String(new File(PathKit.getRootClassPath()+"/XueYuan.sql")); - String prompt = "你是一个数据库SQL的血缘关系分析专家,我将提供SQL语句给你,帮我整理数据血缘关系,并且绘制HTML页面,以展示最终的可视化展现。"; + String prompt = "你是一个数据库SQL的血缘关系分析专家,我将提供SQL语句给你,帮我整理数据血缘关系,并且绘制HTML页面,以展示最终的可视化形式展现。"; callDeepSeekStream(prompt + "\n" + sql, new SSEListener() { @Override