-
Notifications
You must be signed in to change notification settings - Fork 13
/
ai_gemini.go
68 lines (57 loc) 路 1.3 KB
/
ai_gemini.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
// archiver - ai.go
// 2024-02-20 20:58
// Benny <[email protected]>
package main
import (
"bytes"
"encoding/json"
log "github.com/sirupsen/logrus"
"io"
"net/http"
)
type Response struct {
Candidates []Candidate `json:"candidates"`
}
type Candidate struct {
Content ContentData `json:"content"`
}
type ContentData struct {
Parts []Part `json:"parts"`
}
type Part struct {
Text string `json:"text"`
}
type Content struct {
Role string `json:"role"`
Parts []Part `json:"parts"`
}
type Data struct {
Contents []Content `json:"contents"`
}
func askGemini(userID int64) string {
var data Data
chats := getChats(userID)
if len(chats) > 0 {
for _, chat := range chats {
part := Part{Text: chat.Text}
content := Content{
Role: chat.Role,
Parts: []Part{part},
}
data.Contents = append(data.Contents, content)
}
}
jsonData, _ := json.Marshal(data)
log.Infoln(string(jsonData))
resp, err := http.Post(geminiURL, "application/json", bytes.NewBuffer(jsonData))
defer resp.Body.Close()
if err != nil || resp.StatusCode != http.StatusOK {
body, _ := io.ReadAll(resp.Body)
text := string(body)
log.Errorf("Request failed, %s", text)
return text
}
var response Response
_ = json.NewDecoder(resp.Body).Decode(&response)
return response.Candidates[0].Content.Parts[0].Text
}