连接到在 google colab 中运行 Gemini AI 的 Google 服务器时出错

问题描述 投票:0回答:1

我按照Google指南来在Google Colab中的Jupyter Notebook中运行Gemini AI,但是在列表模块步骤中遇到了错误:(我没有Google Cloud帐户和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.

首先,我用这个 Gemini 指南

打开 Google Colab

然后我打开Google Colab并一一输入所有命令从vertex ai收到的API密钥将其放置在这里并将

gemini
值放入
GOOGLE_API_KEY

apikey

我到达了列表模型阶段并遇到了上述错误。

python google-colaboratory google-cloud-vertex-ai google-gemini
1个回答
0
投票

假设我们已经拥有并导入

google-generativeai
并且拥有正确的 Gemini-API-Keys

我遇到了和你一样的问题

ERROR:tornado.access:500 POST /v1beta/models/gemini-pro:generateContent?%24alt=json%3Benum-encoding%3Dint (127.0.0.1) 2181.34ms

因为我调用模型来循环生成内容,我的意思是我应用该模型的函数来使用
model.generate_content()
生成内容,然后在
.apply()
中调用它以便数据帧在所有行中应用函数。

我对此一无所知并尝试寻找答案。我发现错误是因为 Google 端(服务器)尝试连接到我的请求,但没有正确获取请求。如果我错了请纠正我。

这是我解决这个问题的方法:
因为我在函数中使用

model.generate_content()
,所以我添加了
retry
库中的
tenacity
装饰器。

这是示例代码:

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:
  response = model.generate_content(text)
  return response.text

df['generated_content'] = df['prompt'].apply(lambda text: summarize_comment(text))
© www.soinside.com 2019 - 2024. All rights reserved.