/
listModels.go
59 lines (48 loc) · 1.32 KB
/
listModels.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
package run
import (
"errors"
"fmt"
"os/signal"
"syscall"
"github.com/google/generative-ai-go/genai"
"github.com/kubetrail/gini/pkg/flags"
"github.com/spf13/cobra"
"github.com/spf13/viper"
"google.golang.org/api/iterator"
"google.golang.org/api/option"
"gopkg.in/yaml.v3"
)
func ListModels(cmd *cobra.Command, args []string) error {
ctx := cmd.Context()
ctx, stop := signal.NotifyContext(ctx, syscall.SIGINT, syscall.SIGTERM)
defer stop()
_ = viper.BindPFlag(flags.ApiKey, cmd.Flag(flags.ApiKey))
_ = viper.BindEnv(flags.ApiKey, flags.ApiKeyEnv)
apiKey := viper.GetString(flags.ApiKey)
if len(apiKey) == 0 {
return fmt.Errorf("api-key cannot be empty")
}
client, err := genai.NewClient(ctx, option.WithAPIKey(apiKey))
if err != nil {
return fmt.Errorf("failed to create new genai client: %w", err)
}
defer client.Close()
modelIterator := client.ListModels(ctx)
for {
model, err := modelIterator.Next()
if errors.Is(err, iterator.Done) {
break
}
if err != nil {
return fmt.Errorf("failed to iterator over model list: %w", err)
}
b, err := yaml.Marshal(model)
if err != nil {
return fmt.Errorf("failed to serialize model info: %w", err)
}
if _, err := fmt.Fprintf(cmd.OutOrStdout(), "%s\n", b); err != nil {
return fmt.Errorf("failed to write to output: %w", err)
}
}
return nil
}