Compare commits
4 Commits
cbbc862d56
...
c7319a564d
Author | SHA1 | Date |
---|---|---|
|
c7319a564d | 4 months ago |
|
8daef61652 | 6 months ago |
|
c7e68ac35f | 6 months ago |
|
e0a97ceb8b | 6 months ago |
@ -0,0 +1,5 @@
|
||||
package com.learning.newdemo.service;
|
||||
|
||||
public interface WxReviewService {
|
||||
String getReview(String content);
|
||||
}
|
@ -0,0 +1,91 @@
|
||||
package com.learning.newdemo.service.impl;
|
||||
|
||||
import com.learning.newdemo.service.WxReviewService;
|
||||
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 WxReviewServiceImpl implements WxReviewService {
|
||||
|
||||
private final RestTemplate restTemplate;
|
||||
|
||||
@Value("${ai.argument.header.Authorization}") private String authorizationHeader;
|
||||
@Value("${ai.argument.body.message.role-sys}") private String roleSys;
|
||||
@Value("${ai.review.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 WxReviewServiceImpl(RestTemplate restTemplate) {
|
||||
this.restTemplate = restTemplate;
|
||||
}
|
||||
|
||||
@Override
|
||||
public String getReview(String content) {
|
||||
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\": \"" + content + "\""
|
||||
+ "}"
|
||||
+ "],"
|
||||
+ "\"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");
|
||||
}
|
||||
}
|
Binary file not shown.
Binary file not shown.
Binary file not shown.
Binary file not shown.
Binary file not shown.
Binary file not shown.
Binary file not shown.
Binary file not shown.
@ -1,4 +1,22 @@
|
||||
{
|
||||
"miniprogramRoot": "",
|
||||
"libVersion": "3.0.2"
|
||||
}
|
||||
"libVersion": "3.0.2",
|
||||
"appid": "wxdc5c8df2ec2453e4",
|
||||
"compileType": "miniprogram",
|
||||
"packOptions": {
|
||||
"ignore": [],
|
||||
"include": []
|
||||
},
|
||||
"setting": {
|
||||
"babelSetting": {
|
||||
"ignore": [],
|
||||
"disablePlugins": [],
|
||||
"outputPath": ""
|
||||
}
|
||||
},
|
||||
"condition": {},
|
||||
"editorSetting": {
|
||||
"tabIndent": "insertSpaces",
|
||||
"tabSize": 2
|
||||
}
|
||||
}
|
@ -0,0 +1,19 @@
|
||||
create database if not exists wx_miniapp default charset utf8mb4;
|
||||
|
||||
use wx_miniapp;
|
||||
|
||||
CREATE TABLE IF NOT EXISTS `wx_user` (
|
||||
`id` int(11) NOT NULL AUTO_INCREMENT COMMENT '主键ID',
|
||||
`openid` varchar(100) NOT NULL COMMENT '微信openid',
|
||||
`nickname` varchar(50) DEFAULT NULL COMMENT '昵称',
|
||||
`avatar_url` varchar(500) DEFAULT NULL COMMENT '头像URL',
|
||||
`gender` tinyint(4) DEFAULT NULL COMMENT '性别 0-未知 1-男 2-女',
|
||||
`country` varchar(50) DEFAULT NULL COMMENT '国家',
|
||||
`province` varchar(50) DEFAULT NULL COMMENT '省份',
|
||||
`city` varchar(50) DEFAULT NULL COMMENT '城市',
|
||||
`language` varchar(50) DEFAULT NULL COMMENT '语言',
|
||||
`create_time` datetime DEFAULT CURRENT_TIMESTAMP COMMENT '创建时间',
|
||||
`update_time` datetime DEFAULT NULL ON UPDATE CURRENT_TIMESTAMP COMMENT '更新时间',
|
||||
PRIMARY KEY (`id`),
|
||||
UNIQUE KEY `uk_openid` (`openid`) COMMENT 'openid唯一索引'
|
||||
) ENGINE=InnoDB DEFAULT CHARSET=utf8mb4 COMMENT='微信用户表';
|
Loading…
Reference in new issue