-
Notifications
You must be signed in to change notification settings - Fork 0
/
text.go
61 lines (43 loc) · 1.55 KB
/
text.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
package main
import "github.com/google/generative-ai-go/genai"
import "google.golang.org/api/option"
import (
"fmt"
"log"
"context"
"os"
// "encoding/json" // json.MarshalIndent()
)
func main() {
fmt.Println("hello from go")
ctx := context.Background()
// Access your API key as an environment variable (see "Set up your API key" above)
client, err := genai.NewClient(ctx, option.WithAPIKey(os.Getenv("GOOGLE_API_KEY")))
if err != nil {
log.Fatal(err)
}
defer client.Close()
// For text-only input, use the gemini-pro model
model := client.GenerativeModel("gemini-pro")
resp, err := model.GenerateContent(ctx, genai.Text("Write a story about a magic backpack."))
if err != nil {
log.Fatal(err)
}
// Sometimes, this returns a result:
// 2024/03/24 09:49:24 blocked: candidate: FinishReasonSafety
// https://pkg.go.dev/github.com/google/generative-ai-go/genai#GenerateContentResponse
//it := genai.GenerateContentResponseIterator(resp)
//it := genai.GenerateContentResponseIterator(resp)
//it := client.GenerateContentResponseIterator(resp)
//it := genai.client.GenerateContentResponseIterator(resp)
//for it.HasNext() {
// res := it.Next()
// fmt.Println(res)
//}
// https://eli.thegreenplace.net/2023/using-gemini-models-from-go/
//bs, _ := json.MarshalIndent(resp, "", " ")
//fmt.Println(string(bs))
// Grab the specific text result
//fmt.Println(resp.Candidates)
fmt.Println(resp.Candidates[0].Content.Parts)
}