Zum Hauptinhalt springen
Konzept: Middleware | API: Evaluation API
RAILMiddleware umschließt jede asynchrone Funktion, die LLM-Antworten generiert, und fügt eine automatische RAIL-Bewertung hinzu, ohne Ihre LLM-Aufruflogik zu ändern.
import asyncio
from rail_score_sdk import RAILMiddleware

async def my_llm(messages, **kwargs):
    # Ihr LLM-Aufruf hier — gibt einen String zurück
    return "Das LLM hat diese Antwort generiert."

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": "Sie sind ein hilfreicher Assistent."},
            {"role": "user", "content": "Erklären Sie Quantencomputing."},
        ]
    )
    print(f"Score: {result.score}, Met threshold: {result.threshold_met}")
    print(f"Content: {result.content}")

asyncio.run(main())

Parameter

ParameterTypStandardBeschreibung
api_keystrRAIL API-Schlüssel
generate_fnasync callableIhre LLM-Funktion
thresholdfloatNoneBlockieren/regenerieren unter diesem Wert
policystr"block""block" oder "regenerate"
eval_inputboolFalseAuch die Eingabemeldungen bewerten
input_thresholdfloatNoneSchwellenwert für die Eingabebewertung
max_iterationsint3Maximalversuche zur Regeneration