|
|
|
@ -7,6 +7,8 @@ import com.learning.newdemo.mapper.WxDebateMapper;
|
|
|
|
|
import com.learning.newdemo.service.WxDebateService;
|
|
|
|
|
import com.learning.newdemo.util.JwtUtil;
|
|
|
|
|
import lombok.extern.slf4j.Slf4j;
|
|
|
|
|
import org.json.JSONArray;
|
|
|
|
|
import org.json.JSONObject;
|
|
|
|
|
import org.springframework.beans.factory.annotation.Value;
|
|
|
|
|
import org.springframework.http.*;
|
|
|
|
|
import org.springframework.stereotype.Service;
|
|
|
|
@ -48,12 +50,22 @@ public class WxDebateServiceImpl implements WxDebateService {
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
@Override
|
|
|
|
|
public String GetDebate(String history, String userMessage){
|
|
|
|
|
public String GetDebate(Long conversationId, String userMessage){
|
|
|
|
|
try {
|
|
|
|
|
HttpHeaders headers = new HttpHeaders();
|
|
|
|
|
headers.setContentType(MediaType.APPLICATION_JSON);
|
|
|
|
|
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();
|
|
|
|
|
requestBodyBuilder.append("{")
|
|
|
|
|
.append("\"messages\": [")
|
|
|
|
@ -86,7 +98,17 @@ 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();
|
|
|
|
|
|
|
|
|
|
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){
|
|
|
|
|
log.error("模拟辩论获取失败", e);
|
|
|
|
|
return null;
|
|
|
|
|