|
|
@ -3,7 +3,11 @@ package com.learning.newdemo.service.impl;
|
|
|
|
import com.learning.newdemo.service.WxArgumentService;
|
|
|
|
import com.learning.newdemo.service.WxArgumentService;
|
|
|
|
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.*;
|
|
|
|
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.stereotype.Service;
|
|
|
|
import org.springframework.web.client.RestTemplate;
|
|
|
|
import org.springframework.web.client.RestTemplate;
|
|
|
|
|
|
|
|
|
|
|
@ -11,66 +15,23 @@ import org.springframework.web.client.RestTemplate;
|
|
|
|
@Slf4j
|
|
|
|
@Slf4j
|
|
|
|
public class WxArgumentServiceImpl implements WxArgumentService {
|
|
|
|
public class WxArgumentServiceImpl implements WxArgumentService {
|
|
|
|
|
|
|
|
|
|
|
|
// 依赖注入
|
|
|
|
|
|
|
|
private final RestTemplate restTemplate;
|
|
|
|
private final RestTemplate restTemplate;
|
|
|
|
|
|
|
|
|
|
|
|
// 使用@Value注解注入配置属性
|
|
|
|
@Value("${ai.argument.header.Authorization}") private String authorizationHeader;
|
|
|
|
@Value("${ai.argument.header.Authorization}")
|
|
|
|
@Value("${ai.argument.body.message.role-sys}") private String roleSys;
|
|
|
|
private String authorizationHeader;
|
|
|
|
@Value("${ai.argument.body.message.content-sys}") private String contentSys;
|
|
|
|
|
|
|
|
@Value("${ai.argument.body.message.role-user}") private String roleUser;
|
|
|
|
@Value("${ai.argument.body.message.role-sys}")
|
|
|
|
@Value("${ai.argument.body.model}") private String model;
|
|
|
|
private String roleSys;
|
|
|
|
@Value("${ai.argument.body.frequency_penalty}") private int frequencyPenalty;
|
|
|
|
|
|
|
|
@Value("${ai.argument.body.max_tokens}") private int maxTokens;
|
|
|
|
@Value("${ai.argument.body.message.content-sys}")
|
|
|
|
@Value("${ai.argument.body.presence_penalty}") private int presencePenalty;
|
|
|
|
private String contentSys;
|
|
|
|
@Value("${ai.argument.body.response_format}") private String responseFormatType;
|
|
|
|
|
|
|
|
@Value("${ai.argument.body.stream}") private boolean stream;
|
|
|
|
@Value("${ai.argument.body.message.role-user}")
|
|
|
|
@Value("${ai.argument.body.temperature}") private double temperature;
|
|
|
|
private String roleUser;
|
|
|
|
@Value("${ai.argument.body.top_p}") private double topP;
|
|
|
|
|
|
|
|
@Value("${ai.argument.body.tool_choice}") private String toolChoice;
|
|
|
|
@Value("${ai.argument.body.model}")
|
|
|
|
@Value("${ai.argument.body.logprobs}") private boolean logprobs;
|
|
|
|
private String model;
|
|
|
|
@Value("${ai.argument.url}") private String url;
|
|
|
|
|
|
|
|
|
|
|
|
@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.stop}")
|
|
|
|
|
|
|
|
private String stop;
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
@Value("${ai.argument.body.stream}")
|
|
|
|
|
|
|
|
private boolean stream;
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
@Value("${ai.argument.body.stream_options}")
|
|
|
|
|
|
|
|
private String streamOptions;
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
@Value("${ai.argument.body.temperature}")
|
|
|
|
|
|
|
|
private double temperature;
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
@Value("${ai.argument.body.top_p}")
|
|
|
|
|
|
|
|
private double topP;
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
@Value("${ai.argument.body.tools}")
|
|
|
|
|
|
|
|
private String tools;
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
@Value("${ai.argument.body.tool_choice}")
|
|
|
|
|
|
|
|
private String toolChoice;
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
@Value("${ai.argument.body.logprobs}")
|
|
|
|
|
|
|
|
private boolean logprobs;
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
@Value("${ai.argument.body.top_logprobs}")
|
|
|
|
|
|
|
|
private String topLogprobs;
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
@Value("${ai.argument.url}")
|
|
|
|
|
|
|
|
private String url;
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
public WxArgumentServiceImpl(RestTemplate restTemplate) {
|
|
|
|
public WxArgumentServiceImpl(RestTemplate restTemplate) {
|
|
|
|
this.restTemplate = restTemplate;
|
|
|
|
this.restTemplate = restTemplate;
|
|
|
@ -78,73 +39,57 @@ public class WxArgumentServiceImpl implements WxArgumentService {
|
|
|
|
|
|
|
|
|
|
|
|
@Override
|
|
|
|
@Override
|
|
|
|
public String getArgument(String topic, String stance) {
|
|
|
|
public String getArgument(String topic, String stance) {
|
|
|
|
|
|
|
|
|
|
|
|
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 requestBody = String.format(
|
|
|
|
String requestBody = "{"
|
|
|
|
"{"
|
|
|
|
+ "\"messages\": ["
|
|
|
|
+ "\"messages\": ["
|
|
|
|
+ "{"
|
|
|
|
+ "{"
|
|
|
|
+ "\"role\": \"" + roleSys + "\","
|
|
|
|
+ "\"role\": \"%s\","
|
|
|
|
+ "\"content\": \"" + escapeJson(contentSys) + "\""
|
|
|
|
+ "\"content\": \"%s\""
|
|
|
|
+ "},"
|
|
|
|
+ "},"
|
|
|
|
+ "{"
|
|
|
|
+ "{"
|
|
|
|
+ "\"role\": \"" + roleUser + "\","
|
|
|
|
+ "\"role\": \"%s\","
|
|
|
|
+ "\"content\": \"" + escapeJson(topic + ",我的立场是" + stance) + "\""
|
|
|
|
+ "\"content\": \"%s\\n我的立场是%s\""
|
|
|
|
+ "}"
|
|
|
|
+ "}"
|
|
|
|
+ "],"
|
|
|
|
+ "],"
|
|
|
|
+ "\"model\": \"" + model + "\","
|
|
|
|
+ "\"model\": \"%s\","
|
|
|
|
+ "\"frequency_penalty\": " + frequencyPenalty + ","
|
|
|
|
+ "\"frequency_penalty\": %d,"
|
|
|
|
+ "\"max_tokens\": " + maxTokens + ","
|
|
|
|
+ "\"max_tokens\": %d,"
|
|
|
|
+ "\"presence_penalty\": " + presencePenalty + ","
|
|
|
|
+ "\"presence_penalty\": %d,"
|
|
|
|
+ "\"response_format\": {\"type\": \"" + responseFormatType + "\"},"
|
|
|
|
+ "\"response_format\": {"
|
|
|
|
+ "\"stop\": null,"
|
|
|
|
+ "\"type\": \"%s\""
|
|
|
|
+ "\"stream\": " + stream + ","
|
|
|
|
+ "},"
|
|
|
|
+ "\"stream_options\": null,"
|
|
|
|
+ "\"stop\": %s,"
|
|
|
|
+ "\"temperature\": " + temperature + ","
|
|
|
|
+ "\"stream\": %b,"
|
|
|
|
+ "\"top_p\": " + topP + ","
|
|
|
|
+ "\"stream_options\": %s,"
|
|
|
|
+ "\"tools\": null,"
|
|
|
|
+ "\"temperature\": %f,"
|
|
|
|
+ "\"tool_choice\": \"" + toolChoice + "\","
|
|
|
|
+ "\"top_p\": %f,"
|
|
|
|
+ "\"logprobs\": " + logprobs + ","
|
|
|
|
+ "\"tools\": %s,"
|
|
|
|
+ "\"top_logprobs\": null"
|
|
|
|
+ "\"tool_choice\": \"%s\","
|
|
|
|
+ "}";
|
|
|
|
+ "\"logprobs\": %b,"
|
|
|
|
|
|
|
|
+ "\"top_logprobs\": %s"
|
|
|
|
|
|
|
|
+ "}",
|
|
|
|
|
|
|
|
roleSys,
|
|
|
|
|
|
|
|
contentSys,
|
|
|
|
|
|
|
|
roleUser,
|
|
|
|
|
|
|
|
topic,
|
|
|
|
|
|
|
|
stance,
|
|
|
|
|
|
|
|
model,
|
|
|
|
|
|
|
|
frequencyPenalty,
|
|
|
|
|
|
|
|
maxTokens,
|
|
|
|
|
|
|
|
presencePenalty,
|
|
|
|
|
|
|
|
responseFormatType,
|
|
|
|
|
|
|
|
stop,
|
|
|
|
|
|
|
|
stream,
|
|
|
|
|
|
|
|
streamOptions,
|
|
|
|
|
|
|
|
temperature,
|
|
|
|
|
|
|
|
topP,
|
|
|
|
|
|
|
|
tools,
|
|
|
|
|
|
|
|
toolChoice,
|
|
|
|
|
|
|
|
logprobs,
|
|
|
|
|
|
|
|
topLogprobs
|
|
|
|
|
|
|
|
);
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
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();
|
|
|
|
return response.getBody();
|
|
|
|
}catch(Exception e){
|
|
|
|
} catch (Exception e) {
|
|
|
|
log.error("向AI获取立论失败",e);
|
|
|
|
log.error("向AI获取立论失败", e);
|
|
|
|
return null;
|
|
|
|
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");
|
|
|
|
|
|
|
|
}
|
|
|
|
|
|
|
|
}
|