A RAG-powered conversational assistant built with Streamlit and OpenRouter to assist importers, exporters, and customs brokers navigating the US-Mexico (USMCA / T-MEC) trade corridor and Mexican customs legislation.
👉 Try the Live App: Asesor AI de Comercio Exterior y Aduanas
- Dynamic In-Memory RAG Pipeline: Ingests legal & trade documentation from
./pdf_files_comercio_exteriorusingPyPDFDirectoryLoader, applies domain-specific legal text normalization (expanding abbreviations like Art., Fracc., L.A., CFF), and adaptively splits text into chunks viaRecursiveCharacterTextSplittercalculated from total document volume. - Smart Context Selection: Ranks document chunks by keyword overlap and length scaling metrics (
calculate_chunk_relevance) to dynamically select diverse source chunks within a 5,000-token context boundary usingtiktoken. - Streaming Multi-Model LLM Engine with Fallback: Integrates with OpenRouter API via a streaming fallback architecture (
generate_completion_with_fallback). Automatically cycles through active free endpoints (google/gemma-4-31b:free,google/gemma-4-26b-a4b:free,minimax/minimax-m3:free,thinking-machines/inkling-small:free,nvidia/nemotron-3-nano-omni:free,cohere/north-mini-code:free,openrouter/free-models-router) with a 5,000-token generation ceiling. - Risk & Compliance Focus: Generates structured technical compliance reports with specific legal citations (Ley Aduanera, Anexo 22, CFF, T-MEC), tax breakdowns (IGE, IVA, DTA), mandatory legal disclaimers (
## Aviso Legal (Disclaimer)), and a required RAID (Risks, Actions, Issues, Decisions) matrix table. - Contextual Greeting & Intent Handler: Detects Spanish conversational greetings without triggering unnecessary LLM token consumption. Returns time-aware responses (Buenos días / Tardes / Noches) and displays domain guidance when no technical query is appended.
- Secure Key Management: Uses native Streamlit Secrets (
st.secrets["OPENROUTER_API_KEY"]) as the primary credential source with fallback to environment variables.
- Frontend / UI: Streamlit (
>=1.30.0) - LLM Engine: OpenRouter API via standard
openaiSDK (openai>=1.0.0) - Document Processing:
langchain-community,langchain-text-splitters,pypdf - Token Encoding & Utilities:
tiktoken
git clone https://github.com/robert0777/mexican-trade-chatbot.git
cd mexican-trade-chatbotIn your deployed app dashboard on Streamlit Cloud, navigate to Settings -> Secrets, and store your OpenRouter API key:
OPENROUTER_API_KEY = "your_openrouter_api_key_here"Place official Mexican Customs & Trade Compliance documentation PDF files into:
./pdf_files_comercio_exterior/
Point Streamlit Cloud deployment to:
app.py
streamlit>=1.30.0
langchain-core>=0.1.0
langchain-community>=0.0.20
langchain-nvidia-ai-endpoints>=0.1.0
langgraph>=0.0.20
openai>=1.0.0
tiktoken>=0.5.0
pypdf>=3.0.0
reportlab>=4.0.0
faiss-cpu>=1.7.4.
├── app.py # Main Streamlit application file
├── pdf_files_comercio_exterior/ # Directory containing legal & trade PDF files
├── ai-advisor-icon.svg # Application branding icon
├── requirements.txt # Python package dependencies
└── README.md # Project documentation
Dr. Robert Hernández Martínez
Consultant in Actuarial Science, Finance, Risk Modeling, and Applied AI