-
Notifications
You must be signed in to change notification settings - Fork 1
/
gemini.go
153 lines (143 loc) · 3.51 KB
/
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
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
package main
import (
"context"
"encoding/json"
"fmt"
"log"
"strings"
"github.com/google/generative-ai-go/genai"
"google.golang.org/api/iterator"
"google.golang.org/api/option"
)
type GeminiClient struct {
chatID int
client *genai.Client
model *genai.GenerativeModel
chatSession *genai.ChatSession
ctx context.Context
conf GeminiChatConfig
}
func newGeminiClient(ctx context.Context, chatID int, conf GeminiChatConfig) (*GeminiClient, error) {
client, err := genai.NewClient(ctx, option.WithAPIKey(conf.GoogleAIKey))
if err != nil {
return nil, err
}
model := client.GenerativeModel(conf.ModelName)
model.SafetySettings = conf.SafetySetting
return &GeminiClient{
chatID: chatID,
client: client,
ctx: ctx,
conf: conf,
model: model,
}, nil
}
func (g *GeminiClient) startChat(history []*genai.Content) {
cs := g.model.StartChat()
if cs == nil {
log.Fatal("Chat session is nil")
}
cs.History = history
g.chatSession = cs
}
func (g *GeminiClient) sendMessageStream(text string) *genai.GenerateContentResponseIterator {
prompt := genai.Text(text)
iter := g.chatSession.SendMessageStream(g.ctx, prompt)
return iter
}
func (g *GeminiClient) genTitle() string {
// 生成对话标题prompt
promtp := "Generate title based on my question, Please give the title directly without any additional explanation or additional characters."
iter := g.sendMessageStream(promtp)
modelPart := make([]string, 0)
for {
resp, err := iter.Next()
if err == iterator.Done {
break
}
if err != nil {
log.Println(err.Error())
break
}
if resp != nil &&
len(resp.Candidates) > 0 &&
resp.Candidates[0].Content != nil &&
len(resp.Candidates[0].Content.Parts) > 0 {
p := fmt.Sprint(resp.Candidates[0].Content.Parts[0])
modelPart = append(modelPart, p)
}
}
return strings.ReplaceAll(strings.Join(modelPart, ""), "*", "")
}
func (g *GeminiClient) sendMessageToTui(textChan chan string, historyChan chan string, genFlagChan chan bool, titleChan chan string, db *DB) {
firstQuestion := true
for {
text := <-textChan
historyChan <- "[red]Q:" + text + "\n"
tx, err := db.SqliteDB.Begin()
if err != nil {
log.Fatal(err)
}
userPromptArr := []string{text}
jarr, err := json.Marshal(userPromptArr)
if err != nil {
log.Fatal(err)
}
err = db.InsertHistoryWithTX(tx, GeminiChatHistory{
ChatID: int64(g.chatID),
Prompt: string(jarr),
Role: "user",
})
if err != nil {
log.Fatal(err)
}
iter := g.sendMessageStream(text)
modelPart := make([]string, 0)
historyChan <- "[green]A: "
for {
resp, err := iter.Next()
if err == iterator.Done {
break
}
if err != nil {
log.Println(err.Error())
break
}
if resp != nil &&
len(resp.Candidates) > 0 &&
resp.Candidates[0].Content != nil &&
len(resp.Candidates[0].Content.Parts) > 0 {
p := fmt.Sprint(resp.Candidates[0].Content.Parts[0])
modelPart = append(modelPart, p)
historyChan <- p
}
}
historyChan <- "\n"
genFlagChan <- false
modelArr, err := json.Marshal(modelPart)
if err != nil {
log.Fatal(err)
}
err = db.InsertHistoryWithTX(tx, GeminiChatHistory{
ChatID: int64(g.chatID),
Prompt: string(modelArr),
Role: "model",
})
if err != nil {
log.Fatal(err)
}
tx.Commit()
if firstQuestion {
// TODO 插入标题
firstQuestion = false
go func() {
title := g.genTitle()
titleChan <- title
db.InsertChat(GeminiChatList{
ChatID: int64(g.chatID),
ChatTitle: title,
})
}()
}
}
}