🔄Update: 设置系统提示词

This commit is contained in:
2025-10-13 19:04:05 +08:00
parent fe8dde8c73
commit c4acef5887
3 changed files with 48 additions and 2 deletions
@@ -4,4 +4,14 @@ 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" +
"【输出格式】" +
"[总结]: ...";
}
@@ -16,7 +16,7 @@ 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)));
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);
@@ -5,7 +5,43 @@ public class AIServiceTest extends TestCase {
public void testGetAIResponse() {
AIService aiService = new AIService();
String response = aiService.getAIResponse("你好");
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);
}