MCP-1st-Birthday/commerce-shopping-agent
Commerce Shopping Agent
AI-powered shopping agent with input/output guardrails for safe interactions.
Features
- ๐ Natural language product search
- ๐ฐ Multi-retailer price comparison
- ๐ Detailed product information
- ๐ก๏ธ Input/Output Guardrails using GuardrailsAI framework
- ๐ค Powered by LangGraph + MCP
Guardrails
This agent includes comprehensive guardrails for both input and output:
Input Guardrails
- Toxic Language Detection: Filters inappropriate language
- PII Detection: Detects and redacts personally identifiable information
- Jailbreak Detection: Prevents prompt injection attacks
- Topic Restriction: Ensures conversations stay on-topic (shopping, products, e-commerce)
- Competitor Check: Optional detection of competitor mentions
Output Guardrails
- Toxic Language Detection: Ensures responses are appropriate
- PII Redaction: Removes sensitive information from responses
- Topic Compliance: Ensures responses stay relevant
- Reading Time Check: Prevents overly long responses
Setup
- Install dependencies:
pip install -r requirements.txt- Install GuardrailsAI validators (if needed):
guardrails hub install hub://guardrails/toxic_language
guardrails hub install hub://guardrails/detect_pii
guardrails hub install hub://guardrails/detect_jailbreak
guardrails hub install hub://guardrails/restrict_to_topic- Configure environment variables (see
.env.example)
- Run the application:
python app.pyConfiguration
Environment Variables
ChromaDB Cloud (Optional):
CHROMA_API_KEY: Your ChromaDB Cloud API keyCHROMA_TENANT: Your ChromaDB Cloud tenant IDCHROMA_DATABASE: Your ChromaDB Cloud database name
If these are set, the app will use ChromaDB Cloud for persistent memory storage. Otherwise, it uses local storage.
HuggingFace OAuth (for HuggingFace Spaces):
- OAuth works automatically in HuggingFace Spaces
- For local development, you may need to set
HF_TOKENor runhuggingface-cli login
Guardrails: Guardrails can be configured in src/guardrails.py:
- Enable/disable input/output guardrails
- Customize topic restrictions
- Configure PII entities to detect
- Set competitor lists
Testing
The project includes comprehensive tests for guardrails functionality.
Install Test Dependencies
pip install -e ".[dev]"
# or
pip install pytest pytest-asyncio pytest-mockRun Tests
Run all tests:
pytestRun with verbose output:
pytest -vRun only guardrails tests:
pytest tests/test_guardrails.pyRun memory tests:
pytest tests/test_memory.pyRun memory example script (standalone):
python -m pytest tests/test_memory.py::test_memory_script_example -v -s
# or run directly:
python tests/test_memory.pyRun integration tests:
pytest -m integrationRun specific test class:
pytest tests/test_guardrails.py::TestInputValidation
pytest tests/test_memory.py::TestMemoryManagerTest Coverage
The test suite includes:
- โ Input validation tests (valid/invalid inputs, PII handling, edge cases)
- โ Output validation tests (valid/invalid outputs, PII redaction)
- โ Configuration tests (enabled/disabled guardrails, custom settings)
- โ Error handling tests (exception handling, fallback responses)
- โ Integration tests (app and shopping agent integration)
- โ Edge case tests (empty strings, unicode, special characters)
- โ Mock tests (guardrails-ai unavailable scenarios)
- โ Memory tests (storage, retrieval, semantic search, filtering)
- โ Memory integration tests (agent memory capture and retrieval)
Evaluations
The project includes two evaluation frameworks to measure agent performance:
1. LangSmith Evaluations
Custom evaluators with LangSmith integration for tracking and analysis.
Setup:
export LANGSMITH_API_KEY=your_api_key_here
export LANGSMITH_PROJECT=commerce-shopping-agent-evalsRun:
python -m evals.evaluate_agentMetrics:
- Relevance, Completeness, Correctness, Safety
2. Ragas Evaluations
Specialized agent metrics using the Ragas framework for AI agent evaluation.
Setup:
export OPENAI_API_KEY=your_openai_key # Required for evaluator LLMRun:
python -m evals.evaluate_agent_ragasMetrics:
- Agent Goal Accuracy: Measures if the agent achieved the user's goal
- Topic Adherence: Ensures the agent stays on topic
- Tool Call Accuracy: Validates correct tool usage
- Tool Call F1: Measures precision and recall of tool calls
Ragas provides LLM-as-a-judge metrics specifically designed for evaluating AI agents with tool use capabilities.
See evals/README.md for detailed documentation on both evaluation frameworks.
Check out the configuration reference at https://huggingface.co/docs/hub/spaces-config-reference
