Agent-Based Dynamic Slot Filling

Open in ColabOpen in GitHub

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.

Agent-Based-Dynamic-Slot-Filling

Table of Contents

References

Environment Setup

Set up the environment. You may refer to Environment Setup for more details.

[Note]

  • langchain-opentutorial is a package that provides a set of easy-to-use environment setup, useful functions and utilities for tutorials.

  • You can checkout the langchain-opentutorial for 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 type

  • TASK_RULES : Defines the rules for information collection for each task type

  • TASK_EXAMPLES : Provides examples of user requests for each task type

  • TASK_CLASSIFICATION_TEMPLATE : A prompt template for classifying the user's request into a task

  • SLOT_EXTRACTION_TEMPLATE : A prompt template for extracting information from the user's message

  • RESPONSE_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 analyzed

  • task_type : The type of task selected by the agent

  • confidence : 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 extracted

  • required_slots : The slots that need to be extracted

  • slots : The current state of the slots

  • messages : The conversation history

  • last_message : The last message from the user

Please follow these rules strictly:

  1. 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

  2. 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

  3. Numbers must be converted to numeric format (e.g., "four people" β†’ 4 )

  4. Use location names as is (e.g., "Manhattan", "New York")

  5. 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 collected

  • required_slots : The slots that need to be collected

  • slots : The current state of the slots

  • messages : The conversation history

  • last_message : The last message from the user

Please follow these rules:

  • task_rules: The rules specific to the current task type

  • Respond 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 history

  • task_type : Tracks current task type

  • confidence : Task classification confidence score

  • slots : Stores collected information

  • current_slot : Currently processing slot

  • completed : Task completion status

  • stage : 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 message

    • Identifies 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 message

    • Initializes required slots based on selected task

    • Stores initialized slot state in state variables

  • extract_slots: Extracts necessary information from user message

    • Uses LLM to extract structured information from natural language

    • Validates extracted information

    • Updates with new information while maintaining existing slot values

  • generate_response: Generates appropriate response based on current state

    • Branches 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 StateGraph to define the conversation flow

  • Each node is connected to a function that performs a specific task

  • Conditional edges to control flow based on state

Execution Flow

  1. Receive user input

  2. Task type classification (classify_task)

  3. Slot initialization (initialize_slots)

  4. Information extraction (extract_slots)

  5. Generate response (generate_response)

  6. Repeat 2-5 if necessary

  7. 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.

png

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