|
| 1 | +from dataclasses import dataclass |
| 2 | + |
| 3 | +from google import genai |
| 4 | +from google.auth import default |
| 5 | +from google.auth.credentials import Credentials |
| 6 | +from loguru import logger |
| 7 | + |
| 8 | +from pinjected import instance |
| 9 | + |
| 10 | + |
| 11 | +@dataclass |
| 12 | +class GenAIAuthClient: |
| 13 | + """Authentication client for Google Gen AI with Vertex AI support.""" |
| 14 | + |
| 15 | + credentials: Credentials |
| 16 | + project_id: str |
| 17 | + location: str = "global" |
| 18 | + |
| 19 | + def create_client(self) -> genai.Client: |
| 20 | + """Create a Gen AI client with Vertex AI and ADC support.""" |
| 21 | + # Use vertexai=True to enable Vertex AI mode with ADC |
| 22 | + client = genai.Client( |
| 23 | + vertexai=True, |
| 24 | + project=self.project_id, |
| 25 | + location=self.location, |
| 26 | + credentials=self.credentials, |
| 27 | + ) |
| 28 | + logger.info( |
| 29 | + f"Created Gen AI client with Vertex AI mode for project {self.project_id} " |
| 30 | + f"in {self.location}" |
| 31 | + ) |
| 32 | + return client |
| 33 | + |
| 34 | + |
| 35 | +@instance |
| 36 | +def genai_auth_client_adc() -> GenAIAuthClient: |
| 37 | + """Default Gen AI auth client using Application Default Credentials.""" |
| 38 | + logger.info("Creating Gen AI auth client using Application Default Credentials") |
| 39 | + credentials, project = default() |
| 40 | + |
| 41 | + if not project: |
| 42 | + raise ValueError( |
| 43 | + "Could not determine GCP project ID from Application Default Credentials. " |
| 44 | + "Please set GOOGLE_CLOUD_PROJECT environment variable or use gcloud config." |
| 45 | + ) |
| 46 | + |
| 47 | + logger.info(f"Using ADC with project: {project}") |
| 48 | + return GenAIAuthClient(credentials=credentials, project_id=project) |
| 49 | + |
| 50 | + |
| 51 | +@instance |
| 52 | +def genai_auth_client( |
| 53 | + genai_auth_client_adc: GenAIAuthClient, |
| 54 | + genai_location: str, |
| 55 | +) -> GenAIAuthClient: |
| 56 | + """Gen AI authentication client - default uses ADC, can be overridden via injection.""" |
| 57 | + # Update location from injection |
| 58 | + genai_auth_client_adc.location = genai_location |
| 59 | + return genai_auth_client_adc |
| 60 | + |
| 61 | + |
| 62 | +@instance |
| 63 | +def genai_location() -> str: |
| 64 | + """Default GCP location for Gen AI API. |
| 65 | +
|
| 66 | + Note: Gemini 2.5 Flash Image Preview (nano-banana) is only available |
| 67 | + on the global endpoint, not regional endpoints. |
| 68 | + """ |
| 69 | + return "global" |
| 70 | + |
| 71 | + |
| 72 | +@instance |
| 73 | +def genai_client( |
| 74 | + genai_auth_client: GenAIAuthClient, |
| 75 | +) -> genai.Client: |
| 76 | + """Singleton Gen AI client with Vertex AI and ADC support.""" |
| 77 | + client = genai_auth_client.create_client() |
| 78 | + logger.info( |
| 79 | + f"Created Gen AI client for project {genai_auth_client.project_id} " |
| 80 | + f"in {genai_auth_client.location}" |
| 81 | + ) |
| 82 | + return client |
| 83 | + |
| 84 | + |
| 85 | +@instance |
| 86 | +def genai_model_name() -> str: |
| 87 | + """Default model name for image generation (nano-banana).""" |
| 88 | + return "gemini-2.5-flash-image-preview" |
0 commit comments