diff --git a/dsAi/src/main/java/com/dsideal/Res/Test/61B49F1B-DF15-4ed1-9C8C-3CF1E1499748.txt b/dsAi/src/main/java/com/dsideal/Res/Test/61B49F1B-DF15-4ed1-9C8C-3CF1E1499748.txt deleted file mode 100644 index bf7ec7d6..00000000 Binary files a/dsAi/src/main/java/com/dsideal/Res/Test/61B49F1B-DF15-4ed1-9C8C-3CF1E1499748.txt and /dev/null differ diff --git a/dsAi/src/main/java/com/dsideal/Res/Test/CallDeepSeek.java b/dsAi/src/main/java/com/dsideal/Res/Test/CallDeepSeek.java new file mode 100644 index 00000000..f1197528 --- /dev/null +++ b/dsAi/src/main/java/com/dsideal/Res/Test/CallDeepSeek.java @@ -0,0 +1,118 @@ +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; + +import java.io.File; + +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 + String htmlPrompt = prompt + "请只返回HTML代码,不要包含任何解释或Markdown格式。" + + "确保HTML是完整的,包含和标签。"; + + 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()) { + 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); + } + } + } + } + } + + // 保存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"); + } + } else { + listener.onError("API请求失败: " + response.getStatus()); + } + } catch (Exception e) { + listener.onError(e.getMessage()); + } + }).start(); + } + + public static void main(String[] args) { + String sql = FileUtil.readUtf8String(new File(PathKit.getRootClassPath()+"/XueYuan.sql")); + String prompt = "你是一个数据库SQL的血缘关系分析专家,我将提供SQL语句给你,帮我整理数据血缘关系,并且绘制HTML页面,以展示最终的可视化展现。"; + + 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); + } +} + diff --git a/dsAi/src/main/java/com/dsideal/Res/Util/CallDeepSeek.java b/dsAi/src/main/java/com/dsideal/Res/Util/CallDeepSeek.java deleted file mode 100644 index 7e5b45d5..00000000 --- a/dsAi/src/main/java/com/dsideal/Res/Util/CallDeepSeek.java +++ /dev/null @@ -1,81 +0,0 @@ -package com.dsideal.Res.Util; - -import cn.hutool.http.HttpRequest; -import cn.hutool.http.HttpResponse; -import cn.hutool.json.JSONObject; -import cn.hutool.json.JSONUtil; - -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(() -> { - try { - JSONObject json = new JSONObject(); - json.set("model", "deepseek-chat"); - json.set("messages", JSONUtil.parseArray( - "[{\"role\":\"user\",\"content\":\"" + prompt + "\"}]" - )); - json.set("stream", true); // 启用流式响应 - - HttpRequest request = HttpRequest.post(API_URL) - .header("Content-Type", "application/json") - .header("Authorization", "Bearer " + API_KEY) - .header("Accept", "text/event-stream") // SSE支持 - .body(json.toString()); - - 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]")) { - listener.onData(data); - } - } - } - listener.onComplete(); - } else { - listener.onError("API请求失败: " + response.getStatus()); - } - } catch (Exception e) { - listener.onError(e.getMessage()); - } - }).start(); - } - - public static void main(String[] args) { - callDeepSeekStream("你好", new SSEListener() { - @Override - public void onData(String data) { - System.out.println("收到数据: " + data); - } - - @Override - public void onComplete() { - System.out.println("流式传输完成"); - } - - @Override - public void onError(String error) { - System.err.println("错误: " + error); - } - }); - - // 保持主线程运行 - try { - Thread.sleep(30000); // 等待30秒 - } catch (InterruptedException e) { - e.printStackTrace(); - } - } - - public interface SSEListener { - void onData(String data); - void onComplete(); - void onError(String error); - } -} diff --git a/dsAi/src/main/resources/XueYuan.sql b/dsAi/src/main/resources/XueYuan.sql new file mode 100644 index 00000000..a2af0cba --- /dev/null +++ b/dsAi/src/main/resources/XueYuan.sql @@ -0,0 +1,2 @@ +select tsl.person_id,tsl.person_name,tsl.mz,tdm.mz_name,tsl.xb,tdx.xb_name from t_sys_loginperson as tsl inner join t_dm_mz as tdm on tsl.mz=tdm.mz_id + inner join t_dm_xb as tdx on tsl.xb=tdx.xb_id \ No newline at end of file diff --git a/dsAi/target/classes/XueYuan.sql b/dsAi/target/classes/XueYuan.sql new file mode 100644 index 00000000..a2af0cba --- /dev/null +++ b/dsAi/target/classes/XueYuan.sql @@ -0,0 +1,2 @@ +select tsl.person_id,tsl.person_name,tsl.mz,tdm.mz_name,tsl.xb,tdx.xb_name from t_sys_loginperson as tsl inner join t_dm_mz as tdm on tsl.mz=tdm.mz_id + inner join t_dm_xb as tdx on tsl.xb=tdx.xb_id \ No newline at end of file