Skip to content

Commit 2c6db71

Browse files
committed
cmd/vulnreport: fix handling of numSuggestions for Gemini
Unlike the PaLM API, which could generate multiple candidate responses at once, the Gemini API only gives one response at a time (at least for now). To accommodate this, modify suggest to make a separate request to the API to generate each suggestion. Change-Id: I5c3adce302abdb5228aef7a8a94d8b3ee5881371 Reviewed-on: https://go-review.googlesource.com/c/vulndb/+/559599 LUCI-TryBot-Result: Go LUCI <[email protected]> Reviewed-by: Damien Neil <[email protected]>
1 parent 079fd4d commit 2c6db71

File tree

1 file changed

+17
-8
lines changed

1 file changed

+17
-8
lines changed

cmd/vulnreport/suggest.go

+17-8
Original file line numberDiff line numberDiff line change
@@ -16,7 +16,7 @@ import (
1616

1717
var (
1818
interactive = flag.Bool("i", false, "for suggest, interactive mode")
19-
numSuggestions = flag.Int("n", 4, "for suggest, the number of suggestions to attempt to generate (max is 8)")
19+
numSuggestions = flag.Int("n", 1, "for suggest, the number of suggestions to generate (>1 can be slow)")
2020
)
2121

2222
func suggestCmd(ctx context.Context, filename string) (err error) {
@@ -47,6 +47,8 @@ func suggestCmd(ctx context.Context, filename string) (err error) {
4747

4848
// In interactive mode, allow user to accept the suggestion,
4949
// see the next one, or quit.
50+
// TODO(tatianabradley): In interactive mode, call the API as requested
51+
// instead of upfront.
5052
if *interactive {
5153
if i == found-1 {
5254
outlog.Printf("\naccept or quit? (a=accept/Q=quit) ")
@@ -77,20 +79,27 @@ func suggestCmd(ctx context.Context, filename string) (err error) {
7779
}
7880

7981
func suggest(ctx context.Context, c genai.Client, r *report.Report, max int) (suggestions []*genai.Suggestion, err error) {
80-
suggestions, err = genai.Suggest(ctx, c, &genai.Input{
81-
Module: r.Modules[0].Module,
82-
Description: r.Description.String(),
83-
})
84-
if err != nil {
85-
return nil, fmt.Errorf("GenAI API error: %s", err)
82+
attempts := 0
83+
maxAttempts := max + 2
84+
for len(suggestions) < max && attempts < maxAttempts {
85+
s, err := genai.Suggest(ctx, c, &genai.Input{
86+
Module: r.Modules[0].Module,
87+
Description: r.Description.String(),
88+
})
89+
if err != nil {
90+
return nil, fmt.Errorf("GenAI API error: %s", err)
91+
}
92+
suggestions = append(suggestions, s...)
93+
attempts++
8694
}
95+
8796
if len(suggestions) > max {
8897
suggestions = suggestions[:max]
8998
}
9099

91100
found := len(suggestions)
92101
if found == 0 {
93-
return nil, fmt.Errorf("could not generate any valid suggestions for report %s (try again?)", r.ID)
102+
return nil, fmt.Errorf("could not generate any valid suggestions for report %s after %d attempts", r.ID, attempts)
94103
}
95104

96105
return suggestions, nil

0 commit comments

Comments
 (0)