立论后端接口实现

main
Heife 4 months ago
parent 6e4d1365de
commit cbbc862d56

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

@ -3,7 +3,11 @@ 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.*;
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;
@ -11,66 +15,23 @@ import org.springframework.web.client.RestTemplate;
@Slf4j
public class WxArgumentServiceImpl implements WxArgumentService {
// 依赖注入
private final RestTemplate restTemplate;
// 使用@Value注解注入配置属性
@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.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;
@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;
@ -78,73 +39,57 @@ public class WxArgumentServiceImpl implements WxArgumentService {
@Override
public String getArgument(String topic, String stance) {
try {
HttpHeaders headers = new HttpHeaders();
headers.setContentType(MediaType.APPLICATION_JSON);
headers.set("Authorization", authorizationHeader);
// 构建请求体
String requestBody = String.format(
"{"
// 构建请求体,使用字符串拼接
String requestBody = "{"
+ "\"messages\": ["
+ "{"
+ "\"role\": \"%s\","
+ "\"content\": \"%s\""
+ "\"role\": \"" + roleSys + "\","
+ "\"content\": \"" + escapeJson(contentSys) + "\""
+ "},"
+ "{"
+ "\"role\": \"%s\","
+ "\"content\": \"%s\\n我的立场是%s\""
+ "\"role\": \"" + roleUser + "\","
+ "\"content\": \"" + escapeJson(topic + ",我的立场是" + stance) + "\""
+ "}"
+ "],"
+ "\"model\": \"%s\","
+ "\"frequency_penalty\": %d,"
+ "\"max_tokens\": %d,"
+ "\"presence_penalty\": %d,"
+ "\"response_format\": {"
+ "\"type\": \"%s\""
+ "},"
+ "\"stop\": %s,"
+ "\"stream\": %b,"
+ "\"stream_options\": %s,"
+ "\"temperature\": %f,"
+ "\"top_p\": %f,"
+ "\"tools\": %s,"
+ "\"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
);
+ "\"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);
} 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");
}
}

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

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

Loading…
Cancel
Save