-
Notifications
You must be signed in to change notification settings - Fork 22
/
gpt.go
163 lines (130 loc) · 3.14 KB
/
gpt.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
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
package EdgeGPT
import (
"encoding/json"
"fmt"
"github.com/pavel-one/EdgeGPT-Go/config"
"github.com/pavel-one/EdgeGPT-Go/internal/CookieManager"
"github.com/pavel-one/EdgeGPT-Go/internal/Helpers"
"github.com/pavel-one/EdgeGPT-Go/internal/Logger"
"github.com/pavel-one/EdgeGPT-Go/responses"
"io"
"net/http"
"time"
)
var log = Logger.NewLogger("GPT Service")
const (
StyleCreative = "h3imaginative,clgalileo,gencontentv3"
StyleBalanced = "galileo"
StylePrecise = "h3precise,clgalileo"
DelimiterByte = uint8(30)
Delimiter = "\x1e"
)
type GPT struct {
Config *config.GPT
client *http.Client
cookies []*http.Cookie
Conversation *Conversation
ExpiredAt time.Time
Hub *Hub
}
// NewGPT create new service
func NewGPT(conf *config.GPT) (*GPT, error) {
manager, err := CookieManager.NewManager()
if err != nil {
return nil, err
}
gpt := &GPT{
Config: conf,
cookies: Helpers.MapToCookies(manager.GetBestCookie()),
ExpiredAt: time.Now().Add(time.Minute * 120),
client: &http.Client{
Timeout: conf.TimeoutRequest,
},
}
if err := gpt.createConversation(); err != nil {
return nil, err
}
hub, err := NewHub(gpt.Conversation, conf)
if err != nil {
return nil, err
}
gpt.Hub = hub
return gpt, nil
}
// createConversation request for getting new dialog
func (g *GPT) createConversation() error {
req, err := http.NewRequest("GET", g.Config.ConversationUrl.String(), nil)
for k, v := range g.Config.Headers {
req.Header.Set(k, v)
}
if err != nil {
return err
}
for _, cookie := range g.cookies {
req.AddCookie(cookie)
}
resp, err := g.client.Do(req)
if err != nil {
return err
}
defer resp.Body.Close()
if resp.StatusCode != http.StatusOK {
return fmt.Errorf("status code not ok: %d, %s", resp.StatusCode, resp.Status)
}
r, err := io.ReadAll(resp.Body)
if err != nil {
return err
}
conversation := new(Conversation)
if err := json.Unmarshal(r, conversation); err != nil {
return err
}
if conversation.Result.Value.ValueOrZero() != "Success" {
return nil
}
g.Conversation = conversation
log.Infoln("New conversation", conversation)
return nil
}
/*
AskAsync getting answer async:
Example:
gpt, err := EdgeGPT.NewGPT(conf) //create service
if err != nil {
log.Fatalln(err)
}
mw, err := gpt.AskAsync("Привет, ты живой?") // send ask to gpt
if err != nil {
log.Fatalln(err)
}
go mw.Worker() // Run reading websocket messages
for _ = range mw.Chan {
// update answer
log.Println(mw.Answer.GetAnswer())
}
*/
func (g *GPT) AskAsync(style, message string) (*responses.MessageWrapper, error) {
if len(message) > 2000 {
return nil, fmt.Errorf("message very long, max: %d", 2000)
}
log.Infoln("New ask:", message)
return g.Hub.send(style, message)
}
// AskSync getting answer sync
func (g *GPT) AskSync(style, message string) (*responses.MessageWrapper, error) {
if len(message) > 2000 {
return nil, fmt.Errorf("message very long, max: %d", 2000)
}
m, err := g.Hub.send(style, message)
if err != nil {
return nil, err
}
go m.Worker()
for range m.Chan {
if m.Final {
break
}
}
log.Infoln("New ask:", message)
return m, nil
}