始める無料で始める

ReAct エージェントを作成する

LangChain の基本コンポーネントを学んだところで、さっそく ReAct エージェントを作成しましょう。ツール count_r_in_word を使って、任意の単語に含まれる 'r' の数を数えられるエージェントにします。

次のオブジェクトはすでに読み込まれています:toolChatOpenAIcreate_react_agentmathmodel

この演習はコースの一部です

LangChainで設計するエージェント型システム

コースを見る

演習の手順

  • create_react_agent() を使ってエージェント app をセットアップし、modelcount_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'][____].____)
コードを編集して実行