Compare commits

..
3 Commits
Author SHA1 Message Date
wonder c4acef5887 🔄Update: 设置系统提示词 2025-10-13 19:04:05 +08:00
wonder fe8dde8c73 🔄Update: 引入 AI 服务 2025-10-13 18:43:42 +08:00
wonder ff2494dcf2 ✨Feat: 获取文件内容 2025-10-13 15:36:03 +08:00
5 changed files with 142 additions and 0 deletions
+14
View File
@@ -28,6 +28,20 @@
<groupId>org.springframework.boot</groupId> <groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-web</artifactId> <artifactId>spring-boot-starter-web</artifactId>
</dependency> </dependency>
<!-- https://mvnrepository.com/artifact/cn.hutool/hutool-http -->
<dependency>
<groupId>cn.hutool</groupId>
<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> </dependencies>
</project> </project>
@@ -0,0 +1,17 @@
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";
public static final String AI_SYSTEM_PROMPT = "【身份】你是一位代码总结专家,你擅长总结用户给出的代码并且做出有见解的回答。" +
"【要求】你需要根据用户给出的 commit messages 以及 代码文件的变化,总结出代码的改动内容,并给出有见解的回答。回答结果需要简洁精炼专业,默认情况下不分点描述。对于简单的技术内容,通过概括性的语言描述即可,避免口语化回答。你不需要输出带有 MarkDown 格式的文本,因为用户的终端不支持该选项。在适宜的地方需要使用换行。" +
"【输入格式】" +
"[Commit Messages]: ...\n" +
"[Added Files]: ...\n" +
"[Removed Files]: ...\n" +
"[Modified Files]: ...\n" +
"【输出格式】" +
"[总结]: ...";
}
@@ -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", "system", "content", AIConstant.AI_SYSTEM_PROMPT), 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,33 @@
package cn.hezhaohui.webhook.service;
import cn.hutool.http.HttpUtil;
import org.springframework.stereotype.Service;
import java.util.List;
@Service
public class FileService {
// repo: http://47.121.181.112:3000/wonder/lark-webhook-sender
// prefix: /raw/branch/main/
// raw: gitea-webhook/src/main/java/cn/hezhaohui/webhook/controller/HookController.java
// parse: http://47.121.181.112:3000/wonder/lark-webhook-sender/raw/branch/main/gitea-webhook/src/main/java/cn/hezhaohui/webhook/controller/HookController.java
public String getRawUrl(String repo, String raw) {
return repo +"/raw/branch/main/" +raw;
}
public String getRawContents(String repo, String raw) {
String url = getRawUrl(repo, raw);
String fileName = raw.substring(raw.lastIndexOf("/")+1);
String fileContent = HttpUtil.get(url);
return "[FileName]" + fileName + "\n" + "[FileContent]" + fileContent + "\n";
}
public String getAllFiles(String repo, List<String> files) {
StringBuilder sb = new StringBuilder();
for (String file : files) {
sb.append(getRawContents(repo, file));
}
return sb.toString();
}
}
@@ -0,0 +1,48 @@
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("[Commit Messages]: Chore: 增加日志\n" +
" Fix: 修复缺失的响应头\n" +
" ✨Feat: 获取文件修改" +
"[Added Files]:" +
"[FileName] AIService.java\n" +
"package cn.hezhaohui.webhook.service;\n" +
"\n" +
"import cn.hezhaohui.webhook.constant.AIConstant;\n" +
"import cn.hutool.http.HttpRequest;\n" +
"import cn.hutool.http.HttpResponse;\n" +
"import cn.hutool.http.HttpUtil;\n" +
"import cn.hutool.json.JSONUtil;\n" +
"import org.springframework.stereotype.Service;\n" +
"\n" +
"import java.util.HashMap;\n" +
"import java.util.List;\n" +
"import java.util.Map;\n" +
"\n" +
"@Service\n" +
"public class AIService {\n" +
" public String getAIResponse(String prompt) {\n" +
" Map<String, Object> body = new HashMap<>();\n" +
" body.put(\"model\", AIConstant.AI_MODEL);\n" +
" body.put(\"messages\", List.of(Map.of(\"role\", \"system\", \"content\", AIConstant.AI_SYSTEM_PROMPT), Map.of(\"role\", \"user\", \"content\", prompt)));\n" +
"\n" +
" String jsonBody = JSONUtil.toJsonStr(body);\n" +
"\n" +
" HttpResponse response = HttpRequest.post(AIConstant.AI_URL)\n" +
" .header(\"Content-Type\", \"application/json\")\n" +
" .header(\"Authorization\", \"Bearer \" + AIConstant.AI_TOKEN)\n" +
" .body(jsonBody)\n" +
" .execute();\n" +
" return \"AI response: \" + response.body();\n" +
" }\n" +
"}\n" +
"[Removed Files]:" +
"[Modified Files]:");
System.out.println(response);
assertNotNull(response);
}
}