-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathchatGLM.go
63 lines (56 loc) · 1.43 KB
/
chatGLM.go
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
package main
import (
"encoding/json"
"errors"
"time"
"github.com/moxcomic/ihttp"
log "github.com/sirupsen/logrus"
)
var glmUrl = "http://127.0.0.1:8000"
type chatglmPost struct {
Prompt string `json:"prompt"`
History []string `json:"history"`
}
type chatglmResp struct {
Response string `json:"response"`
History [][]string `json:"history"`
}
func (p *chatglmPost) post() (*chatglmResp, error) {
postData, _ := json.Marshal(p)
log.Debug("[ChatGLM2] post: ", *p)
ipost := ihttp.New().WithUrl(glmUrl).
WithHeader("Content-Type", "application/json").
WithBody(postData)
resp, err := ipost.Post().ToString()
if resp == "Internal Server Error" { //初始化重试
log.Info("[ChatGLM2] 初始化")
time.Sleep(time.Second)
newipost := ihttp.New().WithUrl(glmUrl).
WithHeader("Content-Type", "application/json").
WithBody(postData)
resp, err = newipost.Post().ToString()
}
if err != nil {
log.Error("[ChatGLM2] post err: ", err)
return nil, errors.New("ChatGLM2后端连接失败 " + err.Error())
}
log.Debug("[ChatGLM2] resp: ", resp)
r := &chatglmResp{}
err = json.Unmarshal([]byte(resp), r)
if err != nil {
log.Error("[ChatGLM2] Unmarshal err: ", err)
}
return r, nil
}
func sendToChatGLMSingle(input string) (output string, err error) {
post := &chatglmPost{
Prompt: input,
History: []string{},
}
resp, err := post.post()
if err != nil {
return "", err
}
output = resp.Response
return
}