From db5d4b06c64f8746e696113aaf2608ee533d9d43 Mon Sep 17 00:00:00 2001 From: tzdwindows 7 <3076584115@qq.com> Date: Sun, 9 Feb 2025 12:06:50 +0800 Subject: [PATCH] =?UTF-8?q?feat(box):=20=E5=AE=9E=E7=8E=B0=20AI=20?= =?UTF-8?q?=E5=AF=B9=E8=AF=9D=E7=AA=97=E5=8F=A3=E5=B9=B6=E6=B7=BB=E5=8A=A0?= =?UTF-8?q?=E7=BD=91=E7=BB=9C=20API=20=E8=B0=83=E7=94=A8?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit - 新增 LMApi 类实现 API 调用获取 AI 回复 - 修改 LocalWindow 类,添加系统提示信息- 更新 Main 类,引入 Markdown处理库 - 在 build.gradle 中添加相关依赖 --- build.gradle | 26 ++--- logo.ico | Bin 4286 -> 0 bytes .../java/com/axis/innovators/box/Main.java | 5 +- .../axis/innovators/box/gui/LocalWindow.java | 1 + src/main/java/org/tzd/lm/LM.java | 57 ++++++++--- src/main/java/org/tzd/lm/LMApi.java | 91 ++++++++++++++++++ 6 files changed, 150 insertions(+), 30 deletions(-) delete mode 100644 logo.ico create mode 100644 src/main/java/org/tzd/lm/LMApi.java diff --git a/build.gradle b/build.gradle index 3223498..3d15d65 100644 --- a/build.gradle +++ b/build.gradle @@ -24,28 +24,22 @@ repositories { dependencies { testImplementation platform('org.junit:junit-bom:5.10.0') testImplementation 'org.junit.jupiter:junit-jupiter' + // https://mvnrepository.com/artifact/org.commonmark/commonmark + implementation 'org.commonmark:commonmark:0.24.0' + // https://mvnrepository.com/artifact/org.commonjava.googlecode.markdown4j/markdown4j + implementation 'org.commonjava.googlecode.markdown4j:markdown4j:2.2-cj-1.1' + // https://mvnrepository.com/artifact/com.google.code.gson/gson + implementation 'com.google.code.gson:gson:2.8.9' } application { mainClass = 'com.axis.innovators.box.Main' } -shadowJar { - archiveBaseName = 'app' - archiveClassifier = '' - archiveVersion = '' -} - -launch4j { - mainClassName = 'com.axis.innovators.box.Main' - outfile = version + '.exe' - icon = "${projectDir}/logo.ico" - jar = shadowJar.archiveFile.get() - bundledJrePath = 'jre' -} - -tasks.named('launch4j') { - dependsOn shadowJar +jar { + manifest { + attributes 'Main-Class': 'com.axis.innovators.box.Main' + } } test { diff --git a/logo.ico b/logo.ico deleted file mode 100644 index 57b1602c89ab2216dedd78e4a8f16742b6b3bb13..0000000000000000000000000000000000000000 GIT binary patch literal 0 HcmV?d00001 literal 4286 zcmds)KWh|G6vdyQBbmY^jaX#@rmzwOAp{F;L_dLzZ4d&nNR>)b_%UJ#NQw8$oV|D6yYHU+?!E8LCWK@9o1PAQht@<0 zr$Y$m1Pz5N0^{wE=FXlkPz?N|-$@!mKa65KUEVRpa5VzGi0P;t^|1W;;r&``C?SHki|0?^sebM!k$@jc)TUdNpg*yUsmxNCO+oLsUExC messages) throws IOException { + String jsonInput = GSON.toJson(Map.of("messages", messages)); + + URL url = URI.create(API_URL).toURL(); + HttpURLConnection conn = (HttpURLConnection) url.openConnection(); + conn.setRequestMethod("POST"); + conn.setRequestProperty("Content-Type", "application/json"); + conn.setDoOutput(true); + + try(OutputStream os = conn.getOutputStream()) { + byte[] input = jsonInput.getBytes(StandardCharsets.UTF_8); + os.write(input, 0, input.length); + } + + int responseCode = conn.getResponseCode(); + if (responseCode == HttpURLConnection.HTTP_OK) { + try (BufferedReader br = new BufferedReader( + new InputStreamReader(conn.getInputStream(), "GBK"))) { + String response = readAllLines(br); + Map result = GSON.fromJson(response, + new TypeToken>(){}.getType()); + return result.get("message"); + } + } else { + throw new IOException("API请求失败,状态码:" + responseCode); + } + } + + private String readAllLines(BufferedReader reader) throws IOException { + StringBuilder sb = new StringBuilder(); + String line; + while ((line = reader.readLine()) != null) { + sb.append(line); + } + return sb.toString(); + } + + public static void main(String[] args) { + List messages = new ArrayList<>(); + messages.add(new Message("system", "You are a helpful assistant.")); + messages.add(new Message("user", "你好啊")); + + try { + String reply = new LMApi().getAIResponse(messages); + System.out.println(reply); + } catch (IOException e) { + e.printStackTrace(); + } + } +} \ No newline at end of file