From e1b933c0c344494afe86bae9162dca9bdc34fd56 Mon Sep 17 00:00:00 2001
From: 陈昶聿 <chychen@nbjetron.com>
Date: 星期三, 22 七月 2026 17:04:14 +0800
Subject: [PATCH] 【丽水】扫码生成外链
---
smartor/src/main/java/com/smartor/DeepSeekApi.java | 75 +++++++++++++++++++++++++++++++++++++
1 files changed, 75 insertions(+), 0 deletions(-)
diff --git a/smartor/src/main/java/com/smartor/DeepSeekApi.java b/smartor/src/main/java/com/smartor/DeepSeekApi.java
new file mode 100644
index 0000000..ff9fb7f
--- /dev/null
+++ b/smartor/src/main/java/com/smartor/DeepSeekApi.java
@@ -0,0 +1,75 @@
+package com.smartor;
+
+;
+import com.alibaba.fastjson.JSON;
+import com.alibaba.fastjson.JSONObject;
+import okhttp3.*;
+
+import java.io.IOException;
+import java.util.ArrayList;
+import java.util.HashMap;
+import java.util.List;
+import java.util.Map;
+
+public class DeepSeekApi {
+ // 鏇挎崲涓轰綘鑷繁鐨刱ey
+ private static final String API_KEY = "sk-";
+ private static final String API_URL = "https://api.deepseek.com/v1/chat/completions";
+
+ public static void main(String[] args) {
+ String resp = chatCompletion("鐢↗ava鍐欎竴涓啋娉℃帓搴�");
+ System.out.println("DeepSeek杩斿洖缁撴灉锛歕n" + resp);
+ }
+
+ public static String chatCompletion(String userPrompt) {
+ OkHttpClient client = new OkHttpClient();
+
+ // 1. 缁勮娑堟伅浣�
+ List<Map<String, String>> messages = new ArrayList<>();
+ // 绯荤粺瑙掕壊锛堝彲閫夛紝鐢ㄤ簬璁惧畾AI韬唤锛�
+ Map<String, String> sysMsg = new HashMap<>();
+ sysMsg.put("role", "system");
+ sysMsg.put("content", "浣犳槸涓撲笟Java寮�鍙戝伐绋嬪笀锛屽洖绛旂畝娲佽鑼�");
+ messages.add(sysMsg);
+ // 鐢ㄦ埛鎻愰棶
+ Map<String, String> userMsg = new HashMap<>();
+ userMsg.put("role", "user");
+ userMsg.put("content", userPrompt);
+ messages.add(userMsg);
+
+ // 2. 璇锋眰鍙傛暟
+ Map<String, Object> requestBody = new HashMap<>();
+ requestBody.put("model", "deepseek-chat");
+ requestBody.put("messages", messages);
+ requestBody.put("temperature", 0.7); // 闅忔満鎬�0~1锛岃秺浣庤秺涓ヨ皑
+ requestBody.put("max_tokens", 1024);
+
+ // 3. 鏋勫缓璇锋眰
+ String jsonBody = JSON.toJSONString(requestBody);
+ RequestBody body = RequestBody.create(MediaType.parse("application/json"), jsonBody);
+
+ Request request = new Request.Builder()
+ .url(API_URL)
+ .header("Authorization", "Bearer " + API_KEY)
+ .header("Content-Type", "application/json")
+ .post(body)
+ .build();
+
+ // 4. 鍙戦�佽姹�
+ try (Response response = client.newCall(request).execute()) {
+ if (!response.isSuccessful()) {
+ return "璇锋眰澶辫触锛岀姸鎬佺爜锛�" + response.code() + "锛岄敊璇俊鎭細" + response.body().string();
+ }
+ String resJson = response.body().string();
+ JSONObject jsonObj = JSON.parseObject(resJson);
+ // 鎻愬彇AI鍥炵瓟鍐呭
+ return jsonObj.getJSONArray("choices")
+ .getJSONObject(0)
+ .getJSONObject("message")
+ .getString("content");
+ } catch (IOException e) {
+ e.printStackTrace();
+ return "鎺ュ彛璋冪敤寮傚父锛�" + e.getMessage();
+ }
+ }
+}
\ No newline at end of file
--
Gitblit v1.9.3