@@ -48,15 +48,51 @@ func NewClient(ctx context.Context, cfg *latest.ModelConfig, env environment.Pro
4848
4949 var clientFn func (context.Context ) (* genai.Client , error )
5050 if gateway := globalOptions .Gateway (); gateway == "" {
51- apiKey := env .Get (ctx , "GOOGLE_API_KEY" )
52- if apiKey == "" {
53- return nil , errors .New ("GOOGLE_API_KEY environment variable is required" )
51+ var (
52+ httpClient * http.Client
53+ backend genai.Backend
54+ apiKey string
55+ project string
56+ location string
57+ )
58+ // project/location take priority over API key, like in the genai client.
59+ if cfg .ProviderOpts ["project" ] != nil || cfg .ProviderOpts ["location" ] != nil {
60+ var err error
61+
62+ project , err = environment .Expand (ctx , providerOption (cfg , "project" ), env )
63+ if err != nil {
64+ return nil , fmt .Errorf ("expanding project: %w" , err )
65+ }
66+ if project == "" {
67+ return nil , errors .New ("project must be set" )
68+ }
69+
70+ location , err = environment .Expand (ctx , providerOption (cfg , "location" ), env )
71+ if err != nil {
72+ return nil , fmt .Errorf ("expanding location: %w" , err )
73+ }
74+ if location == "" {
75+ return nil , errors .New ("location must be set" )
76+ }
77+
78+ backend = genai .BackendVertexAI
79+ httpClient = nil // Use default client
80+ } else {
81+ apiKey = env .Get (ctx , "GOOGLE_API_KEY" )
82+ if apiKey == "" {
83+ return nil , errors .New ("GOOGLE_API_KEY environment variable is required" )
84+ }
85+
86+ backend = genai .BackendGeminiAPI
87+ httpClient = httpclient .NewHTTPClient ()
5488 }
5589
5690 client , err := genai .NewClient (ctx , & genai.ClientConfig {
5791 APIKey : apiKey ,
58- Backend : genai .BackendGeminiAPI ,
59- HTTPClient : httpclient .NewHTTPClient (),
92+ Project : project ,
93+ Location : location ,
94+ Backend : backend ,
95+ HTTPClient : httpClient ,
6096 HTTPOptions : genai.HTTPOptions {
6197 BaseURL : cfg .BaseURL ,
6298 },
@@ -548,3 +584,11 @@ func defaultsTo(value, defaultValue string) string {
548584 }
549585 return defaultValue
550586}
587+
588+ func providerOption (cfg * latest.ModelConfig , name string ) string {
589+ v := cfg .ProviderOpts [name ]
590+ if v , ok := v .(string ); ok {
591+ return v
592+ }
593+ return ""
594+ }
0 commit comments