🔄Update: 引入 AI 服务

This commit is contained in:
2025-10-13 18:43:42 +08:00
parent ff2494dcf2
commit fe8dde8c73
4 changed files with 56 additions and 0 deletions
+7
View File
@@ -35,6 +35,13 @@
<artifactId>hutool-http</artifactId>
<version>5.8.27</version>
</dependency>
<!-- https://mvnrepository.com/artifact/cn.hutool/hutool-json -->
<dependency>
<groupId>cn.hutool</groupId>
<artifactId>hutool-json</artifactId>
<version>5.8.27</version>
</dependency>
</dependencies>
</project>
@@ -0,0 +1,7 @@
package cn.hezhaohui.webhook.constant;
public class AIConstant {
public static final String AI_URL = "https://api.siliconflow.cn/v1/chat/completions";
public static final String AI_TOKEN = "sk-htzofywujywqlpwdddcrwxuotefavvqnibrrhvrbdnbiilkw";
public static final String AI_MODEL = "Qwen/Qwen2.5-Coder-32B-Instruct";
}
@@ -0,0 +1,30 @@
package cn.hezhaohui.webhook.service;
import cn.hezhaohui.webhook.constant.AIConstant;
import cn.hutool.http.HttpRequest;
import cn.hutool.http.HttpResponse;
import cn.hutool.http.HttpUtil;
import cn.hutool.json.JSONUtil;
import org.springframework.stereotype.Service;
import java.util.HashMap;
import java.util.List;
import java.util.Map;
@Service
public class AIService {
public String getAIResponse(String prompt) {
Map<String, Object> body = new HashMap<>();
body.put("model", AIConstant.AI_MODEL);
body.put("messages", List.of(Map.of("role", "user", "content", prompt)));
String jsonBody = JSONUtil.toJsonStr(body);
HttpResponse response = HttpRequest.post(AIConstant.AI_URL)
.header("Content-Type", "application/json")
.header("Authorization", "Bearer " + AIConstant.AI_TOKEN)
.body(jsonBody)
.execute();
return "AI response: " + response.body();
}
}
@@ -0,0 +1,12 @@
package cn.hezhaohui.webhook.service;
import junit.framework.TestCase;
public class AIServiceTest extends TestCase {
public void testGetAIResponse() {
AIService aiService = new AIService();
String response = aiService.getAIResponse("你好");
System.out.println(response);
assertNotNull(response);
}
}