64 lines
1.6 KiB
Go
64 lines
1.6 KiB
Go
|
package controller
|
|||
|
|
|||
|
import (
|
|||
|
//"encoding/json"
|
|||
|
"fmt"
|
|||
|
"github.com/gin-gonic/gin"
|
|||
|
"knowledge/utils"
|
|||
|
"log"
|
|||
|
"net/http"
|
|||
|
"net/url"
|
|||
|
"os"
|
|||
|
)
|
|||
|
|
|||
|
func PostChatGLM(c *gin.Context) {
|
|||
|
// 将响应头中的Content-Type设置为text/plain,表示响应内容为文本
|
|||
|
c.Header("Content-Type", "text/html;charset=utf-8;")
|
|||
|
// 关闭输出缓冲,使得每次写入的数据能够立即发送给客户端
|
|||
|
f, _ := c.Writer.(http.Flusher)
|
|||
|
|
|||
|
keywords, _ := url.QueryUnescape(c.Query("keywords"))
|
|||
|
if keywords == "" {
|
|||
|
keywords = c.PostForm("keywords")
|
|||
|
}
|
|||
|
system, _ := url.QueryUnescape(c.Query("system"))
|
|||
|
if system == "" {
|
|||
|
system = c.PostForm("system")
|
|||
|
}
|
|||
|
|
|||
|
if keywords == "" {
|
|||
|
c.Writer.Write([]byte("请求参数为空"))
|
|||
|
return
|
|||
|
}
|
|||
|
//历史记录
|
|||
|
//history := c.PostForm("history")
|
|||
|
//匹配不到
|
|||
|
prompt := c.PostForm("prompt")
|
|||
|
|
|||
|
if prompt != "" {
|
|||
|
prompt = fmt.Sprintf("对于与知识信息无关的问题,你应拒绝并告知用户“%s”", prompt)
|
|||
|
}
|
|||
|
glm6Bkey := os.Getenv("CHATGLM_6B_KEY")
|
|||
|
chatglm, err := utils.NewChatGLM(glm6Bkey)
|
|||
|
if err != nil {
|
|||
|
c.Writer.Write([]byte(err.Error()))
|
|||
|
return
|
|||
|
}
|
|||
|
ask := fmt.Sprintf("%s,%s,我的问题是:%s", system, prompt, keywords)
|
|||
|
list := make([]map[string]string, 0)
|
|||
|
//json.Unmarshal([]byte(history), &list)
|
|||
|
list = append(list, map[string]string{"role": "user", "content": ask})
|
|||
|
log.Println(list)
|
|||
|
res, err := chatglm.StreamChatGLM6B(list, 0.79, 0.7, true)
|
|||
|
for {
|
|||
|
str, err := chatglm.StreamRecv(res)
|
|||
|
if err != nil {
|
|||
|
log.Println(err)
|
|||
|
break
|
|||
|
}
|
|||
|
log.Println(str)
|
|||
|
c.Writer.Write([]byte(str))
|
|||
|
f.Flush()
|
|||
|
}
|
|||
|
}
|