🦞
Gemini (Google AI Studio) の Python SDK で BASE_URL を変えたい!
みなさんも変えたいですよね
Cloudflare AI Gateway とか使いたいですよね
でもPython の Google AI Python SDK for the Gemini APIでは設定する方法がないんですよね多分
import google.generativeai as genai
genai.configure(
api_key=env.GEMINI_API_KEY,
base_url="https://base-url", # ← 許されない
)
model = genai.GenerativeModel(model_name="gemini-2.0-flash")
response = model.generate_content("こんちわ")
# ↑ エラーになる
結論
新しい Python SDK(Google Gen AI SDK)を使おう!
from google import genai
client = genai.Client(
api_key=env.GEMINI_API_KEY,
http_options=genai.types.HttpOptions(
base_url="https://base-url" # ←設定できる!
)
)
response = client.models.generate_content(
model="gemini-2.0-flash",
contents="こんちわ",
)
# ↑ 通る!
探すの苦労したので公式ドキュメントのわかりやすいところに書いてください!
余談
ちなみに JavaScript の Google AI SDK for JavaScript なら base_url 指定できます
import { GoogleGenerativeAI } from "@google/generative-ai";
const api_token = env.GOOGLE_AI_STUDIO_TOKEN;
const genAI = new GoogleGenerativeAI(api_token);
const model = genAI.getGenerativeModel(
{ model: "gemini-2.0-flash" },
{
baseUrl: "https://base-url", // ←これが許される
},
);
Discussion