Get hands-on experience with 20+ free Google Cloud products and $300 in free credit for new customers.

NameError: name 'generative_models' is not defined

import vertexai
from vertexai.language_models import ChatModel, InputOutputTextPair

vertexai.init(project="medspeech-436218", location="us-central1")
chat_model = ChatModel.from_pretrained("chat-bison")
parameters = {
"candidate_count": 1,
"max_output_tokens": 1024,
"temperature": 0.2,
"top_p": 0.8,
"top_k": 40
},
safety_settings={
generative_models.HarmCategory.HARM_CATEGORY_HATE_SPEECH: generative_models.HarmBlockThreshold.BLOCK_MEDIUM_AND_ABOVE,
generative_models.HarmCategory.HARM_CATEGORY_DANGEROUS_CONTENT: generative_models.HarmBlockThreshold.BLOCK_MEDIUM_AND_ABOVE,
generative_models.HarmCategory.HARM_CATEGORY_SEXUALLY_EXPLICIT: generative_models.HarmBlockThreshold.BLOCK_MEDIUM_AND_ABOVE,
generative_models.HarmCategory.HARM_CATEGORY_HARASSMENT: generative_models.HarmBlockThreshold.BLOCK_MEDIUM_AND_ABOVE,
}
chat = chat_model.start_chat(
context="""""",
)
response = chat.send_message("""What does Cymbal sell?""", **parameters)
print(f"Response from Model: {response.text}")
response = chat.send_message("""When was the company founded?""", **parameters)
print(f"Response from Model: {response.text}")
response = chat.send_message("""How much is the price of Cymbal clothes?""", **parameters)
print(f"Response from Model: {response.text}").                       I got this error:

NameError                                 Traceback (most recent call last)
Cell In[25], line 14
      5 chat_model = ChatModel.from_pretrained("chat-bison")
      6 parameters = {
      7     "candidate_count": 1,
      8     "max_output_tokens": 1024,
   (...)
     11     "top_k": 40
     12 },
     13 safety_settings={
---> 14           generative_models.HarmCategory.HARM_CATEGORY_HATE_SPEECH: generative_models.HarmBlockThreshold.BLOCK_MEDIUM_AND_ABOVE,
     15           generative_models.HarmCategory.HARM_CATEGORY_DANGEROUS_CONTENT: generative_models.HarmBlockThreshold.BLOCK_MEDIUM_AND_ABOVE,
     16           generative_models.HarmCategory.HARM_CATEGORY_SEXUALLY_EXPLICIT: generative_models.HarmBlockThreshold.BLOCK_MEDIUM_AND_ABOVE,
     17           generative_models.HarmCategory.HARM_CATEGORY_HARASSMENT: generative_models.HarmBlockThreshold.BLOCK_MEDIUM_AND_ABOVE,
     18     }
     19 chat = chat_model.start_chat(
     20     context="""""",
     21 )
     22 response = chat.send_message("""What does Cymbal sell?""", **parameters)

NameError: name 'generative_models' is not defined
0 1 817
1 REPLY 1

Hi @Omehrpour

Welcome to Google Cloud Community!

You're using the generative_models module without importing it explicitly.

The generative_models module containing HarmCategory and HarmBlockThreshold is not imported in your code. You need to add this line at the beginning of your script:

import vertexai
from vertexai.language_models import ChatModel, InputOutputTextPair
from vertexai.language_models import generative_models # Add this line

You may also consider to check the following:

  • Vertex AI Project and Location: Ensure that the project and location you've provided in vertexai.init() are correct and that you have sufficient permissions to access the Vertex AI resources.
  • Network Connectivity: Make sure you have a stable internet connection to communicate with Vertex AI's services.
  • Model Availability: The chat-bison model might be subject to availability or quota restrictions. 

I hope the above information is helpful.