-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathsetup_memory.py
More file actions
55 lines (46 loc) · 1.71 KB
/
Copy pathsetup_memory.py
File metadata and controls
55 lines (46 loc) · 1.71 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
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
import os
import pymongo
from dotenv import load_dotenv
from sentence_transformers import SentenceTransformer
from datasets import load_dataset
# 1. Load Secrets
load_dotenv()
MONGO_URI = os.getenv("MONGO_URI")
if not MONGO_URI: raise ValueError("MONGO_URI not found in .env file. Please create one!")
# 2. Settings
DB_NAME = "rag"
COLLECTION_NAME = "knowledgebase"
DATASET_ID = "FreedomIntelligence/medical-o1-reasoning-SFT"
# 3. Connect to DB
print("Connecting to MongoDB...")
client = pymongo.MongoClient(MONGO_URI)
db = client[DB_NAME]
collection = db[COLLECTION_NAME]
# Check if data exists
#if collection.count_documents({}) > 0:
# print("Database already contains data! Skipping upload.")
# exit()
# 4. Load Embedding Model
print("Loading embedding model...")
embed_model = SentenceTransformer('sentence-transformers/all-MiniLM-L6-v2')
# 5. Load Data & Upload
print(f"Downloading dataset {DATASET_ID}...")
dataset = load_dataset(DATASET_ID, 'en', split="train")
# Select first 1000 for speed
batch_data = dataset.select(range(19704))
documents = []
print("Processing and uploading...")
for i, entry in enumerate(batch_data):
q = entry.get('Question', entry.get('question', ''))
a = entry.get('Response', entry.get('answer', ''))
text_content = f"Question: {q}\nAnswer: {a}"
doc = {
"text": text_content,
"embedding": embed_model.encode(text_content).tolist(),
"source": "medical_sft"
}
documents.append(doc)
if i % 100 == 0: print(f"Processed {i}...", end="\r")
collection.insert_many(documents)
print(f"\nSuccessfully uploaded {len(documents)} medical records to MongoDB!")
print("REMINDER: Ensure your Vector Search Index 'vector_index' is created in MongoDB Atlas.")