-
Notifications
You must be signed in to change notification settings - Fork 0
/
common.go
43 lines (38 loc) · 839 Bytes
/
common.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
package gogpt
import (
"bytes"
"encoding/json"
"net/http"
)
type Header struct {
Url string
Auth string
}
func (c *Context) query() *http.Response {
c.count++
client := &http.Client{}
jsonData, err := json.Marshal(c.body)
checkErr(err)
body := bytes.NewBuffer(jsonData)
req, err := http.NewRequest("POST", c.header.Url, body)
checkErr(err)
req.Header.Set("Content-Type", "application/json")
req.Header.Set("Authorization", c.header.Auth)
if c.stream {
req.Header.Set("Accept", "text/event-stream")
req.Header.Set("Cache-Control", "no-cache")
req.Header.Set("Connection", "keep-alive")
}
res, err := client.Do(req)
checkErr(err)
return res
}
func (c *Context) checkAndReset() {
if c.count >= c.maxTurns {
c.Clear()
}
}
func (c *Context) Clear(){
c.body["messages"] = []map[string]string{}
c.count = 0
}