File size: 3,262 Bytes
9d82eed 80b3b2e 9d82eed 80b3b2e 9d82eed | 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 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 | """
One-shot smoke test: verify GOOGLE_API_KEY against the Gemini API.
Default model matches Parlay (`agent/gemini_client.py`: ``gemini-2.5-flash``).
If that model is unavailable (404), the script retries once with
``gemini-1.5-flash``. Override: ``set GEMINI_MODEL=...``.
Usage (from repo root, with venv active):
python scripts/check_gemini.py
Loads .env from the project root if python-dotenv is available.
Exits 0 on success, 1 on configuration/API error.
"""
from __future__ import annotations
import os
import sys
from pathlib import Path
# Keep in sync with agent/gemini_client.MODEL_ID
_DEFAULT_PARLAY_MODEL = "gemini-2.5-flash"
_FALLBACK_MODEL = "gemini-2.5-flash-lite"
def _load_dotenv() -> None:
root = Path(__file__).resolve().parent.parent
try:
from dotenv import load_dotenv # type: ignore[import-not-found]
load_dotenv(root / ".env")
except ImportError:
pass
def main() -> int:
_load_dotenv()
key = (os.environ.get("GOOGLE_API_KEY") or "").strip()
if not key:
print("error: GOOGLE_API_KEY is empty or not set (add to .env or the environment).", file=sys.stderr)
return 1
try:
from google import genai
from google.genai import types
except ImportError as exc:
print(
f"error: google-genai is not installed: {exc}\n pip install google-genai",
file=sys.stderr,
)
return 1
model = (os.environ.get("GEMINI_MODEL") or "").strip() or _DEFAULT_PARLAY_MODEL
client = genai.Client(api_key=key)
def _call(m: str):
return client.models.generate_content(
model=m,
contents="Reply with exactly the single word: ok",
config=types.GenerateContentConfig(
max_output_tokens=16,
temperature=0.0,
),
)
response = None
last_err: Exception | None = None
try:
response = _call(model)
except Exception as exc: # noqa: BLE001 — surface API / auth errors to the user
last_err = exc
msg = str(exc).lower()
if (
model == _DEFAULT_PARLAY_MODEL
and ("404" in str(exc) or "not_found" in msg)
and _FALLBACK_MODEL not in msg
):
print(
f"note: {_DEFAULT_PARLAY_MODEL} not available for this key; "
f"retrying {_FALLBACK_MODEL} (set GEMINI_MODEL to pin a model).",
file=sys.stderr,
)
try:
model = _FALLBACK_MODEL
response = _call(model)
last_err = None
except Exception as exc2: # noqa: BLE001
last_err = exc2
if last_err is not None:
print(f"error: request failed: {type(last_err).__name__}: {last_err}", file=sys.stderr)
return 1
assert response is not None
text = (getattr(response, "text", None) or "").strip()
if not text:
print("error: empty response (check model name and account quota).", file=sys.stderr)
return 1
print(f"Model: {model}")
print(f"Response: {text!r}")
print("ok: Gemini API responded successfully.")
return 0
if __name__ == "__main__":
raise SystemExit(main())
|