-
-
Notifications
You must be signed in to change notification settings - Fork 1
/
gemini.go
122 lines (110 loc) · 2.92 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
package gemini
import (
"bytes"
"context"
"fmt"
"html/template"
"os"
"github.com/google/generative-ai-go/genai"
"github.com/k1LoW/tbls-ask/templates"
"github.com/k1LoW/tbls/schema"
"google.golang.org/api/option"
)
const (
DefaultModelChat = "gemini-pro"
quoteStart = "```sql"
quoteEnd = "```"
)
type Gemini struct {
client *genai.Client
model string
promptTmpl string
queryPromptTmpl string
}
func New(key, model string) *Gemini {
client, err := genai.NewClient(context.Background(), option.WithAPIKey(key))
if err != nil {
return nil
}
return &Gemini{
client: client,
model: model,
promptTmpl: templates.DefaultPromtTmpl,
queryPromptTmpl: templates.DefaultQueryPromptTmpl,
}
}
func (g *Gemini) Ask(ctx context.Context, q string, s *schema.Schema) (string, error) {
if os.Getenv("GEMINI_API_KEY") == "" {
return "", fmt.Errorf("GEMINI_API_KEY is not set")
}
client, err := genai.NewClient(ctx, option.WithAPIKey(os.Getenv("GEMINI_API_KEY")))
if err != nil {
return "", err
}
defer client.Close()
tpl, err := template.New("").Parse(templates.DefaultPromtTmpl)
if err != nil {
return "", err
}
buf := new(bytes.Buffer)
if err := tpl.Execute(buf, map[string]any{
"DatabaseVersion": templates.DatabaseVersion(s),
"QuoteStart": "```sql",
"QuoteEnd": "```",
"DDL": templates.GenerateDDLRoughly(s),
"Question": q,
}); err != nil {
return "", err
}
model := client.GenerativeModel(g.model)
resp, err := model.GenerateContent(ctx, genai.Text(buf.String()))
if err != nil {
return "", err
}
answer := extractResponse(resp)
return answer, nil
}
func (g *Gemini) AskQuery(ctx context.Context, q string, s *schema.Schema) (string, error) {
if os.Getenv("GEMINI_API_KEY") == "" {
return "", fmt.Errorf("GEMINI_API_KEY is not set")
}
client, err := genai.NewClient(ctx, option.WithAPIKey(os.Getenv("GEMINI_API_KEY")))
if err != nil {
return "", err
}
defer client.Close()
tpl, err := template.New("").Parse(templates.DefaultQueryPromptTmpl)
if err != nil {
return "", err
}
buf := new(bytes.Buffer)
if err := tpl.Execute(buf, map[string]any{
"DatabaseVersion": templates.DatabaseVersion(s),
"QuoteStart": "```sql",
"QuoteEnd": "```",
"DDL": templates.GenerateDDLRoughly(s),
"Question": q,
}); err != nil {
return "", err
}
model := client.GenerativeModel(g.model)
resp, err := model.GenerateContent(ctx, genai.Text(buf.String()))
if err != nil {
return "", err
}
answer := extractResponse(resp)
return answer, nil
}
func extractResponse(resp *genai.GenerateContentResponse) string {
response := ""
for _, candidate := range resp.Candidates {
if candidate.Content != nil && len(candidate.Content.Parts) > 0 {
for _, part := range candidate.Content.Parts {
if part != nil {
response = fmt.Sprintf("%s", part)
}
}
}
}
return response
}