-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathhaystack_demo.py
More file actions
275 lines (210 loc) · 8.45 KB
/
haystack_demo.py
File metadata and controls
275 lines (210 loc) · 8.45 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
"""
Haystack RAG Demo - Working Version
Demonstrates Haystack concepts without requiring torch.
"""
import os
import re
from typing import List, Dict, Any
from collections import Counter
import math
class HaystackStyleRAG:
"""
RAG system that mimics Haystack's architecture and concepts.
"""
def __init__(self, document_dir: str = "documents"):
"""Initialize the RAG system."""
self.document_dir = document_dir
self.document_store = [] # Simulates Haystack's document store
self.retriever = None
self.pipeline = None
self.setup_pipeline()
def setup_pipeline(self):
"""Set up the pipeline (simulating Haystack's pipeline concept)."""
print("🔧 Setting up Haystack-style pipeline...")
# Simulate Haystack's document store
self.document_store = []
# Simulate Haystack's retriever
self.retriever = BM25Retriever()
# Simulate Haystack's pipeline
self.pipeline = RAGPipeline(self.retriever)
print("✅ Haystack-style pipeline setup completed!")
def load_documents(self):
"""Load documents (simulating Haystack's document loading)."""
if not os.path.exists(self.document_dir):
print(f"❌ Document directory '{self.document_dir}' not found!")
return
print("📚 Loading documents into Haystack-style document store...")
for filename in os.listdir(self.document_dir):
if filename.endswith('.txt'):
file_path = os.path.join(self.document_dir, filename)
try:
with open(file_path, 'r', encoding='utf-8') as file:
content = file.read()
# Simulate Haystack's document format
document = {
'content': content,
'meta': {'filename': filename, 'source': file_path},
'id': len(self.document_store)
}
self.document_store.append(document)
print(f"✓ Indexed: {filename}")
except Exception as e:
print(f"✗ Error loading {filename}: {e}")
if self.document_store:
print(f"✅ Document store contains {len(self.document_store)} documents!")
else:
print("❌ No documents found to index!")
def run_pipeline(self, query: str, top_k: int = 3):
"""
Run the Haystack-style pipeline.
Args:
query: Search query
top_k: Number of results to return
Returns:
Pipeline results
"""
print(f"🔄 Running Haystack pipeline for query: '{query}'")
# Simulate Haystack's pipeline execution
results = self.pipeline.run(
query=query,
documents=self.document_store,
top_k=top_k
)
return results
class BM25Retriever:
"""
BM25 Retriever (simulating Haystack's BM25Retriever).
"""
def __init__(self):
self.name = "BM25Retriever"
print(f"✅ Initialized {self.name}")
def retrieve(self, query: str, documents: List[Dict], top_k: int = 3):
"""
Retrieve relevant documents using BM25 scoring.
Args:
query: Search query
documents: List of documents to search
top_k: Number of results to return
Returns:
List of relevant documents with scores
"""
if not documents:
return []
# Tokenize query
query_words = self.tokenize(query.lower())
# Calculate BM25 scores
scored_docs = []
for doc in documents:
score = self.calculate_bm25_score(query_words, doc['content'])
if score > 0:
scored_docs.append({
'document': doc,
'score': score
})
# Sort by score and return top_k
scored_docs.sort(key=lambda x: x['score'], reverse=True)
return scored_docs[:top_k]
def tokenize(self, text: str) -> List[str]:
"""Simple tokenization."""
return re.findall(r'\b\w+\b', text.lower())
def calculate_bm25_score(self, query_words: List[str], content: str) -> float:
"""Calculate BM25 score for a document."""
content_lower = content.lower()
content_words = self.tokenize(content)
if not content_words:
return 0.0
# BM25 parameters
k1 = 1.2
b = 0.75
# Calculate document length
doc_length = len(content_words)
avg_doc_length = doc_length # Simplified for demo
score = 0.0
for word in query_words:
if word in content_words:
tf = content_words.count(word)
# Simplified IDF calculation
idf = math.log(1 + 1) # Simplified for demo
# BM25 formula
numerator = tf * (k1 + 1)
denominator = tf + k1 * (1 - b + b * (doc_length / avg_doc_length))
score += idf * (numerator / denominator)
return score
class RAGPipeline:
"""
RAG Pipeline (simulating Haystack's Pipeline).
"""
def __init__(self, retriever):
self.retriever = retriever
self.name = "RAGPipeline"
print(f"✅ Initialized {self.name}")
def run(self, query: str, documents: List[Dict], top_k: int = 3):
"""
Run the RAG pipeline.
Args:
query: Search query
documents: List of documents
top_k: Number of results to return
Returns:
Pipeline results
"""
print(f"🔄 Executing {self.name}...")
# Step 1: Retrieve relevant documents
retrieved_docs = self.retriever.retrieve(query, documents, top_k)
# Step 2: Generate answer
answer = self.generate_answer(query, retrieved_docs)
# Step 3: Return results
results = {
'query': query,
'documents': retrieved_docs,
'answer': answer,
'sources': [doc['document']['meta']['filename'] for doc in retrieved_docs]
}
return results
def generate_answer(self, query: str, documents: List[Dict]) -> str:
"""Generate answer from retrieved documents."""
if not documents:
return "I couldn't find any relevant information to answer your question."
# Combine context from retrieved documents
context_parts = []
for doc_info in documents:
doc = doc_info['document']
context_parts.append(f"From {doc['meta']['filename']}:\n{doc['content'][:300]}...")
context = "\n\n".join(context_parts)
answer = f"Based on the available information:\n\n{context}"
return answer
def main():
"""Main function to demonstrate the Haystack-style RAG system."""
print("🤖 Haystack-Style RAG System Demo")
print("=" * 50)
# Initialize RAG system
rag = HaystackStyleRAG(document_dir="documents")
# Load documents
rag.load_documents()
if not rag.document_store:
print("❌ No documents loaded. Please check your documents directory.")
return
# Demo questions
questions = [
"What is artificial intelligence?",
"What are neural networks?",
"What is machine learning?",
"How does deep learning work?"
]
print("\n" + "="*60)
print("HAYSTACK-STYLE RAG DEMO")
print("="*60)
for question in questions:
print(f"\n❓ Question: {question}")
print("-" * 60)
# Run the pipeline
results = rag.run_pipeline(question, top_k=2)
if results['documents']:
print(f"🤖 Answer: {results['answer']}")
print(f"📚 Sources: {', '.join(results['sources'])}")
print(f"🔍 Retrieved {len(results['documents'])} relevant documents")
else:
print("❌ No relevant documents found")
print("\n" + "="*60)
if __name__ == "__main__":
main()