Get startedGet started for free

Orchestrating the text-to-query agent

This exercise is part of the course

Text-to-Query Agents with MongoDB and LangGraph

View Course

Exercise instructions

  • Define the graph state using the GraphState class, with a single attribute called messages.
  • Define the agent node that reads the messages attribute of the graph state, invokes the llm_with_tools with the messages, and appends the result to the messages attribute of the graph state.
  • Define the tool node that reads the tool_calls attribute of the most recent message in the graph state, extracts the name of the tool to execute (name), invokes it with the arguments (args) of the tool call, and appends the result to the messages attribute of the graph state.
  • Initialize the graph using the StateGraph class in LangGraph, with the graph state (GraphState) we defined previously.
  • Add the nodes and edges to the graph and compile it.


Note: If you’re running DataLab in Restricted Mode, this exercise isn’t supported yet. We’re actively working on making it available in the future.


Hands-on interactive exercise

Turn theory into action with one of our interactive exercises

Start Exercise