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

Fix initialization of google-generativeai #76

Closed
NP4567-dev opened this issue Aug 28, 2024 · 7 comments
Closed

Fix initialization of google-generativeai #76

NP4567-dev opened this issue Aug 28, 2024 · 7 comments
Labels
bug Something isn't working

Comments

@NP4567-dev
Copy link
Collaborator

Description

Problem:

Starting from a new venv, Ecologits.init() raises the following error when initiating google_instrumentor:

ModuleNotFoundError: No module named 'google'

Relevant information:

  • the project relies on a pyproject.toml but does not use poetry
  • google generative-ai is not in the pyproject.toml

Workaround:

Adding google-generativeai to my pyproject.toml solves the problem.
This works but is not perfect as I install a bunch of packages I'll never need.

"Easy fix":

  • Editing init_google_instrumentor:
    if importlib.util.find_spec("google.generativeai") is not None:
    becomes:
    if importlib.util.find_spec("google-generativeai") is not None:
    Everything works fine in my case but I have no way to validate it works for an user using google-generativeai.

New feature solution:

Enabling the user to choose which publisher to load at the init.
This could avert the occurrence of an environment-specific issue that renders the entire package unusable.
I started adding such a feature on my fork, let me know if you are interested on adding it to the main repo.

@NP4567-dev NP4567-dev added the feature request Feature request label Aug 28, 2024
@samuelrince
Copy link
Member

samuelrince commented Aug 28, 2024

Hey @NP4567-dev, are you using the latest version of EcoLogits (0.3.2) we have already fixed the issue with google in #62 ?

Otherwise, I agree on the fact that we need to let the user choose which tracer to initialize, discussed in #65. We will update it very soon.

@NP4567-dev
Copy link
Collaborator Author

NP4567-dev commented Aug 28, 2024

I'm on version 0.3.2, I think the problem comes from having google.generativeai instead of google-generativeai (as the fix in #62 suggested)

@samuelrince
Copy link
Member

Changing google.generativeai in the importlib.util.find_spec function by google-generativeai will make google provider unusable. The check is about finding whether a python module exists or not. For google-generativeai library, we need to look for the associated module, which is google.generativeai. A module cannot contain dashes "-".

Can you @NP4567-dev, please provide the full error trace and the output of the pip freeze command? I don't know what is going on with Google libraries, but we do not have this type of issue with others...

Thanks 🙏

@samuelrince samuelrince added bug Something isn't working and removed feature request Feature request labels Aug 28, 2024
@samuelrince samuelrince changed the title Enabling the user to choose which instrumentor to init Fix initialization of google-generativeai Aug 28, 2024
@NP4567-dev
Copy link
Collaborator Author

NP4567-dev commented Aug 28, 2024

I believe it is related to the find_spec mechanism when searching for the parent package..
I ran it in debug, the error is raised at this point in the find_spec function.
When reaching this point, the value of parent_name is 'google' whereas for other providers, it is just an empty string and does not raise an error. So this might not be google specific but "module with a parent" specific.

Here is as much stacktrace as I can provide:

  File "{project_path}/src/my_package/api/main.py", line 17, in <module>
    EcoLogits.init()
  File "{project_path}/.venv/lib/python3.11/site-packages/ecologits/ecologits.py", line 42, in init
    init_instruments()
  File "{project_path}/.venv/lib/python3.11/site-packages/ecologits/ecologits.py", line 52, in init_instruments
    init_google_instrumentor()
  File "{project_path}/.venv/lib/python3.11/site-packages/ecologits/ecologits.py", line 100, in init_google_instrumentor
    if importlib.util.find_spec("google.generativeai") is not None:
       ^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^
  File " {python_path}/python_3_11/lib/python3.11/importlib/util.py", line 94, in find_spec
    parent = __import__(parent_name, fromlist=['__path__'])
             ^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^
ModuleNotFoundError: No module named 'google'

@samuelrince
Copy link
Member

Thanks, so probably changing to the following would fix that issue. First look for google, then google.generativeai.

def init_google_instrumentor() -> None:
    if importlib.util.find_spec("google") is not None \
            and importlib.util.find_spec("google.generativeai") is not None:
        from ecologits.tracers.google_tracer import GoogleInstrumentor

        instrumentor = GoogleInstrumentor()
        instrumentor.instrument()

@NP4567-dev
Copy link
Collaborator Author

I applied this change locally and it works, this seems like a viable fix 🥳
Thanks for checking 😃

@samuelrince
Copy link
Member

Fixed by #77

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
bug Something isn't working
Projects
None yet
Development

No branches or pull requests

2 participants