LangChainエージェントの概要

LangChainを使ったLLMアプリケーション開発

Jonathan Bennion

AI Engineer & LangChain Contributor

エージェントとは?

エージェント: LLMを使ってアクション{{1}}を実行する

ツール: エージェントによって呼び出される functions

  • 今 → ReAct エージェント

ユーザーの入力に基づいて、どのツールを使うかを判断するエージェント。

LangChainを使ったLLMアプリケーション開発

ReActエージェント

  • 理由 + 行動

ジャマイカのキングストンの天気はどうですか?

Thought: I should call Weather() to find the
weather in Kingston, Jamaica.


Act: Weather("Kingston, Jamaica")
Observe: The weather is mostly sunny with temperatures of 82°F.

思考、行動、観察のループの一例。

LangChainを使ったLLMアプリケーション開発

LangGraph

LangGraphロゴ。

  • LangChainのブランチで、エージェントシステムの設計を中心とした
  • 統一された、ツールに依存しない構文
  • pip install langgraph==0.2.74
LangChainを使ったLLMアプリケーション開発

ReActエージェント

from langgraph.prebuilt import create_react_agent
from langchain_community.agent_toolkits.load_tools import load_tools

llm = ChatOpenAI(model="gpt-4o-mini", api_key=openai_api_key) tools = load_tools(["llm-math"], llm=llm)
agent = create_react_agent(llm, tools)
messages = agent.invoke({"messages": [("human", "What is the square root of 101?")]})
print(messages)
LangChainを使ったLLMアプリケーション開発

ReActエージェント

{'messages': [
    HumanMessage(content='What is the square root of 101?', ...),
    AIMessage(content='', ..., tool_calls=[{'name': 'Calculator', 'args': {'__arg1': 'sqrt(101)'}, ...),
    ToolMessage(content='Answer: 10.04987562112089', ...),
    AIMessage(content='The square root of 101 is approximately 10.05.', ...)
]}
print(messages['messages'][-1].content)
The square root of 101 is approximately 10.05.
LangChainを使ったLLMアプリケーション開発

練習しましょう!

LangChainを使ったLLMアプリケーション開発

Preparing Video For Download...