-
Notifications
You must be signed in to change notification settings - Fork 1
/
Copy pathsession.go
63 lines (54 loc) · 1.24 KB
/
session.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
package application
import (
"context"
"fmt"
"log"
"sync"
"github.com/google/generative-ai-go/genai"
)
type Session struct {
Sender int64
ChatSession *genai.ChatSession
}
type Sessions struct {
m map[int64]*Session
mu sync.Mutex
}
func (s *Sessions) add(session *Session) {
s.mu.Lock()
defer s.mu.Unlock()
if s.m == nil {
s.m = map[int64]*Session{}
}
s.m[session.Sender] = session
}
func (s *Sessions) get(sender int64) *Session {
s.mu.Lock()
defer s.mu.Unlock()
return s.m[sender]
}
func (s *Sessions) delete(sender int64) {
s.mu.Lock()
defer s.mu.Unlock()
delete(s.m, sender)
}
var sessions = &Sessions{}
func (sessions *Sessions) Ask(sender int64, content string) (string, error) {
session := sessions.get(sender)
if session == nil {
session = &Session{
Sender: sender,
ChatSession: gemini().StartChat(),
}
sessions.add(session)
}
if len(session.ChatSession.History) > 11 {
session.ChatSession.History = session.ChatSession.History[:10]
}
resp, err := session.ChatSession.SendMessage(context.Background(), genai.Text(content))
log.Printf("[gemini] ask:%s, resp:%+v, err:%+v\n", content, resp, err)
if err != nil {
return "", err
}
return fmt.Sprintf("%s", resp.Candidates[0].Content.Parts[0]), nil
}