Upgrade to Pro — share decks privately, control downloads, hide ads and more …

2024-05-25LangChain Agentの仕組み@機械学習社会実装勉強会第35回

2024-05-25LangChain Agentの仕組み@機械学習社会実装勉強会第35回

Avatar for Naka Masato

Naka Masato

May 24, 2024
Tweet

More Decks by Naka Masato

Other Decks in Technology

Transcript

  1. Agentとは The core idea of agents is to use a

    language model to choose a sequence of actions to take. 言語モデルを使って一連の実行アクションを選択する Agent Tool Google検索 Tool Graph可視化 日本の人口の 推移をグラフ にして
  2. Concepts 1. Schema: AgentAction, AgentFinish, Intermediate Steps a. Agentから返されるのは次の AgentのアクションかAgentのアクションの終了サインか

    b. 中間Stepsの結果はIntermediate stepsに格納 2. Agent: Agent inputs & Agent outputs a. AgentのInputに合わせてAgentを実行してOutputを取得 3. AgentExecutor: Agentを実際に呼び出し、Actionを実行し、Outputをベースに次の Actionを実行していく 4. Tools: Agentが実行できる選択肢 a. 例. TavilySearchResults https://tavily.com/ (月1000コールまで無料)、自分で書いた関数を Toolとし て使うこともできる
  3. Example 1. tool: a. TavilySearchResults: https://docs.tavily.com/docs/welcome (search engine for AI

    agents) b. multiply: custom tool 2. prompt: hub 3. llm: ChatOpenAI 4. agent: create_react_agent 5. agent_executor
  4. Step1: Toolの準備 1. TavilySearchResults a. API keyが必要 b. 1000 requests

    per monthまで無料 2. multiply a. 2 つの数字の掛け算をする関数 + @tool
  5. Step2: Chat model & Prompt 1. ChatOpenAI を設定 2. Promptを準備

    a. https://smith.langchain.com/hub/hwchase17/openai-functions-agent b. langchain hubで公開されているpromptを仕様できる
  6. Step3: Agentをinvoke例: “What’s the weather in SF?” 1. input: What’s

    the weather in SF? 2. output: ToolAgentAction(tool=’tavily_search_results_json’, tool_input={‘query’: ‘current weather in San Francisco’}})
  7. Step3: Agentをinvoke例: “What’s 3 times 2?” 1. input: What’s 3

    times 2? 2. output: ToolAgentAction(tool=’multiply’, tool_input={‘a’: 3, ‘b’: 2}})
  8. Agent.steps[0] format_to_tool_messages: AgentActionとツールのアウトプットからFunctionMessageへ変 換する 1. Input: intermediate_steps: Sequence[Tuple[AgentAction, str]] 2.

    Output: List[BaseMessage] AgentActionを実行し、ToolのOutputを取得してから、それらを中間ステップ (intermediate_steps)にBaseMessageのリストとしてためていくためのステップ
  9. Agent: agent.steps[1].prompt 一つ目のステップで生成した、過去のAgentActionとToolのOutputの歴史が入ったもの も含めて、次のllm呼び出しで使うpromptを決める 1. input_variables: a. agent_scratchpad: 1つ目のstepで生成されたagentとToolのOutputの歴史 b.

    input: もともとの質問 2. input_types 3. messages: a. SystemMessagePromptTemplate: “You are a helpful assistant” b. MessagesPlaceholder: chat_history c. HumanMessagePromptTemplate: input d. MessagesPlaceholder: agent_scratchpad
  10. llm.bind_tools 1. llm.bind_tools(tools, tool_choice, **kwargs) a. Runnableを返す 2. toolを追加 3.

    BaseChatOpenAI.bind_tools(tools, tool_choice) a. 4. BaseChatModel.bind(**kwargs) a. Useful when a runnable in a chain requires an argument that is not in the output of the previous runnable or included in the user input. b. kwargsにはtool 5. Runnable.bind() -> Runnable 6.
  11. ここまでのまとめ 1. AgentのStepは4つ a. steps[0]: AgentActionとToolの結果からMessageに変換してagent_scratchpadへ格納 b. steps[1]: prompt (input,

    agent_scratchpad)をinput variablesにもつ c. steps[2]: RunnableBinding llmにtoolsをbindしたもので、inputからどのツールを使えばよいかを llm に聞くステップ d. steps[3]: Output parser 上のステップでllmから返ってきた結果を ToolAgentAction/AgentFinishに変 換する 2. 注意 a. 実際のToolの実行はしない b. Agentで定義されているのは、次の Actionを決定すること
  12. AgentExecutorに戻って _call 1. whileループ: continueの条件を満 たす限り 2. self._take_next_step: agentを呼び Next

    Actionを取得し、 AgentActionがあればtoolを呼び実 行した結果を返す 3. next_step_outputに値よって終了 か、intermediate_stepsに追加 4. (_get_tool_return: agentからの結 果を直接返すかどうか) 5. _return(): 最終的な結果を返す
  13. まとめ 1. Agent a. 質問の内容と今までの Agentの実行履歴から、次の Actionを返す b. Input: 最初のユーザからの質問、

    intermediate_steps c. Output: 次のAction (AgentFinish or AgentAction) 2. AgentExecutor a. AgentがFinishを返すか、実行最大数 or最大時間に 到達するまで、Agentを繰り返し呼ぶ b. Agentに選ばれたツールを実行し、その結果を intermediate_stepsを更新しながらAgentを呼び続ける