问题描述

最近跑前段时间写的一个调用gemini的项目,发现一直报错。报错提示如下:

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
Traceback (most recent call last):
File "D:\anaconda3\lib\site-packages\google\api_core\grpc_helpers.py", line 79, in error_remapped_callable
return callable_(*args, **kwargs)
File "D:\anaconda3\lib\site-packages\grpc\_channel.py", line 1160, in __call__
return _end_unary_response_blocking(state, call, False, None)
File "D:\anaconda3\lib\site-packages\grpc\_channel.py", line 1003, in _end_unary_response_blocking
raise _InactiveRpcError(state) # pytype: disable=not-instantiable
grpc._channel._InactiveRpcError: <_InactiveRpcError of RPC that terminated with:
status = StatusCode.RESOURCE_EXHAUSTED
details = "Resource has been exhausted (e.g. check quota)."
debug_error_string = "UNKNOWN:Error received from peer ipv4:127.0.0.1:7890 {grpc_message:"Resource has been exhausted (e.g. check quota).", grpc_status:8, created_time:"2024-02-27T03:18:28.3008412+00:00"}"
>

The above exception was the direct cause of the following exception:

Traceback (most recent call last):
File "E:\code\python\project\HUGEMINI\backend.py", line 19, in text
result = m.gemini_pro(history)
File "E:\code\python\project\HUGEMINI\models.py", line 94, in gemini_pro
response = self.model_pro.generate_content(history_processed)
File "D:\anaconda3\lib\site-packages\google\generativeai\generative_models.py", line 243, in generate_content
response = self._client.generate_content(request)
File "D:\anaconda3\lib\site-packages\google\ai\generativelanguage_v1beta\services\generative_service\client.py", line 566, in generate_content
response = rpc(
File "D:\anaconda3\lib\site-packages\google\api_core\gapic_v1\method.py", line 131, in __call__
return wrapped_func(*args, **kwargs)
File "D:\anaconda3\lib\site-packages\google\api_core\retry.py", line 372, in retry_wrapped_func
return retry_target(
File "D:\anaconda3\lib\site-packages\google\api_core\retry.py", line 207, in retry_target
result = target()
File "D:\anaconda3\lib\site-packages\google\api_core\timeout.py", line 120, in func_with_timeout
return func(*args, **kwargs)
File "D:\anaconda3\lib\site-packages\google\api_core\grpc_helpers.py", line 81, in error_remapped_callable
raise exceptions.from_grpc_error(exc) from exc
google.api_core.exceptions.ResourceExhausted: 429 Resource has been exhausted (e.g. check quota).

解决过程

报错里有127.0.0.1:7890,检查代理,发现没问题,访问官方文档也没有被拦截(如果在某个地区不能使用他的文档会直接拦截),说明不是代理的问题。且

1
genai.configure(api_key=API_KEY)

这句代码能够被正常运行,说明连接没有问题。

进一步排查:

前几周都还能用,但是现在不能用了,检查API_KEY,发现key还活着,官网上网页版的ai也是能正常使用,盲猜是接口更新了。打印可以使用的模型。

1
2
3
4
5
genai.configure(api_key=API_KEY)
# 打印可用模型
for m in genai.list_models():
if 'generateContent' in m.supported_generation_methods:
print(m.name)
1
2
3
4
5
6
7
8
models/gemini-1.0-pro
models/gemini-1.0-pro-001
models/gemini-1.0-pro-latest
models/gemini-1.0-pro-vision-latest
models/gemini-pro
models/gemini-pro-vision

2024.2.27

发现确实是更新了,这里可以发现官方文档还是写的老的接口。

image-20240227112511854

这时候只要改成新的接口就好了。

1
2
3
self.model_pro = genai.GenerativeModel(model_name='gemini-pro')
# 改成👇
self.model_pro = genai.GenerativeModel(model_name='models/gemini-1.0-pro-latest')

测试下

image-20240227112730993

问题解决!


还有一个这里报了429错误,网上可以看到,http的429是指频繁访问。

image-20240227113050208

并不是很清楚为啥报这个错。