后端轮数

main
guo-yao-whu 2 months ago
parent 9f8fef15c7
commit d40a16a1b6

@ -1,14 +1,17 @@
package com.learning.newdemo.controller; package com.learning.newdemo.controller;
import com.learning.newdemo.common.Result; import com.learning.newdemo.common.Result;
import com.learning.newdemo.entity.DebateHistory;
import com.learning.newdemo.service.DebateHistoryService;
import com.learning.newdemo.service.WxArgumentService; import com.learning.newdemo.service.WxArgumentService;
import com.learning.newdemo.service.WxDebateService; import com.learning.newdemo.service.WxDebateService;
import com.learning.newdemo.service.WxReviewService; import com.learning.newdemo.service.WxReviewService;
import com.learning.newdemo.util.JwtUtil;
import lombok.extern.slf4j.Slf4j; import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.factory.annotation.Autowired; import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.PostMapping; import org.springframework.web.bind.annotation.PostMapping;
import org.springframework.web.bind.annotation.RequestBody; import org.springframework.web.bind.annotation.RequestBody;
import org.springframework.web.bind.annotation.RequestHeader;
import org.springframework.web.bind.annotation.RequestMapping; import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController; import org.springframework.web.bind.annotation.RestController;
@ -28,10 +31,14 @@ public class WxAIController {
@Autowired @Autowired
private WxDebateService wxDebateService; private WxDebateService wxDebateService;
private String topic; @Autowired
private DebateHistoryService debateHistoryService;
private String stance; @Autowired
private JwtUtil jwtUtil;
private String topic;
private String stance;
private String content; private String content;
@PostMapping("/argument") @PostMapping("/argument")
@ -39,7 +46,6 @@ public class WxAIController {
topic = params.get("topic"); topic = params.get("topic");
stance = params.get("stance"); stance = params.get("stance");
if(topic == null || stance == null) { if(topic == null || stance == null) {
return Result.error("立论主题或者内容为空"); return Result.error("立论主题或者内容为空");
} }
@ -54,7 +60,6 @@ public class WxAIController {
Map<String, Object> data = new HashMap<>(); Map<String, Object> data = new HashMap<>();
data.put("argument", argument); data.put("argument", argument);
// 查看data
log.info("立论获取成功:{}", argument); log.info("立论获取成功:{}", argument);
return Result.success(data); return Result.success(data);
@ -67,7 +72,6 @@ public class WxAIController {
@PostMapping("/review") @PostMapping("/review")
public Result<Map<String, Object>> review(@RequestBody Map<String, String> params) { public Result<Map<String, Object>> review(@RequestBody Map<String, String> params) {
log.info("请求内容: {}", params); log.info("请求内容: {}", params);
content = params.get("content"); content = params.get("content");
try { try {
@ -78,7 +82,6 @@ public class WxAIController {
Map<String, Object> data = new HashMap<>(); Map<String, Object> data = new HashMap<>();
data.put("review", review); data.put("review", review);
// 查看data
log.info("复盘获取成功:{}", review); log.info("复盘获取成功:{}", review);
return Result.success(data); return Result.success(data);
@ -87,22 +90,58 @@ public class WxAIController {
return Result.error("复盘获取失败:" + e.getMessage()); return Result.error("复盘获取失败:" + e.getMessage());
} }
} }
@PostMapping("/debate") @PostMapping("/debate")
public Result<Map<String, Object>> debate(@RequestBody Map<String, String> params){ public Result<Map<String, Object>> debate(@RequestBody Map<String, String> params,
@RequestHeader("Authorization") String token) {
log.info("请求内容: {}", params); log.info("请求内容: {}", params);
String history = params.get("history"); String history = params.get("history");
String userMessage = params.get("userMessage"); String userMessage = params.get("userMessage");
Integer currentRound = params.get("currentRound") != null ? Integer.parseInt(params.get("currentRound")) : 1;
String debateMode = params.get("debateMode"); // "ten" 或 "twenty"
// 根据辩论模式设置最大轮数
int maxRounds = "twenty".equals(debateMode) ? 20 : 10;
try { try {
String debate = wxDebateService.GetDebate(history, userMessage); // 获取辩论结果
Map<String, Object> debateResult = wxDebateService.GetDebate(history, userMessage, currentRound, maxRounds);
String debate = (String) debateResult.get("response");
boolean isDebateEnded = (boolean) debateResult.get("isEnded");
if (debate == null) { if (debate == null) {
return Result.error("辩论获取失败"); return Result.error("辩论获取失败");
} }
Map<String, Object> data = new HashMap<>(); Map<String, Object> data = new HashMap<>();
data.put("debate", debate); data.put("debate", debate);
// 查看data data.put("currentRound", currentRound);
log.info("辩论获取成功:{}", debate); data.put("maxRounds", maxRounds);
data.put("isEnded", isDebateEnded);
data.put("debateMode", debateMode);
// 如果辩论结束,自动保存历史记录
if (isDebateEnded) {
Integer userId = jwtUtil.getUserIdFromToken(token);
if (userId != null) {
DebateHistory debateHistory = new DebateHistory();
debateHistory.setUserId(userId);
debateHistory.setTopic(topic);
debateHistory.setStance(stance);
debateHistory.setContent(history + "\n\n用户发言: " + userMessage + "\n\nAI回复: " + debate);
debateHistory.setRounds(currentRound);
// 生成并保存复盘
String review = wxReviewService.GetReview(debateHistory.getContent());
debateHistory.setReview(review);
debateHistoryService.saveDebateHistory(debateHistory);
data.put("review", review);
data.put("historyId", debateHistory.getId());
}
}
log.info("辩论获取成功:{}", debate);
return Result.success(data); return Result.success(data);
} catch (Exception e) { } catch (Exception e) {
log.error("辩论获取失败", e); log.error("辩论获取失败", e);

@ -1,5 +1,9 @@
package com.learning.newdemo.service; package com.learning.newdemo.service;
import java.util.Map;
public interface WxDebateService { public interface WxDebateService {
String GetDebate(String history, String userMessage); Map<String, Object> GetDebate(String history, String userMessage, int currentRound, int maxRounds);
} }

@ -1,20 +1,27 @@
package com.learning.newdemo.service.impl; package com.learning.newdemo.service.impl;
import com.learning.newdemo.service.WxDebateService; import com.learning.newdemo.service.WxDebateService;
import com.learning.newdemo.service.WxReviewService;
import lombok.extern.slf4j.Slf4j; import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.beans.factory.annotation.Value; import org.springframework.beans.factory.annotation.Value;
import org.springframework.http.*; import org.springframework.http.*;
import org.springframework.stereotype.Service; import org.springframework.stereotype.Service;
import org.springframework.web.client.RestTemplate; import org.springframework.web.client.RestTemplate;
import java.util.HashMap;
import java.util.Map;
@Service @Service
@Slf4j @Slf4j
public class WxDebateServiceImpl implements WxDebateService { public class WxDebateServiceImpl implements WxDebateService {
// 通过构造函数从IOC容器中注入RestTemplate
private final RestTemplate restTemplate; private final RestTemplate restTemplate;
@Autowired
private WxReviewService wxReviewService;
@Value("${ai.debate.body.message.content-sys}") private String contentSys; @Value("${ai.debate.body.message.content-sys}") private String contentSys;
@Value("${ai.debate.header.Authorization}") private String authorizationHeader; @Value("${ai.debate.header.Authorization}") private String authorizationHeader;
@Value("${ai.debate.body.message.role-user}") private String roleUser; @Value("${ai.debate.body.message.role-user}") private String roleUser;
@ -36,18 +43,29 @@ public class WxDebateServiceImpl implements WxDebateService {
} }
@Override @Override
public String GetDebate(String history, String userMessage){ public Map<String, Object> GetDebate(String history, String userMessage, int currentRound, int maxRounds) {
Map<String, Object> result = new HashMap<>();
try { try {
HttpHeaders headers = new HttpHeaders(); HttpHeaders headers = new HttpHeaders();
headers.setContentType(MediaType.APPLICATION_JSON); headers.setContentType(MediaType.APPLICATION_JSON);
headers.set("Authorization", authorizationHeader); headers.set("Authorization", authorizationHeader);
// 构建系统提示,包含轮数信息
String systemPrompt = contentSys + String.format("\n当前是第%d轮辩论共%d轮。", currentRound, maxRounds);
if (currentRound >= maxRounds) {
systemPrompt += "\n这是最后一轮辩论请给出总结性回复。";
} else if (maxRounds == 10) {
systemPrompt += "\n这是10回合快速对辩模式请保持回答简洁有力。";
} else if (maxRounds == 20) {
systemPrompt += "\n这是20回合深度对辩模式可以进行更深入的分析和讨论。";
}
StringBuilder requestBodyBuilder = new StringBuilder(); StringBuilder requestBodyBuilder = new StringBuilder();
requestBodyBuilder.append("{") requestBodyBuilder.append("{")
.append("\"messages\": [") .append("\"messages\": [")
.append("{") .append("{")
.append("\"role\": \"").append(roleSys).append("\",") .append("\"role\": \"").append(roleSys).append("\",")
.append("\"content\": \"").append(escapeJson(contentSys)).append("\"") .append("\"content\": \"").append(escapeJson(systemPrompt)).append("\"")
.append("},") .append("},")
.append("{") .append("{")
.append("\"role\": \"").append(roleUser).append("\",") .append("\"role\": \"").append(roleUser).append("\",")
@ -74,10 +92,21 @@ public class WxDebateServiceImpl implements WxDebateService {
log.info("请求体:{}", requestBody); log.info("请求体:{}", requestBody);
HttpEntity<String> requestEntity = new HttpEntity<>(requestBody, headers); HttpEntity<String> requestEntity = new HttpEntity<>(requestBody, headers);
ResponseEntity<String> response = restTemplate.exchange(url, HttpMethod.POST, requestEntity, String.class); ResponseEntity<String> response = restTemplate.exchange(url, HttpMethod.POST, requestEntity, String.class);
return response.getBody();
// 判断辩论是否结束
boolean isDebateEnded = currentRound >= maxRounds;
result.put("response", response.getBody());
result.put("currentRound", currentRound);
result.put("maxRounds", maxRounds);
result.put("isEnded", isDebateEnded);
return result;
} catch (Exception e) { } catch (Exception e) {
log.error("模拟辩论获取失败", e); log.error("模拟辩论获取失败", e);
return null; result.put("response", null);
result.put("isEnded", false);
return result;
} }
} }

Loading…
Cancel
Save