-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathcreate_embeddings.py
More file actions
290 lines (232 loc) · 10.7 KB
/
create_embeddings.py
File metadata and controls
290 lines (232 loc) · 10.7 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
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
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
#!/usr/bin/env python3
"""
Simple Wikipedia Embedding Creator
Creates embeddings for Simple Wikipedia articles and stores them in ChromaDB.
Uses sentence-transformers/all-mpnet-base-v2 for high-quality embeddings.
"""
import os
import json
import re
from typing import List, Dict, Any
from datetime import datetime
import chromadb
from sentence_transformers import SentenceTransformer
from datasets import load_dataset
from tqdm import tqdm
class WikipediaEmbeddingCreator:
def __init__(self,
embedding_model: str = "sentence-transformers/all-mpnet-base-v2",
chunk_size: int = 512,
overlap: int = 50,
batch_size: int = 100):
"""
Initialize the embedding creator.
Args:
embedding_model: Name of the sentence transformer model
chunk_size: Maximum tokens per chunk
overlap: Number of tokens to overlap between chunks
batch_size: Number of articles to process at once
"""
self.embedding_model_name = embedding_model
self.chunk_size = chunk_size
self.overlap = overlap
self.batch_size = batch_size
# Initialize components
self.model = None
self.chroma_client = None
self.collection = None
def setup_model(self):
"""Load the sentence transformer model."""
print(f"Loading embedding model: {self.embedding_model_name}")
print("This may take a moment on first run...")
self.model = SentenceTransformer(self.embedding_model_name)
print("✅ Model loaded successfully!")
def setup_chromadb(self, collection_name: str = "simple_wikipedia"):
"""Initialize ChromaDB client and collection."""
print("Setting up ChromaDB...")
# Initialize ChromaDB client
self.chroma_client = chromadb.PersistentClient(path="./chroma_db")
# Create or get collection
try:
self.collection = self.chroma_client.get_collection(collection_name)
print(f"Found existing collection: {collection_name}")
except:
self.collection = self.chroma_client.create_collection(
name=collection_name,
metadata={"description": "Simple Wikipedia embeddings"}
)
print(f"Created new collection: {collection_name}")
print("✅ ChromaDB ready!")
def clean_text(self, text: str) -> str:
"""Clean and normalize text."""
# Remove extra whitespace and normalize
text = re.sub(r'\s+', ' ', text.strip())
# Remove Wikipedia markup (basic cleanup)
text = re.sub(r'\[\[([^\]]+)\]\]', r'\1', text) # Remove [[]] markup
text = re.sub(r'\[([^\]]+)\]', r'\1', text) # Remove [] links
text = re.sub(r'\{\{[^\}]+\}\}', '', text) # Remove {{}} templates
return text
def split_text_into_chunks(self, text: str, title: str) -> List[Dict[str, Any]]:
"""Split text into overlapping chunks."""
# Simple token-based splitting (approximate)
words = text.split()
chunks = []
start = 0
chunk_id = 0
while start < len(words):
end = min(start + self.chunk_size, len(words))
chunk_words = words[start:end]
chunk_text = ' '.join(chunk_words)
if chunk_text.strip(): # Only add non-empty chunks
chunks.append({
'text': chunk_text,
'title': title,
'chunk_id': chunk_id,
'start_word': start,
'end_word': end,
'total_words': len(words)
})
chunk_id += 1
start = end - self.overlap # Overlap for context
return chunks
def process_article(self, article: Dict[str, Any]) -> List[Dict[str, Any]]:
"""Process a single article into chunks."""
# Simple Wikipedia uses 'title' and 'text' keys
title = article.get('title', 'Unknown Title')
text = article.get('text', '')
# Clean and process text
text = self.clean_text(text)
# Skip empty articles
if not text.strip():
return []
# Split into chunks
chunks = self.split_text_into_chunks(text, title)
return chunks
def process_batch(self, articles: List[Dict[str, Any]]) -> List[Dict[str, Any]]:
"""Process a batch of articles."""
all_chunks = []
for article in articles:
chunks = self.process_article(article)
all_chunks.extend(chunks)
return all_chunks
def create_embeddings_batch(self, chunks: List[Dict[str, Any]]) -> List[List[float]]:
"""Create embeddings for a batch of text chunks."""
texts = [chunk['text'] for chunk in chunks]
embeddings = self.model.encode(texts, show_progress_bar=False)
return embeddings.tolist()
def store_in_chromadb(self, chunks: List[Dict[str, Any]], embeddings: List[List[float]]):
"""Store chunks and embeddings in ChromaDB."""
ids = []
documents = []
metadatas = []
for i, chunk in enumerate(chunks):
# Create unique ID
chunk_id = f"{chunk['title'].replace(' ', '_')}_{chunk['chunk_id']}"
ids.append(chunk_id)
documents.append(chunk['text'])
metadatas.append({
'title': chunk['title'],
'chunk_id': chunk['chunk_id'],
'start_word': chunk['start_word'],
'end_word': chunk['end_word'],
'total_words': chunk['total_words'],
'created_at': datetime.now().isoformat()
})
# Add to ChromaDB
self.collection.add(
ids=ids,
embeddings=embeddings,
documents=documents,
metadatas=metadatas
)
def create_embeddings(self, dataset_path: str = "./simple_wikipedia"):
"""Main method to create embeddings from Simple Wikipedia."""
print("🚀 Starting Wikipedia embedding creation...")
# Setup
self.setup_model()
self.setup_chromadb()
# Load dataset
print("Loading Simple Wikipedia dataset...")
# Load directly from HuggingFace to ensure we get the right format
if dataset_path == "./simple_wikipedia":
print("Loading from HuggingFace directly...")
dataset = load_dataset("wikipedia", "20220301.simple")
else:
dataset = load_dataset(dataset_path)
train_dataset = dataset['train']
total_articles = len(train_dataset)
# Debug: Check first article structure
if total_articles > 0:
first_article = train_dataset[0]
print(f"📋 First article keys: {list(first_article.keys())}")
print(f"📋 Sample title: {first_article.get('title', 'NO TITLE')}")
print(f"📋 Sample text length: {len(first_article.get('text', ''))}")
print(f"📊 Processing {total_articles:,} articles...")
# Limit articles for testing (remove this line for full processing)
if total_articles > 1000:
print("🔧 Limiting to first 1000 articles for testing...")
total_articles = 1000
# Process in batches
total_chunks = 0
total_embeddings = 0
for batch_start in tqdm(range(0, total_articles, self.batch_size),
desc="Processing articles"):
batch_end = min(batch_start + self.batch_size, total_articles)
batch_articles = [train_dataset[i] for i in range(batch_start, batch_end)]
# Process batch
chunks = self.process_batch(batch_articles)
if not chunks:
continue
# Create embeddings
embeddings = self.create_embeddings_batch(chunks)
# Store in ChromaDB
self.store_in_chromadb(chunks, embeddings)
total_chunks += len(chunks)
total_embeddings += len(embeddings)
# Progress update
if batch_start % (self.batch_size * 10) == 0:
print(f" Processed {batch_end:,}/{total_articles:,} articles")
print(f" Created {total_chunks:,} chunks, {total_embeddings:,} embeddings")
print(f"\n✅ Embedding creation complete!")
print(f"📊 Final Statistics:")
print(f" Articles processed: {total_articles:,}")
print(f" Total chunks created: {total_chunks:,}")
print(f" Total embeddings: {total_embeddings:,}")
print(f" Average chunks per article: {total_chunks/total_articles:.1f}")
# Test retrieval
print(f"\n🔍 Testing retrieval...")
test_results = self.collection.query(
query_texts=["What is a dog?"],
n_results=3
)
print("Sample results for 'What is a dog?':")
for i, (doc, metadata) in enumerate(zip(test_results['documents'][0],
test_results['metadatas'][0])):
print(f" {i+1}. {metadata['title']} (chunk {metadata['chunk_id']})")
print(f" {doc[:100]}...")
print(f"\n🎯 ChromaDB collection ready for use!")
print(f"Location: ./chroma_db")
def main():
"""Main function to run embedding creation."""
import argparse
parser = argparse.ArgumentParser(description="Create Wikipedia embeddings")
parser.add_argument("--model",
default="sentence-transformers/all-mpnet-base-v2",
help="Embedding model to use")
parser.add_argument("--chunk-size", type=int, default=512,
help="Maximum tokens per chunk")
parser.add_argument("--batch-size", type=int, default=100,
help="Number of articles to process at once")
parser.add_argument("--dataset-path", default="./simple_wikipedia",
help="Path to Simple Wikipedia dataset")
args = parser.parse_args()
# Create embedding creator
creator = WikipediaEmbeddingCreator(
embedding_model=args.model,
chunk_size=args.chunk_size,
batch_size=args.batch_size
)
# Run embedding creation
creator.create_embeddings(args.dataset_path)
if __name__ == "__main__":
main()