Skip to content

Commit 1e8d192

Browse files
authored
Add patch for google_genai (#1454)
1 parent c9bf1dd commit 1e8d192

File tree

1 file changed

+27
-0
lines changed

1 file changed

+27
-0
lines changed

patches/sitecustomize.py

Lines changed: 27 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -117,3 +117,30 @@ def new_configure(*args, **kwargs):
117117

118118
module.configure = new_configure
119119
module.configure() # generativeai can use GOOGLE_API_KEY env variable, so make sure we have the other configs set
120+
121+
@wrapt.when_imported('google.genai')
122+
def post_genai_import_logic(module):
123+
if os.getenv('KAGGLE_DISABLE_GOOGLE_GENERATIVE_AI_INTEGRATION'):
124+
return
125+
126+
if not (os.getenv('KAGGLE_DATA_PROXY_TOKEN') and
127+
os.getenv('KAGGLE_USER_SECRETS_TOKEN') and
128+
os.getenv('KAGGLE_DATA_PROXY_URL')):
129+
return
130+
@wrapt.patch_function_wrapper(module, 'Client.__init__')
131+
def init_wrapper(wrapped, instance, args, kwargs):
132+
# Don't want to forward requests that are to Vertex AI, debug mode, or have their own http_options specified
133+
# Thus, if the client constructor contains any params other than api_key, we don't set up forwarding
134+
if any(value is not None for key, value in kwargs.items() if key != 'api_key'):
135+
return wrapped(*args, **kwargs)
136+
137+
default_metadata = {
138+
"x-kaggle-proxy-data": os.environ['KAGGLE_DATA_PROXY_TOKEN'],
139+
'x-kaggle-authorization': f"Bearer {os.environ['KAGGLE_USER_SECRETS_TOKEN']}"
140+
}
141+
http_options = {
142+
'base_url': os.getenv('KAGGLE_DATA_PROXY_URL') + '/palmapi/',
143+
'headers': default_metadata
144+
}
145+
kwargs['http_options'] = http_options
146+
return wrapped(*args, **kwargs)

0 commit comments

Comments
 (0)