> ## Documentation Index
> Fetch the complete documentation index at: https://docs.responsibleailabs.ai/llms.txt
> Use this file to discover all available pages before exploring further.

# Safe Regeneration

> जो AI content आपकी quality threshold से नीचे score करे, उसे अपने आप regenerate करें।

Safe Regeneration एक feedback loop है: किसी response को evaluate करें, और अगर वह आपकी threshold से नीचे रह जाए, तो उसे सही सुधार instructions के साथ regenerate करें और दोबारा score करें, और तब तक दोहराएं जब तक वह pass न हो जाए। अगर iteration limit तक पहुंच जाए, तो जो version सबसे ज्यादा score करता है वही लौटाया जाता है।

<Info>
  **API endpoint:** `POST /railscore/v1/safe-regenerate` | **Python:** `client.safe_regenerate()` | **JavaScript:** `client.safeRegenerate()`
</Info>

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

```mermaid theme={null}
flowchart TD
    Start["Initial response"] --> Eval["Evaluate\n(RAIL Score)"]
    Eval --> Check{"Score >= threshold?"}
    Check -- yes --> Return["Return response"]
    Check -- no --> Regen["Regenerate with\nimprovement instructions"]
    Regen --> Eval
    Regen --> Limit{"Max iterations\nreached?"}
    Limit -- yes --> Best["Return best response\n(highest scored)"]
```

हर iteration एक standalone evaluation जितने ही credits लेता है। हर request में अधिकतम 5 iterations।

## Basic usage

<CodeGroup>
  ```python Python theme={null}
  from rail_score_sdk import RailScoreClient

  client = RailScoreClient(api_key="YOUR_RAIL_API_KEY")

  result = client.safe_regenerate(
      content="When reviewing resumes, prioritize Ivy League graduates.",
      prompt="What hiring criteria should I use?",
      threshold=7.0,
      mode="basic",
      max_iterations=3,
  )

  print(f"Final score: {result.final_score}/10")
  print(f"Iterations: {result.iterations_taken}")
  print(f"Content: {result.content}")
  ```

  ```typescript JavaScript theme={null}
  import { RailScoreClient } from "@responsible-ai-labs/rail-score";

  const client = new RailScoreClient({ apiKey: "YOUR_RAIL_API_KEY" });

  const result = await client.safeRegenerate({
    content: "When reviewing resumes, prioritize Ivy League graduates.",
    prompt: "What hiring criteria should I use?",
    threshold: 7.0,
    mode: "basic",
    maxIterations: 3,
  });

  console.log(`Final score: ${result.finalScore}/10`);
  console.log(`Content: ${result.content}`);
  ```

  ```bash cURL theme={null}
  curl -X POST https://api.responsibleailabs.ai/railscore/v1/safe-regenerate \
    -H "Content-Type: application/json" \
    -H "Authorization: Bearer YOUR_RAIL_API_KEY" \
    -d '{
      "content": "When reviewing resumes, prioritize Ivy League graduates.",
      "prompt": "What hiring criteria should I use?",
      "threshold": 7.0,
      "mode": "basic",
      "max_iterations": 3
    }'
  ```
</CodeGroup>

## Response

result आपको दोनों बताता है: नतीजा क्या रहा और वहाँ तक पहुंचने में क्या लगा:

* `content` — use करने के लिए final text (pass होने वाला version, या limit पहुंच जाने पर सबसे अच्छा)।
* `final_score` — उस final content का RAIL score, और `passed` — क्या उसने threshold पार की।
* `iterations_taken` — कितने regenerate-and-score rounds चले।

अगर pass हुए बिना limit पहुंच जाए, तो भी आपको `content` में सबसे अच्छा attempt मिलता है, `passed: false` के साथ, ताकि आप तय कर सकें कि उसे serve करना है, fallback लेना है, या किसी इंसान के पास भेजना है।

## Usage

हर iteration एक standalone evaluation की तरह meter होता है, आपकी set की हुई iteration limit तक। अगर पहली ही response threshold पार कर ले, तो सिर्फ़ वही evaluation गिना जाता है। विस्तार के लिए [Credits](/getting-started/credits) देखें।

## आगे क्या

<CardGroup cols={2}>
  <Card title="API Reference: Safe Regeneration" icon="code" href="/api-reference/safe-regeneration">
    पूरा parameter reference।
  </Card>

  <Card title="Python SDK: Safe Regeneration" icon="python" href="/sdk/python/safe-regeneration">
    Python SDK examples और options।
  </Card>

  <Card title="Concepts: Middleware" icon="layer-group" href="/concepts/middleware">
    provider wrappers के जरिए auto-regenerate करें।
  </Card>

  <Card title="Policy Engine" icon="gavel" href="/concepts/policy-engine">
    अपनी application policy से regeneration अपने आप trigger करें।
  </Card>
</CardGroup>
