From d05fd60438dc88284a0215675a6d2805c572457c Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?=E8=80=81=E9=AB=98?= <3218960885@qq.com> Date: Fri, 24 Jan 2025 10:42:30 +0800 Subject: [PATCH] first --- pom.xml | 99 +++ .../deepseek/EasyDeepSeekApplication.java | 13 + .../laogao/deepseek/config/CorsConfig.java | 18 + .../deepseek/controller/easyAiController.java | 85 +++ .../EasyDeepSeekApplicationTests.java | 13 + static/index.html | 624 ++++++++++++++++++ 6 files changed, 852 insertions(+) create mode 100644 pom.xml create mode 100644 src/main/java/sincon/laogao/deepseek/EasyDeepSeekApplication.java create mode 100644 src/main/java/sincon/laogao/deepseek/config/CorsConfig.java create mode 100644 src/main/java/sincon/laogao/deepseek/controller/easyAiController.java create mode 100644 src/test/java/sincon/laogao/deepseek/EasyDeepSeekApplicationTests.java create mode 100644 static/index.html diff --git a/pom.xml b/pom.xml new file mode 100644 index 0000000..5bd0335 --- /dev/null +++ b/pom.xml @@ -0,0 +1,99 @@ + + + 4.0.0 + sincon.laogao + EasyDeepSeek + 0.0.1-SNAPSHOT + aiDemoApi + aiDemoApi + + 1.8 + UTF-8 + UTF-8 + 2.6.13 + + + + org.springframework.boot + spring-boot-starter + + + + org.springframework.boot + spring-boot-starter-web + + + com.squareup.okhttp3 + okhttp + 4.9.3 + + + org.json + json + 20210307 + + + + org.apache.commons + commons-lang3 + 3.1 + + + com.alibaba + fastjson + 1.2.83 + + + org.springframework.boot + spring-boot-starter-test + test + + + + + + org.springframework.boot + spring-boot-dependencies + ${spring-boot.version} + pom + import + + + + + + + + + + + jar + + + + + + org.springframework.boot + spring-boot-maven-plugin + 2.7.4 + + true + JAR + + + + + repackage + + + false + + + + + + + + + diff --git a/src/main/java/sincon/laogao/deepseek/EasyDeepSeekApplication.java b/src/main/java/sincon/laogao/deepseek/EasyDeepSeekApplication.java new file mode 100644 index 0000000..4082785 --- /dev/null +++ b/src/main/java/sincon/laogao/deepseek/EasyDeepSeekApplication.java @@ -0,0 +1,13 @@ +package sincon.laogao.deepseek; + +import org.springframework.boot.SpringApplication; +import org.springframework.boot.autoconfigure.SpringBootApplication; + +@SpringBootApplication +public class EasyDeepSeekApplication { + + public static void main(String[] args) { + SpringApplication.run(EasyDeepSeekApplication.class, args); + } + +} diff --git a/src/main/java/sincon/laogao/deepseek/config/CorsConfig.java b/src/main/java/sincon/laogao/deepseek/config/CorsConfig.java new file mode 100644 index 0000000..fb5b8ce --- /dev/null +++ b/src/main/java/sincon/laogao/deepseek/config/CorsConfig.java @@ -0,0 +1,18 @@ +package sincon.laogao.deepseek.config; + +import org.springframework.context.annotation.Configuration; +import org.springframework.web.servlet.config.annotation.CorsRegistry; +import org.springframework.web.servlet.config.annotation.WebMvcConfigurer; + +@Configuration +public class CorsConfig implements WebMvcConfigurer { + @Override + public void addCorsMappings(CorsRegistry registry) { + registry.addMapping("/**") + .allowedOriginPatterns("*") // 允许所有来源,生产环境建议指定具体域名 + .allowedMethods("GET", "POST", "PUT", "DELETE", "OPTIONS") + .allowedHeaders("*") + .allowCredentials(true) + .maxAge(3600); + } +} \ No newline at end of file diff --git a/src/main/java/sincon/laogao/deepseek/controller/easyAiController.java b/src/main/java/sincon/laogao/deepseek/controller/easyAiController.java new file mode 100644 index 0000000..a7c6451 --- /dev/null +++ b/src/main/java/sincon/laogao/deepseek/controller/easyAiController.java @@ -0,0 +1,85 @@ +package sincon.laogao.deepseek.controller; + +import okhttp3.MediaType; +import okhttp3.OkHttpClient; +import okhttp3.Request; +import okhttp3.Response; +import okhttp3.ResponseBody; +import org.json.JSONArray; +import org.json.JSONObject; +import org.springframework.web.bind.annotation.*; + +import java.io.IOException; +import java.util.concurrent.TimeUnit; + +@RestController +@RequestMapping("/ai") +public class easyAiController { + private static final String API_KEY = "sk-2d525ebc32ba41cd8e57064332480226"; + private static final String BASE_URL = "https://api.deepseek.com"; + private static final OkHttpClient client = new OkHttpClient.Builder() + .connectTimeout(30, TimeUnit.SECONDS) + .readTimeout(30, TimeUnit.SECONDS) + .writeTimeout(30, TimeUnit.SECONDS) + .build(); + + + @PostMapping("/msg") + public String createEvent(@RequestBody String req) { + String response = ""; + try { + response = createChatCompletion(false,req); + } catch (IOException e) { + e.printStackTrace(); + } catch (Exception e) { + throw new RuntimeException(e); + } + + return response; + } + private static String createChatCompletion(boolean streamFlag, String next) throws Exception { + + String url = BASE_URL + "/v1/chat/completions"; + + JSONArray messages = new JSONArray() + .put(new JSONObject().put("role", "system").put("content", ",你是一名我的世界资深技术,你参与过mojang我的世界开发," + + "也自己搭建过我的世界java服务器,同时熟悉所有MineCraft的插件,并能准确的识别报错和提供指导,希望你准确无误的回答我的问题," + + "并且不能回复超过500字!")) +// .put(new JSONObject().put("role", "user").put("content", "Hello! How can I assist you today? \uD83D\uDE0A")); + .put(new JSONObject().put("role", "user").put("content", next)); + + JSONObject jsonBody = new JSONObject() + .put("model", "deepseek-chat") + .put("messages", messages) + .put("stream", streamFlag); + + okhttp3.RequestBody body = okhttp3.RequestBody.create( + jsonBody.toString(), + MediaType.get("application/json; charset=utf-8") + ); + + Request request = new Request.Builder() + .url(url) + .header("Authorization", "Bearer " + API_KEY) + .post(body) + .build(); + Response response = client.newCall(request).execute(); + if(!streamFlag) { + if (!response.isSuccessful()) throw new IOException("Unexpected code " + response); + + ResponseBody responseBody = response.body(); + if (responseBody != null) { + JSONObject jsonResponse = new JSONObject(responseBody.string()); + return jsonResponse.getJSONArray("choices") + .getJSONObject(0) + .getJSONObject("message") + .getString("content"); + } + }else{ + + } + + return null; + } + +} diff --git a/src/test/java/sincon/laogao/deepseek/EasyDeepSeekApplicationTests.java b/src/test/java/sincon/laogao/deepseek/EasyDeepSeekApplicationTests.java new file mode 100644 index 0000000..86e268e --- /dev/null +++ b/src/test/java/sincon/laogao/deepseek/EasyDeepSeekApplicationTests.java @@ -0,0 +1,13 @@ +package sincon.laogao.deepseek; + +import org.junit.jupiter.api.Test; +import org.springframework.boot.test.context.SpringBootTest; + +@SpringBootTest +class EasyDeepSeekApplicationTests { + + @Test + void contextLoads() { + } + +} diff --git a/static/index.html b/static/index.html new file mode 100644 index 0000000..7f7e435 --- /dev/null +++ b/static/index.html @@ -0,0 +1,624 @@ + + + + + + AI 对话界面 + + + + +
+
+ + + + +
+
+ +
+
+
+ +
+
+ +
+ +
+
+
🔥 cmi插件如何使用?
+
AI助手教你cmi插件如何使用
+
+
+
📚 ess插件如何使用?
+
将教会您如何使用ess插件
+
+
+
💡 什么是VPS?
+
对VPS进行详解
+
+
+
🌟 如何搭建一个基础服务端?
+
告诉你最终结果
+
+
+
🔍 我的世界宝可梦模组攻略?
+
我的世界宝可梦科普解读
+
+ + +
+ +
+
+
+ AI +
+
+
恭喜智能AI - 征途下士
+
+

您好!我是华欣云AI助手。

+

我可以帮您:

+
    +
  • 解决简单报错
  • +
  • 寻找插件资源
  • +
  • 提供编程帮助
  • +
  • 甚至您可以让我给您进行插件的基础问题解答.
  • +
+

感谢您对华欣云的支持,祝您生活愉快!

+
+
+
+
+ +
+ + +
+
+ + + + \ No newline at end of file