> For the complete documentation index, see [llms.txt](https://unsloth.ai/docs/llms.txt). Markdown versions of documentation pages are available by appending `.md` to page URLs; this page is available as [Markdown](https://unsloth.ai/docs/jp/meru/reinforcement-learning-rl-guide/training-ai-agents-with-rl.md).

# RLによるAIエージェントの学習

強化学習（RL）を使って、実世界のタスク向けAIエージェントを学習する方法を学びましょう。

「エージェント的」AIは、時間の経過とともにますます人気になっています。この文脈では、「エージェント」とは、高レベルの目標と、それを達成するためのツール群を与えられたLLMのことです。エージェントは通常「マルチターン」でもあり、行動を実行し、それが環境にどのような影響を与えたかを確認し、その後さらに別の行動を繰り返し、目標を達成するか試行に失敗するまで続けられます。

残念ながら、非常に高性能なLLMであっても、複雑なマルチターンのエージェント的タスクを確実にこなすのは難しいことがあります。興味深いことに、私たちは、次のRLアルゴリズムを使ってエージェントを学習させると [GRPO（Group Relative Policy Optimization）](https://unsloth.ai/docs/jp/meru/reinforcement-learning-rl-guide/tutorial-train-your-own-reasoning-model-with-grpo.md) その信頼性を大幅に高められることがわかりました！このガイドでは、オープンソースツールを使って信頼性の高いAIエージェントを構築する方法を学びます。

## 🎨 ARTでRLエージェントを学習させる

[ART（Agent Reinforcement Trainer）](https://github.com/openpipe/art) を基盤として構築された [Unsloth](https://github.com/unslothai/unsloth)’s GRPOTrainerを基盤として、マルチターンエージェントの学習を可能かつ容易にするツールです。すでにGRPOでUnslothを使っていて、複雑なマルチターンのやり取りを扱えるエージェントを学習させる必要があるなら、ARTがそのプロセスを簡素化します。

<div align="left"><figure><img src="https://735611837-files.gitbook.io/~/files/v0/b/gitbook-x-prod.appspot.com/o/spaces%2FxhOjnexMCB3dmuQFQ2Zq%2Fuploads%2Fgit-blob-c97e63a69ecd17685c6e09cb41fd625d43a3545d%2FScreenshot_2025-07-19_at_1.23.18_PM.webp?alt=media" alt="" width="375"><figcaption><p>Unsloth+ARTで学習したエージェントモデルは、多くの場合、エージェント的ワークフローにおいてプロンプトのみのモデルを上回る性能を発揮できます。</p></figcaption></figure></div>

### ART + Unsloth

ARTは、Unslothのメモリ効率と計算効率に優れたGRPO実装の上に構築されています。さらに、次の機能が追加されています：

#### 1. マルチターンエージェント学習

ARTは「trajectory（軌跡）」という概念を導入します。これはエージェントが実行されるにつれて構築されます。これらの軌跡は採点され、GRPOに利用できます。軌跡は複雑になり得て、非線形な履歴やサブエージェント呼び出しなども含められます。ツール呼び出しと応答もサポートしています。

#### 2. 既存コードベースへの柔軟な統合

すでにプロンプト付きモデルで動作しているエージェントがある場合、ARTは既存のエージェントループをラップして学習に使うために必要な変更点を最小限に抑えようとします。

アーキテクチャ上、ARTは、コードベース内に存在しAPI経由で実際の学習が行われる「バックエンド」と通信する「フロントエンド」クライアントに分かれています（ARTの `LocalBackend`）。これにより、いくつかの重要な利点があります：

* **最小限のセットアップで済む**：ARTフロントエンドは依存関係が少なく、既存のPythonコードベースに簡単に追加できます。
* **どこからでも学習可能**：ARTクライアントをノートパソコンで実行し、ARTサーバーに一時的なGPU有効環境を起動させることも、ローカルGPUで実行することもできます
* **OpenAI互換API**：ARTバックエンドは、学習中のモデルをOpenAI互換API経由で提供し、ほとんどの既存コードベースと互換性があります。

#### 3. RULER: ゼロショットのエージェント報酬

ARTには、次のような組み込みの汎用報酬関数もあります： [RULER](https://art.openpipe.ai/fundamentals/ruler) （Relative Universal LLM-Elicited Rewards）で、手作りの報酬関数を不要にできる場合があります。驚くことに、RULERの自動報酬関数でRL学習したエージェントは、手書きの報酬関数で学習したエージェントと同等かそれ以上の性能を示すことがよくあります。これにより、RLを始めやすくなります。

<figure><img src="https://735611837-files.gitbook.io/~/files/v0/b/gitbook-x-prod.appspot.com/o/spaces%2FxhOjnexMCB3dmuQFQ2Zq%2Fuploads%2Fgit-blob-b4168c3c380f6c8258c31083dcb65a5fcd2308b8%2FScreenshot_2025-07-19_at_1.21.08_PM.webp?alt=media" alt="" width="375"><figcaption></figcaption></figure>

```python
# 以前：何時間もの報酬設計
def complex_reward_function(trajectory):
    # 慎重に作られた採点ロジックが50行以上...
    pass

# 以後：RULERで1行
judged_group = await ruler_score_group(group, "openai/o3")
```

### ARTを選ぶべき場面

ARTは、次のようなニーズがあるプロジェクトに適しています：

1. **複数ステップのエージェント機能**：複数のアクションを実行する必要がある、ツールを使う、あるいは長い会話を行うエージェントを扱うユースケース
2. **報酬設計なしの迅速なプロトタイピング**：RULERの自動報酬スコアリングにより、プロジェクトの開発時間を2〜3倍短縮できます
3. **既存システムとの統合**：既存のエージェント的コードベースに最小限の変更でRL機能を追加したいとき

### コード例：動作するART

```python
import art
from art.rewards import ruler_score_group

# Unsloth対応のベースモデルでモデルを初期化
model = art.TrainableModel(
    name="agent-001",
    project="my-agentic-task",
    base_model="Qwen/Qwen2.5-14B-Instruct",  # 任意のUnsloth対応モデル
)

# ロールアウト関数を定義
async def rollout(model: art.Model, scenario: Scenario) -> art.Trajectory:
    openai_client = model.openai_client()
    trajectory = art.Trajectory(
        messages_and_choices=[
            {"role": "system", "content": "..."},
            {"role": "user", "content": "..."}
        ]
    )
    # ここにエージェントのロジックを記述...    
    return trajectory

# 自動報酬のためにRULERで学習
groups = await art.gather_trajectory_groups(
    (
        art.TrajectoryGroup(rollout(model, scenario) for _ in range(8))
        for scenario in scenarios
    ),
    after_each=lambda group: ruler_score_group(
        group,
        "openai/o3",
        swallow_exceptions=True
    )
)

await model.train(groups)
```

### はじめに

UnslothベースのプロジェクトにARTを追加するには：

```bash
pip install openpipe-art # または `uv add openpipe-art`
```

その後、次をチェックしてください： [サンプルノートブック](https://art.openpipe.ai/getting-started/notebooks) で、ARTが次のようなタスクで実際に動く様子を確認できます：

* o3を上回るメール検索エージェント
* ゲームプレイエージェント（2048、三目並べ、Codenames）
* 複雑な推論タスク（Temporal Clue）


---

# Agent Instructions
This documentation is published with GitBook. GitBook is the documentation platform designed so that both humans and AI agents can read, navigate, and reason over technical content effectively. Learn more at gitbook.com.

## Querying This Documentation
If you need additional information that is not directly available in this page, you can query the documentation by asking a question.

Perform an HTTP GET request on the following URL with the `ask` and `goal` query parameters:

```
GET https://unsloth.ai/docs/jp/meru/reinforcement-learning-rl-guide/training-ai-agents-with-rl.md?ask=<question>&goal=<user_goal>
```

`ask` is the immediate question: it should be specific, self-contained, and written in natural language.
`goal` is what the user is ultimately trying to achieve, the reason they need the answer. Sharing it helps GitBook give you a better, more relevant answer. A goal is most helpful when it describes the outcome the user wants rather than restating the question. For example, with `ask=how do I create an API token`, a goal like `automate deployments from our CI pipeline` lets GitBook tailor the answer to that use case.

The response will contain a direct answer to the question and relevant excerpts and sources from the documentation.

Use this mechanism when the answer is not explicitly present in the current page, you need clarification or additional context, or you want to retrieve related documentation sections.
