Skip to content

Commit 94e4180

Browse files
committed
add googleai
1 parent 81f5b39 commit 94e4180

File tree

3 files changed

+91
-1
lines changed

3 files changed

+91
-1
lines changed

go.mod

Lines changed: 7 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -25,6 +25,7 @@ require (
2525
github.com/gabriel-vasile/mimetype v1.4.5
2626
github.com/gage-technologies/mistral-go v1.1.0
2727
github.com/gdamore/tcell/v2 v2.7.4
28+
github.com/google/generative-ai-go v0.18.0
2829
github.com/google/renameio/v2 v2.0.0
2930
github.com/gowebpki/jcs v1.0.1
3031
github.com/hbollon/go-edlib v1.6.0
@@ -54,6 +55,11 @@ require (
5455
sigs.k8s.io/yaml v1.4.0
5556
)
5657

58+
require (
59+
cloud.google.com/go/ai v0.8.0 // indirect
60+
cloud.google.com/go/longrunning v0.5.9 // indirect
61+
)
62+
5763
require (
5864
cloud.google.com/go v0.115.0 // indirect
5965
cloud.google.com/go/auth v0.7.1 // indirect
@@ -131,7 +137,7 @@ require (
131137
golang.org/x/sys v0.25.0 // indirect
132138
golang.org/x/text v0.18.0 // indirect
133139
golang.org/x/time v0.5.0 // indirect
134-
google.golang.org/api v0.188.0 // indirect
140+
google.golang.org/api v0.188.0
135141
google.golang.org/genproto v0.0.0-20240711142825-46eb208f015d // indirect
136142
google.golang.org/genproto/googleapis/api v0.0.0-20240711142825-46eb208f015d // indirect
137143
google.golang.org/genproto/googleapis/rpc v0.0.0-20240711142825-46eb208f015d // indirect

go.sum

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,8 @@
11
cloud.google.com/go v0.26.0/go.mod h1:aQUYkXzVsufM+DwF1aE+0xfcU+56JwCaLick0ClmMTw=
22
cloud.google.com/go v0.115.0 h1:CnFSK6Xo3lDYRoBKEcAtia6VSC837/ZkJuRduSFnr14=
33
cloud.google.com/go v0.115.0/go.mod h1:8jIM5vVgoAEoiVxQ/O4BFTfHqulPZgs/ufEzMcFMdWU=
4+
cloud.google.com/go/ai v0.8.0 h1:rXUEz8Wp2OlrM8r1bfmpF2+VKqc1VJpafE3HgzRnD/w=
5+
cloud.google.com/go/ai v0.8.0/go.mod h1:t3Dfk4cM61sytiggo2UyGsDVW3RF1qGZaUKDrZFyqkE=
46
cloud.google.com/go/auth v0.7.1 h1:Iv1bbpzJ2OIg16m94XI9/tlzZZl3cdeR3nGVGj78N7s=
57
cloud.google.com/go/auth v0.7.1/go.mod h1:VEc4p5NNxycWQTMQEDQF0bd6aTMb6VgYDXEwiJJQAbs=
68
cloud.google.com/go/auth/oauth2adapt v0.2.3 h1:MlxF+Pd3OmSudg/b1yZ5lJwoXCEaeedAguodky1PcKI=
@@ -155,6 +157,8 @@ github.com/golang/protobuf v1.4.1/go.mod h1:U8fpvMrcmy5pZrNK1lt4xCsGvpyWQ/VVv6QD
155157
github.com/golang/protobuf v1.4.3/go.mod h1:oDoupMAO8OvCJWAcko0GGGIgR6R6ocIYbsSw735rRwI=
156158
github.com/golang/protobuf v1.5.4 h1:i7eJL8qZTpSEXOPTxNKhASYpMn+8e5Q6AdndVa1dWek=
157159
github.com/golang/protobuf v1.5.4/go.mod h1:lnTiLA8Wa4RWRcIUkrtSVa5nRhsEGBg48fD6rSs7xps=
160+
github.com/google/generative-ai-go v0.18.0 h1:6ybg9vOCLcI/UpBBYXOTVgvKmcUKFRNj+2Cj3GnebSo=
161+
github.com/google/generative-ai-go v0.18.0/go.mod h1:JYolL13VG7j79kM5BtHz4qwONHkeJQzOCkKXnpqtS/E=
158162
github.com/google/go-cmp v0.2.0/go.mod h1:oXzfMopK8JAjlY9xF4vHSVASa0yLyX7SntLO5aqRK0M=
159163
github.com/google/go-cmp v0.3.0/go.mod h1:8QqcDgzrUqlUb/G2PQTWiueGozuR1884gddMywk6iLU=
160164
github.com/google/go-cmp v0.3.1/go.mod h1:8QqcDgzrUqlUb/G2PQTWiueGozuR1884gddMywk6iLU=

pkg/mutators/single/googleai.go

Lines changed: 80 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,80 @@
1+
package mutators
2+
3+
import (
4+
"context"
5+
"errors"
6+
"io"
7+
"os"
8+
9+
"github.com/batmac/ccat/pkg/log"
10+
"github.com/google/generative-ai-go/genai"
11+
"google.golang.org/api/iterator"
12+
"google.golang.org/api/option"
13+
)
14+
15+
func init() {
16+
singleRegister("googleai", googleai,
17+
withDescription("googleai, X:gemini-1.5-flash is the model (Requires a valid key in $GOOGLE_API_KEY)"),
18+
withAliases("gai"),
19+
withHintSlow(), // output asap (when no other mutator is used)
20+
withCategory("external APIs"),
21+
withConfigBuilder(stdConfigStringWithDefault("gemini-1.5-flash")),
22+
)
23+
}
24+
25+
func googleai(w io.WriteCloser, r io.ReadCloser, config any) (int64, error) {
26+
modelname := config.(string)
27+
28+
ctx := context.Background()
29+
30+
apikey := os.Getenv("GOOGLE_API_KEY")
31+
log.Debugln("masked apikey: ", mask(apikey))
32+
client, err := genai.NewClient(ctx, option.WithAPIKey(apikey))
33+
if err != nil {
34+
return 0, err
35+
}
36+
defer client.Close()
37+
38+
model := client.GenerativeModel(modelname)
39+
40+
prompt, err := io.ReadAll(r)
41+
if err != nil {
42+
return 0, err
43+
}
44+
45+
iter := model.GenerateContentStream(ctx, genai.Text(prompt))
46+
47+
totalWritten := int64(0)
48+
for {
49+
resp, err := iter.Next()
50+
if errors.Is(err, iterator.Done) {
51+
break
52+
}
53+
if err != nil {
54+
log.Debugln("error: ", err)
55+
return 0, err
56+
}
57+
58+
p := ""
59+
for i, c := range resp.Candidates[0].Content.Parts {
60+
log.Debugln("part ", i, ": ", c)
61+
if t, ok := c.(genai.Text); ok {
62+
p += string(t)
63+
}
64+
}
65+
66+
n, err := io.WriteString(w, p)
67+
if err != nil {
68+
return 0, err
69+
}
70+
totalWritten += int64(n)
71+
}
72+
return totalWritten, nil
73+
}
74+
75+
func mask(s string) string {
76+
if len(s) <= 8 {
77+
return "********"
78+
}
79+
return "****..." + s[len(s)-2:]
80+
}

0 commit comments

Comments
 (0)