Skip to content

Commit d84aa1b

Browse files
committed
后端 - AI 能力接入
实现了 AI 生成关卡功能 AI 生成结果报告功能还需要再修复
1 parent 66727b7 commit d84aa1b

File tree

8 files changed

+292
-213
lines changed

8 files changed

+292
-213
lines changed

AI_INTEGRATION_GUIDE.md

Lines changed: 142 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,142 @@
1+
# AI 功能集成说明
2+
3+
## 概述
4+
5+
本项目已成功集成 LangChain4j 框架,实现了基于阿里云通义千问大模型的 AI 功能,包括:
6+
7+
1. **AI 生成关卡**:根据用户薪资动态生成程序员技术练兵场关卡
8+
2. **AI 生成结果报告**:根据用户答题情况生成详细的结果分析报告
9+
10+
## 已完成的功能
11+
12+
### 1. AI 配置类 (`AiConfig.java`)
13+
- 配置阿里云通义千问大模型
14+
- 创建 AI 服务的 Bean 实例
15+
- 支持通过配置文件动态配置模型参数
16+
17+
### 2. AI 服务接口
18+
- `LevelGenerationAiService`:关卡生成服务
19+
- `ResultReportAiService`:结果报告生成服务
20+
- 使用 `@SystemMessage``@UserMessage` 注解定义提示词
21+
22+
### 3. 服务实现类集成
23+
- `LevelServiceImpl`:集成关卡生成 AI 服务,包含降级处理
24+
- `UserLevelServiceImpl`:集成结果报告生成 AI 服务,包含降级处理
25+
26+
### 4. 数据传输对象 (DTO)
27+
- `LevelGenerationResponse`:关卡生成响应
28+
- `ResultReportResponse`:结果报告响应
29+
- `LevelOption`:关卡选项
30+
31+
## 配置说明
32+
33+
### 1. 环境变量配置
34+
`application.yml` 中配置:
35+
```yaml
36+
langchain4j:
37+
dashscope:
38+
api-key: ${DASHSCOPE_API_KEY:your-api-key-here}
39+
model-name: qwen-max
40+
temperature: 0.7
41+
timeout: 60s
42+
```
43+
44+
### 2. 本地开发配置
45+
`application-local.yml` 中直接配置 API Key:
46+
```yaml
47+
langchain4j:
48+
dashscope:
49+
api-key: your-actual-api-key
50+
model-name: qwen-max
51+
temperature: 0.7
52+
timeout: 60s
53+
```
54+
55+
## API 接口
56+
57+
### 1. 生成关卡
58+
```http
59+
POST /api/level/generate
60+
Content-Type: application/json
61+
62+
{
63+
"salary": 10000
64+
}
65+
```
66+
67+
### 2. 提交答案
68+
```http
69+
POST /api/user-level/submit
70+
Content-Type: application/json
71+
72+
{
73+
"levelId": "关卡ID",
74+
"userOptions": ["选项1", "选项2"]
75+
}
76+
```
77+
78+
## 提示词文件
79+
80+
### 1. 关卡生成提示词 (`prompts/level-generation-system.txt`)
81+
定义了如何根据用户薪资生成合适难度的关卡,包括:
82+
- 关卡名称和描述
83+
- 技术选项(正确答案和干扰项)
84+
- 难度等级
85+
- 目标薪资范围
86+
87+
### 2. 结果报告提示词 (`prompts/result-report-system.txt`)
88+
定义了如何生成详细的结果分析报告,包括:
89+
- 评分和评价
90+
- 薪资调整建议
91+
- 公司投递建议
92+
- 标准答案解析
93+
94+
## 错误处理和降级机制
95+
96+
### 1. 关卡生成降级
97+
当 AI 服务不可用时,会生成一个默认的临时关卡,确保用户体验不中断。
98+
99+
### 2. 结果报告降级
100+
当 AI 服务不可用时,会生成一个默认的结果报告,给出及格分数但不调整薪资。
101+
102+
## 测试
103+
104+
### 1. 单元测试
105+
运行 `AiServiceTest` 来测试 AI 服务的基本功能:
106+
```bash
107+
mvn test -Dtest=AiServiceTest
108+
```
109+
110+
### 2. 集成测试
111+
启动应用后,可以通过 Swagger UI 测试完整的 API 流程:
112+
- 访问:http://localhost:8123/api/doc.html
113+
114+
## 注意事项
115+
116+
1. **API Key 安全**:
117+
- 生产环境请使用环境变量配置 API Key
118+
- 不要将 API Key 提交到代码仓库
119+
120+
2. **模型选择**:
121+
- 当前使用 `qwen-max` 模型以获得最佳效果
122+
- 可根据成本考虑调整为 `qwen-turbo` 或其他模型
123+
124+
3. **错误处理**:
125+
- 所有 AI 调用都有降级机制
126+
- 日志记录详细的错误信息便于调试
127+
128+
4. **性能优化**:
129+
- AI 调用可能较慢,建议在前端添加加载提示
130+
- 考虑添加缓存机制减少重复调用
131+
132+
## 依赖版本
133+
134+
- LangChain4j: 1.4.0-beta10
135+
- Spring Boot: 3.5.5
136+
- Java: 21
137+
138+
## 故障排除
139+
140+
1. **编译错误**:确保使用 Java 21
141+
2. **API 调用失败**:检查 API Key 配置和网络连接
142+
3. **JSON 解析错误**:检查 AI 返回的 JSON 格式是否正确
Lines changed: 58 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,58 @@
1+
package com.yupi.codertestbackend.config;
2+
3+
import com.yupi.codertestbackend.service.ai.LevelGenerationAiService;
4+
import com.yupi.codertestbackend.service.ai.ResultReportAiService;
5+
import dev.langchain4j.community.model.dashscope.QwenChatModel;
6+
import dev.langchain4j.model.chat.ChatModel;
7+
import dev.langchain4j.service.AiServices;
8+
import org.springframework.beans.factory.annotation.Value;
9+
import org.springframework.context.annotation.Bean;
10+
import org.springframework.context.annotation.Configuration;
11+
12+
/**
13+
* AI 配置类
14+
*/
15+
@Configuration
16+
public class AiConfig {
17+
18+
@Value("${langchain4j.dashscope.api-key}")
19+
private String apiKey;
20+
21+
@Value("${langchain4j.dashscope.model-name}")
22+
private String modelName;
23+
24+
@Value("${langchain4j.dashscope.temperature:0.7}")
25+
private Float temperature;
26+
27+
/**
28+
* 配置 ChatModel Bean
29+
*/
30+
@Bean
31+
public ChatModel chatModel() {
32+
return QwenChatModel.builder()
33+
.apiKey(apiKey)
34+
.modelName(modelName)
35+
.temperature(temperature)
36+
.build();
37+
}
38+
39+
/**
40+
* 配置关卡生成 AI 服务
41+
*/
42+
@Bean
43+
public LevelGenerationAiService levelGenerationAiService(ChatModel chatModel) {
44+
return AiServices.builder(LevelGenerationAiService.class)
45+
.chatModel(chatModel)
46+
.build();
47+
}
48+
49+
/**
50+
* 配置结果报告生成 AI 服务
51+
*/
52+
@Bean
53+
public ResultReportAiService resultReportAiService(ChatModel chatModel) {
54+
return AiServices.builder(ResultReportAiService.class)
55+
.chatModel(chatModel)
56+
.build();
57+
}
58+
}

src/main/java/com/yupi/codertestbackend/service/ai/LevelGenerationAiService.java

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,8 @@
11
package com.yupi.codertestbackend.service.ai;
22

33
import com.yupi.codertestbackend.model.dto.ai.LevelGenerationResponse;
4+
import dev.langchain4j.service.SystemMessage;
5+
import dev.langchain4j.service.UserMessage;
46

57
/**
68
* AI生成关卡服务
@@ -13,5 +15,7 @@ public interface LevelGenerationAiService {
1315
* @param salary 用户当前薪资
1416
* @return 生成的关卡信息
1517
*/
18+
@SystemMessage(fromResource = "prompts/level-generation-system.txt")
19+
@UserMessage("当前薪资:{{salary}}")
1620
LevelGenerationResponse generateLevel(Integer salary);
1721
}

src/main/java/com/yupi/codertestbackend/service/ai/ResultReportAiService.java

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,8 @@
11
package com.yupi.codertestbackend.service.ai;
22

33
import com.yupi.codertestbackend.model.dto.ai.ResultReportResponse;
4+
import dev.langchain4j.service.SystemMessage;
5+
import dev.langchain4j.service.UserMessage;
46

57
/**
68
* AI生成结果报告服务
@@ -17,6 +19,8 @@ public interface ResultReportAiService {
1719
* @param salary 用户当前薪资
1820
* @return 生成的结果报告
1921
*/
22+
@SystemMessage(fromResource = "prompts/result-report-system.txt")
23+
@UserMessage("### 关卡名称\n{{levelName}}\n\n## 关卡的需求描述\n{{levelDesc}}\n\n## 用户选择的选项\n{{userOptions}}\n\n### 本关卡的正确选项\n{{trueOptions}}\n\n### 用户当前的薪资\n{{salary}}")
2024
ResultReportResponse generateResultReport(String levelName, String levelDesc,
2125
String userOptions, String trueOptions, Integer salary);
2226
}

src/main/java/com/yupi/codertestbackend/service/ai/impl/LevelGenerationAiServiceImpl.java

Lines changed: 0 additions & 89 deletions
This file was deleted.

0 commit comments

Comments
 (0)