对话内容的content提取和辩论历史记录通过数据库获取

hot-fix
heiferleaf 2 months ago
parent 16dccb1f00
commit da100df835

@ -77,6 +77,12 @@
<scope>runtime</scope> <scope>runtime</scope>
</dependency> </dependency>
<dependency>
<groupId>org.json</groupId>
<artifactId>json</artifactId>
<version>20240303</version>
</dependency>
<!-- 测试依赖 --> <!-- 测试依赖 -->
<dependency> <dependency>
<groupId>org.springframework.boot</groupId> <groupId>org.springframework.boot</groupId>

@ -96,12 +96,11 @@ public class WxAIController {
@PostMapping("/debate") @PostMapping("/debate")
public Result<Map<String, Object>> debate(@RequestHeader ("Authorization") String token, @RequestBody Map<String, String> params){ public Result<Map<String, Object>> debate(@RequestHeader ("Authorization") String token, @RequestBody Map<String, String> params){
log.info("请求内容: {}", params); log.info("请求内容: {}", params);
String history = params.get("history");
String userMessage = params.get("userMessage"); String userMessage = params.get("userMessage");
conversationId = Long.parseLong(params.get("conversationId")); conversationId = Long.parseLong(params.get("conversationId"));
try { try {
String debate = wxDebateService.GetDebate(history, userMessage); String debate = wxDebateService.GetDebate(conversationId, userMessage);
if (debate == null) { if (debate == null) {
return Result.error("辩论获取失败"); return Result.error("辩论获取失败");
} }

@ -1,6 +1,6 @@
package com.learning.newdemo.service; package com.learning.newdemo.service;
public interface WxDebateService { public interface WxDebateService {
String GetDebate(String history, String userMessage); String GetDebate(Long conversationId, String userMessage);
long UpdateDebate(Long conversationId, String content, String userMessage, String token); long UpdateDebate(Long conversationId, String content, String userMessage, String token);
} }

@ -1,19 +1,16 @@
package com.learning.newdemo.service.impl; package com.learning.newdemo.service.impl;
import org.json.JSONObject;
import org.json.JSONArray;
import com.learning.newdemo.entity.WxArgument; import com.learning.newdemo.entity.WxArgument;
import com.learning.newdemo.entity.WxConversation; import com.learning.newdemo.entity.WxConversation;
import com.learning.newdemo.mapper.WxArgumentMapper; import com.learning.newdemo.mapper.WxArgumentMapper;
import com.learning.newdemo.mapper.WxConversationMapper; import com.learning.newdemo.mapper.WxConversationMapper;
import com.learning.newdemo.service.WxArgumentService; import com.learning.newdemo.service.WxArgumentService;
import com.learning.newdemo.util.JwtUtil; import com.learning.newdemo.util.JwtUtil;
import lombok.Data;
import lombok.extern.slf4j.Slf4j; import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.factory.annotation.Value; import org.springframework.beans.factory.annotation.Value;
import org.springframework.http.HttpEntity; import org.springframework.http.*;
import org.springframework.http.HttpHeaders;
import org.springframework.http.HttpMethod;
import org.springframework.http.MediaType;
import org.springframework.http.ResponseEntity;
import org.springframework.stereotype.Service; import org.springframework.stereotype.Service;
import org.springframework.web.client.RestTemplate; import org.springframework.web.client.RestTemplate;
@ -90,7 +87,16 @@ public class WxArgumentServiceImpl implements WxArgumentService {
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();
if (response.getStatusCode() == HttpStatus.OK && response.getBody() != null) {
JSONObject json = new JSONObject(response.getBody());
JSONArray choices = json.getJSONArray("choices");
if (!choices.isEmpty()) {
JSONObject message = choices.getJSONObject(0).getJSONObject("message");
return message.getString("content");
}
}
return null;
} catch (Exception e) { } catch (Exception e) {
log.error("向AI获取立论失败", e); log.error("向AI获取立论失败", e);
return null; return null;

@ -7,6 +7,8 @@ import com.learning.newdemo.mapper.WxDebateMapper;
import com.learning.newdemo.service.WxDebateService; import com.learning.newdemo.service.WxDebateService;
import com.learning.newdemo.util.JwtUtil; import com.learning.newdemo.util.JwtUtil;
import lombok.extern.slf4j.Slf4j; import lombok.extern.slf4j.Slf4j;
import org.json.JSONArray;
import org.json.JSONObject;
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;
@ -48,12 +50,22 @@ public class WxDebateServiceImpl implements WxDebateService {
} }
@Override @Override
public String GetDebate(String history, String userMessage){ public String GetDebate(Long conversationId, String userMessage){
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 history;
if(conversationId == -1){
history = "";
}
else {
history = wxDebateMapper.selectByConversationId(conversationId).stream()
.map(WxDebate::getContent)
.reduce("", (a, b) -> a + b);
}
StringBuilder requestBodyBuilder = new StringBuilder(); StringBuilder requestBodyBuilder = new StringBuilder();
requestBodyBuilder.append("{") requestBodyBuilder.append("{")
.append("\"messages\": [") .append("\"messages\": [")
@ -86,7 +98,17 @@ 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();
if (response.getStatusCode() == HttpStatus.OK && response.getBody() != null) {
JSONObject json = new JSONObject(response.getBody());
JSONArray choices = json.getJSONArray("choices");
if (!choices.isEmpty()) {
JSONObject message = choices.getJSONObject(0).getJSONObject("message");
return message.getString("content");
}
}
return null;
} catch (Exception e){ } catch (Exception e){
log.error("模拟辩论获取失败", e); log.error("模拟辩论获取失败", e);
return null; return null;

@ -7,6 +7,8 @@ import com.learning.newdemo.mapper.WxReviewMapper;
import com.learning.newdemo.service.WxReviewService; import com.learning.newdemo.service.WxReviewService;
import com.learning.newdemo.util.JwtUtil; import com.learning.newdemo.util.JwtUtil;
import lombok.extern.slf4j.Slf4j; import lombok.extern.slf4j.Slf4j;
import org.json.JSONArray;
import org.json.JSONObject;
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;
@ -85,7 +87,16 @@ public class WxReviewServiceImpl implements WxReviewService {
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();
if (response.getStatusCode() == HttpStatus.OK && response.getBody() != null) {
JSONObject json = new JSONObject(response.getBody());
JSONArray choices = json.getJSONArray("choices");
if (!choices.isEmpty()) {
JSONObject message = choices.getJSONObject(0).getJSONObject("message");
return message.getString("content");
}
}
return null;
} catch (Exception e) { } catch (Exception e) {
log.error("向AI获取立论失败", e); log.error("向AI获取立论失败", e);
return null; return null;

Loading…
Cancel
Save