A curated collection of practical AI projects implementing OCR systems, RAG, AI agents, and other AI use cases.
audit: fix ai_customer_support_agent - add env comments, rewrite em dashes, add docstrings, improve README clarity
Tiioluwanibd549bfa6357
diff --git a/ai_agents/ai_customer_support_agent/.env.example b/ai_agents/ai_customer_support_agent/.env.exampleindex b486d7f..dcd1401 100644--- a/ai_agents/ai_customer_support_agent/.env.example+++ b/ai_agents/ai_customer_support_agent/.env.example@@ -1,2 +1,4 @@+# Mem0 Cloud API key for persistent memory storage. Get yours free at https://app.mem0.aiMEM0_API_KEY=your_mem0_api_key_here+# Mistral AI API key used to call Mistral Small 4 for LLM responses. Get yours at https://platform.mistral.aiMISTRAL_API_KEY=your_mistral_api_key_herediff --git a/ai_agents/ai_customer_support_agent/README.md b/ai_agents/ai_customer_support_agent/README.mdindex 811ad51..a0ceddb 100644--- a/ai_agents/ai_customer_support_agent/README.md+++ b/ai_agents/ai_customer_support_agent/README.md@@ -1,10 +1,10 @@-# CartMate — AI Customer Support Agent+# CartMate: AI Customer Support Agent> A memory-powered e-commerce support agent that remembers your customers across every session.## Overview-CartMate is a conversational support agent for NovaMart built with Streamlit and Mem0. It greets returning users by name, recalls their past orders and reported issues, and picks up conversations exactly where they left off — even after the app restarts. Mem0 Cloud handles persistent memory storage, and Mistral Small 4 drives the responses.+CartMate is a conversational support agent for NovaMart built with Streamlit and Mem0. It greets returning users by name, recalls their past orders and reported issues, and picks up conversations exactly where they left off, persisting memory even after the app restarts. Mem0 Cloud handles persistent memory storage, and Mistral Small 4 drives the responses.## Demo@@ -12,11 +12,11 @@ CartMate is a conversational support agent for NovaMart built with Streamlit and## Features-- **Persistent cross-session memory** — customer preferences, order history, and past issues survive app restarts-- **Personalised responses** — CartMate addresses users by name and references past interactions naturally-- **Real-time memory sidebar** — all stored memories for the current user are visible and refreshed after every message-- **One-click memory reset** — users can clear their stored memories directly from the sidebar-- **Warm returning-user greeting** — CartMate detects prior history and opens with context instead of starting from scratch+- **Persistent cross-session memory**: customer preferences, order history, and past issues survive app restarts+- **Personalised responses**: CartMate addresses users by name and references past interactions naturally+- **Real-time memory sidebar**: all stored memories for the current user are visible and refreshed after every message+- **One-click memory reset**: users can clear their stored memories directly from the sidebar+- **Warm returning-user greeting**: CartMate detects prior history and opens with context instead of starting from scratch## Tech Stack@@ -31,8 +31,8 @@ CartMate is a conversational support agent for NovaMart built with Streamlit and## Prerequisites- Python 3.10 or higher-- A [Mem0 Cloud](https://app.mem0.ai) account and API key-- A [Mistral AI](https://platform.mistral.ai) account and API key+- A [Mem0 Cloud](https://app.mem0.ai) account and API key (free tier available)+- A [Mistral AI](https://platform.mistral.ai) account and API key (free tier available)## Installation@@ -40,7 +40,7 @@ CartMate is a conversational support agent for NovaMart built with Streamlit and```bashgit clone https://github.com/Sumanth077/Hands-On-AI-Engineering.git-cd Hands-On-AI-Engineering/ai_agents/ai_agents/ai_customer_support_agent+cd Hands-On-AI-Engineering/ai_agents/ai_customer_support_agent```**2. Create and activate a virtual environment**@@ -69,7 +69,21 @@ pip install -r requirements.txtcp .env.example .env```-Open `.env` and fill in your API keys (see [Environment Variables](#environment-variables)).+Open `.env` and fill in your API keys.++## Environment Variables++Create a `.env` file in the project root with the following keys:++| Variable | Description | Where to get it |+|---|---|---|+| `MEM0_API_KEY` | Mem0 Cloud API key for persistent memory storage | [app.mem0.ai](https://app.mem0.ai) |+| `MISTRAL_API_KEY` | Mistral AI API key for LLM responses | [platform.mistral.ai](https://platform.mistral.ai) |++```env+MEM0_API_KEY=your_mem0_api_key_here+MISTRAL_API_KEY=your_mistral_api_key_here+```## Usage@@ -79,6 +93,8 @@ streamlit run app.pyOpen the URL printed in the terminal (default: `http://localhost:8501`).+When the app loads, enter your name to identify yourself; CartMate uses this name to store and retrieve your memories across sessions.+**Example interaction:**```text@@ -105,26 +121,16 @@ The **CartMate Memory** sidebar shows each stored fact as a bullet point, e.g.:• Alex prefers email updates over SMS.```-## Environment Variables--Create a `.env` file in the project root with the following keys:--| Variable | Description | Where to get it |-|---|---|---|-| `MEM0_API_KEY` | Mem0 Cloud API key for persistent memory storage | [app.mem0.ai](https://app.mem0.ai) |-| `MISTRAL_API_KEY` | Mistral AI API key for LLM responses | [platform.mistral.ai](https://platform.mistral.ai) |--```env-MEM0_API_KEY=your_mem0_api_key_here-MISTRAL_API_KEY=your_mistral_api_key_here-```-## Project Structure```text-ai-customer-support-agent/+ai_customer_support_agent/├── app.py # Streamlit app — UI, memory helpers, LLM response logic├── requirements.txt # Python dependencies├── .env # API keys (not committed)└── .env.example # Environment variable template```++---++[⬆ Back to Top](#cartmate-ai-customer-support-agent)diff --git a/ai_agents/ai_customer_support_agent/app.py b/ai_agents/ai_customer_support_agent/app.pyindex 36a8171..92aefc9 100644--- a/ai_agents/ai_customer_support_agent/app.py+++ b/ai_agents/ai_customer_support_agent/app.py@@ -1,3 +1,5 @@+"""Streamlit customer support chatbot for NovaMart, powered by Mem0 persistent memory and Mistral Small 4."""+import timeimport streamlit as stimport os@@ -24,11 +26,13 @@ st.set_page_config(# ---------------------------------------------------------------------------@st.cache_resource(show_spinner="Connecting to memory…")def init_memory() -> MemoryClient:+ """Create and cache the Mem0 MemoryClient singleton."""return MemoryClient(api_key=MEM0_API_KEY)@st.cache_resource(show_spinner="Loading AI model…")def init_llm() -> ChatMistralAI:+ """Create and cache the Mistral LLM singleton."""return ChatMistralAI(model=LLM_MODEL,mistral_api_key=MISTRAL_API_KEY,@@ -48,6 +52,7 @@ def _unwrap(results) -> list:def get_all_memories(user_id: str) -> list:+ """Return all stored memories for the given user, or an empty list on error."""try:return _unwrap(init_memory().get_all(filters={"user_id": user_id}))except Exception:@@ -55,6 +60,7 @@ def get_all_memories(user_id: str) -> list:def search_memories(query: str, user_id: str, limit: int = 5) -> list:+ """Search stored memories for the most relevant results matching the query."""try:return _unwrap(init_memory().search(query, filters={"user_id": user_id}, limit=limit))except Exception:@@ -62,6 +68,7 @@ def search_memories(query: str, user_id: str, limit: int = 5) -> list:def add_to_memory(user_msg: str, assistant_msg: str, user_id: str) -> None:+ """Save a user/assistant message pair to Mem0 and record the outcome in session state."""# Store outcome in session_state so it survives st.rerun() and can be# displayed in the sidebar on the next render pass.try:@@ -84,6 +91,7 @@ def add_to_memory(user_msg: str, assistant_msg: str, user_id: str) -> None:def memory_text(mem) -> str:+ """Extract the display text from a memory object returned by Mem0."""if isinstance(mem, dict):return mem.get("memory", mem.get("text", str(mem)))return str(mem)@@ -93,6 +101,7 @@ def memory_text(mem) -> str:# LLM response# ---------------------------------------------------------------------------def build_system_prompt(user_name: str, memories: list) -> str:+ """Build the CartMate system prompt with user context and relevant memories injected."""if memories:bullet_list = "\n".join(f"- {memory_text(m)}" for m in memories)memory_block = (@@ -129,6 +138,7 @@ def build_system_prompt(user_name: str, memories: list) -> str:def generate_response(user_name: str, user_msg: str, history: list, memories: list) -> str:+ """Invoke the LLM with the system prompt, recent history, and current message to produce a reply."""llm = init_llm()msgs = [SystemMessage(content=build_system_prompt(user_name, memories))]