zyx 2 лет назад
Родитель
Сommit
4306fea786

+ 7 - 0
build.gradle

@@ -20,6 +20,13 @@ repositories {
 }
 
 dependencies {
+
+    implementation 'com.github.ben-manes.caffeine:caffeine:2.9.2'
+    implementation 'mysql:mysql-connector-java:8.0.28'
+
+    implementation 'com.baomidou:mybatis-plus-boot-starter:3.5.3'
+    implementation 'com.baomidou:dynamic-datasource-spring-boot-starter:3.6.1'
+
     implementation 'commons-io:commons-io:2.11.0'
     implementation 'com.aliyun.openservices:aliyun-log-producer:0.3.11'
     implementation 'org.apache.httpcomponents.client5:httpclient5:5.2.1'

+ 1 - 1
build.sh

@@ -2,6 +2,6 @@ cd `dirname $0`
 git reset HEAD --hard
 git pull
 pm2 stop chatgpt
-gradle build -Dorg.gradle.java.home=/opt/jdk-17.0.4.1
+gradle build -x test -Dorg.gradle.java.home=/opt/jdk-17.0.4.1
 pm2 start chatgpt
 # pm2 start 'java -jar /home/chatgpt/build/libs/chatgpt-0.0.1-SNAPSHOT.jar' --name chatgpt

+ 2 - 0
src/main/java/com/sxtvs/ChatgptApplication.java

@@ -1,5 +1,6 @@
 package com.sxtvs;
 
+import org.mybatis.spring.annotation.MapperScan;
 import org.springframework.boot.SpringApplication;
 import org.springframework.boot.autoconfigure.SpringBootApplication;
 
@@ -7,6 +8,7 @@ import java.net.http.HttpClient;
 import java.net.http.HttpRequest;
 
 @SpringBootApplication
+@MapperScan("com.sxtvs")
 public class ChatgptApplication {
 
     public static void main(String[] args) {

+ 15 - 12
src/main/java/com/sxtvs/api/chatgpt/service/ChatGptService.java

@@ -5,6 +5,7 @@ import com.sxtvs.api.chatgpt.dto.CompletionsParamsDto;
 import com.sxtvs.api.chatgpt.dto.CompletionsRequestDto;
 import com.sxtvs.api.chatgpt.dto.CompletionsResponseDto;
 import com.sxtvs.api.chatgpt.dto.GptResponse;
+import com.sxtvs.api.youmei.service.YoumeiAccountServiceImpl;
 import com.sxtvs.core.advice.BizException;
 import com.sxtvs.core.sls.AliyunLogger;
 import lombok.Cleanup;
@@ -32,6 +33,9 @@ public class ChatGptService {
     @Autowired
     private ObjectMapper objectMapper;
 
+    @Autowired
+    private YoumeiAccountServiceImpl youmeiAccountService;
+
     private final CloseableHttpClient client = HttpClients.createDefault();
 
 
@@ -49,9 +53,7 @@ public class ChatGptService {
     @SneakyThrows
     private synchronized String request(String params) {
         while (true) {
-//            var token = queue.poll(1, TimeUnit.DAYS);
             String result;
-//            try {
             var httpPost = new HttpPost("https://api.openai.com/v1/completions");
             httpPost.setHeader("Authorization", "Bearer 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");
             httpPost.setEntity(new StringEntity(params, ContentType.APPLICATION_JSON));
@@ -66,12 +68,10 @@ public class ChatGptService {
             logger.info("response", result);
             if (response.getStatusLine().getStatusCode() >= HttpStatus.SC_REDIRECTION) {
                 logger.error("error", result);
-                throw new BizException(1000, "服务器走丢了 请重试");
+                throw new BizException(1000, "当前访问人数过多,请重试。");
             }
             return result;
-//            } finally {
-//                queue.put(token);
-//            }
+
         }
 
     }
@@ -79,7 +79,8 @@ public class ChatGptService {
 
     @SneakyThrows
     public CompletionsResponseDto completions(@RequestBody CompletionsRequestDto dto) {
-        var paramsDto = new CompletionsParamsDto(dto.toText());
+        var text = dto.toText();
+        var paramsDto = new CompletionsParamsDto(text);
         var params = objectMapper.writeValueAsString(paramsDto);
         logger.info("key", "completions",
                 "request", params,
@@ -88,13 +89,15 @@ public class ChatGptService {
                 "userName", dto.getUserName()
         );
 
-        var result = request(params);
-
-        var gptResponse = objectMapper.readValue(result, GptResponse.class);
+        if (youmeiAccountService.wordCheckCache(text)) {
+            throw new BizException(1001, "您的问题不符合相关法律法规,请修改后重试。");
+        }
 
+        var body = request(params);
+        var gptResponse = objectMapper.readValue(body, GptResponse.class);
         var completionsResponseDto = new CompletionsResponseDto();
-        var text = gptResponse.getChoices().stream().map(GptResponse.ChoicesDTO::getText).findFirst().orElse("").trim();
-        completionsResponseDto.setResult(text);
+        var result = gptResponse.getChoices().stream().map(GptResponse.ChoicesDTO::getText).findFirst().orElse("").trim();
+        completionsResponseDto.setResult(result);
         return completionsResponseDto;
     }
 }

+ 9 - 0
src/main/java/com/sxtvs/api/youmei/dto/CheckWordParam.java

@@ -0,0 +1,9 @@
+package com.sxtvs.api.youmei.dto;
+
+import lombok.Data;
+
+@Data
+public class CheckWordParam {
+    private String text;
+    private String raw;
+}

+ 104 - 0
src/main/java/com/sxtvs/api/youmei/dto/CheckWordResponse.java

@@ -0,0 +1,104 @@
+package com.sxtvs.api.youmei.dto;
+
+import com.fasterxml.jackson.annotation.JsonProperty;
+import lombok.Data;
+import lombok.NoArgsConstructor;
+
+import java.util.List;
+
+@NoArgsConstructor
+@Data
+public class CheckWordResponse {
+
+    @JsonProperty("msg")
+    private String msg;
+    @JsonProperty("umeiTransactionId")
+    private String umeiTransactionId;
+    @JsonProperty("code")
+    private Integer code;
+    @JsonProperty("data")
+    private DataDTO data;
+    @JsonProperty("transactionId")
+    private String transactionId;
+
+    @NoArgsConstructor
+    @Data
+    public static class DataDTO {
+        @JsonProperty("checklist")
+        private List<ChecklistDTO> checklist;
+
+        private String rawText;
+        private String replaceText;
+
+        @NoArgsConstructor
+        @Data
+        public static class ChecklistDTO {
+            @JsonProperty("um_error_level")
+            private Integer umErrorLevel;
+            @JsonProperty("length")
+            private Integer length;
+            @JsonProperty("suggest")
+            private List<String> suggest;
+            @JsonProperty("source")
+            private Integer source;
+            @JsonProperty("explanation")
+            private String explanation;
+            @JsonProperty("type")
+            private TypeDTO type;
+            @JsonProperty("checkModule")
+            private CheckModuleDTO checkModule;
+            @JsonProperty("htmlWords")
+            private List<HtmlWordsDTO> htmlWords;
+            @JsonProperty("context")
+            private String context;
+            @JsonProperty("action")
+            private ActionDTO action;
+            @JsonProperty("position")
+            private Integer position;
+            @JsonProperty("word")
+            private String word;
+            @JsonProperty("wordHtml")
+            private String wordHtml;
+
+            private int colorLength;
+            private int colorPosition;
+            private String youmeiWordName;
+
+            @NoArgsConstructor
+            @Data
+            public static class TypeDTO {
+                @JsonProperty("name")
+                private String name;
+                @JsonProperty("belongId")
+                private Integer belongId;
+                @JsonProperty("id")
+                private Integer id;
+                @JsonProperty("desc")
+                private String desc;
+            }
+
+            @NoArgsConstructor
+            @Data
+            public static class CheckModuleDTO {
+                @JsonProperty("id")
+                private Integer id;
+            }
+
+            @NoArgsConstructor
+            @Data
+            public static class ActionDTO {
+                @JsonProperty("id")
+                private Integer id;
+            }
+
+            @NoArgsConstructor
+            @Data
+            public static class HtmlWordsDTO {
+                @JsonProperty("position")
+                private Integer position;
+                @JsonProperty("word")
+                private String word;
+            }
+        }
+    }
+}

+ 29 - 0
src/main/java/com/sxtvs/api/youmei/entity/YoumeiAccount.java

@@ -0,0 +1,29 @@
+package com.sxtvs.api.youmei.entity;
+
+import com.baomidou.mybatisplus.annotation.IdType;
+import com.baomidou.mybatisplus.annotation.TableId;
+import lombok.Getter;
+import lombok.Setter;
+
+import java.io.Serializable;
+
+/**
+ * <p>
+ *
+ * </p>
+ *
+ * @author zyx
+ * @since 2021-12-17
+ */
+@Getter
+@Setter
+public class YoumeiAccount implements Serializable {
+
+    private static final long serialVersionUID = 1L;
+
+    @TableId(value = "open_id", type = IdType.INPUT)
+    private String openId;
+
+    private String accessToken;
+
+}

+ 16 - 0
src/main/java/com/sxtvs/api/youmei/mapper/YoumeiAccountMapper.java

@@ -0,0 +1,16 @@
+package com.sxtvs.api.youmei.mapper;
+
+import com.baomidou.mybatisplus.core.mapper.BaseMapper;
+import com.sxtvs.api.youmei.entity.YoumeiAccount;
+
+/**
+ * <p>
+ *  Mapper 接口
+ * </p>
+ *
+ * @author zyx
+ * @since 2021-12-17
+ */
+public interface YoumeiAccountMapper extends BaseMapper<YoumeiAccount> {
+
+}

+ 8 - 0
src/main/java/com/sxtvs/api/youmei/service/IYoumeiAccountService.java

@@ -0,0 +1,8 @@
+package com.sxtvs.api.youmei.service;
+
+import com.baomidou.mybatisplus.extension.service.IService;
+import com.sxtvs.api.youmei.entity.YoumeiAccount;
+
+public interface IYoumeiAccountService extends IService<YoumeiAccount> {
+
+}

+ 102 - 0
src/main/java/com/sxtvs/api/youmei/service/YoumeiAccountServiceImpl.java

@@ -0,0 +1,102 @@
+package com.sxtvs.api.youmei.service;
+
+import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
+import com.fasterxml.jackson.databind.ObjectMapper;
+import com.github.benmanes.caffeine.cache.Cache;
+import com.github.benmanes.caffeine.cache.Caffeine;
+import com.sxtvs.api.youmei.dto.CheckWordResponse;
+import com.sxtvs.api.youmei.entity.YoumeiAccount;
+import com.sxtvs.api.youmei.mapper.YoumeiAccountMapper;
+import lombok.Cleanup;
+import lombok.SneakyThrows;
+import lombok.extern.slf4j.Slf4j;
+import org.apache.hc.client5.http.classic.methods.HttpPost;
+import org.apache.hc.client5.http.entity.UrlEncodedFormEntity;
+import org.apache.hc.client5.http.fluent.ContentResponseHandler;
+import org.apache.hc.client5.http.impl.classic.CloseableHttpClient;
+import org.apache.hc.client5.http.impl.classic.HttpClients;
+import org.apache.hc.core5.http.NameValuePair;
+import org.apache.hc.core5.http.message.BasicNameValuePair;
+import org.apache.http.Consts;
+import org.springframework.stereotype.Service;
+
+import java.nio.charset.StandardCharsets;
+import java.util.ArrayList;
+import java.util.HashMap;
+import java.util.List;
+import java.util.concurrent.TimeUnit;
+
+@Service
+@Slf4j
+public class YoumeiAccountServiceImpl extends ServiceImpl<YoumeiAccountMapper, YoumeiAccount> implements IYoumeiAccountService {
+
+    private final ObjectMapper objectMapper;
+
+    public YoumeiAccountServiceImpl(ObjectMapper objectMapper) {
+        this.objectMapper = objectMapper;
+    }
+
+    private final HashMap<Integer, String> belongMap = new HashMap<>() {{
+        put(105, "涉国家统一、主权和领土完整");
+        put(109, "涉民族宗教");
+        put(112, "涉黄、暴、恐、赌、毒");
+        put(111, "涉低俗辱骂");
+        put(108, "涉违法违规");
+        put(118, "其他敏感内容");
+    }};
+
+    private final Cache<String, Boolean> cache = Caffeine.newBuilder()
+            .expireAfterWrite(10, TimeUnit.MINUTES)
+            .maximumSize(10000)
+            .build();
+
+    private final Cache<String, String> tokenCache = Caffeine.newBuilder()
+            .expireAfterWrite(1, TimeUnit.MINUTES)
+            .build();
+
+
+    public String getWbjcTokenCache() {
+        return tokenCache.get("sxgdwbjc", this::getWbjcToken);
+    }
+
+    public String getWbjcToken(String accountName) {
+        return this.lambdaQuery()
+                .eq(YoumeiAccount::getOpenId, accountName)
+                .one().getAccessToken();
+    }
+
+    private final CloseableHttpClient client = HttpClients.createDefault();
+
+    @SneakyThrows
+    public Boolean wordCheck(String text) {
+        List<NameValuePair> formparams = new ArrayList<>();
+        formparams.add(new BasicNameValuePair("accessToken", getWbjcTokenCache()));
+        formparams.add(new BasicNameValuePair("text", text));
+        @Cleanup
+        UrlEncodedFormEntity params = new UrlEncodedFormEntity(formparams, Consts.UTF_8);
+
+        var httpPost = new HttpPost("https://api-open-wx-www.yqt365.com/dataapp/api/umei/fw/open/wbjc/article_correct_external");
+        httpPost.setEntity(params);
+
+        var body = client.execute(httpPost, new ContentResponseHandler());
+
+        CheckWordResponse checkWordDto = objectMapper.readValue(body.asString(StandardCharsets.UTF_8), CheckWordResponse.class);
+        CheckWordResponse.DataDTO data = checkWordDto.getData();
+
+        for (CheckWordResponse.DataDTO.ChecklistDTO checklistDTO : data.getChecklist()) {
+            var belongId = checklistDTO.getType().getBelongId();
+            if (belongMap.containsKey(belongId)) {
+                return true;
+            }
+        }
+
+        return false;
+    }
+
+
+    public Boolean wordCheckCache(String text) {
+        return cache.get(text, this::wordCheck);
+    }
+
+
+}

+ 0 - 2
src/main/resources/application.properties

@@ -1,2 +0,0 @@
-
-server.port=80

+ 42 - 0
src/main/resources/application.yml

@@ -0,0 +1,42 @@
+
+
+mybatis-plus:
+  mapper-locations: classpath*:com/smcic/**/*.xml
+  configuration:
+    map-underscore-to-camel-case: true
+    cache-enabled: true
+    lazy-loading-enabled: true
+    multiple-result-sets-enabled: true
+  global-config:
+    banner: false
+    enable-sql-runner: true
+    db-config:
+      id-type: assign_id
+      table-underline: true
+
+
+spring:
+  application:
+    name: bigdata
+  servlet:
+    multipart:
+      max-request-size: 500MB
+      max-file-size: 500MB
+  datasource:
+    dynamic:
+      hikari:
+        driver-class-name: com.mysql.cj.jdbc.Driver
+        max-lifetime: 180000
+        idle-timeout: 30000
+        max-pool-size: 100
+        min-idle: 5
+      primary: collect
+      strict: false
+      datasource:
+        collect:
+          url: jdbc:mysql://rm-2vc3039h858t9ab7sfo.mysql.cn-chengdu.rds.aliyuncs.com:3306/collect?autoReconnect=true&useUnicode=true&characterEncoding=utf8&zeroDateTimeBehavior=CONVERT_TO_NULL&useSSL=false&nullCatalogMeansCurrent=true&rewriteBatchedStatements=true
+          username: cxzx
+          password: sxtvs53$68HD
+
+server:
+  port: 80

+ 7 - 1
src/test/java/com/sxtvs/ChatgptApplicationTests.java

@@ -1,12 +1,18 @@
 package com.sxtvs;
 
+import com.sxtvs.api.youmei.service.YoumeiAccountServiceImpl;
 import org.junit.jupiter.api.Test;
+import org.springframework.beans.factory.annotation.Autowired;
+import org.springframework.boot.test.context.SpringBootTest;
 
-
+@SpringBootTest
 class ChatgptApplicationTests {
 
+    @Autowired
+    private YoumeiAccountServiceImpl youmeiAccountService;
     @Test
     void contextLoads() throws InterruptedException {
+        youmeiAccountService.wordCheck("操你妈");
     }
 
 }