graphify icon indicating copy to clipboard operation
graphify copied to clipboard

Feature: Add Vertex AI backend for semantic extraction

Open kaovilai opened this issue 6 months ago • 1 comments

Feature idea

Add Google Vertex AI as a backend for graphify extract. Vertex AI is the enterprise-managed route to Gemini models for teams that don't use (or can't use) a direct GEMINI_API_KEY.

Why this helps

  • Enterprise/GCP teams already govern model access through Vertex AI — they may not have a standalone Gemini API key at all.
  • Vertex AI exposes an OpenAI-compatible endpoint, so the implementation can reuse _call_openai_compat() from llm.py.
  • Enables headless CI extraction in GCP-native pipelines (Cloud Build, GitHub Actions with Workload Identity Federation) using Application Default Credentials or a service account token.

Authentication: Application Default Credentials (ADC) support

Many enterprise organizations restrict or prohibit creating service account key files (JSON key export). The backend should support Google Application Default Credentials (ADC) as the primary auth method, not just explicit API keys/tokens.

ADC automatically resolves credentials from (in order):

  1. GOOGLE_APPLICATION_CREDENTIALS env var (service account key file, where allowed)
  2. User credentials from gcloud auth application-default login
  3. Attached service account (GCE, Cloud Run, GKE Workload Identity)
  4. Workload Identity Federation (GitHub Actions OIDC → GCP, no keys at all)

This can be implemented using the google-auth Python library to obtain a bearer token, then passing it to the OpenAI-compatible endpoint:

import google.auth
import google.auth.transport.requests

def _get_vertex_token() -> str:
    """Get access token via Application Default Credentials."""
    credentials, project = google.auth.default(
        scopes=["https://www.googleapis.com/auth/cloud-platform"]
    )
    credentials.refresh(google.auth.transport.requests.Request())
    return credentials.token

This is critical for CI/CD environments where:

  • Org policy blocks service account key creation (constraints/iam.disableServiceAccountKeyCreation)
  • Workload Identity Federation provides keyless auth (e.g., GitHub Actions OIDC → GCP)
  • Users authenticate via gcloud auth application-default login locally

Proposed implementation

Add a "vertex" entry to the BACKENDS dict in graphify/llm.py:

"vertex": {
    "base_url": os.environ.get(
        "GRAPHIFY_VERTEX_BASE_URL",
        "https://us-central1-aiplatform.googleapis.com/v1beta1/openai/"
    ),
    "default_model": "gemini-2.5-flash",
    "env_keys": ["GRAPHIFY_VERTEX_API_KEY", "GOOGLE_ACCESS_TOKEN"],
    "model_env_key": "GRAPHIFY_VERTEX_MODEL",
    "pricing": {"input": 0.50, "output": 3.00},
    "temperature": 0,
    "max_completion_tokens": 16384,
}

Auth resolution order:

  1. GRAPHIFY_VERTEX_API_KEY — explicit token (e.g., from gcloud auth print-access-token)
  2. GOOGLE_ACCESS_TOKEN — explicit token via env var
  3. Application Default Credentials — automatic via google-auth library (fallback when no explicit key is set)

Usage:

# With ADC (preferred — no explicit token management)
gcloud auth application-default login  # one-time setup
graphify extract . --backend vertex

# With explicit token
export GRAPHIFY_VERTEX_API_KEY=$(gcloud auth print-access-token)
graphify extract . --backend vertex

# In GitHub Actions with Workload Identity Federation (keyless)
# ADC is automatically configured by google-github-actions/auth
graphify extract . --backend vertex

# With a custom regional endpoint
export GRAPHIFY_VERTEX_BASE_URL=https://europe-west1-aiplatform.googleapis.com/v1beta1/openai/
graphify extract . --backend vertex

The base_url needs to be configurable via env var since it includes the GCP region.

Related

  • #959 — requests OPENAI_BASE_URL override for the openai backend (similar pattern)
  • #723 — PR adding a generic custom backend with configurable base_url (would also solve this, but a named vertex backend is more discoverable)
  • #757 / Bedrock backend — precedent for adding cloud-provider-specific backends (Bedrock similarly uses SDK-based auth rather than a simple API key)

[!Note] Responses generated with Claude

kaovilai avatar May 22 '26 05:05 kaovilai

This is really important. Hope we get this soon

ArielGonzalezC avatar Jun 05 '26 19:06 ArielGonzalezC