跳转到主要内容

Installation

pip install "rail-score-sdk[google]"

Usage

import asyncio
from rail_score_sdk.integrations import RAILGemini

async def main():
    client = RAILGemini(
        google_api_key="AIza...",
        rail_api_key="YOUR_RAIL_API_KEY",
        rail_threshold=7.0,
        rail_policy="regenerate",
        rail_mode="basic",
    )

    response = await client.generate_content(
        model="gemini-1.5-pro",
        contents="Explain the risks of algorithmic bias in hiring.",
    )

    print(f"Content: {response.content}")
    print(f"RAIL Score: {response.rail_score}/10")
    print(f"Threshold met: {response.threshold_met}")
    print(f"Was regenerated: {response.was_regenerated}")

asyncio.run(main())

Vertex AI

To use Gemini via Vertex AI instead of Google AI Studio, pass use_vertex=True:
from rail_score_sdk.integrations import RAILGemini

client = RAILGemini(
    use_vertex=True,
    gcp_project="your-gcp-project-id",
    gcp_region="us-central1",
    rail_api_key="YOUR_RAIL_API_KEY",
    rail_threshold=7.0,
)

response = await client.generate_content(
    model="gemini-1.5-pro",
    contents="...",
)
Vertex AI uses Application Default Credentials. Run gcloud auth application-default login or set GOOGLE_APPLICATION_CREDENTIALS to your service account key file.

Configuration options

ParameterTypeDefaultDescription
google_api_keystrGoogle AI Studio API key (not needed when use_vertex=True)
rail_api_keystrYour RAIL API key
rail_thresholdfloatNoneBlock or regenerate below this score
rail_policystr"log_only""log_only" | "block" | "regenerate"
rail_modestr"basic""basic" | "deep"
use_vertexboolFalseRoute through Vertex AI instead of Google AI Studio
gcp_projectstrGCP project ID (required when use_vertex=True)
gcp_regionstr"us-central1"Vertex AI region
Pass rail_skip=True to generate_content() to bypass RAIL evaluation for a single call.

What’s next

OpenAI Integration

RAILOpenAI wrapper for chat completions.

Anthropic Integration

RAILAnthropic wrapper for Claude.

Concepts: Middleware

How provider wrappers work under the hood.

Python SDK Overview

Full Python SDK installation and reference.