LLMエージェントを1から解説 — ツール使用とループの設計
「考える→道具を使う→結果を見て考え直す」というループはどう設計されているのか。ReAct論文(Yao et al., 2022)の本文だけを根拠に、行動空間に言語を足すという発想、function callingの実装の形、実測された失敗モード(ループの暴走・幻覚したツール呼び出し)まで。
ReAct: Synergizing Reasoning and Acting in Language Models
一次資料 — この記事の根拠
論文の発表 2022-10-06→この解説の公開 2026-08-063年10か月後
ReAct: Synergizing Reasoning and Acting in Language ModelsShunyu Yao, Jeffrey Zhao, Dian Yu ほか · 2022-10-06 · v3arXiv:2210.03629論文ページ·PDF原文の要旨(Abstract)を読む
While large language models (LLMs) have demonstrated impressive capabilities across tasks in language understanding and interactive decision making, their abilities for reasoning (e.g. chain-of-thought prompting) and acting (e.g. action plan generation) have primarily been studied as separate topics. In this paper, we explore the use of LLMs to generate both reasoning traces and task-specific actions in an interleaved manner, allowing for greater synergy between the two: reasoning traces help the model induce, track, and update action plans as well as handle exceptions, while actions allow it to interface with external sources, such as knowledge bases or environments, to gather additional information. We apply our approach, named ReAct, to a diverse set of language and decision making tasks and demonstrate its effectiveness over state-of-the-art baselines, as well as improved human interpretability and trustworthiness over methods without reasoning or acting components. Concretely, on question answering (HotpotQA) and fact verification (Fever), ReAct overcomes issues of hallucination and error propagation prevalent in chain-of-thought reasoning by interacting with a simple Wikipedia API, and generates human-like task-solving trajectories that are more interpretable than baselines without reasoning traces. On two interactive decision making benchmarks (ALFWorld and WebShop), ReAct outperforms imitation and reinforcement learning methods by an absolute success rate of 34% and 10% respectively, while being prompted with only one or two in-context examples. Project site with code: https://react-lm.github.io
一度の生成では届かない場所
あるホテルの「現在の」客室数を聞かれたモデルを想像してください。学習時に読んだ記憶だけで答えるなら、それは推測です。ReAct論文はこの状況を、chain-of-thought による推論は「静的なブラックボックス」であり、モデルは自分の内部表現だけで思考を生成していて外界に接地していない、と表現しました(§1)。接地していないと、事実の幻覚と、その誤りの伝播が起きます。
では逆に、考えずに道具だけ叩くエージェントはどうか。論文の Figure 1 が示すのは、行動しかしないエージェントが検索そのものはできるのに、集めた情報から最終的な答えを組み立てる段で失敗する様子です(§2)。
人間はどちらもやっている、というのが出発点です。台所で料理をするとき、私たちは行動と行動の間に言葉で考えます。「切り終わったから湯を沸かそう」(進捗の追跡)、「塩がないから醤油で代用しよう」(例外処理)、「生地の作り方が分からないから調べよう」(外部情報が要ると気づく)。この往復を言語モデルに持ち込んだのが ReAct(Reason + Act)です(§1)。
ループの骨格を式で書く
エージェントの標準的な定式化から始めます。時刻 でエージェントは環境から観測 を受け取り、方策 に従って行動 を選ぶ。ここで は、それまでに見たもの・やったこと全部です(§2)。 は「履歴を見て次の一手を決める関数」、 は「その履歴」。文脈から行動への写像が込み入っているほど、この方策を学ぶのは難しくなります。
ReAct の答えは1行で書けます(§2)。
は本来の行動空間(検索する、ボタンを押す)、 は言語の空間です。式(1)は「行動の選択肢に、ただ言葉を書くだけの行動を足す」と言っています。つまり、ハットは「広げた」という印で、 は「足す」以上の意味を持ちません。これまでできた外界への働きかけはそのまま全部残り、そこに「言葉を言う」という項目が1つ増えるだけ、ということです。この言語空間の行動を論文は思考(thought)と呼び、決定的な性質をひとつ与えます。思考は外部環境を変えないので、観測が返ってこない。代わりに文脈が と伸び、次の推論と行動を支えます。
つまりエージェントの「考える」は、副作用のない行動としてループに組み込まれている。各社のフレームワークで見かける Thought → Action → Observation という並びの正体はこれです。
もうひとつ、設計上どうしても効いてくる性質があります。ステップが進むたびに は伸び、モデルは毎回その全部を読み直します。1ステップの入力が線形に伸びるということは、走り切るまでに読むトークンの総量はステップ数の2乗で効いてくるということです。
論文が実際に測ったこと
実験は PaLM-540B を凍結したまま few-shot プロンプトで動かす設定です。HotpotQA には6例、Fever には3例の人手軌跡を与えました(§3.2)。道具は Wikipedia を模した3つだけ——該当ページの冒頭5文を返す search[エンティティ]、その文字列を含む次の文を返す lookup[文字列](ブラウザの Ctrl+F 相当)、そして finish[答え]。論文自身が「最先端の検索器より明らかに弱い」と認めた道具立てで、言語による明示的な推論で検索させることが目的でした(§3.1)。
結果は素直ではありません。表1(PaLM-540B)では HotpotQA の完全一致が Standard 28.7、CoT 29.4、ReAct 27.4。ReAct 単独は CoT に負けています。一方 Fever の正解率は CoT 56.3 に対し ReAct 60.9 で勝ち。両者を切り替える ReAct→CoT-SC が HotpotQA 35.1、CoT-SC→ReAct が Fever 64.6 で最高値でした(§3.3)。
コメント
コメントにはログインが必要です