This project implements a Streamlit‑based AI customer service agent for Shake Shack, powered by OpenAI GPT‑4 via LangChain. Users can inquire about menu items, prices, nutritional information, and manage orders through a chat interface.
- Natural Language Chat: Ask about menu items, prices, calories, store locations, hours, and more.
- Order Management:
- Place orders by specifying items and quantities.
- View, update, and remove items in your cart.
- Checkout to save orders to MongoDB.
- Dynamic Menu:
- Menu loaded from MongoDB, with fallback to a small hardcoded list.
- Categorized display in chat and sidebar.
- API Key Handling:
- Supports user‑provided OpenAI API key via sidebar input.
- Falls back to environment
OPENAI_API_KEYif set.
- Error Handling & Logging:
- Centralized LLM error handler with user‑friendly messages.
- Comprehensive logging to
shakeshack_agent.log.
- Session & Caching:
- Streamlit session state for order/cart and menu caching.
st.cache_datadecorator to cache menu queries for 1 hour.
- Python 3.8+
- Streamlit for the web UI
- LangChain + OpenAI GPT‑4 for NLP and intent classification
- MongoDB (via PyMongo) for order persistence
- python‑dotenv for
.envconfiguration
├── app.py # Main entry point: initializes logging, UI, and requirements
├── database.py # MongoDB connection, menu retrieval, order persistence
├── llm.py # Intent classification, item extraction, message processing
├── menu.py # Fuzzy menu lookup & formatted menu display
├── order.py # Order/cart state management and checkout logic
├── ui.py # Streamlit UI layout, sidebar, chat history
├── utils.py # Decorators and helper functions (timing, retry, JSON, etc.)
├── requirements.txt # Python dependencies
└── .env.example # Example environment variables (create your own `.env`)
- Python 3.8 or higher
- MongoDB instance (optional: for production persistence)
- OpenAI API key (to call GPT‑4)
-
Clone the repository
git clone https://github.com/yourusername/shakeshack-agent.git cd shakeshack-agent -
Create & activate a virtual environment
python -m venv .venv source .venv/bin/activate # on Linux/macOS .\.venv\\Scripts\\activate # on Windows
-
Install dependencies
pip install -r requirements.txt
-
Set up environment variables Create a
.envfile in the project root with:OPENAI_API_KEY=your_openai_api_key MONGODB_URI=mongodb+srv://<user>:<pass>@cluster0.mongodb.net/?retryWrites=true&w=majority
Alternatively, configure
MONGODB_URIin Streamlit's secrets manager when deploying.
streamlit run app.py- Open your browser at
http://localhost:8501. - Enter your OpenAI API key in the sidebar (unless set in
.env). - Start chatting!
- Initializes logging (
shakeshack_agent.log). - Loads
.envvariables and writesrequirements.txtviaupdate_requirements_file(). - Registers
cleanup()on exit to close DB connections. - Calls
render_ui()fromui.py.
- Manages MongoDB connection with
get_mongodb_client()&get_db(). - Provides:
get_all_menu_items()(with fallback hardcoded list).get_menu_by_category(category).save_order(order_data).close_connections().
- Uses
st.cache_dataandst.session_state.menu_cachefor caching.
find_menu_item(item_name): fuzzy‑matches user input to menu items.prepare_menu_info(): formats menu as text for LLM prompts.display_formatted_menu(): returns a Markdown string for chat display.
- Initializes session state:
order,total_price,update_sidebar. add_to_order(),update_order_quantity(),remove_from_order(),get_order_summary().finalize_order(): saves to DB and clears session order.clear_order().
get_llm(): returns a configuredChatOpenAIinstance (session or env key).- Intent classification and extraction functions:
classify_intent()extract_order_items_from_text()&..._using_llm()process_quantity_update()extract_item_to_remove(),extract_price_inquiry_item()
general_conversation(): fallback LLM handler with menu context and links.process_message(): orchestrates intent dispatch and calls menu/order/LLM functions.- Decorator
@llm_error_handler()wraps LLM calls for robust error handling.
- Sets up Streamlit page config and hides default header/footer.
- Sidebar for API key input and live order summary with Clear/Checkout buttons.
- Main chat area with logo, welcome text, and warning for missing API key.
- Chat history synced between Streamlit session and LangChain’s
StreamlitChatMessageHistory. render_ui()ties everything together and triggers re-runs when state changes.cleanup()closes DB on shutdown.
- Decorators:
@timeit: logs execution time.@retry: retry on failure with exponential backoff.
- Helpers:
safe_json_loads(),create_directory_if_not_exists(),normalize_item_name().format_price(),format_currency(),truncate_text().
- All logs written to
shakeshack_agent.log. - Streamlit console also shows logs.
- LLM errors return user‑friendly messages in the chat.
- Menu data cached for 1 hour via
@st.cache_data(ttl=3600). - Chat history stored in session state and in LangChain's history.