A robust, repeatable AI-driven solution that automatically retrieves voyage numbers and arrival dates for HMM shipping line booking IDs using natural language processing and browser automation. This system includes both a command-line interface for direct tracking and an interactive chat application.
Video Demonstration link: https://drive.google.com/file/d/1Z8Mibtq_VGG_x3FDtOefeAHkoDSyXKx8/view?usp=sharing
Assignment ID: SINI25432400 (example) Objective: Develop an AI-powered system to extract shipping information from seacargotracking.net (and potentially other sources) without hardcoded web interactions, accessible via CLI and a chat interface.
- Natural Language Processing: Uses AI to interact with websites through natural language commands.
- Zero Hardcoding: Aims to minimize hardcoded selectors or interaction patterns.
- Adaptive Navigation: Designed to adapt to website changes and different layouts.
- Data Persistence: Stores successful interaction patterns for improved performance (for the agent).
- Cross-Platform: Run scripts provided for Windows, macOS, and Linux.
- Interactive Chat Interface: A user-friendly web UI (via Chainlit) for tracking shipments and configuring LLM providers.
- Multi-LLM Support: Supports various LLM providers (e.g., OpenAI, Groq, Google Gemini) configurable via the chat UI or environment variables.
- Robust Error Handling: Comprehensive error handling and retry mechanisms.
- Real-time Log Streaming: The chat interface can display real-time logs from the agent.
CLI Execution:
User (CLI) --> run.sh / run.bat --> agent.py --> AI Agent Logic --> Browser Automation --> Target Website
^ |
| v
Data Storage (Cache)
Chat Application Execution:
User (Web Browser) --> Chainlit UI (chat.py) --> SimpleShippingChatBot --+
|
v
ShippingTrackingAgent (agent.py)
|
v
AI Agent Logic
|
v
Browser Automation
|
v
Target Website
- Python 3.8+ (Python 3.11 recommended)
uv(recommended for environment and package management) orpipandvenv- Internet connection
- API key for your chosen LLM provider (e.g., OpenAI, Groq, Google Gemini)
-
Clone or download the project files. If you have git:
git clone <repository-url> cd <project-directory>
-
Set up Python environment. (Using
uvis recommended)Using
uv(Recommended):# Install uv if you haven't already: pip install uv uv venv --python 3.11 # Creates a .venv directory # Activate environment: # On Windows: .venv\Scripts\activate # On Mac/Linux: source .venv/bin/activate
Using standard
venv:python3 -m venv venv # On Windows: venv\Scripts\activate # On Mac/Linux: source venv/bin/activate
-
Install dependencies.
# Using uv (fastest) uv pip install -r requirements.txt # Or using pip pip install -r requirements.txt # Install Playwright browsers (required for browser automation) playwright install
-
Configure environment variables. Copy the example environment file:
cp .env.example .env
Edit the
.envfile and add your API key(s) for the desired LLM provider(s). For example:# For OpenAI OPENAI_API_KEY=your_openai_sk_key_here # For Groq GROQ_API_KEY=your_groq_gsk_key_here # For Google Gemini GOOGLE_API_KEY=your_google_ai_key_here # You can also set a default LLM_PROVIDER (openai, groq, gemini) # LLM_PROVIDER=groq
The applications (
agent.pyandchat.py) will use these keys. The chat interface also allows setting these at runtime.
There are two main ways to use this system:
This is for direct, scripted tracking of booking IDs.
Using the run scripts (Recommended for CLI):
# On Windows:
run.bat SINI25432400
# On Mac/Linux (make sure it's executable: chmod +x run.sh):
./run.sh SINI25432400The script will guide you if the .env file is not configured.
Direct Python execution (CLI):
Make sure your virtual environment is activated and .env is configured.
python agent.py SINI25432400
# For multiple booking IDs:
python agent.py SINI25432400 ABCD12345678Expected CLI Output (Example):
π’ Starting shipment tracking for booking ID: SINI25432400
π€ Using Groq (llama-4-scout-17b-16e-instruct) # Example LLM
β
Successfully retrieved shipping information!
==================================================
TRACKING RESULTS
==================================================
π¦ Booking ID: SINI25432400
π’ Voyage Number: HMM001E
π
Arrival Date: 2025-06-15
π Vessel Name: HMM COPENHAGEN
π Status: In Transit
π€ LLM Provider: groq
β
Tracking completed!
This provides a web-based UI for tracking shipments, configuring LLM providers, and viewing real-time agent logs.
Running the chat application: Make sure your virtual environment is activated.
chainlit run chat.py -wThe -w flag enables auto-reloading when you make changes to chat.py.
Open your web browser and navigate to the URL provided by Chainlit (usually http://localhost:8000).
Using the Chat Application:
- On the first run, or by clicking the settings (βοΈ) icon, configure your LLM Provider and API Key.
- Type your tracking request into the chat input (e.g., "Track SINI25432400").
- Observe the real-time logs and the agent's progress.
- Receive a formatted response with the tracking details.
Beyond API keys, other settings can be configured in the .env file (primarily for agent.py's direct use or as defaults):
# Browser settings for agent.py when run directly
BROWSER_HEADLESS=true # Set to false to see browser window
BROWSER_TIMEOUT=30000 # Timeout in milliseconds
BROWSER_SLOW_MO=100 # Slow down automation for debugging
# Caching for agent.py
CACHE_EXPIRY_HOURS=24 # How long to cache results
# Retry settings for agent.py
MAX_RETRY_ATTEMPTS=3 # Number of retry attempts
RETRY_DELAY_SECONDS=5 # Delay between retries
# Debugging for agent.py
ENABLE_SCREENSHOTS=false # Save screenshots for debugging agent.py
LOG_LEVEL=INFO # Logging level (DEBUG, INFO, WARNING, ERROR) for agent.py
LOG_FILE=shipping_tracking.log # Log file for agent.py direct runsThe chat.py application has its own logging and settings UI for some of these.
You can script agent.py for batch processing multiple booking IDs. Refer to the agent.py's main() function or create a custom script that imports ShippingTrackingAgent.
- Booking IDs: The AI agent is designed to be flexible. The prompts in
agent.pycan be adjusted to better guide the AI for specific ID formats or websites. - Shipping Lines: To support new shipping lines:
- Update the AI prompts in
agent.py(e.g.,tracking_prompt) to include instructions for the new line/website. - The
Configclass (config.py) could be extended if specific URLs or patterns for new lines need to be managed systemically, though the current approach relies more on AI flexibility.
- Update the AI prompts in
- Console Output (CLI): Real-time status updates and results from
agent.py. - Chat Interface (UI): Formatted responses and detailed tracking results viewable in the UI.
- Real-time Logs (UI): The chat interface streams logs from the agent.
- Database Storage (
shipping_tracking.db):agent.pycan cache results. - Screenshots: If
ENABLE_SCREENSHOTS=truein.env,agent.pymay save screenshots in ascreenshots/directory during debugging. - Log File: For CLI runs of
agent.py, logs are stored inshipping_tracking.log(or as configured).
The system's adaptability relies on:
- LLM Capabilities: The core of understanding and navigating websites is handled by the LLM.
- Flexible Prompts: The prompts given to the AI in
agent.pyare crucial. They guide the AI without hardcoding specific steps. - Browser Automation Tools:
browser_uselibrary abstracts many browser interaction complexities. - Error Handling and Fallbacks:
agent.pyincludes logic for retries and potential fallback strategies.
-
API Key Not Found/Invalid:
- Ensure your
.envfile is correctly formatted and contains the valid API key for the selected LLM provider. - In the chat app, double-check the API key entered in the settings panel.
- Verify the environment variable name matches what the application expects (e.g.,
OPENAI_API_KEY,GROQ_API_KEY).
- Ensure your
-
"Browser launch failed" / Playwright Issues:
- Ensure Playwright browsers are installed:
playwright install. - Try running with
BROWSER_HEADLESS=false(in.envforagent.py) to see if the browser window provides more clues.
- Ensure Playwright browsers are installed:
-
"No tracking results found" / AI Agent Issues:
- The target website might have changed. Try adjusting the prompt in
agent.py(tracking_prompt). - The booking ID might be invalid or not in the system.
- Check
LOG_LEVEL=DEBUGin.envfor more detailed logs fromagent.py. - In the chat app, enable "Show Real-time Agent Logs" for insights.
- The target website might have changed. Try adjusting the prompt in
-
Module Not Found Errors:
- Ensure your virtual environment is activated.
- Reinstall dependencies:
uv pip install -r requirements.txtorpip install -r requirements.txt.
-
Chainlit App Not Starting / UI Issues:
- Check the terminal output when you run
chainlit run chat.py -wfor any error messages. - Ensure Chainlit is installed correctly in your virtual environment.
- Try accessing in a different browser or incognito mode.
- Check the terminal output when you run
- For
agent.py(CLI): Set in.env:BROWSER_HEADLESS=false,ENABLE_SCREENSHOTS=true,LOG_LEVEL=DEBUG. - For
chat.py(UI): Use the "Show Real-time Agent Logs" switch in the chat settings. Examine terminal output fromchainlit run ....
agent.py:browser_use: AI-powered browser automation.- LangChain (
langchain-openai,langchain-google-genai,langchain-groq): LLM integration and orchestration. - Playwright: Underlying browser automation engine.
- Dotenv: Environment variable management.
chat.py:- Chainlit: Python framework for creating chat UIs.
- Standard Python libraries (asyncio, logging, json, re).
- LLMs: OpenAI GPT models, Groq's Llama/Mixtral models, Google Gemini models.
- SQLite: Local data persistence for caching by
agent.py.
This README provides a comprehensive guide to setting up, running, and troubleshooting the AI-Based Shipping Line Tracking System.