Menambahkan alat eksternal ke chatbot

Merancang Sistem Agentic dengan LangChain

Dilini K. Sumanapala, PhD

Founder & AI Engineer, Genverv, Ltd.

Alat eksternal dengan LangGraph

  Monitor desktop dengan gambar mikrochip di tengah bertuliskan "API".

     

  • Alat API untuk chatbot

     
    • Berita
    • Basis data
    • Media sosial
    • Dll.
Merancang Sistem Agentic dengan LangChain

Menambahkan alat Wikipedia

Orang-orang membaca, duduk di atas buku-buku besar.

  Ikon Wikipedia.

Merancang Sistem Agentic dengan LangChain

Menambahkan alat 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]
Merancang Sistem Agentic dengan LangChain

Menambahkan alat 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"])]}

   

  • Ikat alat

 

  • Perbarui node chatbot

  • Aktifkan Wikipedia

  • LLM menentukan pemanggilan alat

Merancang Sistem Agentic dengan LangChain

Alat API lainnya

  Monitor desktop dengan gambar mikrochip di tengah bertuliskan "API".

   

  • Dokumentasi LangChain API

Merancang Sistem Agentic dengan LangChain

Menambahkan node alat

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

Node chatbot.

Merancang Sistem Agentic dengan LangChain

Menambahkan node alat

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

Node chatbot dan alat.

Merancang Sistem Agentic dengan LangChain

Menambahkan node alat

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



Chatbot dengan node END dan alat, dihubungkan garis putus-putus sebagai tepi bersyarat.

Merancang Sistem Agentic dengan LangChain

Menambahkan node alat

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

Diagram chatbot lengkap dengan node chatbot terhubung ke alat dan node END, serta node START di atas.

Merancang Sistem Agentic dengan LangChain

Ayo berlatih!

Merancang Sistem Agentic dengan LangChain

Preparing Video For Download...