Multi-Agent Scheduler System
Author: Ilgyun Jeong
Peer Review: Mark(), Taylor(Jihyun Kim)
Proofread : Q0211
This is a part of LangChain Open Tutorial
Overview
The Multi-Agent Scheduler System represents an innovative approach to automating information retrieval and delivery through a coordinated network of specialized AI agents. At its core, this system transforms simple natural language requests into scheduled, automated search and delivery operations, making it particularly valuable for researchers, analysts, and anyone needing regular, scheduled information updates.
Imagine asking "Find me the latest RAG papers at 7 AM tomorrow." Instead of manually searching and compiling information early in the morning, the system automatically handles the entire process - from understanding your request to delivering a well-formatted email with relevant research papers at precisely 7 AM. This automation removes the need for manual intervention while ensuring timely delivery of critical information.
System Architecture
The system's architecture is built around five specialized agents, each handling a crucial aspect of the information retrieval and delivery process:
Query Analysis AgentThis agent serves as the system's front door, interpreting natural language queries to extract critical informationSearch RouterActing as the system's traffic controller, the Search Router directs queries to the most appropriate specialized search agent:Response AgentThis agent transforms raw search results into well-structured, readable content by:Scheduling System and Email ServiceThe scheduling component manages the temporal aspects of the system: This ensures that all operations occur at their specified times without conflicts. The system implements a robust email delivery service using yagmail that provides:
System Flow
The entire process follows this sequence:

This architecture ensures reliable, automated information retrieval and delivery, with each agent optimized for its specific role in the process.
Table of Contents
The system's modular design allows for easy expansion and customization, making it adaptable to various use cases while maintaining consistent performance and reliability. Whether you're tracking research papers, monitoring news, or gathering general information, the Multi-Agent Scheduler System automates the entire process from query to delivery, saving time and ensuring consistent, timely access to important information.
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.
Query Analysis Agent
The QueryAnalysisAgent serves as the initial interpreter in our multi-agent scheduler system, transforming natural language queries into structured data that other agents can process. This agent employs advanced language understanding capabilities through GPT-4 to accurately parse user intentions and timing requirements.
Core Components
The agent is built around three essential classes:
Time Extraction Processor: Handles temporal information
Task Analysis Engine: Understands search requirements
Query Coordinator: Combines and validates results
Core Functionality
The agent performs two primary functions:
Time Extraction
Task Analysis
Usage Example
This structured approach ensures reliable query interpretation while maintaining flexibility for various query types and formats.
Install and Import Required Libraries
The QueryAnalysisAgent class represents a specialized natural language query processor that utilizes OpenAI's language models. Let's break down its core components: The initialization method sets up the language model with temperature=0 to ensure consistent, deterministic responses:
The setup_prompt_templates method defines two essential templates:
Time Extraction Template This template focuses solely on extracting and standardizing time information from queries.
Task Analysis Template This template structures the query analysis with specific rules:
Categorizes searches into three types: research_paper, news, or general
Distinguishes between normal and urgent time sensitivity
Separates search keywords from temporal terms
Maintains consistent task typing as "search"
These templates work together to transform natural language queries into structured, actionable data that the rest of the system can process efficiently. The clear separation between time extraction and task analysis allows for precise handling of each aspect of the query.
For example, a query like "Find RAG papers at 7 AM" would be processed to extract both the time (07:00) and the search parameters (research papers about RAG), while filtering out temporal terms from the actual search keywords.
Core Methods 1
extract_time()
Functionality: Extracts and processes time information from natural language queries
Features:
Converts various time formats (e.g., "morning 7", "afternoon 3:30") to standardized datetime objects
Maintains timezone awareness using pytz for accurate scheduling
Automatically schedules for next day if requested time has already passed
Strips unnecessary time components (seconds, microseconds) for cleaner scheduling
Error Handling: Raises ValueError with detailed error messages for invalid time formats
Returns: UTC-aware datetime object representing the target execution time
Core Methods 2
analyze_task()
Functionality: Breaks down queries into structured task components
Features:
Identifies search type (research_paper, news, general)
Extracts relevant keywords while filtering temporal terms
Determines task urgency (normal vs urgent)
Identifies specific requirements (e.g., minimum result count)
Error Handling: Handles JSON parsing errors and invalid query formats
Returns: Dictionary containing parsed task information and parameters
Core Methods 3
analyze_query()
Functionality: Combines time extraction and task analysis into a complete query interpretation
Features:
Coordinates between time extraction and task analysis
Sets execution time 5 minutes before target time
Validates and combines all query components
Error Handling: Catches and reports errors from both time and task processing
Returns: Combined dictionary with timing and task information
Core Methods 4
datetime_handler(obj)
Functionality: Converts datetime objects to JSON-serializable string format
Features:
Accepts any object and checks if it's a datetime instance
Converts datetime to standardized string format (YYYY-MM-DD HH:MM:SS+ZZZZ)
Maintains timezone information in the output string
Error Handling: Raises TypeError with descriptive message for non-datetime objects
Returns: String representation of datetime in consistent format
Use Cases:
JSON serialization for API responses
Database storage of temporal data
Logging and debugging timestamp formatting
Examples:
Input:
datetime(2024, 2, 6, 15, 30, tzinfo=pytz.UTC)Output:
"2024-02-06 15:30:00+0000"
The function serves as a critical utility for converting Python's datetime objects into a standardized string format that can be easily stored, transmitted, and later reconstructed. This is particularly important in our scheduling system where accurate time representation and timezone awareness are essential for reliable task execution.
Test
Search Router and Specialized Agents
The Search Router system acts as an intelligent traffic controller, directing queries to the most appropriate specialized search agent based on the query analysis. This architecture ensures that each type of search request is handled by an agent specifically optimized for that domain.
Core Components
Each specialized agent is designed to handle specific types of searches:
Paper Search Agent
This agent specializes in academic paper searches, interfacing with arXiv's API to retrieve scholarly articles and research papers.
News Search Agent
This agent handles news-related searches, connecting to NewsAPI to gather current events and news articles.
General Search Agent
This agent manages general web searches using SerpAPI, handling broader information gathering needs.
This routing system ensures that each query is handled by the most appropriate agent while maintaining consistent error handling and result formatting across all search types. The modular design allows for easy addition of new specialized agents as needed, making the system highly extensible and maintainable.
Each agent provides standardized outputs despite their different data sources and search methodologies, enabling seamless integration with the rest of the system components.
PaperSearchAgent
This agent focuses on academic content retrieval. It interfaces with the arXiv API to fetch scholarly papers and research documents. Key features include filtering papers by relevance, date ranges, and processing XML responses into structured data. The agent is particularly useful for researchers and academics needing current papers in their field.
NewsSearchAgent
This agent handles current events and news article searches. It connects to NewsAPI to access a wide range of news sources. The agent supports features like language filtering, date range specification, and source selection. It's especially valuable for users needing real-time information or tracking specific topics in the news.
GeneralSearchAgent
This agent manages broader web searches through SerpAPI. It handles diverse information needs that don't fit strictly into academic or news categories. The agent includes features like language-specific searches, result ranking, and content type filtering. It's particularly useful for general research, product information, or any broad information gathering needs.
SearchRouter: The System's Traffic Controller
The SearchRouter acts as the central coordinator for our multi-agent search system, intelligently directing queries to specialized search agents based on the type of information needed. Think of it as an expert traffic controller at a busy airport, making sure each "flight" (query) goes to the right "runway" (search agent).
The SearchRouter's modular design allows for easy expansion - new specialized search agents can be added without modifying the existing code, making the system highly adaptable to evolving search needs.
Through this central coordination, the SearchRouter ensures efficient and reliable information retrieval across different types of searches while maintaining a consistent interface for the rest of the system.
Response Agent
Crafting User-Friendly Information Delivery
The ResponseAgent serves as our system's expert communicator, transforming raw search results into well-structured, readable content that meets users' needs. This agent is particularly crucial as it represents the final step in our information delivery pipeline, ensuring that complex search results are presented in a clear, digestible format.
The agent maintains three specialized prompt templates for different types of content:
Key Features of the Response Agent:
Content Customization
Adapts formatting based on content type (papers, news, general)
Maintains consistent structure while accommodating different information types
Ensures appropriate context and explanations are included
Email Optimization
Creates clear, professional email subjects
Structures content for easy scanning and reading
Includes all necessary context and source information
The ResponseAgent represents the crucial final step in our information delivery pipeline, ensuring that users receive not just raw data, but well-organized, contextually relevant information that directly addresses their queries. Through its careful formatting and organization, it helps users quickly understand and act upon the information they've requested.
This agent demonstrates how automated systems can maintain a human touch in their communications, making complex information accessible and actionable for end users.
Test Search System
Scheduling System and Email Service
The ScheduledSearchSystem manages the complete lifecycle of search tasks, from scheduling to result delivery. Here's its core structure and functionality:
Key Components
Collection Management
Task Scheduling
Search Execution
Email Delivery
The system uses threading for non-blocking operation and includes comprehensive logging for monitoring task progress and debugging issues.
Multi-Agent Scheduler System Usage Guide
The system starts 5 minutes before the scheduled request time.
Email Configuration
Enable Gmail 2-Step Verification
Generate App Password: https://myaccount.google.com/security > 2-Step Verification > App passwords
Initialize System and Schedule Task
Wait for Completion
Search results will be automatically emailed to the specified address upon completion.
The system supports various query types:
Research papers: "find RAG papers at 7 AM"
News: "find AI news at 9 AM"
General search: "find restaurants in Seoul at 6 PM"
Note
You need to get app password from google account.
Last updated