本文以Gemini刚出的nano-banana为例,演示如何通过new-api程序转发后的api,如何实现使用gemini的sdk包进行生图。
注意:以下转发系统以本站UnifyLLM为例进行演示。
new-api版本已经兼容了gemini的aistudio格式的绘图,只需要在系统设置中,将绘图程序加上以下两个:
[ "gemini-2.0-flash-preview-image-generation", "gemini-2.5-flash-image-preview" ]
所谓的nano-banana实际就是api模型gemini-2.5-flash-image-preview。
但是在vertex渠道接入的绘图,目前尚未完全兼容,除了完成上述设置外,还要增加一个额外的参数:
"generationConfig": { "responseModalities": [ "TEXT", "IMAGE" ] }
curl方式:
可以参考示例:https://unifyllm.apifox.cn/317173846e0
请求体部分像这样填写:
{ "contents": [ { "parts": [ { "text": "Hi, can you create a 3d rendered image of a pig with wings and a top hat flying over a happy futuristic scifi city with lots of greenery?" } ] } ], "generationConfig": { "responseModalities": [ "TEXT", "IMAGE" ] } }
python中使用genai(即gemini的sdk包请求):
from google import genai from google.genai import types from PIL import Image from io import BytesIO # 创建客户端 client = genai.Client( api_key='sk-xxx', http_options=types.HttpOptions( base_url='https://apihk.unifyllm.top' ) ) prompt = ( "Create a picture of a nano banana dish in a fancy restaurant with a Gemini theme" ) # 添加 generationConfig 配置 generation_config = { "responseModalities": [ "TEXT", "IMAGE" ] } response = client.models.generate_content( model="gemini-2.5-flash-image-preview", contents=[prompt], config=generation_config, # 添加这行配置 ) for part in response.candidates[0].content.parts: if part.text is not None: print(part.text) elif part.inline_data is not None: image = Image.open(BytesIO(part.inline_data.data)) image.save("generated_image.png")
以上方式就基本实现了绘图调用,我们可以再进一步,省去confg的烦恼,这样就不会遇到报错:
{ "error": { "message": "The request is not supported by this model.", "type": "upstream_error", "param": "", "code": 400 } }
具体方法:
在new-api的渠道中,参数覆盖填写如下:
{ "generationConfig": { "responseModalities": [ "TEXT", "IMAGE" ] } }
如此以来,就可以像接入aistudio那样顺畅调用了。