-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathmain.go
52 lines (47 loc) · 943 Bytes
/
main.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
package main
import (
"bufio"
"context"
"fmt"
"log"
"os"
"strings"
"github.com/google/generative-ai-go/genai"
"google.golang.org/api/option"
)
func main() {
ctx := context.Background()
key := os.Getenv("API_KEY")
if key == "" {
log.Fatal("missing env API_KEY")
}
client, err := genai.NewClient(ctx, option.WithAPIKey(os.Getenv("API_KEY")))
if err != nil {
log.Fatal(err)
}
defer client.Close()
model := client.GenerativeModel("gemini-pro")
reader := bufio.NewReader(os.Stdin)
for {
fmt.Print("You: ")
text, _ := reader.ReadString('\n')
text = strings.TrimSpace(text)
if text == "" {
continue
}
if text == "exit()" {
break
}
fmt.Println("Gemini:")
resp, err := model.GenerateContent(ctx, genai.Text(text))
if err != nil {
log.Fatal(err)
}
for _, candidate := range resp.Candidates {
for _, part := range candidate.Content.Parts {
fmt.Print(part)
}
}
fmt.Println()
}
}