forked from bard-cli/gobard-api
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathgobard-api.go
405 lines (329 loc) · 9.49 KB
/
gobard-api.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
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
package gobard-api
import (
"bytes"
"encoding/json"
"fmt"
"net/http"
"net/url"
"regexp"
"strings"
"time"
"github.com/go-resty/resty/v2"
"github.com/tidwall/gjson"
"k8s.io/apimachinery/pkg/util/rand"
)
// References:
//
// Information about Google Batch Execute Protocol:
// https://kovatch.medium.com/deciphering-google-batchexecute-74991e4e446c
//
// Information about Google Bard "API" (there is actually no official API):
// https://github.com/dsdanielpark/Bard-API/blob/main/bardapi/core.py
//
const (
authCookieName = "__Secure-1PSID"
maxNumOfAnswers = 3 // bard provides up to 3 answers to each question
)
// configurable values
const (
timeoutSnim0e = 5 // timeout for the snim0e request (in seconds)
timeoutQuery = 15 // timeout for the query request (in seconds)
)
const bardURL string = "https://bard.google.com/_/BardChatUi/data/assistant.lamda.BardFrontendService/StreamGenerate"
type Answer struct {
content string
conversationID string
responseID string
choiceID string
}
func (a *Answer) setContent(value string) {
a.content = value
}
func (a *Answer) setConversationID(value string) {
a.conversationID = value
}
func (a *Answer) setResponseID(value string) {
a.responseID = value
}
func (a *Answer) setChoiceID(value string) {
a.choiceID = value
}
func (a *Answer) getContent() string {
return a.content
}
func (a *Answer) getConversationID() string {
return a.conversationID
}
func (a *Answer) getResponseID() string {
return a.responseID
}
func (a *Answer) getChoiceID() string {
return a.choiceID
}
// Bard is the main struct for the bard.google.com API.
type Bard struct {
cookie string // auth cookie
answers map[int]*Answer // up to 3 answers per question
currAnswer int // current answer
numAnswers int // current number of answers
client *resty.Client // resty client
}
// New creates a new Bard instance.
func New(cookie string) *Bard {
b := &Bard{
cookie: cookie,
answers: make(map[int]*Answer),
}
for i := 0; i < maxNumOfAnswers; i++ {
b.answers[i] = &Answer{}
}
b.currAnswer = 0
return b
}
// Ask asks a question to bard.google.com.
func (b *Bard) Ask(prompt string) error {
prompt = url.QueryEscape(prompt)
b.createRestClient()
// Prepare request
snim0e, err := b.getSnim0eValue()
if err != nil {
return err
}
session, err := b.createSession(prompt) // will use the current answer for the session
if err != nil {
return err
}
request, err := b.createRequest(session)
if err != nil {
return err
}
// Prepare the client
b.client.SetBaseURL(bardURL)
b.client.SetTimeout(timeoutQuery * time.Second)
b.client.SetDoNotParseResponse(true)
b.client.SetFormData(b.createFormData(snim0e, request))
b.client.SetQueryParams(b.createBatchExecuteReqParams())
// Send the request (will reset the current answer to 0)
return b.doAsk()
}
// GetAnswer returns the current answer.
func (b *Bard) GetAnswer() string {
return b.getAnswer(b.currAnswer).getContent()
}
// NextAnswer moves to the next answer and returns it.
func (b *Bard) NextAnswer() string {
b.Next()
return b.GetAnswer()
}
// PrevAnswer moves to the previous answer and returns it.
func (b *Bard) PrevAnswer() string {
b.Prev()
return b.GetAnswer()
}
// Next moves to the next answer.
func (b *Bard) Next() {
switch b.currAnswer {
case 0:
b.currAnswer = 1
case 1:
b.currAnswer = 2
case 2:
b.currAnswer = 0
}
}
// Prev moves to the previous answer.
func (b *Bard) Prev() {
switch b.currAnswer {
case 0:
b.currAnswer = 2
case 1:
b.currAnswer = 0
case 2:
b.currAnswer = 1
}
}
// Reset resets the bard instance (new conversation).
func (b *Bard) Reset() {
for i := 0; i < maxNumOfAnswers; i++ {
b.answers[i] = &Answer{}
}
b.currAnswer = 0
}
// GetNumOfAnswers returns the current number of answers.
func (b *Bard) GetNumOfAnswers() int {
return b.numAnswers
}
//
// Getters and setters
//
func (b *Bard) getAnswer(id int) *Answer {
return b.answers[id]
}
func (b *Bard) getAllAnswers() []*Answer {
var values []*Answer
for i := 0; i < maxNumOfAnswers; i++ {
values = append(values, b.getAnswer(i))
}
return values
}
//
// Bard related functions
//
var headers map[string]string = map[string]string{
"Host": "bard.google.com",
"X-Same-Domain": "1",
"User-Agent": "Mozilla/5.0 (Windows NT 10.0; Win64; x64) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/114.0.0.0 Safari/537.36 Edg/114.0.1823.82",
"Content-Type": "application/x-www-form-urlencoded;charset=UTF-8",
"Origin": "https://bard.google.com",
"Referer": "https://bard.google.com/",
}
// createRestClient creates a resty client with the needed configuration.
func (b *Bard) createRestClient() {
b.client = resty.New()
b.client.SetLogger(Logger{})
b.client.SetDebug(false)
b.client.SetHeaders(headers)
b.client.SetCookie(&http.Cookie{
Name: authCookieName,
Value: b.cookie,
})
}
var snim0eRegex = regexp.MustCompile(`SNlM0e\":\"(.*?)\"`)
// getSnim0eValue gets the snim0e value from bard.google.com.
func (b *Bard) getSnim0eValue() (string, error) {
// snim0e: AJWyuYX8NLX7SKFihs03g0AoLU-o:1689960334051 (e.g)
b.client.SetBaseURL("https://bard.google.com")
b.client.SetTimeout(timeoutSnim0e * time.Second)
resp, err := b.client.R().Get("/")
if err != nil {
return "", err
}
if resp.StatusCode() != 200 {
return "", fmt.Errorf("status code: %d", resp.StatusCode())
}
if len(snim0eRegex.FindStringSubmatch(resp.String())) < 2 {
return "", fmt.Errorf("could not find snim0e")
}
return snim0eRegex.FindStringSubmatch(resp.String())[1], nil
}
// createSession creates the session for the query request.
func (b *Bard) createSession(prompt string) ([]byte, error) {
// sessionM: [["Hello.+How+are+you+%3F"],null,["conversationId","responseId","choiceId"]]
session := []interface{}{
[]string{
prompt,
},
nil,
[]string{
b.getAnswer(b.currAnswer).getConversationID(),
b.getAnswer(b.currAnswer).getResponseID(),
b.getAnswer(b.currAnswer).getChoiceID(),
},
}
sessionM, err := json.Marshal(session)
if err != nil {
return []byte{}, err
}
return sessionM, err
}
// createRequest creates the request body for the query request.
func (b *Bard) createRequest(session []byte) ([]byte, error) {
// reqM: [null,"[[\"Hello.+How+are+you+%3F\"],null,[\"conversationId\",\"responseId\",\"choiceId\"]]"]
req := []interface{}{
nil,
string(session), // stringify marshalled session []byte
}
reqM, err := json.Marshal(req)
if err != nil {
return []byte{}, err
}
return reqM, nil
}
// createFormData creates the form data for the query request.
func (b *Bard) createFormData(snim0e string, request []byte) map[string]string {
// f.req = array of envelopes for each payload in the batch
// at = XSRF mitigation (time tied to user's google account paired with unix ts)
formData := map[string]string{
"f.req": string(request), // stringify marshalled request []byte
"at": snim0e,
}
return formData
}
// createBatchExecuteReqParams creates a map with needed request parameters.
func (b *Bard) createBatchExecuteReqParams() map[string]string {
return map[string]string{
"bl": "boq_assistant-bard-web-server_20230718.13_p2", // name and version of the backend software handling the requests
"_reqid": fmt.Sprintf("%d", rand.IntnRange(100000, 999999)), // request id (random 4 digits + 100000)
"rt": "c", // response type (c = standard, b = protobuf, none = easier json)
}
}
func (b *Bard) doAsk() error {
b.currAnswer = 0 // reset current answer
// Send the request
response, err := b.client.R().Post("")
if err != nil {
return fmt.Errorf("post error: % w", err)
}
if response.StatusCode() != 200 {
return fmt.Errorf("status code: %d", response.StatusCode())
}
// Read the response
bodyBufferBytes := new(bytes.Buffer)
_, err = bodyBufferBytes.ReadFrom(response.RawResponse.Body)
if err != nil {
return fmt.Errorf("read error: %w", err)
}
bodyBufferStrJson := strings.Split(bodyBufferBytes.String(), "\n")[3]
// Parse the response
var rawResponse [][]interface{}
err = json.Unmarshal([]byte(bodyBufferStrJson), &rawResponse)
if err != nil {
return fmt.Errorf("unmarshal error: %w", err)
}
// Parse the answers
responseStrJson, ok := rawResponse[0][2].(string)
if !ok {
return fmt.Errorf("no answer ?")
}
for _, answer := range b.getAllAnswers() {
answer.setConversationID(gjson.Get(responseStrJson, "1.0").String())
answer.setResponseID(gjson.Get(responseStrJson, "1.1").String())
}
promptAnswer := gjson.Get(responseStrJson, "4").Array()
// Sanity check
if len(promptAnswer) == 0 {
return fmt.Errorf("no answer ?")
}
// Set the current number of answers
b.numAnswers = len(promptAnswer)
// Sanity check
if b.numAnswers > maxNumOfAnswers {
promptAnswer = promptAnswer[:maxNumOfAnswers]
}
// Set the answers
for i := 0; i < len(promptAnswer); i++ {
b.getAnswer(i).setChoiceID(promptAnswer[i].Array()[0].String())
b.getAnswer(i).setContent(promptAnswer[i].Array()[1].Array()[0].String())
}
return nil
}
//
// Logger mock
//
var log Logger
type Logger struct{}
func (l Logger) Debugf(msg string, args ...interface{}) {
fmt.Printf("DEBUG: "+msg, args...)
}
func (l Logger) Infof(msg string, args ...interface{}) {
fmt.Printf("INFO: "+msg, args...)
}
func (l Logger) Warnf(msg string, args ...interface{}) {
fmt.Printf("WARN: "+msg, args...)
}
func (l Logger) Errorf(msg string, args ...interface{}) {
fmt.Printf("ERROR: "+msg, args...)
}
func (l Logger) Fatalf(msg string, args ...interface{}) {
fmt.Printf("FATAL: "+msg, args...)
}