LLMでのツール呼び出し

from langchain_core.tools import tool

@tool
def mytool1():
# ツール1の処理

@tool
def mytool2():
# ツール2の処理

llm = ChatOpenAI()
llm_with_tool = llm.bind_tools([mytool1, mytool2])
response = llm_with_tool.invoke(クエリ)

message.append(response)

if response.tool_calls:
for call in res.tool_calls:
val = # 処理
messages.append(ToolMessage(val, tool_call_id=call["id"]))

response = llm_with_tool.invoke(message)

ツールをもったエージェントの作成

from langchain.agents import AgentExecutor, create_tool_calling_agent
from langchain_core.tools import tool

tools = [mytool1, mytool2]
agent = create_tool_calling_agent(llm, tools, prompt)
agent_executor = AgentExecutor(agent=agent, tools=tools)
response = agent_executor.invoke(クエリ)