LoginSignup
2
1

LangChain: AgentExecutor の action 解析

Last updated at Posted at 2024-03-01

背景

LangChain で、OpenAI Tools Agent を使った際に、Agent が Tool をどう使ったかを表示したかった。
その際の備忘録

結論

return_intermediate_steps=True

を設定して、以下のようにして変換したら、あとは Front で 思考の連鎖として表示してやれば OK

intermediate steps を分解
def serialize_intermediate_step(step):
    return {
        "tool": step[0].tool,
        "tool_input": step[0].tool_input,
        "log": step[0].log,
        "message_log": str(step[0].message_log),
        "tool_call_id": step[0].tool_call_id,
        "output": str(step[1]),
    }
利用例
thoughts = str([serialize_intermediate_step(step) for step in response["intermediate_steps"]])

詳細

  1. return_intermediate_steps を設定して

    agent_executor = AgentExecutor(
    agent=agent, tools=tools, verbose=True, return_intermediate_steps=True
    )

  2. Invoke() したら、以下で取得可能

    response["intermediate_steps"])

ドキュメントは以下参照

あとがき

Agent は便利、だけど思った通り動かすのが一苦労
実際のSampleをもっと見ないと・・

2
1
0

Register as a new user and use Qiita more conveniently

  1. You get articles that match your needs
  2. You can efficiently read back useful information
  3. You can use dark theme
What you can do with signing up
2
1