跳转到主要内容
Concept: Middleware | API: Evaluation API
RAILMiddleware wraps any async function that generates LLM responses, adding automatic RAIL scoring without modifying your LLM call logic.
import asyncio
from rail_score_sdk import RAILMiddleware

async def my_llm(messages, **kwargs):
    # Your LLM call here — returns a string
    return "The LLM generated this response."

async def main():
    mw = RAILMiddleware(
        api_key="YOUR_RAIL_API_KEY",
        generate_fn=my_llm,
        threshold=7.0,
        policy="block",
        eval_input=True,
        input_threshold=5.0,
    )

    result = await mw.run(
        messages=[
            {"role": "system", "content": "You are a helpful assistant."},
            {"role": "user", "content": "Explain quantum computing."},
        ]
    )
    print(f"Score: {result.score}, Met threshold: {result.threshold_met}")
    print(f"Content: {result.content}")

asyncio.run(main())

Parameters

ParameterTypeDefaultDescription
api_keystrRAIL API key
generate_fnasync callableYour LLM function
thresholdfloatNoneBlock/regenerate below this score
policystr"block""block" or "regenerate"
eval_inputboolFalseAlso score the input messages
input_thresholdfloatNoneThreshold for input scoring
max_iterationsint3Max regeneration attempts