Spaces:
Running
Running
Update
Browse files- rag_pipeline.py +134 -132
rag_pipeline.py
CHANGED
|
@@ -1,132 +1,134 @@
|
|
| 1 |
-
|
| 2 |
-
|
| 3 |
-
|
| 4 |
-
|
| 5 |
-
|
| 6 |
-
|
| 7 |
-
|
| 8 |
-
|
| 9 |
-
#
|
| 10 |
-
|
| 11 |
-
|
| 12 |
-
|
| 13 |
-
|
| 14 |
-
|
| 15 |
-
|
| 16 |
-
|
| 17 |
-
|
| 18 |
-
|
| 19 |
-
|
| 20 |
-
|
| 21 |
-
|
| 22 |
-
|
| 23 |
-
|
| 24 |
-
df =
|
| 25 |
-
|
| 26 |
-
#
|
| 27 |
-
df
|
| 28 |
-
|
| 29 |
-
|
| 30 |
-
|
| 31 |
-
|
| 32 |
-
|
| 33 |
-
|
| 34 |
-
|
| 35 |
-
|
| 36 |
-
|
| 37 |
-
|
| 38 |
-
|
| 39 |
-
|
| 40 |
-
|
| 41 |
-
|
| 42 |
-
|
| 43 |
-
|
| 44 |
-
|
| 45 |
-
|
| 46 |
-
|
| 47 |
-
|
| 48 |
-
|
| 49 |
-
|
| 50 |
-
|
| 51 |
-
|
| 52 |
-
|
| 53 |
-
|
| 54 |
-
|
| 55 |
-
|
| 56 |
-
|
| 57 |
-
|
| 58 |
-
|
| 59 |
-
|
| 60 |
-
|
| 61 |
-
|
| 62 |
-
|
| 63 |
-
|
| 64 |
-
|
| 65 |
-
|
| 66 |
-
|
| 67 |
-
|
| 68 |
-
|
| 69 |
-
|
| 70 |
-
|
| 71 |
-
|
| 72 |
-
|
| 73 |
-
|
| 74 |
-
|
| 75 |
-
|
| 76 |
-
|
| 77 |
-
|
| 78 |
-
|
| 79 |
-
|
| 80 |
-
|
| 81 |
-
|
| 82 |
-
|
| 83 |
-
|
| 84 |
-
|
| 85 |
-
|
| 86 |
-
|
| 87 |
-
|
| 88 |
-
|
| 89 |
-
|
| 90 |
-
|
| 91 |
-
|
| 92 |
-
"""
|
| 93 |
-
|
| 94 |
-
|
| 95 |
-
|
| 96 |
-
|
| 97 |
-
|
| 98 |
-
|
| 99 |
-
|
| 100 |
-
|
| 101 |
-
|
| 102 |
-
|
| 103 |
-
|
| 104 |
-
|
| 105 |
-
|
| 106 |
-
|
| 107 |
-
|
| 108 |
-
|
| 109 |
-
|
| 110 |
-
|
| 111 |
-
|
| 112 |
-
|
| 113 |
-
|
| 114 |
-
|
| 115 |
-
|
| 116 |
-
|
| 117 |
-
|
| 118 |
-
|
| 119 |
-
|
| 120 |
-
|
| 121 |
-
|
| 122 |
-
|
| 123 |
-
|
| 124 |
-
|
| 125 |
-
|
| 126 |
-
|
| 127 |
-
|
| 128 |
-
|
| 129 |
-
|
| 130 |
-
|
| 131 |
-
|
| 132 |
-
|
|
|
|
|
|
|
|
|
| 1 |
+
from datasets import load_dataset
|
| 2 |
+
from sentence_transformers import SentenceTransformer
|
| 3 |
+
import chromadb
|
| 4 |
+
import pandas as pd
|
| 5 |
+
|
| 6 |
+
# CONFIG
|
| 7 |
+
|
| 8 |
+
HF_DATASET_NAME = "dralsarrani/prompt_safety_with_synthetic_labeled"
|
| 9 |
+
EMBEDDING_MODEL = "all-MiniLM-L6-v2" # fast, free, good enough
|
| 10 |
+
CHROMA_DIR = "./chroma_db" # local folder, created automatically
|
| 11 |
+
COLLECTION_NAME = "safety_prompts"
|
| 12 |
+
TOP_K = 5 # how many similar prompts to retrieve
|
| 13 |
+
|
| 14 |
+
|
| 15 |
+
|
| 16 |
+
# 1 LOAD DATASET
|
| 17 |
+
|
| 18 |
+
def load_safety_dataset():
|
| 19 |
+
print("Loading dataset from HuggingFace...")
|
| 20 |
+
dataset = load_dataset(HF_DATASET_NAME, cache_dir="./hf_cache")
|
| 21 |
+
df = dataset["train"].to_pandas()
|
| 22 |
+
|
| 23 |
+
# Normalise column names to lowercase
|
| 24 |
+
df.columns = [c.lower().strip() for c in df.columns]
|
| 25 |
+
|
| 26 |
+
# Keep only rows with valid prompt + label
|
| 27 |
+
df = df.dropna(subset=["text", "label"])
|
| 28 |
+
df = df[df["label"].isin(["safe", "unsafe"])]
|
| 29 |
+
df = df.reset_index(drop=True)
|
| 30 |
+
|
| 31 |
+
# cap at 50K, balanced between safe/unsafe
|
| 32 |
+
|
| 33 |
+
df = df.groupby("label", group_keys=False).apply(
|
| 34 |
+
lambda x: x.sample(min(len(x), 25_000), random_state=42)
|
| 35 |
+
).reset_index(drop=True)
|
| 36 |
+
|
| 37 |
+
print(f" Loaded {len(df)} rows | SAFE: {(df.label==0).sum()} UNSAFE: {(df.label==1).sum()}")
|
| 38 |
+
return df
|
| 39 |
+
|
| 40 |
+
|
| 41 |
+
# 2 BUILD CHROMA VECTOR STORE
|
| 42 |
+
|
| 43 |
+
def build_vector_store(df: pd.DataFrame):
|
| 44 |
+
print("Building vector store...")
|
| 45 |
+
model = SentenceTransformer(EMBEDDING_MODEL)
|
| 46 |
+
client = chromadb.PersistentClient(path=CHROMA_DIR)
|
| 47 |
+
|
| 48 |
+
# Check if already built β skip if so
|
| 49 |
+
try:
|
| 50 |
+
collection = client.get_collection(COLLECTION_NAME)
|
| 51 |
+
if collection.count() > 0:
|
| 52 |
+
print(f" Vector store already exists ({collection.count()} vectors). Skipping rebuild.")
|
| 53 |
+
return collection, model
|
| 54 |
+
except Exception:
|
| 55 |
+
pass
|
| 56 |
+
|
| 57 |
+
collection = client.create_collection(COLLECTION_NAME)
|
| 58 |
+
|
| 59 |
+
prompts = df["text"].tolist()
|
| 60 |
+
labels = df["label"].tolist()
|
| 61 |
+
ids = [str(i) for i in range(len(prompts))]
|
| 62 |
+
|
| 63 |
+
# Embed in batches of 512 to avoid memory issues on large datasets
|
| 64 |
+
batch_size = 512
|
| 65 |
+
all_embeddings = []
|
| 66 |
+
for i in range(0, len(prompts), batch_size):
|
| 67 |
+
batch = prompts[i : i + batch_size]
|
| 68 |
+
embeddings = model.encode(batch, show_progress_bar=False).tolist()
|
| 69 |
+
all_embeddings.extend(embeddings)
|
| 70 |
+
print(f" Embedded {min(i + batch_size, len(prompts))}/{len(prompts)}")
|
| 71 |
+
|
| 72 |
+
batch_size_chroma = 5000
|
| 73 |
+
for i in range(0, len(ids), batch_size_chroma):
|
| 74 |
+
batch_ids = ids[i : i + batch_size_chroma]
|
| 75 |
+
batch_embeds = all_embeddings[i : i + batch_size_chroma]
|
| 76 |
+
batch_docs = prompts[i : i + batch_size_chroma]
|
| 77 |
+
batch_metadatas = [{"label": l} for l in labels[i : i + batch_size_chroma]]
|
| 78 |
+
collection.add(
|
| 79 |
+
ids=batch_ids,
|
| 80 |
+
embeddings=batch_embeds,
|
| 81 |
+
documents=batch_docs,
|
| 82 |
+
metadatas=batch_metadatas
|
| 83 |
+
)
|
| 84 |
+
|
| 85 |
+
|
| 86 |
+
print(f" Stored {collection.count()} vectors in Chroma")
|
| 87 |
+
return collection, model
|
| 88 |
+
|
| 89 |
+
# 3 RETRIEVAL FUNCTION
|
| 90 |
+
|
| 91 |
+
def retrieve_similar(query: str, collection, model, top_k: int = TOP_K):
|
| 92 |
+
"""
|
| 93 |
+
Given a new prompt, return the top_k most similar prompts
|
| 94 |
+
from the dataset with their labels and similarity scores.
|
| 95 |
+
"""
|
| 96 |
+
query_embedding = model.encode([query]).tolist()
|
| 97 |
+
|
| 98 |
+
results = collection.query(
|
| 99 |
+
query_embeddings = query_embedding,
|
| 100 |
+
n_results = top_k,
|
| 101 |
+
include = ["documents", "metadatas", "distances"],
|
| 102 |
+
)
|
| 103 |
+
|
| 104 |
+
similar = []
|
| 105 |
+
for doc, meta, dist in zip(
|
| 106 |
+
results["documents"][0],
|
| 107 |
+
results["metadatas"][0],
|
| 108 |
+
results["distances"][0],
|
| 109 |
+
):
|
| 110 |
+
similar.append({
|
| 111 |
+
"prompt": doc,
|
| 112 |
+
"label": meta["label"],
|
| 113 |
+
"similarity": round(1 - dist, 3), # cosine distance β similarity
|
| 114 |
+
})
|
| 115 |
+
|
| 116 |
+
return similar
|
| 117 |
+
|
| 118 |
+
|
| 119 |
+
# 4 LOAD EXISTING STORE (skip rebuild if already done)
|
| 120 |
+
|
| 121 |
+
def load_vector_store():
|
| 122 |
+
"""Load an already-built Chroma store without re-embedding."""
|
| 123 |
+
model = SentenceTransformer(EMBEDDING_MODEL)
|
| 124 |
+
client = chromadb.PersistentClient(path=CHROMA_DIR)
|
| 125 |
+
|
| 126 |
+
try:
|
| 127 |
+
collection = client.get_collection(COLLECTION_NAME)
|
| 128 |
+
print(f"Loaded existing vector store ({collection.count()} vectors)")
|
| 129 |
+
except Exception:
|
| 130 |
+
print("No existing vector store found β building from scratch...")
|
| 131 |
+
df = load_safety_dataset()
|
| 132 |
+
collection, model = build_vector_store(df)
|
| 133 |
+
|
| 134 |
+
return collection, model
|