立论后端接口实现

main
Heife 4 months ago
parent 6e4d1365de
commit cbbc862d56

@ -29,6 +29,7 @@ public class WxAIController {
topic = params.get("topic"); topic = params.get("topic");
stance = params.get("stance"); stance = params.get("stance");
if(topic == null || stance == null){ if(topic == null || stance == null){
return Result.error("立论主题或者内容为空"); return Result.error("立论主题或者内容为空");
} }
@ -41,6 +42,9 @@ public class WxAIController {
Map<String, Object> data = new HashMap<>(); Map<String, Object> data = new HashMap<>();
data.put("argument", argument); data.put("argument", argument);
// 查看data
log.info("立论获取成功:{}", argument);
return Result.success(data); return Result.success(data);
}catch (Exception e){ }catch (Exception e){
log.error("立论获取失败", e); log.error("立论获取失败", e);

@ -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");
}
}

@ -19,7 +19,6 @@ wechat:
appid: wxf1f6d7657e01d48a appid: wxf1f6d7657e01d48a
secret: fc356336a118366f27c384079688bc15 secret: fc356336a118366f27c384079688bc15
# AI接口配置
# AI接口配置 # AI接口配置
ai: ai:
argument: argument:
@ -103,15 +102,11 @@ ai:
max_tokens: 2048 max_tokens: 2048
presence_penalty: 0 presence_penalty: 0
response_format: text response_format: text
stop: []
stream: false stream: false
stream_options: []
temperature: 1 temperature: 1
top_p: 1 top_p: 1
tools: [] tool_choice: "none"
tool_choice: []
logprobs: false logprobs: false
top_logprobs: []
# JWT配置 # JWT配置
@ -121,5 +116,5 @@ jwt:
# 服务端口配置 # 服务端口配置
server: server:
port: 8000 port: 8080
address: 0.0.0.0 address: 0.0.0.0

@ -19,7 +19,6 @@ wechat:
appid: wxf1f6d7657e01d48a appid: wxf1f6d7657e01d48a
secret: fc356336a118366f27c384079688bc15 secret: fc356336a118366f27c384079688bc15
# AI接口配置
# AI接口配置 # AI接口配置
ai: ai:
argument: argument:
@ -103,15 +102,11 @@ ai:
max_tokens: 2048 max_tokens: 2048
presence_penalty: 0 presence_penalty: 0
response_format: text response_format: text
stop: []
stream: false stream: false
stream_options: []
temperature: 1 temperature: 1
top_p: 1 top_p: 1
tools: [] tool_choice: "none"
tool_choice: []
logprobs: false logprobs: false
top_logprobs: []
# JWT配置 # JWT配置

Loading…
Cancel
Save