parent
c7319a564d
commit
2bee93f3be
@ -1,9 +1,9 @@
|
||||
package com.learning.newdemo.service;
|
||||
|
||||
/**
|
||||
* 微信立论助手接口
|
||||
*/
|
||||
|
||||
public interface WxArgumentService {
|
||||
String getArgument(String topic, String stance);
|
||||
}
|
||||
package com.learning.newdemo.service;
|
||||
|
||||
/**
|
||||
* 微信立论助手接口
|
||||
*/
|
||||
|
||||
public interface WxArgumentService {
|
||||
String GetArgument(String topic, String stance);
|
||||
}
|
||||
|
@ -0,0 +1,5 @@
|
||||
package com.learning.newdemo.service;
|
||||
|
||||
public interface WxDebateService {
|
||||
String GetDebate(String history, String userMessage);
|
||||
}
|
@ -1,5 +1,5 @@
|
||||
package com.learning.newdemo.service;
|
||||
|
||||
public interface WxReviewService {
|
||||
String getReview(String content);
|
||||
String GetReview(String content);
|
||||
}
|
||||
|
@ -1,95 +1,95 @@
|
||||
package com.learning.newdemo.service.impl;
|
||||
|
||||
import com.learning.newdemo.service.WxArgumentService;
|
||||
import lombok.extern.slf4j.Slf4j;
|
||||
import org.springframework.beans.factory.annotation.Value;
|
||||
import org.springframework.http.HttpEntity;
|
||||
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.web.client.RestTemplate;
|
||||
|
||||
@Service
|
||||
@Slf4j
|
||||
public class WxArgumentServiceImpl implements WxArgumentService {
|
||||
|
||||
private final RestTemplate restTemplate;
|
||||
|
||||
@Value("${ai.argument.header.Authorization}") private String authorizationHeader;
|
||||
@Value("${ai.argument.body.message.role-sys}") private String roleSys;
|
||||
@Value("${ai.argument.body.message.content-sys}") private String contentSys;
|
||||
@Value("${ai.argument.body.message.role-user}") private String roleUser;
|
||||
@Value("${ai.argument.body.model}") private String model;
|
||||
@Value("${ai.argument.body.frequency_penalty}") private int frequencyPenalty;
|
||||
@Value("${ai.argument.body.max_tokens}") private int maxTokens;
|
||||
@Value("${ai.argument.body.presence_penalty}") private int presencePenalty;
|
||||
@Value("${ai.argument.body.response_format}") private String responseFormatType;
|
||||
@Value("${ai.argument.body.stream}") private boolean stream;
|
||||
@Value("${ai.argument.body.temperature}") private double temperature;
|
||||
@Value("${ai.argument.body.top_p}") private double topP;
|
||||
@Value("${ai.argument.body.tool_choice}") private String toolChoice;
|
||||
@Value("${ai.argument.body.logprobs}") private boolean logprobs;
|
||||
@Value("${ai.argument.url}") private String url;
|
||||
|
||||
public WxArgumentServiceImpl(RestTemplate restTemplate) {
|
||||
this.restTemplate = restTemplate;
|
||||
}
|
||||
|
||||
@Override
|
||||
public String getArgument(String topic, String stance) {
|
||||
try {
|
||||
HttpHeaders headers = new HttpHeaders();
|
||||
headers.setContentType(MediaType.APPLICATION_JSON);
|
||||
headers.set("Authorization", authorizationHeader);
|
||||
|
||||
// 构建请求体,使用字符串拼接
|
||||
String requestBody = "{"
|
||||
+ "\"messages\": ["
|
||||
+ "{"
|
||||
+ "\"role\": \"" + roleSys + "\","
|
||||
+ "\"content\": \"" + escapeJson(contentSys) + "\""
|
||||
+ "},"
|
||||
+ "{"
|
||||
+ "\"role\": \"" + roleUser + "\","
|
||||
+ "\"content\": \"" + escapeJson(topic + ",我的立场是" + stance) + "\""
|
||||
+ "}"
|
||||
+ "],"
|
||||
+ "\"model\": \"" + model + "\","
|
||||
+ "\"frequency_penalty\": " + frequencyPenalty + ","
|
||||
+ "\"max_tokens\": " + maxTokens + ","
|
||||
+ "\"presence_penalty\": " + presencePenalty + ","
|
||||
+ "\"response_format\": {\"type\": \"" + responseFormatType + "\"},"
|
||||
+ "\"stop\": null,"
|
||||
+ "\"stream\": " + stream + ","
|
||||
+ "\"stream_options\": null,"
|
||||
+ "\"temperature\": " + temperature + ","
|
||||
+ "\"top_p\": " + topP + ","
|
||||
+ "\"tools\": null,"
|
||||
+ "\"tool_choice\": \"" + toolChoice + "\","
|
||||
+ "\"logprobs\": " + logprobs + ","
|
||||
+ "\"top_logprobs\": null"
|
||||
+ "}";
|
||||
|
||||
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) {
|
||||
log.error("向AI获取立论失败", e);
|
||||
return null;
|
||||
}
|
||||
}
|
||||
|
||||
// 工具方法:转义 JSON 字符串中的特殊字符
|
||||
private String escapeJson(String input) {
|
||||
if (input == null) return "";
|
||||
return input.replace("\\", "\\\\")
|
||||
.replace("\"", "\\\"")
|
||||
.replace("\n", "\\n")
|
||||
.replace("\r", "\\r")
|
||||
.replace("\t", "\\t");
|
||||
}
|
||||
package com.learning.newdemo.service.impl;
|
||||
|
||||
import com.learning.newdemo.service.WxArgumentService;
|
||||
import lombok.extern.slf4j.Slf4j;
|
||||
import org.springframework.beans.factory.annotation.Value;
|
||||
import org.springframework.http.HttpEntity;
|
||||
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.web.client.RestTemplate;
|
||||
|
||||
@Service
|
||||
@Slf4j
|
||||
public class WxArgumentServiceImpl implements WxArgumentService {
|
||||
|
||||
private final RestTemplate restTemplate;
|
||||
|
||||
@Value("${ai.argument.header.Authorization}") private String authorizationHeader;
|
||||
@Value("${ai.argument.body.message.role-sys}") private String roleSys;
|
||||
@Value("${ai.argument.body.message.content-sys}") private String contentSys;
|
||||
@Value("${ai.argument.body.message.role-user}") private String roleUser;
|
||||
@Value("${ai.argument.body.model}") private String model;
|
||||
@Value("${ai.argument.body.frequency_penalty}") private int frequencyPenalty;
|
||||
@Value("${ai.argument.body.max_tokens}") private int maxTokens;
|
||||
@Value("${ai.argument.body.presence_penalty}") private int presencePenalty;
|
||||
@Value("${ai.argument.body.response_format}") private String responseFormatType;
|
||||
@Value("${ai.argument.body.stream}") private boolean stream;
|
||||
@Value("${ai.argument.body.temperature}") private double temperature;
|
||||
@Value("${ai.argument.body.top_p}") private double topP;
|
||||
@Value("${ai.argument.body.tool_choice}") private String toolChoice;
|
||||
@Value("${ai.argument.body.logprobs}") private boolean logprobs;
|
||||
@Value("${ai.argument.url}") private String url;
|
||||
|
||||
public WxArgumentServiceImpl(RestTemplate restTemplate) {
|
||||
this.restTemplate = restTemplate;
|
||||
}
|
||||
|
||||
@Override
|
||||
public String GetArgument(String topic, String stance) {
|
||||
try {
|
||||
HttpHeaders headers = new HttpHeaders();
|
||||
headers.setContentType(MediaType.APPLICATION_JSON);
|
||||
headers.set("Authorization", authorizationHeader);
|
||||
|
||||
// 构建请求体,使用字符串拼接
|
||||
String requestBody = "{"
|
||||
+ "\"messages\": ["
|
||||
+ "{"
|
||||
+ "\"role\": \"" + roleSys + "\","
|
||||
+ "\"content\": \"" + escapeJson(contentSys) + "\""
|
||||
+ "},"
|
||||
+ "{"
|
||||
+ "\"role\": \"" + roleUser + "\","
|
||||
+ "\"content\": \"" + escapeJson(topic + ",我的立场是" + stance) + "\""
|
||||
+ "}"
|
||||
+ "],"
|
||||
+ "\"model\": \"" + model + "\","
|
||||
+ "\"frequency_penalty\": " + frequencyPenalty + ","
|
||||
+ "\"max_tokens\": " + maxTokens + ","
|
||||
+ "\"presence_penalty\": " + presencePenalty + ","
|
||||
+ "\"response_format\": {\"type\": \"" + responseFormatType + "\"},"
|
||||
+ "\"stop\": null,"
|
||||
+ "\"stream\": " + stream + ","
|
||||
+ "\"stream_options\": null,"
|
||||
+ "\"temperature\": " + temperature + ","
|
||||
+ "\"top_p\": " + topP + ","
|
||||
+ "\"tools\": null,"
|
||||
+ "\"tool_choice\": \"" + toolChoice + "\","
|
||||
+ "\"logprobs\": " + logprobs + ","
|
||||
+ "\"top_logprobs\": null"
|
||||
+ "}";
|
||||
|
||||
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) {
|
||||
log.error("向AI获取立论失败", e);
|
||||
return null;
|
||||
}
|
||||
}
|
||||
|
||||
// 工具方法:转义 JSON 字符串中的特殊字符
|
||||
private String escapeJson(String input) {
|
||||
if (input == null) return "";
|
||||
return input.replace("\\", "\\\\")
|
||||
.replace("\"", "\\\"")
|
||||
.replace("\n", "\\n")
|
||||
.replace("\r", "\\r")
|
||||
.replace("\t", "\\t");
|
||||
}
|
||||
}
|
@ -0,0 +1,93 @@
|
||||
package com.learning.newdemo.service.impl;
|
||||
|
||||
import com.learning.newdemo.service.WxDebateService;
|
||||
import lombok.extern.slf4j.Slf4j;
|
||||
import org.springframework.beans.factory.annotation.Value;
|
||||
import org.springframework.http.*;
|
||||
import org.springframework.stereotype.Service;
|
||||
import org.springframework.web.client.RestTemplate;
|
||||
|
||||
|
||||
|
||||
@Service
|
||||
@Slf4j
|
||||
public class WxDebateServiceImpl implements WxDebateService {
|
||||
// 通过构造函数从IOC容器中注入RestTemplate
|
||||
private final RestTemplate restTemplate;
|
||||
|
||||
@Value("${ai.debate.body.message.content-sys}") private String contentSys;
|
||||
@Value("${ai.debate.header.Authorization}") private String authorizationHeader;
|
||||
@Value("${ai.debate.body.message.role-user}") private String roleUser;
|
||||
@Value("${ai.debate.body.model}") private String model;
|
||||
@Value("${ai.debate.body.message.role-sys}") private String roleSys;
|
||||
@Value("${ai.debate.body.frequency_penalty}") private int frequencyPenalty;
|
||||
@Value("${ai.debate.body.max_tokens}") private int maxTokens;
|
||||
@Value("${ai.debate.body.presence_penalty}") private int presencePenalty;
|
||||
@Value("${ai.debate.body.response_format}") private String responseFormatType;
|
||||
@Value("${ai.debate.body.stream}") private boolean stream;
|
||||
@Value("${ai.debate.body.temperature}") private double temperature;
|
||||
@Value("${ai.debate.body.top_p}") private double topP;
|
||||
@Value("${ai.debate.body.tool_choice}") private String toolChoice;
|
||||
@Value("${ai.debate.body.logprobs}") private boolean logprobs;
|
||||
@Value("${ai.debate.url}") private String url;
|
||||
|
||||
public WxDebateServiceImpl(RestTemplate restTemplate) {
|
||||
this.restTemplate = restTemplate;
|
||||
}
|
||||
|
||||
@Override
|
||||
public String GetDebate(String history, String userMessage){
|
||||
try {
|
||||
HttpHeaders headers = new HttpHeaders();
|
||||
headers.setContentType(MediaType.APPLICATION_JSON);
|
||||
headers.set("Authorization", authorizationHeader);
|
||||
|
||||
StringBuilder requestBodyBuilder = new StringBuilder();
|
||||
requestBodyBuilder.append("{")
|
||||
.append("\"messages\": [")
|
||||
.append("{")
|
||||
.append("\"role\": \"").append(roleSys).append("\",")
|
||||
.append("\"content\": \"").append(escapeJson(contentSys)).append("\"")
|
||||
.append("},")
|
||||
.append("{")
|
||||
.append("\"role\": \"").append(roleUser).append("\",")
|
||||
.append("\"content\": \"").append(escapeJson("历史对话记录" + history + ",用户发言为" + userMessage)).append("\"")
|
||||
.append("}")
|
||||
.append("],")
|
||||
.append("\"model\": \"").append(model).append("\",")
|
||||
.append("\"frequency_penalty\": ").append(frequencyPenalty).append(",")
|
||||
.append("\"max_tokens\": ").append(maxTokens).append(",")
|
||||
.append("\"presence_penalty\": ").append(presencePenalty).append(",")
|
||||
.append("\"response_format\": {\"type\": \"").append(responseFormatType).append("\"},")
|
||||
.append("\"stop\": null,")
|
||||
.append("\"stream\": ").append(stream).append(",")
|
||||
.append("\"stream_options\": null,")
|
||||
.append("\"temperature\": ").append(temperature).append(",")
|
||||
.append("\"top_p\": ").append(topP).append(",")
|
||||
.append("\"tools\": null,")
|
||||
.append("\"tool_choice\": \"").append(toolChoice).append("\",")
|
||||
.append("\"logprobs\": ").append(logprobs).append(",")
|
||||
.append("\"top_logprobs\": null")
|
||||
.append("}");
|
||||
String requestBody = requestBodyBuilder.toString();
|
||||
|
||||
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){
|
||||
log.error("模拟辩论获取失败", e);
|
||||
return null;
|
||||
}
|
||||
}
|
||||
|
||||
// 工具方法:转义 JSON 字符串中的特殊字符
|
||||
private String escapeJson(String input) {
|
||||
if (input == null) return "";
|
||||
return input.replace("\\", "\\\\")
|
||||
.replace("\"", "\\\"")
|
||||
.replace("\n", "\\n")
|
||||
.replace("\r", "\\r")
|
||||
.replace("\t", "\\t");
|
||||
}
|
||||
}
|
@ -1,2 +0,0 @@
|
||||
# 主要配置在application.yml中
|
||||
# 此文件用于避免IDE报错
|
Loading…
Reference in new issue