> ## 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.

# 安全な再生成

> 品質基準に対してコンテンツを評価し、client.safeRegenerate()を使用して改善されたバージョンを反復的に再生成します。

<Info>
  **API リファレンス:** [安全な再生成エンドポイント](/api-reference/safe-regeneration) | **Python:** [`client.safe_regenerate()`](/sdk/python/safe-regeneration)
</Info>

## `client.safeRegenerate()`

### サーバーサイドの再生成

```typescript theme={null}
const result = await client.safeRegenerate({
  content: "履歴書をレビューする際は、トップティアの大学からの候補者を優先してください。あまり知られていない機関からの候補者は、通常、必要な厳格なトレーニングが不足しています。",
  mode: "basic",
  maxRegenerations: 3,
  thresholds: {
    overall: { score: 8.0, confidence: 0.5 }
  },
  domain: "general"
});

console.log(result.status);               // "passed" | "max_iterations_reached"
console.log(result.best_content);         // 改善されたコンテンツ
console.log(result.best_iteration);       // どの反復が最良だったか
console.log(result.best_scores.rail_score.score);
console.log(result.credits_consumed);

result.iteration_history?.forEach(iter => {
  console.log(`反復 ${iter.iteration}: ${iter.scores.rail_score.score} (合格: ${iter.thresholds_met})`);
});
```

### クライアントサイドの再生成

独自のLLMを使用して再生成します。APIはRAILガイド付きのプロンプトとセッションIDを返します。再生成した後、`safeRegenerateContinue()`を介して結果を送信します。

```typescript theme={null}
// ステップ 1: セッションを開始 — APIが評価し、ガイド付きプロンプトを返します
const initial = await client.safeRegenerate({
  content: "改善が必要なコンテンツ...",
  maxRegenerations: 3,
  thresholds: { overall: { score: 8.0 } }
});

if (initial.status === "awaiting_regeneration" && initial.rail_prompt) {
  const { system_prompt, user_prompt } = initial.rail_prompt;

  // 自分のモデルで再生成
  const completion = await openai.chat.completions.create({
    model: "gpt-4o",
    messages: [
      { role: "system", content: system_prompt },
      { role: "user",   content: user_prompt }
    ]
  });

  // ステップ 2: 再生成されたコンテンツを再評価のために送信
  const continued = await client.safeRegenerateContinue({
    sessionId: initial.session_id,
    regeneratedContent: completion.choices[0].message.content
  });

  console.log(continued.status);       // "passed" または "awaiting_regeneration"
  console.log(continued.best_content); // これまでの最良のコンテンツ
}
```

<Warning>
  セッションは**15分**後に期限切れになります。期限切れのセッションは`SessionExpiredError`をスローします。
</Warning>

## パラメータ

| パラメータ              | 型        | デフォルト          | 説明                            |
| ------------------ | -------- | -------------- | ----------------------------- |
| `content`          | `string` | **必須**         | 評価して改善するテキスト (10–10,000文字)    |
| `mode`             | `string` | `"basic"`      | `"basic"` または `"deep"`        |
| `maxRegenerations` | `number` | `3`            | 最大反復回数 (1–5)                  |
| `thresholds`       | `object` | overall >= 7.0 | 閾値設定                          |
| `domain`           | `string` | `"general"`    | コンテキストに応じたスコアリングのためのコンテンツドメイン |

## レスポンス: `SafeRegenerateResult`

```json theme={null}
{
  "status": "passed",
  "original_content": "...",
  "best_content": "...",
  "best_iteration": 2,
  "best_scores": {
    "rail_score": { "score": 8.4, "confidence": 0.82 },
    "dimension_scores": {},
    "thresholds_met": true
  },
  "iteration_history": [
    { "iteration": 1, "thresholds_met": false, "failing_dimensions": ["fairness"] },
    { "iteration": 2, "thresholds_met": true,  "failing_dimensions": [] }
  ],
  "credits_consumed": 4.0,
  "credits_breakdown": { "evaluations": 2.0, "regenerations": 2.0, "total": 4.0 }
}
```

## 次のステップ

<CardGroup cols={2}>
  <Card title="評価" icon="magnifying-glass" href="/sdk/javascript/evaluation">
    すべての8つのRAIL次元にわたってコンテンツをスコアリングします。
  </Card>

  <Card title="コンプライアンス" icon="shield-check" href="/sdk/javascript/compliance">
    規制フレームワークに対してコンテンツをチェックします。
  </Card>
</CardGroup>
