Bladeren bron

chatglm3相关配置

wukai 1 jaar geleden
bovenliggende
commit
5b4b4f630c

+ 5 - 0
doc-admin/src/main/resources/application-dev.yml

@@ -6,6 +6,11 @@ server:
 ruoyi:
   # 文件路径 示例( Windows配置D:/ruoyi/uploadPath,Linux配置 /home/ruoyi/uploadPath)
   profile: D:/ruoyi/uploadPath
+
+#大语言模型配置
+chat-glm3:
+  flag: true
+  host: http://192.168.101.66:8000/
 # Spring配置
 spring:
   #es配置

+ 75 - 0
doc-biz/src/main/java/com/doc/chatglm3/controller/ChatGlm3Controller.java

@@ -0,0 +1,75 @@
+package com.doc.chatglm3.controller;
+
+import com.doc.chatglm3.util.SseEmitterUtf8;
+import com.doc.common.core.controller.BaseController;
+import com.plexpt.chatgpt.ChatGPTStream;
+import com.plexpt.chatgpt.entity.chat.ChatCompletion;
+import com.plexpt.chatgpt.entity.chat.Message;
+import com.plexpt.chatgpt.listener.SseStreamListener;
+import io.swagger.annotations.Api;
+import io.swagger.annotations.ApiOperation;
+import lombok.extern.slf4j.Slf4j;
+import org.springframework.core.env.Environment;
+import org.springframework.web.bind.annotation.*;
+import org.springframework.web.servlet.mvc.method.annotation.SseEmitter;
+
+import javax.annotation.Resource;
+import java.util.List;
+
+/**
+ * 文件上传
+ *
+ * @author wukai
+ * @date 2023-08-15
+ */
+@Api(tags = "智聚AI接口")
+@RestController
+@RequestMapping("/chat-glm3")
+@Slf4j
+public class ChatGlm3Controller extends BaseController {
+    @Resource
+    private Environment environment;
+
+    @ApiOperation("是否开启")
+    @GetMapping()
+    public Boolean flag() {
+        return "true".equals(environment.getProperty("chat-glm3.flag"));
+    }
+
+    @ApiOperation("开始聊天")
+    @PostMapping()
+    public SseEmitter chat(@RequestBody List<Message> messages) {
+        String openaiApiHost = environment.getProperty("chat-glm3.host");
+        SseEmitterUtf8 sseEmitter = new SseEmitterUtf8(-1L);
+
+        SseStreamListener listener = new SseStreamListener(sseEmitter);
+        ChatCompletion chatCompletion = ChatCompletion.builder()
+                .model("chatglm3-6b")
+                .messages(messages)
+                .stream(true)
+                .temperature(0.8)
+                .topP(0.8)
+                .presencePenalty(0)
+                .frequencyPenalty(0)
+                .maxTokens(100)
+                .user("tom")
+                .build();
+
+        // 不需要代理的话,注销此行
+        //Proxy proxy = Proxys.http("192.168.1.98", 7890);
+        ChatGPTStream chatGPTStream = ChatGPTStream.builder()
+                .timeout(600)
+                .apiKey("empty")
+                //.proxy(proxy)
+                .apiHost(openaiApiHost)
+                .build()
+                .init();
+
+        chatGPTStream.streamChatCompletion(chatCompletion, listener);
+        listener.setOnComplate(msg -> {
+            //回答完成,可以做一些事情
+            sseEmitter.complete();
+        });
+        return sseEmitter;
+    }
+}

+ 2 - 0
doc-framework/src/main/java/com/doc/framework/config/SecurityConfig.java

@@ -112,6 +112,8 @@ public class SecurityConfig extends WebSecurityConfigurerAdapter {
                 .antMatchers("/login", "/register", "/captchaImage").permitAll()
                 // 增加API接口允许匿名访问
                 .antMatchers("/api/**").permitAll()
+                // 增加API接口允许匿名访问
+                .antMatchers("/chat-glm3/**").permitAll()
                 // 增加onlyoffice接口允许匿名访问
                 .antMatchers("/only-office/**").permitAll()
                 // 增加websocket允许匿名访问