Introduction to LangChain agents

การพัฒนาแอปพลิเคชัน LLM ด้วย LangChain

Jonathan Bennion

AI Engineer & LangChain Contributor

Agent คืออะไร?

 

Agents: ใช้ LLMs เพื่อดำเนินการ actions

Tools: ฟังก์ชัน ที่ agent เรียกใช้

 

  • ตอนนี้ReAct Agent

Agent กำลังตัดสินใจเลือกใช้ tool ตาม input ของผู้ใช้

การพัฒนาแอปพลิเคชัน LLM ด้วย LangChain

ReAct agents

  • Reason + Act

 

สภาพอากาศที่ Kingston, Jamaica เป็นอย่างไร?

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.

วนรอบของการคิด การกระทำ และการสังเกต

การพัฒนาแอปพลิเคชัน LLM ด้วย LangChain

LangGraph

LangGraph logo.

 

  • สาขาของ LangChain ที่เน้นการออกแบบ ระบบ agent
  • ไวยากรณ์แบบรวมศูนย์ ไม่ขึ้นกับ tool
  • pip install langgraph==0.2.74
การพัฒนาแอปพลิเคชัน LLM ด้วย LangChain

ReAct agent

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)
การพัฒนาแอปพลิเคชัน LLM ด้วย LangChain

ReAct agent

{'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.
การพัฒนาแอปพลิเคชัน LLM ด้วย LangChain

มาฝึกกันเถอะ!

การพัฒนาแอปพลิเคชัน LLM ด้วย LangChain

Preparing Video For Download...