-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathgemini-operator.go
61 lines (55 loc) · 1.36 KB
/
gemini-operator.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
package websum
import (
"context"
"fmt"
"github.com/google/generative-ai-go/genai"
"google.golang.org/api/option"
)
var ctx context.Context
var client *genai.Client
var cs *genai.ChatSession
func InitGemini(apiKey, prompt string) {
ctx = context.Background()
// Access your API key as an environment variable (see "Set up your API key" above)
var err error
client, err = genai.NewClient(ctx, option.WithAPIKey(apiKey))
if err != nil {
panic(err)
}
//defer client.Close()
model := client.GenerativeModel("gemini-pro")
cs = model.StartChat()
cs.History = []*genai.Content{
&genai.Content{
Parts: []genai.Part{
genai.Text(prompt),
},
Role: "user",
},
&genai.Content{
Parts: []genai.Part{
genai.Text("OK."),
},
Role: "model",
},
}
}
func ImportExistingClientAndChatSession(importedClient *genai.Client, importedCs *genai.ChatSession, importedCtx context.Context) {
client = importedClient
cs = importedCs
ctx = importedCtx
}
func ChatWithGemini(query string) (string, error) {
fmt.Println(query)
resp, err := cs.SendMessage(ctx, genai.Text(query))
if err != nil {
return "something went wrong", err
}
if len(resp.Candidates) == 0 {
return "something went wrong", nil
}
if len(resp.Candidates[0].Content.Parts) == 0 {
return "something went wrong", nil
}
return fmt.Sprintf("%v", resp.Candidates[0].Content.Parts[0]), nil
}