-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathmain.go
60 lines (47 loc) · 1.17 KB
/
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
53
54
55
56
57
58
59
60
package main
import (
"context"
"fmt"
"log"
"os"
"strings"
"github.com/joho/godotenv"
"github.com/google/generative-ai-go/genai"
"google.golang.org/api/option"
)
func main() {
err := godotenv.Load()
if err != nil {
log.Fatal("Error loading .env file")
}
ctx := context.Background()
apiKey, ok := os.LookupEnv("GEMINI_API_KEY")
if !ok {
log.Fatalln("Environment variable GEMINI_API_KEY not set")
}
client, err := genai.NewClient(ctx, option.WithAPIKey(apiKey))
if err != nil {
log.Fatalf("Error creating client: %v", err)
}
defer client.Close()
model := client.GenerativeModel("gemini-2.0-flash-exp")
model.SetTemperature(1)
model.SetTopK(40)
model.SetTopP(0.95)
model.SetMaxOutputTokens(8192)
model.ResponseMIMEType = "text/plain"
session := model.StartChat()
session.History = []*genai.Content{}
if len(os.Args) < 2 {
fmt.Println("Usage: ./main <text to send to Gemini>")
os.Exit(1)
}
inputText := strings.Join(os.Args[1:], " ")
resp, err := session.SendMessage(ctx, genai.Text(inputText))
if err != nil {
log.Fatalf("Error sending message: %v", err)
}
for _, part := range resp.Candidates[0].Content.Parts {
fmt.Printf("%v\n", part)
}
}