मुख्य सामग्री पर जाएं
Middleware का मतलब है हर LLM response को intercept करना और आपकी बाकी application तक पहुँचने से पहले RAIL score attach करना। आप अपने LLM client को RAIL wrapper से replace कर देते हैं। Wrapper LLM को call करता है, response evaluate करता है, और content और scores दोनों एक single object में return करता है।

यह कौन सी problem solve करता है

Middleware के बिना, हर LLM call में responsible-AI checks add करने का मतलब है — हर जगह evaluation code लिखना, logic duplicate करना, coverage gaps का risk, और application code का cluttered होना:
# Eval code हर जगह बिखरा हुआ
async def get_response(user_message):
    response = await openai_client.chat.completions.create(
        model="gpt-4o", messages=[{"role": "user", "content": user_message}]
    )
    content = response.choices[0].message.content

    # हर function में eval करना याद रखो
    score = rail_client.eval(content=content, mode="basic")
    if score.rail_score.score < 7.0:
        raise ValueError("Response below quality threshold")

    return content

कैसे काम करता है

जब आप RAIL wrapper पर कोई method call करते हो, तीन चीज़ें transparently होती हैं:
  1. आपके messages underlying LLM API को normal API call के रूप में forward हो जाते हैं।
  2. LLM response आपके configured mode में RAIL evaluation endpoint को submit होता है।
  3. एक wrapped response object return होता है जिसमें original content, RAIL score, per-dimension scores, और threshold_met boolean — सब एक ही return value में।

Supported providers

WrapperWrapsPythonJavaScript
RAILOpenAIOpenAI chat completionsYesYes
RAILGeminiGoogle GeminiYesYes
RAILAnthropicAnthropic ClaudeYesYes
RAILLangChainAny LangChain LLMYes
Custom wrapperAny HTTP-based LLMYesYes

Observe mode vs Enforce mode

हर response score करो, कभी block मत करो। Response flow interrupt किए बिना quality measure करने के लिए use करो।
client = RAILOpenAI(
    openai_api_key="...",
    rail_api_key="...",
    eval_mode="basic",
    # No threshold — हमेशा response return होता है
)

response = await client.chat(messages=[...])
print(response.content)        # LLM का response
print(response.rail_score)     # RAIL score (हमेशा present)
print(response.threshold_met)  # None — कोई threshold configured नहीं

Custom middleware लिखना

अगर आप कोई ऐसा LLM provider use करते हो जिसका built-in wrapper नहीं है, तो core eval() call से अपना middleware बनाओ:
from rail_score_sdk import RailScoreClient

rail = RailScoreClient(api_key="...")

async def rail_middleware(llm_call, messages, threshold=7.0):
    """किसी भी async LLM call के लिए generic RAIL middleware।"""
    content = await llm_call(messages)

    result = rail.eval(content=content, mode="basic")

    if result.rail_score.score < threshold:
        raise ValueError(
            f"Response scored {result.rail_score.score:.1f} — threshold {threshold} से नीचे। "
            f"Failed: {[d for d, s in result.dimension_scores.items() if s.score < threshold]}"
        )

    return content, result

# किसी भी LLM के साथ use करो:
content, score = await rail_middleware(my_llm_call, messages, threshold=7.5)

आगे क्या करें

Concepts: Policy Engine

Session भर में scores पर act करने के लिए declarative rules।

Python: Integrations

Full provider wrapper documentation और options।

JavaScript: Providers

OpenAI, Gemini, Anthropic के लिए TypeScript wrappers।

Python: Middleware SDK

RAILMiddleware — किसी भी LLM function को wrap करो।