Thêm công cụ bên ngoài vào chatbot

Thiết kế Hệ thống Agentic với LangChain

Dilini K. Sumanapala, PhD

Founder & AI Engineer, Genverv, Ltd.

Công cụ bên ngoài với LangGraph

  Màn hình desktop với hình vi mạch ở giữa, có chữ "API" ở trung tâm.

     

  • Công cụ API cho chatbot

     
    • Tin tức
    • Cơ sở dữ liệu
    • Mạng xã hội
    • Khác
Thiết kế Hệ thống Agentic với LangChain

Thêm công cụ Wikipedia

Mọi người đọc sách, ngồi trên những cuốn sách lớn.

  Biểu tượng Wikipedia.

Thiết kế Hệ thống Agentic với LangChain

Thêm công cụ 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]
Thiết kế Hệ thống Agentic với LangChain

Thêm công cụ 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"])]}

   

  • Gắn tool

 

  • Cập nhật nút chatbot

  • Bật Wikipedia

  • LLM quyết định gọi tool

Thiết kế Hệ thống Agentic với LangChain

Công cụ API khác

  Màn hình desktop với hình vi mạch ở giữa, có chữ "API" ở trung tâm.

   

Thiết kế Hệ thống Agentic với LangChain

Thêm các nút công cụ

# 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)

Nút chatbot.

Thiết kế Hệ thống Agentic với LangChain

Thêm các nút công cụ

# 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)

Các nút chatbot và công cụ.

Thiết kế Hệ thống Agentic với LangChain

Thêm các nút công cụ

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



Chatbot với các nút END và công cụ, nối bằng các đường nét đứt biểu thị cạnh có điều kiện.

Thiết kế Hệ thống Agentic với LangChain

Thêm các nút công cụ

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



# Kết nối tools trở lại chatbot và # thêm các nút START và END graph_builder.add_edge("tools", "chatbot") graph_builder.add_edge(START, "chatbot") graph_builder.add_edge("chatbot", END)

Sơ đồ chatbot đầy đủ với nút chatbot nối với tools và nút END, cùng nút START ở trên.

Thiết kế Hệ thống Agentic với LangChain

Cùng luyện tập nào!

Thiết kế Hệ thống Agentic với LangChain

Preparing Video For Download...