การเพิ่มเครื่องมือภายนอกให้แชทบอท

การออกแบบระบบ Agentic ด้วย LangChain

Dilini K. Sumanapala, PhD

Founder & AI Engineer, Genverv, Ltd.

เครื่องมือภายนอกด้วย LangGraph

  จอมอนิเตอร์พร้อมกราฟิกไมโครชิปตรงกลาง มีข้อความ "API" อยู่ด้านใน

     

  • เครื่องมือ API สำหรับแชทบอท

     
    • ข่าวสาร
    • ฐานข้อมูล
    • โซเชียลมีเดีย
    • และอื่น ๆ
การออกแบบระบบ Agentic ด้วย LangChain

การเพิ่มเครื่องมือ Wikipedia

ผู้คนกำลังอ่านหนังสือ นั่งอยู่บนหนังสือเล่มใหญ่

  ไอคอน Wikipedia

การออกแบบระบบ Agentic ด้วย LangChain

การเพิ่มเครื่องมือ Wikipedia

# Modules for building a Wikipedia tool
from langchain_community.utilities import WikipediaAPIWrapper
from langchain_community.tools import WikipediaQueryRun


# Initialize Wikipedia API wrapper to fetch top 1 result api_wrapper = WikipediaAPIWrapper(top_k_results=1)
# Create a Wikipedia query tool using the API wrapper wikipedia_tool = WikipediaQueryRun(api_wrapper=api_wrapper)
tools = [wikipedia_tool]
การออกแบบระบบ Agentic ด้วย LangChain

การเพิ่มเครื่องมือ Wikipedia

# Bind the Wikipedia tool to 
# the language model
llm_with_tools = llm.bind_tools(tools)


# Modify chatbot function to # respond with Wikipedia def chatbot(state: State): return {"messages": [llm_with_tools.invoke( state["messages"])]}

   

  • เชื่อมต่อเครื่องมือ

 

  • อัปเดต chatbot node

  • เปิดใช้งาน Wikipedia

  • LLM ตัดสินใจเรียกใช้เครื่องมือ

การออกแบบระบบ Agentic ด้วย LangChain

เครื่องมือ API อื่น ๆ

  จอมอนิเตอร์พร้อมกราฟิกไมโครชิปตรงกลาง มีข้อความ "API" อยู่ด้านใน

   

การออกแบบระบบ Agentic ด้วย LangChain

การเพิ่ม tool node

# Modules for adding tool conditions 
# and nodes
from langgraph.prebuilt import 
ToolNode, tools_condition


# Add chatbot node to the graph graph_builder.add_node("chatbot", chatbot)

Chatbot node

การออกแบบระบบ Agentic ด้วย LangChain

การเพิ่ม tool node

# Modules for adding tool conditions 
# and nodes
from langgraph.prebuilt import 
ToolNode, tools_condition

# Add chatbot node to the graph
graph_builder.add_node("chatbot",
                       chatbot)


# Create a ToolNode to handle tool calls # and add it to the graph tool_node = ToolNode(tools=[wikipedia_tool]) graph_builder.add_node("tools", tool_node)

chatbot node และ tool node

การออกแบบระบบ Agentic ด้วย LangChain

การเพิ่ม tool node

# Set up a condition to direct from chatbot 
# to tool or end node
graph_builder.add_conditional_edges(
             "chatbot", tools_condition)



แผนภาพแชทบอทแสดง END node และ tool node เชื่อมต่อด้วยเส้นประแทน conditional edge

การออกแบบระบบ Agentic ด้วย LangChain

การเพิ่ม tool node

# Set up a condition to direct from chatbot 
# to tool or end node
graph_builder.add_conditional_edges(
             "chatbot", tools_condition)



# Connect tools back to chatbot and # add START and END nodes graph_builder.add_edge("tools", "chatbot") graph_builder.add_edge(START, "chatbot") graph_builder.add_edge("chatbot", END)

แผนภาพแชทบอทแบบสมบูรณ์ แสดง chatbot node เชื่อมต่อกับ tools node และ END node พร้อม START node อยู่ด้านบน

การออกแบบระบบ Agentic ด้วย LangChain

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

การออกแบบระบบ Agentic ด้วย LangChain

Preparing Video For Download...