A hierarchical multi-agent system that orchestrates specialized AI workers to perform real-time technical analysis and market research for the Colombo Stock Exchange (CSE).
Unlike standard chatbots that use a single shared context, CSE Smart Scout utilizes a Hierarchical Supervisor Architecture. A top-level "Supervisor" agent acts as a router, delegating tasks to specialized worker agents and aggregating their outputs.
graph TD
User(User Query) --> Supervisor{Supervisor Agent}
Supervisor -->|Quant Task| Analyst[📉 Technical Analyst]
Supervisor -->|News Task| Researcher[📰 Market Researcher]
Analyst -->|Fetch Data| Tools[🛠️ CSE & Calc Tools]
Researcher -->|Web Search| Tools
Tools --> Analyst
Tools --> Researcher
Analyst -->|Summary| Supervisor
Researcher -->|Summary| Supervisor
Supervisor -->|Final Answer| Guardrail[🛡️ Compliance Node]
Guardrail --> Output(Final Response)
The Sri Lankan financial market suffers from a Data Accessibility Gap:
- No Public API: The CSE does not offer a modern public API for developers.
- Context Blindness: Generic LLMs (like ChatGPT) hallucinate when asked about local stocks, often confusing "Dialog Axiata" with "Dialog Semiconductor."
- Regulatory Risk: Standard AI agents lack "Guardrails," often providing hallucinated financial advice, which is a major compliance risk in Fintech.
CSE Smart Scout is not just a chatbot; it is an Orchestrated Agent System.
- Specialized Workers: Separates "Quantitative Analysis" (RSI, MACD) from "Qualitative Research" (News, Sentiment) to prevent context pollution.
- Stateful Orchestration: Uses LangGraph to maintain conversation state, allowing the Supervisor to loop through workers until the answer is complete.
- Compliance First: Includes a dedicated post-processing guardrail node that intercepts and sanitizes financial advice before it reaches the user.
- The Failure: Standard libraries like yfinance return incomplete or delisted data for the CSE.
- The Solution: I performed Network Traffic Analysis on the official cse.lk website. I identified their internal private API endpoints, reverse-engineered the payload structure, and wrote a custom Python wrapper (
cse_tools.py) that mimics a legitimate browser session (Stealth Headers) to fetch tick-by-tick data directly from the source.
- The Failure: During testing, the Llama-3 model persistently tried to call a non-existent tool named
brave_searchinstead of my custom search tool, causing validation crashes. This was due to the model's training bias. - The Solution: I implemented a Namespace Mapping Strategy. Instead of fighting the model with prompts, I renamed my internal tool to
web_searchand bound it strictly within the Pydantic schema. This aligned the code with the model's internal "intuition," eliminating the hallucination immediately without degrading performance.
- The Failure: The Supervisor agent would occasionally get stuck in a loop, endlessly asking the Analyst for the same data.
- The Solution: I engineered a "Worker-Loop" Flow with strict exit conditions:
- Self-Summarization: Workers are forced to summarize their own tool outputs into English before passing control back.
- Pydantic Validation: The Supervisor's routing logic is constrained by a strict
RouteResponseschema, forcing it to choose betweenTechnical_Analyst,Market_Researcher, orFINISH. - Recursion Limits: Configured the graph runtime to handle extended reasoning chains (up to 50 steps) for complex comparative queries.
| Component | Technology | Reasoning |
|---|---|---|
| Reasoning Orchestration | LangGraph | Enables cyclic, stateful multi-agent workflows (Supervisor-Worker pattern). |
| Inference | Groq LPU | Near-instant inference speed (~300 tokens/sec) for real-time agent routing. |
| Model | Llama-3.3-70b | Chosen for its superior instruction-following capability in complex routing tasks. |
| Validation | Pydantic | Enforces structured output for the Supervisor, preventing routing errors. |
| Search | Tavily API | Optimized specifically for RAG/Agents to reduce noise in financial news search. |
- Python 3.10+
- API Keys for Groq and Tavily
-
Clone the repo:
git clone https://github.com/yourusername/cse-smart-scout.git cd cse-smart-scout -
Install dependencies:
pip install -r requirements.txt
-
Configure Environment:
Create a.envfile in the root directory:GROQ_API_KEY=gsk_... TAVILY_API_KEY=tvly-... -
Run the App:
streamlit run app.py
-
Glass-Box Reasoning: Users can expand the "Agent Logic" tab to see the live thought process: Supervisor routing, Tool execution, and raw JSON data.
-
Compliance Guardrails: The system automatically detects risky keywords ("buy", "guarantee", "profit") and appends a regulatory disclaimer to the final output.
-
Multi-Stock Comparison: The Supervisor autonomously parallelizes tasks, fetching data for multiple tickers (e.g., "Compare JKH and DIAL") in a single workflow.
-
Small Language Model (SLM) Optimization: Quantize the worker agents to run on edge devices (Llama-3-8B) while keeping the Supervisor on the cloud.
-
RAG Integration: Connect to a vector database of CSE Annual Reports for "Deep Dive" fundamental analysis.
-
WhatsApp Bot: Deploy the
agent.pylogic via Twilio for mobile access.
