-
-
Notifications
You must be signed in to change notification settings - Fork 58
/
Copy pathchatgpt.go
160 lines (132 loc) · 3.78 KB
/
chatgpt.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
package chatgpt
import (
"encoding/json"
"errors"
"fmt"
"log"
"net/http"
"time"
"github.com/m1guelpf/chatgpt-discord/src/config"
"github.com/m1guelpf/chatgpt-discord/src/expirymap"
"github.com/m1guelpf/chatgpt-discord/src/sse"
)
const KEY_ACCESS_TOKEN = "accessToken"
const USER_AGENT = "Mozilla/5.0 (Macintosh; Intel Mac OS X 10_15_7) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/107.0.0.0 Safari/537.36"
type ChatGPT struct {
SessionToken string
AccessTokenMap expirymap.ExpiryMap
}
type SessionResult struct {
Error string `json:"error"`
Expires string `json:"expires"`
AccessToken string `json:"accessToken"`
}
type MessageResponse struct {
ConversationId string `json:"conversation_id"`
Error string `json:"error"`
Message struct {
ID string `json:"id"`
Content struct {
Parts []string `json:"parts"`
} `json:"content"`
} `json:"message"`
}
type ChatResponse struct {
Message string
MessageId string
ConversationId string
}
func Init(config config.Config) ChatGPT {
return ChatGPT{
AccessTokenMap: expirymap.New(),
SessionToken: config.OpenAISession,
}
}
func (c *ChatGPT) IsAuthenticated() bool {
_, err := c.refreshAccessToken()
return err == nil
}
func (c *ChatGPT) EnsureAuth() error {
_, err := c.refreshAccessToken()
return err
}
func (c *ChatGPT) SendMessage(message string, conversationId string, messageId string) (chan ChatResponse, error) {
r := make(chan ChatResponse)
accessToken, err := c.refreshAccessToken()
if err != nil {
return nil, errors.New(fmt.Sprintf("Couldn't get access token: %v", err))
}
client := sse.Init("https://chat.openai.com/backend-api/conversation")
client.Headers = map[string]string{
"User-Agent": USER_AGENT,
"Authorization": fmt.Sprintf("Bearer %s", accessToken),
}
err = client.Connect(message, conversationId, messageId)
if err != nil {
return nil, errors.New(fmt.Sprintf("Couldn't connect to ChatGPT: %v", err))
}
go func() {
defer close(r)
mainLoop:
for {
select {
case chunk, ok := <-client.EventChannel:
if !ok {
break mainLoop
}
var res MessageResponse
err := json.Unmarshal([]byte(chunk), &res)
if err != nil {
log.Printf("Couldn't unmarshal message response: %v", err)
continue
}
if len(res.Message.Content.Parts) > 0 {
r <- ChatResponse{
MessageId: res.Message.ID,
ConversationId: res.ConversationId,
Message: res.Message.Content.Parts[0],
}
}
}
}
}()
return r, nil
}
func (c *ChatGPT) refreshAccessToken() (string, error) {
cachedAccessToken, ok := c.AccessTokenMap.Get(KEY_ACCESS_TOKEN)
if ok {
return cachedAccessToken, nil
}
req, err := http.NewRequest("GET", "https://chat.openai.com/api/auth/session", nil)
if err != nil {
return "", fmt.Errorf("failed to create request: %v", err)
}
req.Header.Set("User-Agent", USER_AGENT)
req.Header.Set("Cookie", fmt.Sprintf("__Secure-next-auth.session-token=%s", c.SessionToken))
res, err := http.DefaultClient.Do(req)
if err != nil {
return "", fmt.Errorf("failed to perform request: %v", err)
}
defer res.Body.Close()
var result SessionResult
err = json.NewDecoder(res.Body).Decode(&result)
if err != nil {
return "", fmt.Errorf("failed to decode response: %v", err)
}
accessToken := result.AccessToken
if accessToken == "" {
return "", errors.New("unauthorized")
}
if result.Error != "" {
if result.Error == "RefreshAccessTokenError" {
return "", errors.New("Session token has expired")
}
return "", errors.New(result.Error)
}
expiryTime, err := time.Parse(time.RFC3339, result.Expires)
if err != nil {
return "", fmt.Errorf("failed to parse expiry time: %v", err)
}
c.AccessTokenMap.Set(KEY_ACCESS_TOKEN, accessToken, expiryTime.Sub(time.Now()))
return accessToken, nil
}