Skip to content

Commit

Permalink
fix: re-use google credentials
Browse files Browse the repository at this point in the history
  • Loading branch information
jackmcguire1 committed Dec 15, 2023
1 parent f1d0cbd commit ae0e585
Showing 1 changed file with 6 additions and 3 deletions.
9 changes: 6 additions & 3 deletions internal/dom/chatmodels/gemini_api.go
Original file line number Diff line number Diff line change
Expand Up @@ -5,20 +5,23 @@ import (
"encoding/base64"

"github.com/google/generative-ai-go/genai"
"golang.org/x/oauth2/google"
"google.golang.org/api/option"
)

type GeminiApiClient struct {
Token []byte
credentials *google.Credentials
}

func NewGeminiApiClient(token string) *GeminiApiClient {
tkn, _ := base64.StdEncoding.DecodeString(token)
return &GeminiApiClient{Token: tkn}
creds, _ := google.CredentialsFromJSON(context.Background(), tkn, "https://www.googleapis.com/auth/generative-language")

return &GeminiApiClient{credentials: creds}
}

func (api *GeminiApiClient) GeminiChat(ctx context.Context, prompt string) (*genai.GenerateContentResponse, error) {
client, err := genai.NewClient(ctx, option.WithCredentialsJSON(api.Token))
client, err := genai.NewClient(ctx, option.WithCredentials(api.credentials))
if err != nil {
return nil, err
}
Expand Down

0 comments on commit ae0e585

Please sign in to comment.