pyrsth
pyrsth

Reputation: 21

Error connecting to Google servers running Gemini AI in google colab

I came to run Gemini AI in Jupyter Notebook in Google Colab according to the Google guide, but I encountered an error in the list modules step: (I don't have a Google Cloud account and Colab Pro)

ERROR:tornado.access:500 GET /v1beta/models?pageSize=50&%24alt=json%3Benum-encoding%3Dint (127.0.0.1) 3290.39ms

---------------------------------------------------------------------------

InternalServerError                       Traceback (most recent call last)

<ipython-input-20-77709e92acfe> in <cell line: 1>()
----> 1 for m in genai.list_models():
      2   if 'generateContent' in m.supported_generation_methods:
      3     print(m.name)

7 frames

/usr/local/lib/python3.10/dist-packages/google/ai/generativelanguage_v1beta/services/model_service/transports/rest.py in __call__(self, request, retry, timeout, metadata)
    826             # subclass.
    827             if response.status_code >= 400:
--> 828                 raise core_exceptions.from_http_response(response)
    829 
    830             # Return the response

InternalServerError: 500 GET http://localhost:38257/v1beta/models?pageSize=50&%24alt=json%3Benum-encoding%3Dint: TypeError: NetworkError when attempting to fetch resource.

First, I open Google Colab with this Gemini guide

Then I opened Google Colab and entered all the commands one by one received the API key from vertex ai placed it here and put the gemini value in GOOGLE_API_KEY

apikey

and I reached the List models stage and encountered the mentioned error.

Upvotes: 2

Views: 1428

Answers (1)

Kingki19
Kingki19

Reputation: 11

Let's assume we already have and import google-generativeai and have correct Gemini-API-Keys

I encounter same issue like you ERROR:tornado.access:500 POST /v1beta/models/gemini-pro:generateContent?%24alt=json%3Benum-encoding%3Dint (127.0.0.1) 2181.34ms
because i call model to generate content in looping, i mean i apply function that model to generate content using model.generate_content() and then call it in .apply() for dataframe to apply a function in all rows.

I don't know anything about this and try to find answer. I found that the error was because from Google side (server) try to connect to my request and didn't correctly get the request. Correct me if i was wrong.

Here is how i fix this:
Because i use model.generate_content() in a function, i add retry decorator from tenacity library.

Here's example code:

import pandas as pd
import google.generativeai as genai
from tenacity import retry, stop_after_attempt

# initiate gemini
genai.configure(YOUR_GEMINI_API_KEY)
model = genai.GenerativeModel('gemini-pro')

# initiate df
df = pd.read_csv('YOUR_FILE')

@retry(stop=stop_after_attempt(3)) # <-- here's the key
def do_something(text:str) -> str:
  '''Function to generate something from text'''
  response = model.generate_content(text)
  return response.text

# Create new column that contain answer
df['generated_content'] = df['prompt'].apply(lambda text: do_something(text))

Upvotes: 0

Related Questions