中の人がどうなってるのか、にわかに信じられませんが、またまたLangChainの新機能が発表されていました😮
今度の新機能は、「Multi Functions Agent」 です。 Agentが1ステップで複数の関数呼び出しをするという機能です。
Tools定義 google検索ツール(SerpAPI)を tools に登録します。
llm = ChatOpenAI(temperature=0 , model="gpt-3.5-turbo-0613" )
search = SerpAPIWrapper()
tools = [
Tool(
name="Search" ,
func=search.run,
description="Useful when you need to answer questions about current events. You should ask targeted questions."
),
]
Agent初期化 今回の主役:OPENAI_MULTI_FUNCTIONS をagentに指定します。
mrkl = initialize_agent(tools, llm, agent=AgentType.OPENAI_MULTI_FUNCTIONS, verbose=True )
実行サンプル 先日覚えた、debugオプション をつけて動作を確認します。 動きを見ればAgentの機能が一発で理解できると思いますので、ちょっと長いですが、見ていきましょう!
import langchain
langchain.debug = True
時間がない方は、「👈👈」マークを付けたところに注目です!
res = mrkl.run(
"東京と大阪の明日の天気予報を教えて"
)
[chain/start][1:chain:AgentExecutor] Entering Chain run with input: { "input": "東京と大阪の明日の天気予報を教えて" } [llm/start][1:chain:AgentExecutor > 2:llm:ChatOpenAI] Entering LLM run with input: { "prompts": [ "System: You are a helpful AI assistant.\nHuman: 東京と大阪の明日の天気予報を教えて" ] } [llm/end][1:chain:AgentExecutor > 2:llm:ChatOpenAI] [3.48s] Exiting LLM run with output: { "generations": [ [ { "text": "", "generation_info": null, "message": { "content": "", "additional_kwargs": { "function_call": {👈👈 "name": "tool_selection", "arguments": "{\n \"actions\": [\n {\n \"action_name\": \"Search\",\n \"action\": {\n \"tool_input\": \"東京 明日 天気予報\"\n }\n },\n {\n \"action_name\": \"Search\",\n \"action\": {\n \"tool_input\": \"大阪 明日 天気予報\"\n }\n }\n ]\n}" 👈👈 } }, "example": false } } ] ], "llm_output": { "token_usage": { "prompt_tokens": 93, "completion_tokens": 91, "total_tokens": 184 }, "model_name": "gpt-3.5-turbo-0613" }, "run": null } [tool/start][1:chain:AgentExecutor > 3:tool:Search] Entering Tool run with input: "{'tool_input': '東京 明日 天気予報'}"👈👈[tool/end][1:chain:AgentExecutor > 3:tool:Search] [5.19s] Exiting Tool run with output: "東京(東京)の天気予報。今日・明日の天気と風と波、明日までの6時間ごとの降水確率と最高・最低気温を見られます。 " [tool/start][1:chain:AgentExecutor > 4:tool:Search] Entering Tool run with input: "{'tool_input': '大阪 明日 天気予報'}"👈👈 [tool/end][1:chain:AgentExecutor > 4:tool:Search] [5.05s] Exiting Tool run with output: "大阪市の今日明日の天気、気温、降水確率に加え、台風情報、警報注意報、観測ランキング、紫外線指数等を掲載。気象予報士が日々更新する「日直予報士」や季節を楽しむ ..." [llm/start][1:chain:AgentExecutor > 5:llm:ChatOpenAI] Entering LLM run with input: { "prompts": [ "System: You are a helpful AI assistant.\nHuman: 東京と大阪の明日の天気予報を教えて\nAI: {'name': 'tool_selection', 'arguments': '{\\n \"actions\": [\\n {\\n \"action_name\": \"Search\",\\n \"action\": {\\n \"tool_input\": \"東京 明日 天気予報\"\\n }\\n },\\n {\\n \"action_name\": \"Search\",\\n \"action\": {\\n \"tool_input\": \"大阪 明日 天気予報\"\\n }\\n }\\n ]\\n}'}\nFunction: 東京(東京)の天気予報。今日・明日の天気と風と波、明日までの6時間ごとの降水確率と最高・最低気温を見られます。\nAI: {'name': 'tool_selection', 'arguments': '{\\n \"actions\": [\\n {\\n \"action_name\": \"Search\",\\n \"action\": {\\n \"tool_input\": \"東京 明日 天気予報\"\\n }\\n },\\n {\\n \"action_name\": \"Search\",\\n \"action\": {\\n \"tool_input\": \"大阪 明日 天気予報\"\\n }\\n }\\n ]\\n}'}\nFunction: 大阪市の今日明日の天気、気温、降水確率に加え、台風情報、警報注意報、観測ランキング、紫外線指数等を掲載。気象予報士が日々更新する「日直予報士」や季節を楽しむ ..." ] } [llm/end][1:chain:AgentExecutor > 5:llm:ChatOpenAI] [2.62s] Exiting LLM run with output: { "generations": [ [ { "text": "東京の明日の天気予報は、晴れで最高気温は25度、最低気温は18度です。\n\n大阪の明日の天気予報は、曇りで最高気温は23度、最低気温は17度です。", "generation_info": null, "message": { "content": "東京の明日の天気予報は、晴れで最高気温は25度、最低気温は18度です。\n\n大阪の明日の天気予報は、曇りで最高気温は23度、最低気温は17度です。", "additional_kwargs": {}, "example": false } } ] ], "llm_output": { "token_usage": { "prompt_tokens": 448, "completion_tokens": 77, "total_tokens": 525 }, "model_name": "gpt-3.5-turbo-0613" }, "run": null } [chain/end][1:chain:AgentExecutor] [16.35s] Exiting Chain run with output: { "output": "東京の明日の天気予報は、晴れで最高気温は25度、最低気温は18度です。\n\n大阪の明日の天気予報は、曇りで最高気温は23度、最低気温は17度です。" }
print(res)
東京の明日の天気予報は、晴れで最高気温は25度、最低気温は18度です。 大阪の明日の天気予報は、曇りで最高気温は23度、最低気温は17度です。
プロンプトの要求に合わせて、1ステップでToolを2回立て続けに呼び出しているのがわかります。 LLMの呼び出し回数が減るので、待ち時間も明らかに短くなるしコスト的にも楽になりますね。すばらしい!😝
というわけで取り急ぎ。現場からは以上です。😄 おやすみなさい