Agent-Based Dynamic Slot Filling
Author: Jongcheol Kim
Peer Review: kofsitho87, Heeah Kim
Proofread : Q0211
This is a part of LangChain Open Tutorial
Overview
This tutorial explains how to implement an Agent-based Dynamic Slot Filling system. It covers the process of creating an intelligent conversational system that analyzes user requests to automatically collect necessary information and supplements missing information through dialogue.
The system can handle various tasks such as restaurant reservations, meeting scheduling, hotel bookings, and flight reservations, dynamically collecting and validating the required information for each task.
Features
Dynamic Slot Filling: Automatically identifies and collects necessary information by analyzing user requests
Multi-task Support: Handles various tasks including restaurant, meeting, hotel, and flight reservations
Conversational Information Collection: Supplements missing information through natural dialogue
Information Validation: Automatically validates the input information
Graph of Agent-Based Dynamic Slot Filling
This graph shows the workflow of an Agent-based Dynamic Slot Filling system:
Start β Classify
Analyzes user requests and classifies task type
Initialize Slots
Sets up required information fields
Extract Slots
Extracts necessary information from user messages
Identifies missing information
Generate Response
Requests additional information or completes task
Ends when all information is collected
The system iterates through conversation with the user until all necessary information is gathered.

Table of Contents
References
Environment Setup
Set up the environment. You may refer to Environment Setup for more details.
[Note]
langchain-opentutorialis a package that provides a set of easy-to-use environment setup, useful functions and utilities for tutorials.You can checkout the
langchain-opentutorialfor more details.
You can alternatively set API keys such as OPENAI_API_KEY in a .env file and load them.
[Note] This is not necessary if you've already set the required API keys in previous steps.
Constants and Prompt Template Definition
Constants is defined as below:
TASK_SLOTS: Defines the required information for each task typeTASK_RULES: Defines the rules for information collection for each task typeTASK_EXAMPLES: Provides examples of user requests for each task typeTASK_CLASSIFICATION_TEMPLATE: A prompt template for classifying the user's request into a taskSLOT_EXTRACTION_TEMPLATE: A prompt template for extracting information from the user's messageRESPONSE_TEMPLATE: A prompt template for generating a response to the user's message
Task Slots
Task Slots is a structure that defines the required information for each task type. Each task has its own unique set of slots, allowing systematic collection of necessary information.
Task Rules
Defines the rules to follow when collecting information for each task type. These rules serve as guidelines that the conversational agent references when gathering information through user interactions.
Restaurant Reservation: Collect information about location, number of people, and reservation date/time
Hotel Booking: Collect information about location, check-in/out dates, room type, and number of guests
Meeting Room Reservation: Collect information about location, number of attendees, meeting date/time, and duration
Flight Reservation: Collect information about departure/arrival locations, departure/return dates, and number of passengers
Task Classification Template
Defines the prompt template for classifying the user's request into a task.
user_message: The user's message to be analyzedtask_type: The type of task selected by the agentconfidence: The confidence score of the task classification (0.0 ~ 1.0)
Slot Extraction Template
Defines the prompt template for extracting information from the user's message.
task_type: The type of task for which information is being extractedrequired_slots: The slots that need to be extractedslots: The current state of the slotsmessages: The conversation historylast_message: The last message from the user
Please follow these rules strictly:
Date and Time Conversion Rules:
All dates must be in YYYY/MM/DD HH:MM format8
If only "next week" is mentioned, ask for specific date and time (keep as null )
Convert to date only when day of week is specified (e.g., "next Monday")
If no time is specified, keep as null
Incomplete Date/Time Cases:
"Next week" only β null
"Evening" only β null
"Next Monday evening" β YYYY/MM/DD 19:00
"Tomorrow lunch" β YYYY/MM/DD 12:00
Numbers must be converted to numeric format (e.g., "four people" β 4 )
Use location names as is (e.g., "Manhattan", "New York")
Mark uncertain information as null
Response Template
Defines the prompt template for collecting missing information through natural dialogue.
task_type: The type of task for which information is being collectedrequired_slots: The slots that need to be collectedslots: The current state of the slotsmessages: The conversation historylast_message: The last message from the user
Please follow these rules:
task_rules: The rules specific to the current task typeRespond in a natural, conversational manner
State Management
State management plays a crucial role in controlling the flow of the conversation and tracking necessary information.
Defines the SupervisorState class to manage the state of the conversational agent.
Inherits from TypedDict to define and track the state of the conversational agent.
This state management allows maintaining the conversation context with the user and sequentially collecting necessary information.
SupervisorState
messages: Manages conversation historytask_type: Tracks current task typeconfidence: Task classification confidence scoreslots: Stores collected informationcurrent_slot: Currently processing slotcompleted: Task completion statusstage: Current stage ('classify' or 'slot_filling')
Graph Construction
Uses LangGraph's StateGraph to construct the conversation flow.
Main Nodes
classify_task: Classifies the task type based on the user's messageIdentifies reservation/booking intent from user message to select appropriate task
Proceeds to slot initialization or information extraction based on selected task
initialize_slots: Initializes slots for user messageInitializes required slots based on selected task
Stores initialized slot state in state variables
extract_slots: Extracts necessary information from user messageUses
LLMto extract structured information from natural languageValidates extracted information
Updates with new information while maintaining existing slot values
generate_response: Generates appropriate response based on current stateBranches response based on task classification confidence
Requests missing information
Generates reservation completion message
should_continue: Controls conversation flow by determining next step based on:Checks user input waiting status
Branches based on task classification confidence
Checks if slot initialization is needed
Determines whether to continue information extraction
Create Reservation Agent Graph
Creates the integrated reservation system Agent graph.
Uses
StateGraphto define the conversation flowEach node is connected to a function that performs a specific task
Conditional edges to control flow based on state
Execution Flow
Receive user input
Task type classification (
classify_task)Slot initialization (
initialize_slots)Information extraction (
extract_slots)Generate response (
generate_response)Repeat 2-5 if necessary
Complete reservation when all information is collected
This structure allows for collecting necessary information through natural conversation with the user and performing appropriate processing for each reservation type.

Example Execution
Each example demonstrates how the system automatically extracts necessary information and completes the task through natural conversation.
Restaurant Reservation
User: I'd like to make a dinner reservation for 4 people next Friday at 7 PM
AI: Could you please specify the restaurant location (city name)?
User: New York
AI: All information has been entered. I will complete the reservation.
Hotel Booking
User: I want to book a suite room in Manhattan, New York from the 1st to the 3rd of next month
AI: Could you please specify the hotel location (city name)?
User: New York
AI: All information has been entered. I will complete the reservation.
Meeting Scheduling
User: I'm planning to have a one-hour meeting tomorrow at 2 PM in the Downtown conference room
AI: Could you please specify the video conference platform (zoom/teams/google meet)?
User: Zoom
AI: All information has been entered. I will complete the reservation.
Flight Booking
User: I'd like to book 2 economy seats from LAX to New York at 10 AM on the 15th of next month
AI: Could you please specify the departure city?
User: Los Angeles
AI: All information has been entered. I will complete the reservation.
Last updated