File size: 1,700 Bytes
4949db9 | 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 | """Shared Gemini API client and argument utilities."""
import json
import logging
import os
import re
from pathlib import Path
logger = logging.getLogger(__name__)
GEMINI_MODEL = "gemini-3.1-pro-preview"
def add_gemini_args(parser):
"""Add standard Gemini CLI arguments to an argparse parser."""
parser.add_argument("--api_key_file", type=str,
default=None,
help="Path to a Gemini API key file. If omitted, GEMINI_API_KEY is used.")
parser.add_argument("--vertexai", action="store_true")
parser.add_argument("--project", type=str, default="your-gcp-project")
parser.add_argument("--location", type=str, default="global")
def make_client(args):
"""Create a google.genai.Client from parsed CLI arguments."""
from google import genai
if args.vertexai:
client = genai.Client(vertexai=True, project=args.project,
location=args.location)
logger.info("Using Vertex AI (project=%s, location=%s)",
args.project, args.location)
else:
if args.api_key_file:
api_key = Path(args.api_key_file).read_text().strip()
else:
api_key = os.environ.get("GEMINI_API_KEY")
if not api_key:
raise ValueError("Set GEMINI_API_KEY or pass --api_key_file.")
client = genai.Client(api_key=api_key)
return client
def extract_json_array(text):
"""Extract and parse a JSON array from LLM response text."""
match = re.search(r"\[.*?\]", text, re.DOTALL)
if match:
try:
return json.loads(match.group())
except json.JSONDecodeError:
pass
return None
|