Spaces:
Running
Running
talexm
commited on
Commit
•
eb579c5
1
Parent(s):
e4a2031
update
Browse files- rag_sec/README.md +42 -36
rag_sec/README.md
CHANGED
@@ -1,51 +1,57 @@
|
|
1 |
-
|
2 |
|
3 |
-
|
4 |
|
5 |
-
|
6 |
-
|
7 |
-
## Features
|
8 |
-
|
9 |
-
1. **Semantic Search**:
|
10 |
-
- Uses SentenceTransformer embeddings for document similarity.
|
11 |
-
- Retrieves top-k relevant documents for a given query.
|
12 |
-
|
13 |
-
2. **Malicious Query Detection**:
|
14 |
-
- Identifies and blocks malicious or harmful queries using sentiment analysis.
|
15 |
|
16 |
-
|
17 |
-
- Rephrases or enhances ambiguous queries for better processing.
|
18 |
-
|
19 |
-
4. **Generative Response**:
|
20 |
-
- Generates a context-aware response using Hugging Face models like `distilgpt2`.
|
21 |
|
22 |
-
|
23 |
-
|
24 |
-
|
|
|
|
|
|
|
25 |
|
26 |
---
|
27 |
|
28 |
-
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
29 |
|
30 |
-
|
31 |
-
- Detects malicious or inappropriate queries using sentiment analysis (`distilbert-base-uncased-finetuned-sst-2-english`).
|
32 |
|
33 |
-
|
34 |
-
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
35 |
|
36 |
-
|
37 |
-
- Encodes documents into dense vectors using `all-MiniLM-L6-v2` embeddings.
|
38 |
-
- Finds similar documents using cosine similarity.
|
39 |
|
40 |
-
|
41 |
-
|
|
|
|
|
|
|
|
|
42 |
|
43 |
---
|
44 |
|
45 |
-
|
46 |
|
47 |
-
|
48 |
-
|
49 |
-
|
50 |
-
|
51 |
-
```
|
|
|
1 |
+
## Workflow
|
2 |
|
3 |
+
The system follows a well-structured workflow to ensure accurate, secure, and context-aware responses to user queries:
|
4 |
|
5 |
+
### 1. **Input Query**
|
6 |
+
- A user provides a query that can be a general question, ambiguous statement, or potentially malicious intent.
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
7 |
|
8 |
+
---
|
|
|
|
|
|
|
|
|
9 |
|
10 |
+
### 2. **Detection Module**
|
11 |
+
- **Purpose**: Classify the query as "bad" or "good."
|
12 |
+
- **Steps**:
|
13 |
+
1. Use a sentiment analysis model (`distilbert-base-uncased-finetuned-sst-2-english`) to detect malicious or inappropriate intent.
|
14 |
+
2. If the query is classified as "bad" (e.g., SQL injection or inappropriate tone), block further processing and provide a warning message.
|
15 |
+
3. If "good," proceed to the **Transformation Module**.
|
16 |
|
17 |
---
|
18 |
|
19 |
+
### 3. **Transformation Module**
|
20 |
+
- **Purpose**: Rephrase or enhance ambiguous or poorly structured queries for better retrieval.
|
21 |
+
- **Steps**:
|
22 |
+
1. Identify missing context or ambiguous phrasing.
|
23 |
+
2. Transform the query using:
|
24 |
+
- Rule-based transformations for simple fixes.
|
25 |
+
- Text-to-text models (e.g., `google/flan-t5-small`) for more sophisticated rephrasing.
|
26 |
+
3. Pass the transformed query to the **RAG Pipeline**.
|
27 |
|
28 |
+
---
|
|
|
29 |
|
30 |
+
### 4. **RAG Pipeline**
|
31 |
+
- **Purpose**: Retrieve relevant data and generate a context-aware response.
|
32 |
+
- **Steps**:
|
33 |
+
1. **Document Retrieval**:
|
34 |
+
- Encode the transformed query and documents into embeddings using `all-MiniLM-L6-v2`.
|
35 |
+
- Compute semantic similarity between the query and stored documents.
|
36 |
+
- Retrieve the top-k documents relevant to the query.
|
37 |
+
2. **Response Generation**:
|
38 |
+
- Use the retrieved documents as context.
|
39 |
+
- Pass the query and context to a generative model (e.g., `distilgpt2`) to synthesize a meaningful response.
|
40 |
|
41 |
+
---
|
|
|
|
|
42 |
|
43 |
+
### 5. **Semantic Response Generation**
|
44 |
+
- **Purpose**: Provide a concise and meaningful answer.
|
45 |
+
- **Steps**:
|
46 |
+
1. Combine the retrieved documents into a coherent context.
|
47 |
+
2. Generate a response tailored to the query using the generative model.
|
48 |
+
3. Return the response to the user, ensuring clarity and relevance.
|
49 |
|
50 |
---
|
51 |
|
52 |
+
### End-to-End Example
|
53 |
|
54 |
+
#### Input Query:
|
55 |
+
```plaintext
|
56 |
+
"How to improve acting skills?"
|
57 |
+
````
|
|