Skip to content
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

Patching for generativeai Python package #1329

Merged
merged 22 commits into from
Dec 3, 2023
Merged
Show file tree
Hide file tree
Changes from 6 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
4 changes: 4 additions & 0 deletions Dockerfile.tmpl
Original file line number Diff line number Diff line change
Expand Up @@ -634,6 +634,10 @@ RUN mkdir -p ~/src && git clone https://github.com/SohierDane/BigQuery_Helper ~/
sed -i 's/)/packages=["bq_helper"])/g' ~/src/BigQuery_Helper/setup.py && \
pip install -e ~/src/BigQuery_Helper && \
/tmp/clean-layer.sh

RUN pip install wrapt \
google-generativeai && \
/tmp/clean-layer.sh

# Add BigQuery client proxy settings
ENV PYTHONUSERBASE "/root/.local"
Expand Down
26 changes: 26 additions & 0 deletions patches/sitecustomize.py
Original file line number Diff line number Diff line change
Expand Up @@ -7,6 +7,8 @@
import importlib
import importlib.machinery

import wrapt

class GcpModuleFinder(importlib.abc.MetaPathFinder):
_MODULES = [
'google.cloud.bigquery',
Expand Down Expand Up @@ -73,3 +75,27 @@ def exec_module(self, module):

if not hasattr(sys, 'frozen'):
sys.meta_path.insert(0, GcpModuleFinder())

@wrapt.when_imported('google.generativeai')
Copy link
Contributor

@rosbo rosbo Nov 30, 2023

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Is this called only the first time this module is imported or again every time?

To confirm, you can simply add a print statement and test in a notebook by running "import 'google.generativeai" a few times.

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

It's only called the first time it seems.

def post_import_logic(module):
if os.getenv('KAGGLE_DISABLE_GOOGLE_GENERATIVE_AI_INTEGRATION') == 1:
return
old_configure = module.configure
def new_configure(*args, **kwargs):
if ('default_metadata' in kwargs):
default_metadata = kwargs['default_metadata']
else:
default_metadata = []
kwargs['transport'] = 'rest' # Only support REST requests for now
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Because this is hard coded here, how are you going to enable grpc when it's ready? All the users using this image will have this code forever.

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I see, I'll set an environment variable then.

default_metadata.append(("x-kaggle-proxy-data", os.environ['KAGGLE_DATA_PROXY_TOKEN']))
default_metadata.append(('x-kaggle-authorization', f'Bearer {os.environ['KAGGLE_USER_SECRETS_TOKEN']}'))
kwargs['default_metadata'] = default_metadata
if ('client_options' in kwargs):
client_options = kwargs['client_options']
else:
client_options = {}
client_options['api_endpoint'] = os.environ['KAGGLE_DATA_PROXY_URL'] + '/palmapi'
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Same: hard coding this endpoint here means you will never be able to change it without breaking all the users using this image

kwargs['client_options'] = client_options
old_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
Philmod marked this conversation as resolved.
Show resolved Hide resolved