Mendefinisikan node dan edge untuk pemanggilan fungsi yang fleksibel

Merancang Sistem Agentic dengan LangChain

Dilini K. Sumanapala, PhD

Founder & AI Engineer, Genverv, Ltd.

Membangun workflow multi-tool

   

  • Beberapa tool tersedia

    • Palindrome
    • Peristiwa sejarah
    • Wikipedia

Workflow chatbot lengkap

Merancang Sistem Agentic dengan LangChain

Definisikan fungsi workflow

   

  • Buat fungsi penghentian

Node chatbot dan tools dari workflow chatbot.

Merancang Sistem Agentic dengan LangChain

Definisikan fungsi workflow

   

  • Buat fungsi penghentian

    • Periksa pemanggilan tool

Aturan fungsi penghentian disorot.

Merancang Sistem Agentic dengan LangChain

Definisikan fungsi workflow

   

  • Buat fungsi penghentian

    • Periksa pemanggilan tool
    • Akhiri percakapan jika tidak ada

Aturan END disorot dalam workflow

Merancang Sistem Agentic dengan LangChain

Definisikan fungsi workflow

   

  • Buat fungsi penghentian

    • Periksa pemanggilan tool
    • Akhiri percakapan jika tidak ada    
  • Buat pemanggil tool dinamis

    • Kembalikan respons tool jika ada pemanggilan tool

Aturan Call_model disorot.

Merancang Sistem Agentic dengan LangChain

Definisikan fungsi workflow

   

  • Buat fungsi penghentian

    • Periksa pemanggilan tool
    • Akhiri percakapan jika tidak ada    
  • Buat pemanggil tool dinamis

    • Kembalikan respons tool jika ada pemanggilan tool
    • Panggil LLM dengan hanya node chatbot jika tidak ada pemanggilan tool

Aturan Call_model disorot.

Merancang Sistem Agentic dengan LangChain

Definisikan fungsi workflow

   

  • Buat fungsi penghentian

    • Periksa pemanggilan tool
    • Akhiri percakapan jika tidak ada    
  • Buat pemanggil tool dinamis

    • Kembalikan respons tool jika ada pemanggilan tool
    • Panggil LLM dengan hanya node chatbot jika tidak ada pemanggilan tool
  • Kompilasi graf penuh

Aturan Call_model disorot.

Merancang Sistem Agentic dengan LangChain

Buat fungsi kondisi berhenti

from langgraph.graph import MessagesState, START, END


# Use MessagesState to define the state of the stopping function def should_continue(state: MessagesState):
# Get the last message from the state last_message = state["messages"][-1]
# Check if the last message includes tool calls if last_message.tool_calls: return "tools"
# End the conversation if no tool calls are present return END
Merancang Sistem Agentic dengan LangChain

Buat pemanggil tool dinamis

# Extract the last message from the history
def call_model(state: MessagesState):

last_message = state["messages"][-1]
# If the last message has tool calls, return the tool's response if isinstance(last_message, AIMessage) and last_message.tool_calls:
# Return the messages from the tool call return {"messages": [AIMessage(content=last_message.tool_calls[0]["response"])]}
# Otherwise, proceed with a regular LLM response return {"messages": [model_with_tools.invoke(state["messages"])]}
Merancang Sistem Agentic dengan LangChain

Buat graf

workflow = StateGraph(MessagesState)












Merancang Sistem Agentic dengan LangChain

Buat graf

workflow = StateGraph(MessagesState)


# Add nodes for chatbot and tools workflow.add_node("chatbot", call_model) workflow.add_node("tools", tool_node)

Node chatbot dan tools.

Merancang Sistem Agentic dengan LangChain

Buat graf

workflow = StateGraph(MessagesState)


# Add nodes for chatbot and tools workflow.add_node("chatbot", call_model) workflow.add_node("tools", tool_node)
# Connect the START node to the chatbot workflow.add_edge(START, "chatbot")

Tambahkan node START ke chatbot.

Merancang Sistem Agentic dengan LangChain

Buat graf

workflow = StateGraph(MessagesState)


# Add nodes for chatbot and tools workflow.add_node("chatbot", call_model) workflow.add_node("tools", tool_node)
# Connect the START node to the chatbot workflow.add_edge(START, "chatbot")
# Define conditions, then loop back to chatbot workflow.add_conditional_edges("chatbot", should_continue, ["tools", END])

Graf dengan kondisi ditambahkan.

Merancang Sistem Agentic dengan LangChain

Buat graf

workflow = StateGraph(MessagesState)


# Add nodes for chatbot and tools workflow.add_node("chatbot", call_model) workflow.add_node("tools", tool_node)
# Connect the START node to the chatbot workflow.add_edge(START, "chatbot")
# Define conditions, then loop back to chatbot workflow.add_conditional_edges("chatbot", should_continue, ["tools", END])
workflow.add_edge("tools", "chatbot")

Workflow graf penuh.

Merancang Sistem Agentic dengan LangChain

Menambahkan memori

# Set up memory and compile the workflow
memory = MemorySaver()

app = workflow.compile( checkpointer=memory)
display(Image(app.get_graph() .draw_mermaid_png()))

Workflow graf penuh.

Merancang Sistem Agentic dengan LangChain

Ayo berlatih!

Merancang Sistem Agentic dengan LangChain

Preparing Video For Download...