|
@@ -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;
|
|
|
+ }
|
|
|
+}
|