Defining nodes and edges for flexible function calling

Designing Agentic Systems with LangChain

Dilini K. Sumanapala, PhD

Founder & AI Engineer, Genverv, Ltd.

Building a multi-tool workflow

   

  • Multiple available tools

    • Palindrome
    • Historical events
    • Wikipedia

Full chatbot workflow

Designing Agentic Systems with LangChain

Define workflow functions

   

  • Create a stopping function

Chatbot and tools nodes of chatbot workflow.

Designing Agentic Systems with LangChain

Define workflow functions

   

  • Create a stopping function

    • Check for tool calls

Stopping function rules highlighted.

Designing Agentic Systems with LangChain

Define workflow functions

   

  • Create a stopping function

    • Check for tool calls
    • End conversation if none

END rules highlighted in workflow

Designing Agentic Systems with LangChain

Define workflow functions

   

  • Create a stopping function

    • Check for tool calls
    • End conversation if none    
  • Create a dynamic tool caller

    • Return a tool response if tool call present

Call_model rules highlighted.

Designing Agentic Systems with LangChain

Define workflow functions

   

  • Create a stopping function

    • Check for tool calls
    • End conversation if none    
  • Create a dynamic tool caller

    • Return a tool response if tool call present
    • Invoke the LLM with just the chatbot node if no tool calls

Call_model rules highlighted.

Designing Agentic Systems with LangChain

Define workflow functions

   

  • Create a stopping function

    • Check for tool calls
    • End conversation if none    
  • Create a dynamic tool caller

    • Return a tool response if tool call present
    • Invoke the LLM with just the chatbot node if no tool calls
  • Compile the full graph

Call_model rules highlighted.

Designing Agentic Systems with LangChain

Create a stop condition function

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
Designing Agentic Systems with LangChain

Create a dynamic tool caller

# 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"])]}
Designing Agentic Systems with LangChain

Create the graph

workflow = StateGraph(MessagesState)












Designing Agentic Systems with LangChain

Create the graph

workflow = StateGraph(MessagesState)


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

Chatbot and tools nodes.

Designing Agentic Systems with LangChain

Create the graph

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

Add START node to chatbot.

Designing Agentic Systems with LangChain

Create the graph

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

Graph with conditions added.

Designing Agentic Systems with LangChain

Create the graph

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

Full graph workflow.

Designing Agentic Systems with LangChain

Adding memory

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

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

Full graph workflow.

Designing Agentic Systems with LangChain

Let's practice!

Designing Agentic Systems with LangChain

Preparing Video For Download...