Skip to content

Add patch for google_genai #1454

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Merged
merged 1 commit into from
Dec 16, 2024
Merged
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
27 changes: 27 additions & 0 deletions patches/sitecustomize.py
Original file line number Diff line number Diff line change
Expand Up @@ -117,3 +117,30 @@ def new_configure(*args, **kwargs):

module.configure = new_configure
module.configure() # generativeai can use GOOGLE_API_KEY env variable, so make sure we have the other configs set

@wrapt.when_imported('google.genai')
def post_genai_import_logic(module):
if os.getenv('KAGGLE_DISABLE_GOOGLE_GENERATIVE_AI_INTEGRATION'):
return

if not (os.getenv('KAGGLE_DATA_PROXY_TOKEN') and
os.getenv('KAGGLE_USER_SECRETS_TOKEN') and
os.getenv('KAGGLE_DATA_PROXY_URL')):
return
@wrapt.patch_function_wrapper(module, 'Client.__init__')
def init_wrapper(wrapped, instance, args, kwargs):
# Don't want to forward requests that are to Vertex AI, debug mode, or have their own http_options specified
# Thus, if the client constructor contains any params other than api_key, we don't set up forwarding
if any(value is not None for key, value in kwargs.items() if key != 'api_key'):
return wrapped(*args, **kwargs)

default_metadata = {
"x-kaggle-proxy-data": os.environ['KAGGLE_DATA_PROXY_TOKEN'],
'x-kaggle-authorization': f"Bearer {os.environ['KAGGLE_USER_SECRETS_TOKEN']}"
}
http_options = {
'base_url': os.getenv('KAGGLE_DATA_PROXY_URL') + '/palmapi/',
'headers': default_metadata
}
kwargs['http_options'] = http_options
return wrapped(*args, **kwargs)
Loading