見出し画像

Amazon Bedrock × LangChainでAgentを作る①

はじめに


Amazon Bedrockを使い、LangChainのAgentを試したいと思います。よくあるOpenAI APIを使った例とモデルが違うだけでやってることはほぼ同じですが。

いけてるエージェントを構築し、パートナーとして会話したり、作業してもらったり、プラグインして調べ物してもらったり、ウイルスバスティングしてもらったり…というのが憧れですので地道に頑張っていきたいと思います。

Amazon BedrockをLangChain経由で動かすところまでは、下記を参照ください。

それでは、始めていきます!


エージェントとは


LLMは基本的にテキスト入力に対してテキスト生成(マルチモーダルであれば画像や音声などの入出力も可)をするものですが、(擬似的に)モデルに作業能力を与え、自律的な行動を取れるようにしたものがエージェントという仕組みになります。

このAgentの仕組みとして代表的なものに、ReActがあります。ReActとは、Reason+Actの造語で、LLMに対してある目的に対して、「どんな理由でどんな行動を取るべきか?」を考えるように指示を出すことで、論理的な行動を選択させることができるというものです。

LangChainでは、このReActをはじめとしたエージェントを構築するツールが提供されており、簡単に実装することができます。

今回は、LangChainとAmazon Bedrock経由のClaude 3 Haikuを用いて、簡単なReActエージェントを作成します。

web検索とwikipedia検索が可能なエージェントの作成


ReActエージェントが利用できるツールとして、web検索(DuckDuckGo)による情報収集とWikipediaによる知識検索の能力を与えます。いきなり高度なツールを使うように見せかけて、LangChainにより用意されているものなので簡単に使うことができます。

また、エージェントの設定となるプロンプトは、LangChain-hubより公開されているものを取得することができますので、それを利用します。

コードは以下になります。

from langchain_aws import ChatBedrock
from langchain import hub
from langchain.agents import AgentExecutor, load_tools, create_react_agent

# LLMモデルの定義
chat = ChatBedrock(
    model_id="anthropic.claude-3-haiku-20240307-v1:0",
    model_kwargs={"temperature": 0.1, "max_tokens":4096},
)

# ツールの定義 LangChainで用意されているものを利用
tools = load_tools(["ddg-search", "wikipedia"])

# ReActエージェントのプロンプト LangChain-hubより取得
prompt = hub.pull("hwchase17/react")

# エージェントの定義
agent = create_react_agent(chat, tools, prompt)
agent_chain = AgentExecutor(agent=agent, tools=tools, verbose=True, handle_parsing_errors=True)

では、実際に動かしていきます。

まずは、LLMが答えられないであろう、未来のことを聞いてみます。

# エージェントの実行
response = agent_chain.invoke(
        {"input": "明日の大阪の天気を教えて。"},
    )

> Entering new AgentExecutor chain...
Question: 明日の大阪の天気を教えて。
Thought: To answer this question, I will need to search for information on the weather forecast for Osaka, Japan.
Action: duckduckgo_search
Action Input: osaka weather forecastOsaka Weather Forecast. Providing a local hourly Osaka weather forecast of rain, sun, wind, humidity and temperature. The Long-range 12 day forecast also includes detail for Osaka weather today. Live weather reports from 
:
:
Final Answer: According to the weather forecast, the weather in Osaka, Japan for tomorrow is expected to be sunny with some clouds. The high temperature will be around 25°C (77°F) and the low will be around 18°C (64°F). There is a low chance of rain, with only a 20% chance of precipitation. The wind speeds will be moderate, around 10-15 km/h (6-9 mph). Overall, the weather in Osaka tomorrow is expected to be pleasant and comfortable.

> Finished chain.

英語になってしまいましたが、天気予報を調べて答えてくれました。これが本当に大阪の明日の天気の情報なのかは分かりませんが…。

次に、wikipediaの情報検索を試してみます。

# エージェントの実行
response = agent_chain.invoke(
        {"input": "ロックマンエグゼは全部で何作発売された?"},
    )

> Entering new AgentExecutor chain...
Question: ロックマンエグゼは全部で何作発売された?
Thought: To answer this question, I will need to search for information about the Rockman EXE video game series.
Action: wikipedia
Action Input: Rockman EXE
Page: Mega Man Battle Network (video game)
Summary: Mega Man Battle Network is a role-playing video game developed by Capcom for the Game Boy Advance (GBA) handheld console. It is the first title of the Mega Man Battle Network series of games. It was originally released in Japan as a GBA launch game on March 21, 2001 and was released later that year in North America and Europe. It was also released via the Wii U Virtual Console in Japan on July 9, 2014, in Europe on July 24, 2014, and in North America on July 31, 2014.
:
:
The series has been met with positive reviews from critics, although later games, particularly 5 and 6, have been criticized for a perceived lack of innovation; the series was followed-up by a sequel series titled Mega Man Star Force, which is set 200 years after Battle Network and focuses on radio waves. A compilation of all six mainline entries, Mega Man Battle Network Legacy Collection, was released in April 2023 for PlayStation 4, Nintendo Switch and PC.

Based on the information from Wikipedia, the Rockman EXE (Mega Man Battle Network) video game series had a total of 6 mainline games released.

Final Answer: The Rockman EXE (Mega Man Battle Network) series had a total of 6 mainline games released.

> Finished chain.

こちらも英語にはなってしまいましたが、ロックマンエグゼは6までですので、正しく情報が取得できています。

質問に対して、適切にツールを選択して対応してくれています。「エージェント」って感じがします!

プロンプトの中身を確認する


ReActエージェントのプロンプトは、LangChain Hubから取得したものをそのまま流用しましたが、一応ちゃんと中身の確認もしておこうと思います。

中身を確認すると、このようになっています。

Answer the following questions as best you can. You have access to the following tools:

{tools}

Use the following format:

Question: the input question you must answer
Thought: you should always think about what to do
Action: the action to take, should be one of [{tool_names}]
Action Input: the input to the action
Observation: the result of the action
... (this Thought/Action/Action Input/Observation can repeat N times)
Thought: I now know the final answer
Final Answer: the final answer to the original input question

Begin!

Question: {input}
Thought:{agent_scratchpad}

ユーザーからの問い(Question)に対して、何をすべきかを考え(Thought)、取るべき行動(Action)とツールへの入力値(Action Input)を決定して実行、その結果を観察(Observation)し、次に何をすべきかを考え(Thought)...と最終的な答えが得られるまで繰り返すことを指定しています。

ちなみに、{}で囲われている箇所はプロンプトに埋め込まれる変数となります。特に、{agent_scratchpad}箇所にはLLMの出力が埋め込まれ、その地点までのLLMの思考を含めた情報が履歴として渡されることとなります。

このように、思考の枠組みを最初に設定することによって、論理的な行動が取れるようになるわけですから、プロンプトエンジニアリングの重要性を考えさせられます。

まとめ


BedrockとLangChainの連携により、簡単なエージェントを作ることができました。まだ動作は完璧ではなく、対策を色々やっていかないといけないと思いますが、それはおいおいやっていきたいと思います。

次は自作ツールを実行させることと、会話履歴を保持させること、プロンプトの改善などやっていきます。

LangChainに関しては下記書籍を参考に勉強しています。LangChain v0.1.0以前に書かれたものですが、GitHub上にv0.1.0対応版のコードも公開されており大変参考になりますのでおすすめです。

この記事が気に入ったらサポートをしてみませんか?