|
import aiohttp |
|
import json |
|
import re |
|
from typing import Dict, Optional |
|
|
|
|
|
PROJECT_ID_CACHE: Dict[str, str] = {} |
|
|
|
|
|
async def discover_project_id(api_key: str) -> str: |
|
""" |
|
Discover project ID by triggering an intentional error with a non-existent model. |
|
The project ID is extracted from the error message and cached for future use. |
|
|
|
Args: |
|
api_key: The Vertex AI Express API key |
|
|
|
Returns: |
|
The discovered project ID |
|
|
|
Raises: |
|
Exception: If project ID discovery fails |
|
""" |
|
|
|
if api_key in PROJECT_ID_CACHE: |
|
print(f"INFO: Using cached project ID: {PROJECT_ID_CACHE[api_key]}") |
|
return PROJECT_ID_CACHE[api_key] |
|
|
|
|
|
error_url = f"https://aiplatform.googleapis.com/v1/publishers/google/models/gemini-2.7-pro-preview-05-06:streamGenerateContent?key={api_key}" |
|
|
|
|
|
payload = { |
|
"contents": [{"role": "user", "parts": [{"text": "test"}]}] |
|
} |
|
|
|
async with aiohttp.ClientSession() as session: |
|
try: |
|
async with session.post(error_url, json=payload) as response: |
|
response_text = await response.text() |
|
|
|
try: |
|
|
|
error_data = json.loads(response_text) |
|
|
|
|
|
if isinstance(error_data, list) and len(error_data) > 0: |
|
error_data = error_data[0] |
|
|
|
if "error" in error_data: |
|
error_message = error_data["error"].get("message", "") |
|
|
|
|
|
match = re.search(r'projects/(\d+)/locations/', error_message) |
|
if match: |
|
project_id = match.group(1) |
|
PROJECT_ID_CACHE[api_key] = project_id |
|
print(f"INFO: Discovered project ID: {project_id}") |
|
return project_id |
|
except json.JSONDecodeError: |
|
|
|
match = re.search(r'projects/(\d+)/locations/', response_text) |
|
if match: |
|
project_id = match.group(1) |
|
PROJECT_ID_CACHE[api_key] = project_id |
|
print(f"INFO: Discovered project ID from raw response: {project_id}") |
|
return project_id |
|
|
|
raise Exception(f"Failed to discover project ID. Status: {response.status}, Response: {response_text[:500]}") |
|
|
|
except Exception as e: |
|
print(f"ERROR: Failed to discover project ID: {e}") |
|
raise |