使用Google生成式AI API调用imagen-3.0模型时遇404 NotFound错误
解决Google Generative AI API调用Imagen模型时的NotFound错误
问题描述
我尝试使用Google Generative AI API生成图片,但遇到了NotFound错误。我的代码如下:
import os import google.generativeai as genai genai.configure(api_key='my_api_key_here') imagen = genai.ImageGenerationModel("imagen-3.0-generate-001") result = imagen.generate_images( prompt="Fuzzy bunnies in my kitchen", number_of_images=4, safety_filter_level="block_only_high", person_generation="allow_adult", aspect_ratio="3:4", negative_prompt="Outside", )
运行代码后出现以下错误:
File c:\Users\salos\AppData\Local\Programs\Python\Python310\lib\site-packages\google\api_core\grpc_helpers.py:76, in _wrap_unary_errors.<locals>.error_remapped_callable(*args, **kwargs) 75 try: ---> 76 return callable_(*args, **kwargs) 77 except grpc.RpcError as exc: File c:\Users\salos\AppData\Local\Programs\Python\Python310\lib\site-packages\grpc\_channel.py:1181, in _UnaryUnaryMultiCallable.__call__(self, request, timeout, metadata, credentials, wait_for_ready, compression) 1175 ( 1176 state, 1177 call, 1178 ) = self._blocking( 1179 request, timeout, metadata, credentials, wait_for_ready, compression 1180 ) -> 1181 return _end_unary_response_blocking(state, call, False, None) File c:\Users\salos\AppData\Local\Programs\Python\Python310\lib\site-packages\grpc\_channel.py:1006, in _end_unary_response_blocking(state, call, with_call, deadline) 1005 else: -> 1006 raise _InactiveRpcError(state) _InactiveRpcError: <_InactiveRpcError of RPC that terminated with: status = StatusCode.NOT_FOUND details = "models/imagen-3.0-generate-001 is not found for API version v1beta, or is not supported for predict. Call ListModels to see the list of available models and their supported methods." debug_error_string = "UNKNOWN:Error received from peer ipv4:142.250.182.138:443 {created_time:"2024-10-19T14:38:26.5900722+00:00", grpc_status:5, grpc_message:"models/imagen-3.0-generate-001 is not found for API version v1beta, or is not supported for predict. Call ListModels to see the list of available models and their supported methods."}" > ... 76 return callable_(*args, **kwargs) 77 except grpc.RpcError as exc: ---> 78 raise exceptions.from_grpc_error(exc) from exc NotFound: 404 models/imagen-3.0-generate-001 is not found for API version v1beta, or is not supported for predict. Call ListModels to see the list of available models and their supported methods.
我已按照官方指南安装了Gemini API的Python SDK beta版本(pip install -U git+https://github.com/google-gemini/generative-ai-python@imagen)和pillow库。
问题原因
错误提示明确说明imagen-3.0-generate-001模型在v1beta API版本中不存在,或者不支持predict方法。可能是模型名称有误、该模型尚未对当前账号开放,或是SDK版本与模型不匹配导致的。
解决步骤
检查可用模型列表
运行以下代码查看当前账号可访问的所有支持图片生成的模型:import google.generativeai as genai genai.configure(api_key='my_api_key_here') for model in genai.list_models(): if 'generateImages' in model.supported_generation_methods: print(model.name)将代码中的模型ID替换为输出结果里的有效名称。
确认模型名称正确性
当前Imagen的稳定可用模型通常为imagen-2.0-generate-001,若官方文档标注的是最新测试模型,需确认是否已申请该模型的访问权限;若未开放,可先使用稳定版模型测试。验证SDK版本
重新执行安装命令确保SDK是针对Imagen的分支版本:pip install -U git+https://github.com/google-gemini/generative-ai-python@imagen安装完成后检查版本,确保与官方要求一致:
import google.generativeai as genai print(genai.__version__)调整参数兼容性
更换模型后,检查参数是否与目标模型兼容。部分模型可能不支持negative_prompt或特定比例的aspect_ratio,需参考对应模型的文档调整参数。
内容的提问来源于stack exchange,提问作者Dawn of Justize
相关产品推荐
相关产品推荐

