google-gemini / generative-ai-python

The official Python library for the Google Gemini API
https://pypi.org/project/google-generativeai/
Apache License 2.0
1.66k stars 333 forks source link

ModuleNotFoundError: No module named 'google.ai' #143

Closed wangvation closed 11 months ago

wangvation commented 11 months ago

Description of the bug:

I'm encountering an error when trying to use the google.generativeai library in my Python script.

import google.generativeai as genai
genai.configure(api_key="MY_API_KEY")
# Set up the model
generation_config = {
  "temperature": 0.0,
  "top_p": 1,
  "top_k": 1,
  "max_output_tokens": 4096,
}

safety_settings = [
  {
    "category": "HARM_CATEGORY_HARASSMENT",
    "threshold": "BLOCK_MEDIUM_AND_ABOVE"
  },
  {
    "category": "HARM_CATEGORY_HATE_SPEECH",
    "threshold": "BLOCK_MEDIUM_AND_ABOVE"
  },
  {
    "category": "HARM_CATEGORY_SEXUALLY_EXPLICIT",
    "threshold": "BLOCK_MEDIUM_AND_ABOVE"
  },
  {
    "category": "HARM_CATEGORY_DANGEROUS_CONTENT",
    "threshold": "BLOCK_MEDIUM_AND_ABOVE"
  }
]

model = genai.GenerativeModel(model_name="gemini-pro",
                              generation_config=generation_config,
                              safety_settings=safety_settings)

prompt_parts = [
  " You are an AI assistant named 'Translator'. You will be receiving content in various languages. If the material is in English, provide the exact original content; otherwise, translate it into English.",
  "input: Le temps est très beau aujourd'hui, idéal pour jouer au volley-ball à la plage.",
  "ouput: Today's weather is very nice, perfect for playing volleyball on the beach.",
  "input: 明日、一緒に天ぷらを食べに行きませんか?",
  "ouput: How about we go eat tempura together tomorrow?",
  "input: Der Aktienmarkt ist heute um weitere 10% gefallen. Wie kann ich Geld verdienen?",
  "ouput: ",
]

response = model.generate_content(prompt_parts)
print(response.text)

I downloaded the generative-ai-python source code from https://github.com/google/generative-ai-python/archive/refs/tags/v0.3.1.tar.gz. Then I ran the command "python setup.py install" in my python3.8.10 virtual env.

Actual vs expected behavior:

Actual behaviour

When I run the script, I get the following error message:

Traceback (most recent call last):
  File "gemini-api-test.py", line 1, in <module>
    import google.generativeai as genai
  File "/data/env/gemini/lib/python3.8/site-packages/google_generativeai-0.3.1-py3.8.egg/google/generativeai/__init__.py", line 45, in <module>
    from google.generativeai import types
  File "/data/env/gemini/lib/python3.8/site-packages/google_generativeai-0.3.1-py3.8.egg/google/generativeai/types/__init__.py", line 17, in <module>
    from google.generativeai.types.discuss_types import *
  File "/data/env/gemini/lib/python3.8/site-packages/google_generativeai-0.3.1-py3.8.egg/google/generativeai/types/discuss_types.py", line 21, in <module>
    import google.ai.generativelanguage as glm
ModuleNotFoundError: No module named 'google.ai'

Expected behaviour

The script should run without errors and generate translations for the provided prompts.

Any other information you'd like to share?

OS: Ubuntu 20.04 Env: virtualenv, Python:3.8.10 Package: Name: google-generativeai Version: 0.3.1 Summary: Google Generative AI High level API client library and tools. Home-page: https://github.com/google/generative-ai-python Author: Google LLC Author-email: googleapis-packages@google.com License: Apache 2.0 Location: /data/env/gemini/lib/python3.8/site-packages/google_generativeai-0.3.1-py3.8.egg Requires: google-ai-generativelanguage, google-api-core, google-auth, protobuf, tqdm

wangvation commented 11 months ago

My code works fine in Colab AI

Andy963 commented 11 months ago

python 3.8 is not full supported

wangvation commented 11 months ago

python 3.8 is not full supported switch to python 3.10 still exists!

wangvation commented 11 months ago

switch to python 3.10, install google.generativeai with pip and this issue was solved.

RyanBaig commented 10 months ago

now, what to do if you can't upgrade to above python 3.8?