后端轮数

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

@ -1,14 +1,17 @@
package com.learning.newdemo.controller;
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.WxDebateService;
import com.learning.newdemo.service.WxReviewService;
import com.learning.newdemo.util.JwtUtil;
import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.PostMapping;
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.RestController;
@ -27,20 +30,23 @@ public class WxAIController {
@Autowired
private WxDebateService wxDebateService;
@Autowired
private DebateHistoryService debateHistoryService;
@Autowired
private JwtUtil jwtUtil;
private String topic;
private String stance;
private String content;
@PostMapping("/argument")
public Result<Map<String, Object>> getArgument(@RequestBody Map<String, String> params){
public Result<Map<String, Object>> getArgument(@RequestBody Map<String, String> params) {
topic = params.get("topic");
stance = params.get("stance");
if(topic == null || stance == null){
if(topic == null || stance == null) {
return Result.error("立论主题或者内容为空");
}
@ -54,20 +60,18 @@ public class WxAIController {
Map<String, Object> data = new HashMap<>();
data.put("argument", argument);
// 查看data
log.info("立论获取成功:{}", argument);
return Result.success(data);
}catch (Exception e){
} catch (Exception e) {
log.error("立论获取失败", e);
return Result.error("立论获取失败:" + e.getMessage());
}
}
@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);
content = params.get("content");
try {
@ -78,35 +82,70 @@ public class WxAIController {
Map<String, Object> data = new HashMap<>();
data.put("review", review);
// 查看data
log.info("复盘获取成功:{}", review);
return Result.success(data);
}catch (Exception e){
} catch (Exception e) {
log.error("复盘获取失败", e);
return Result.error("复盘获取失败:" + e.getMessage());
}
}
@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);
String history = params.get("history");
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 {
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) {
return Result.error("辩论获取失败");
}
Map<String, Object> data = new HashMap<>();
data.put("debate", debate);
// 查看data
log.info("辩论获取成功:{}", debate);
data.put("currentRound", currentRound);
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);
}catch (Exception e){
} catch (Exception e) {
log.error("辩论获取失败", e);
return Result.error("辩论获取失败:" + e.getMessage());
}
}
}
}

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

@ -1,19 +1,26 @@
package com.learning.newdemo.service.impl;
import com.learning.newdemo.service.WxDebateService;
import com.learning.newdemo.service.WxReviewService;
import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.beans.factory.annotation.Value;
import org.springframework.http.*;
import org.springframework.stereotype.Service;
import org.springframework.web.client.RestTemplate;
import java.util.HashMap;
import java.util.Map;
@Service
@Slf4j
public class WxDebateServiceImpl implements WxDebateService {
// 通过构造函数从IOC容器中注入RestTemplate
private final RestTemplate restTemplate;
@Autowired
private WxReviewService wxReviewService;
@Value("${ai.debate.body.message.content-sys}") private String contentSys;
@Value("${ai.debate.header.Authorization}") private String authorizationHeader;
@ -36,18 +43,29 @@ public class WxDebateServiceImpl implements WxDebateService {
}
@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 {
HttpHeaders headers = new HttpHeaders();
headers.setContentType(MediaType.APPLICATION_JSON);
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();
requestBodyBuilder.append("{")
.append("\"messages\": [")
.append("{")
.append("\"role\": \"").append(roleSys).append("\",")
.append("\"content\": \"").append(escapeJson(contentSys)).append("\"")
.append("\"content\": \"").append(escapeJson(systemPrompt)).append("\"")
.append("},")
.append("{")
.append("\"role\": \"").append(roleUser).append("\",")
@ -74,10 +92,21 @@ public class WxDebateServiceImpl implements WxDebateService {
log.info("请求体:{}", requestBody);
HttpEntity<String> requestEntity = new HttpEntity<>(requestBody, headers);
ResponseEntity<String> response = restTemplate.exchange(url, HttpMethod.POST, requestEntity, String.class);
return response.getBody();
} catch (Exception e){
// 判断辩论是否结束
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) {
log.error("模拟辩论获取失败", e);
return null;
result.put("response", null);
result.put("isEnded", false);
return result;
}
}
@ -90,4 +119,4 @@ public class WxDebateServiceImpl implements WxDebateService {
.replace("\r", "\\r")
.replace("\t", "\\t");
}
}
}
Loading…
Cancel
Save