-
Notifications
You must be signed in to change notification settings - Fork 972
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
Changes from 6 commits
bb9b855
6508445
684dc6a
a49c250
de9912c
3477d4d
a3c4b16
f301127
4c4240a
5e53b3d
5a1504a
4c5f7cd
7d4f55d
a06ec16
0f7eaa5
f1559e0
f89b6a2
a9d45f9
f7b69ea
dfb9145
ff335d6
10b9a81
File filter
Filter by extension
Conversations
Jump to
Diff view
Diff view
There are no files selected for viewing
Original file line number | Diff line number | Diff line change |
---|---|---|
|
@@ -7,6 +7,8 @@ | |
import importlib | ||
import importlib.machinery | ||
|
||
import wrapt | ||
|
||
class GcpModuleFinder(importlib.abc.MetaPathFinder): | ||
_MODULES = [ | ||
'google.cloud.bigquery', | ||
|
@@ -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') | ||
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 | ||
There was a problem hiding this comment. Choose a reason for hiding this commentThe 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. There was a problem hiding this comment. Choose a reason for hiding this commentThe 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' | ||
There was a problem hiding this comment. Choose a reason for hiding this commentThe 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
|
There was a problem hiding this comment.
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.
There was a problem hiding this comment.
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.