You signed in with another tab or window. Reload to refresh your session.You signed out in another tab or window. Reload to refresh your session.You switched accounts on another tab or window. Reload to refresh your session.Dismiss alert
The **Daraz Insight Copilot** is built to be a **helpful, harmless, and honest** AI assistant dedicated exclusively to e-commerce analytics on the Daraz platform. We enforce strong Responsible AI principles through multiple technical and prompt-based guardrails.
118
+
119
+
## 1. Topic Restriction Guardrails
120
+
**Goal**: Prevent discussion of sensitive, political, illegal, or harmful topics unrelated to e-commerce.
121
+
122
+
- **Implementation**
123
+
The system prompt strictly defines the assistant’s persona:
124
+
> “You are an e-commerce analyst for Daraz. You only answer questions using the provided product reviews and data.”
125
+
126
+
- **Behavior**
127
+
Off-topic queries (e.g., “Who is the president?”, “How do I make a bomb?”, or any non-e-commerce request) trigger retrieval of irrelevant product reviews. With no relevant context available, the model is instructed to respond:
128
+
> “I cannot answer this question based on the available product data.”
129
+
130
+
## 2. Anti-Hallucination Measures
131
+
**Goal**: Eliminate invented products, fake reviews, or fabricated insights.
132
+
133
+
- **Implementation**
134
+
Full **Retrieval-Augmented Generation (RAG)** workflow powered by a FAISS vector index of real Daraz reviews.
135
+
136
+
- **Constraint**
137
+
The model is explicitly prohibited from using its pre-trained knowledge forproduct-related facts. Every factual claim **must** be groundedin retrieved chunks, ensuring 100% traceability to real data.
138
+
139
+
## 3. Tone & Style Guidelines
140
+
**Goal**: Maintain professional, objective, and business-appropriate communication.
141
+
142
+
- **Implementation**
143
+
Prompt instructions enforce:
144
+
> “Responses must be concise, data-driven, and professional.”
145
+
146
+
- **Result**
147
+
No slang, memes, overly casual language, or aggressive tone — ideal for analysts, merchants, and business users.
148
+
149
+
## 4. Bias Mitigation
150
+
**Goal**: Deliver fair and accurate sentiment analysis across diverse user expressions.
151
+
152
+
- **Dataset**
153
+
Includes mixed-language (English + Roman Urdu) reviews to properly capture local nuances and avoid penalizing non-native English speakers.
154
+
155
+
- **Transparency**
156
+
Every answer includes the exact review snippets used as sources, enabling users to verify that summaries faithfully reflect the underlying data.
157
+
158
+
By combining strict persona definition, RAG grounding, clear style rules, and transparent sourcing, Daraz Insight Copilot stays focused, truthful, and responsible at all times.
116
159
117
160
We implemented a custom **Policy Engine** (`src/app/guardrails.py`) that intercepts requests at two stages to ensure system safety and compliance.
118
161
@@ -194,9 +237,13 @@ We track operational metrics for the RAG pipeline using a Grafana dashboard.
194
237
* **Token Usage & Cost:** Tracks `llm_token_usage_total` to estimate API costs ($0.50/1M input, $1.50/1M output).
195
238
* **RAG Latency:** Monitors the P95 and P99 latency of the `/ask` endpoint to ensure responsiveness.
196
239
* **Safety Violations:** Logs `guardrail_events_total` to track attempted attacks (Injection/PII).
@@ -208,46 +255,161 @@ We monitor the integrity of our retrieval corpus and tabular data using **Eviden
208
255
209
256
## Cloud Deployment
210
257
211
-
This project is deployed and hosted on **Amazon Web Services (AWS)** using three distinct services: **EC2**, **S3**, and **CloudWatch**, fulfilling the D9 requirement.
258
+
This project is deployed and hosted on **Amazon Web Services (AWS)**
259
+
using three key services: **EC2**, **S3**, and **CloudWatch**,
The machine learning model is trained locally using:
273
+
274
+
``` bash
275
+
python train.py
276
+
```
277
+
278
+
This produces the following artifacts:
279
+
280
+
- `model.joblib`
281
+
- `faiss_index/` folder
282
+
- `my_model/` folder (Sentence Transformer model)
283
+
284
+
### **2. Artifact Storage (S3)**
285
+
286
+
Due to GitHub file-size limits and Docker build timeouts, heavy
287
+
artifacts are stored in **Amazon S3**.
288
+
289
+
Artifacts stored:
290
+
291
+
- `daraz-code-mixed-product-reviews.csv`
292
+
- `faiss_index/` (Vector Database)
293
+
- `my_model/` (Sentence Transformer)
294
+
295
+
<img src="assets/S3_bucket.png" alt="S3_bucket after milestone 2" width="500">
212
296
213
-
### How the ML Workflow Interacts with AWS
297
+
### **3. Inference (EC2)**
214
298
215
-
1. **Training (Local):** The model is trained locally using `python train.py`. This script also generates the `model.joblib` artifact.
216
-
2. **Data/Model Storage (S3):** The `Top_Selling_Product_Data.csv` dataset and the final `model.joblib` artifact are manually uploaded to an **S3 bucket** for persistent, durable storage.
217
-
3. **Inference (EC2):** An **EC2 instance** runs our Docker container. The container (built by the CI/CD pipeline) pulls the code, and when the API starts, it loads the model from its local `models/` directory (which was part of the build).
218
-
4. **Monitoring (CloudWatch):** **CloudWatch** automatically monitors the EC2 instance (CPU, Network, etc.) to ensure the health of our inference API server.
299
+
An **EC2 t3.micro** instance hosts the live FastAPI application.
219
300
220
-
### Services Used
301
+
Instead of downloading models inside Docker (which causes timeouts), the
302
+
instance downloads artifacts from S3 **at the host level**, and they are
303
+
mounted into the container.
221
304
222
-
* **S3 (Simple Storage Service):** Used for persistent data storage.
223
-
* **Why:** S3 is a highly durable and scalable service perfect for storing project artifacts.
224
-
* **How:** The `Top_Selling_Product_Data.csv` dataset and the trained `model.joblib` are stored in an S3 bucket.
*<img src="assets/Annonated_API_running_on_AWS.png" alt="API running on AWS" width="500">
307
+
### **4. Monitoring (CloudWatch)**
232
308
233
-
***CloudWatch:** Used for basic infrastructure monitoring.
234
-
***Why:** CloudWatch is automatically integrated with EC2 and provides essential metrics (CPU, Network, Disk) to monitor the health and performance of our API server.
This application processes user input through Large Language Models (LLMs). To mitigate **Prompt Injection** attacks (where malicious input attempts to override system instructions), we have implemented multiple layers of defense:
6
+
7
+
1.**System Prompt Encapsulation**
8
+
- All user input is strictly encapsulated using clear delimiters (e.g., `"""User Query"""`) before being passed to the LLM.
9
+
- The system prompt explicitly instructs the model to respond **only** based on the provided retrieved context and Daraz product information.
10
+
11
+
2.**Context Grounding (RAG-based)**
12
+
- The model is forbidden from hallucinating or using external knowledge.
13
+
- Responses must cite specific reviews or product data retrieved from the `faiss_index`.
14
+
- If the retrieved context is irrelevant or insufficient, the model falls back to a safe response such as “I cannot answer that.”
15
+
16
+
3.**Input Sanitization**
17
+
- All incoming API requests are validated using Pydantic models to enforce correct data types, formats, and length limits, preventing injection via malformed payloads.
18
+
19
+
## Data Privacy & Handling
20
+
21
+
We prioritize user privacy and minimize data retention:
22
+
23
+
1.**No Permanent Storage of Queries**
24
+
- Queries sent to the `/ask` endpoint are processed entirely in memory.
25
+
- User queries are **never** persisted to databases, S3 buckets, or permanent logs. Server logs are ephemeral and automatically rotated.
26
+
27
+
2.**PII Redaction**
28
+
- Current dataset (`reviews.csv`) contains only public product reviews; any potential PII in the source data is considered public domain.
29
+
-**Planned**: Automated detection and redaction of PII in incoming user queries in future releases.
30
+
31
+
3.**API Key Security**
32
+
- LLM provider API keys (Groq, Gemini, etc.) are injected exclusively as environment variables at runtime.
33
+
- Keys are **never** hardcoded or committed to the repository.
34
+
35
+
## Reporting Security Vulnerabilities
36
+
37
+
If you discover a security vulnerability, a bypass of the implemented guardrails, or any other security issue:
38
+
39
+
**Please do not open a public GitHub issue.**
40
+
41
+
Instead, report it privately by emailing the repository maintainer directly. Responsible disclosures will be acknowledged and addressed promptly.
0 commit comments