ReAct エージェントを作成する
LangChain の基本コンポーネントを学んだところで、さっそく ReAct エージェントを作成しましょう。ツール count_r_in_word を使って、任意の単語に含まれる 'r' の数を数えられるエージェントにします。
次のオブジェクトはすでに読み込まれています:tool、ChatOpenAI、create_react_agent、math、model。
この演習はコースの一部です
LangChainで設計するエージェント型システム
演習の手順
create_react_agent()を使ってエージェントappをセットアップし、modelとcount_r_in_wordをツール一覧に渡します。- ユーザーの質問を文字列として受け取る変数
queryを定義します。 .invoke()でappを呼び出し、"messages"キーを持つ辞書を渡して、queryを"human"としてラベル付けします。responseの最後のメッセージにアクセスし、エージェントの回答を得るためにその.content属性を出力します。
実践的なインタラクティブ演習
このサンプルコードを完成させて、この演習に挑戦してみましょう。
# Create the agent
____ = ____(model=____, tools=[____])
# Create a query
____ = "How many r's are in the word 'Terrarium'?"
# Invoke the agent and store the response
response = app.____({"messages": [("human", ____)]})
# Print the agent's response
print(____['messages'][____].____)