-
Notifications
You must be signed in to change notification settings - Fork 236
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
chore: switch controllerbuilder to use vertexai gemini endpoint
This means we don't need an API Key (but we do need a GCP account)
- Loading branch information
Showing
9 changed files
with
165 additions
and
102 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Large diffs are not rendered by default.
Oops, something went wrong.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,55 @@ | ||
package llm | ||
|
||
import ( | ||
"bytes" | ||
"context" | ||
"fmt" | ||
"os" | ||
"os/exec" | ||
"strings" | ||
|
||
"cloud.google.com/go/vertexai/genai" | ||
"google.golang.org/api/option" | ||
"k8s.io/klog/v2" | ||
) | ||
|
||
// BuildGeminiClient builds a client for the Gemini API. | ||
func BuildGeminiClient(ctx context.Context) (*genai.Client, error) { | ||
log := klog.FromContext(ctx) | ||
|
||
var opts []option.ClientOption | ||
|
||
if s := os.Getenv("GEMINI_API_KEY"); s != "" { | ||
opts = append(opts, option.WithAPIKey(s)) | ||
} | ||
// else { | ||
// creds, err := google.FindDefaultCredentials(ctx, "https://www.googleapis.com/auth/generative-language", "https://www.googleapis.com/auth/cloud-platform") | ||
// if err != nil { | ||
// return nil, fmt.Errorf("finding default credentials: %w", err) | ||
// } | ||
// opts = append(opts, option.WithCredentials(creds)) | ||
// } | ||
|
||
projectID := "" | ||
location := "" | ||
|
||
if projectID == "" { | ||
cmd := exec.CommandContext(ctx, "gcloud", "config", "get", "project") | ||
var stdout bytes.Buffer | ||
cmd.Stdout = &stdout | ||
if err := cmd.Run(); err != nil { | ||
return nil, fmt.Errorf("cannot get project (using gcloud config get project): %w", err) | ||
} | ||
projectID = strings.TrimSpace(stdout.String()) | ||
if projectID == "" { | ||
return nil, fmt.Errorf("project was not set in gcloud config") | ||
} | ||
log.Info("got project from gcloud config", "project", projectID) | ||
} | ||
|
||
client, err := genai.NewClient(ctx, projectID, location, opts...) | ||
if err != nil { | ||
return nil, fmt.Errorf("building gemini client: %w", err) | ||
} | ||
return client, nil | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters