-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathapp.py
More file actions
534 lines (454 loc) Β· 19.6 KB
/
app.py
File metadata and controls
534 lines (454 loc) Β· 19.6 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
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
"""
Streamlit UI for IntelliQuery RAG system.
Features persistent chat interface with conversation memory.
"""
import os
import tempfile
import time
from typing import List, Optional
import streamlit as st
from langchain_community.document_loaders import PyPDFLoader
from langchain_text_splitters import RecursiveCharacterTextSplitter
from langchain_chroma import Chroma
from langchain_ollama import OllamaEmbeddings, ChatOllama
from langchain_core.documents import Document
from langchain_core.runnables import RunnableLambda, RunnablePassthrough
from langchain_core.output_parsers import StrOutputParser
# Import modular components
from config import (
CHUNK_SIZE,
CHUNK_OVERLAP,
RETRIEVAL_K,
RERANK_K,
EMBEDDING_MODEL,
LANGUAGE_MODEL,
LLM_TEMPERATURE,
CHROMA_DIR,
USE_HYBRID,
ENABLE_CONVERSATION_MEMORY,
DEBUG_RETRIEVAL,
)
from prompts import get_single_turn_prompt, get_multi_turn_prompt
from query_processing import QueryProcessor
from retriever import RetrievalSystem, build_context, sentence_citations
from memory import ConversationMemory, MemoryAwareQueryProcessor
from evaluation import RetrievalConfidenceScorer, AnswerGroundingValidator
# ============================================================================
# UTILITY FUNCTIONS
# ============================================================================
def check_ollama_connection(model_name: str, max_retries: int = 3, retry_delay: int = 2) -> bool:
"""
Check if Ollama is accessible and model is available.
Args:
model_name: Name of the model to check
max_retries: Maximum number of retry attempts
retry_delay: Delay between retries in seconds
Returns:
True if connection successful, False otherwise
"""
from langchain_ollama import OllamaEmbeddings
for attempt in range(max_retries):
try:
# Try to create embeddings instance and do a simple test
test_embeddings = OllamaEmbeddings(model=model_name)
# Test with a small query
_ = test_embeddings.embed_query("test")
return True
except Exception as e:
if attempt < max_retries - 1:
time.sleep(retry_delay)
continue
else:
st.warning(f"β οΈ Ollama connection issue (attempt {attempt + 1}/{max_retries}): {str(e)}")
return False
return False
def create_embeddings_with_retry(model_name: str, max_retries: int = 3, retry_delay: int = 2):
"""
Create OllamaEmbeddings with retry logic.
Args:
model_name: Name of the embedding model
max_retries: Maximum number of retry attempts
retry_delay: Delay between retries in seconds
Returns:
OllamaEmbeddings instance
"""
for attempt in range(max_retries):
try:
embeddings = OllamaEmbeddings(model=model_name)
# Test the connection
_ = embeddings.embed_query("test connection")
return embeddings
except Exception as e:
if attempt < max_retries - 1:
time.sleep(retry_delay)
continue
else:
raise ConnectionError(
f"Failed to connect to Ollama after {max_retries} attempts. "
f"Make sure Ollama is running and the model '{model_name}' is available. "
f"Error: {str(e)}"
)
def create_llm_with_retry(model_name: str, temperature: float = 0, max_retries: int = 3, retry_delay: int = 2):
"""
Create ChatOllama with retry logic.
Args:
model_name: Name of the language model
temperature: Temperature for generation
max_retries: Maximum number of retry attempts
retry_delay: Delay between retries in seconds
Returns:
ChatOllama instance
"""
for attempt in range(max_retries):
try:
llm = ChatOllama(model=model_name, temperature=temperature)
# Test the connection with a simple prompt
_ = llm.invoke("test")
return llm
except Exception as e:
if attempt < max_retries - 1:
time.sleep(retry_delay)
continue
else:
raise ConnectionError(
f"Failed to connect to Ollama after {max_retries} attempts. "
f"Make sure Ollama is running and the model '{model_name}' is available. "
f"Error: {str(e)}"
)
# ============================================================================
# INITIALIZATION
# ============================================================================
def initialize_session_state():
"""Initialize Streamlit session state variables."""
if "chain" not in st.session_state:
st.session_state.chain = None
st.session_state.embeddings = None
st.session_state.llm = None
st.session_state.retrieval_system = None
st.session_state.vectordb = None
st.session_state.documents = None
if "memory" not in st.session_state:
st.session_state.memory = ConversationMemory(enabled=ENABLE_CONVERSATION_MEMORY)
if "messages" not in st.session_state:
st.session_state.messages = []
if "query_processor" not in st.session_state:
st.session_state.query_processor = QueryProcessor()
if "memory_processor" not in st.session_state:
st.session_state.memory_processor = MemoryAwareQueryProcessor(st.session_state.memory)
# ============================================================================
# DOCUMENT PROCESSING
# ============================================================================
def build_vectordb_from_uploads(files, embeddings: OllamaEmbeddings) -> tuple:
"""
Build vector database from uploaded PDF files.
Returns:
Tuple of (vectordb, documents)
"""
import shutil
docs: List[Document] = []
splitter = RecursiveCharacterTextSplitter(
chunk_size=CHUNK_SIZE,
chunk_overlap=CHUNK_OVERLAP
)
# Clear existing database to avoid mixing with ./docs files
# Use retry logic for Windows file locking issues
if os.path.exists(CHROMA_DIR):
max_retries = 5
retry_delay = 0.5
for attempt in range(max_retries):
try:
# Close any existing connections first
if "vectordb" in st.session_state and st.session_state.vectordb is not None:
try:
# Try to delete the persist directory reference
del st.session_state.vectordb
except:
pass
# Small delay to allow file handles to close
time.sleep(retry_delay)
# Remove the directory
shutil.rmtree(CHROMA_DIR)
break # Success, exit retry loop
except (PermissionError, OSError) as e:
if attempt < max_retries - 1:
time.sleep(retry_delay * (attempt + 1)) # Exponential backoff
continue
else:
# Last attempt failed, try to continue anyway
# The database will be overwritten
st.warning(f"β οΈ Could not fully clear old database: {str(e)}. Continuing anyway...")
break
with tempfile.TemporaryDirectory() as tmpdir:
for uploaded in files:
file_path = os.path.join(tmpdir, uploaded.name)
with open(file_path, "wb") as f:
f.write(uploaded.read())
loader = PyPDFLoader(file_path)
loaded = loader.load()
for d in loaded:
for i, chunk in enumerate(splitter.split_text(d.page_content)):
# Ensure source is set to uploaded filename, not file path
source_name = uploaded.name
docs.append(
Document(
page_content=chunk,
metadata={
"source": source_name, # Use uploaded filename only
"page": d.metadata.get("page"),
"chunk": i,
},
)
)
# Build fresh vector database with only uploaded documents
vectordb = Chroma.from_documents(
documents=docs,
embedding=embeddings,
persist_directory=CHROMA_DIR,
)
return vectordb, docs
# ============================================================================
# RAG CHAIN CONSTRUCTION
# ============================================================================
def make_rag_chain(
retrieval_system: RetrievalSystem,
embeddings: OllamaEmbeddings,
llm: ChatOllama,
memory: ConversationMemory,
use_memory: bool = True,
):
"""
Build RAG chain with optional conversation memory.
Args:
retrieval_system: Retrieval system instance
embeddings: Embeddings model
llm: Language model
memory: Conversation memory
use_memory: Whether to use conversation memory
Returns:
Runnable chain
"""
query_processor = QueryProcessor()
memory_processor = MemoryAwareQueryProcessor(memory)
def retrieve_and_prepare(x: dict) -> dict:
"""Retrieve documents and prepare context."""
question = x["question"]
# Process query with memory awareness
if use_memory and memory.enabled:
query_info = memory_processor.process_with_context(question)
processed_query = query_info["enhanced_query"]
conversation_context = query_info["conversation_context"]
else:
query_info = query_processor.process(question)
processed_query = query_info["normalized"]
conversation_context = ""
# Retrieve and re-rank
reranked_docs, scores = retrieval_system.retrieve_and_rerank(
processed_query,
retrieval_k=RETRIEVAL_K,
rerank_k=RERANK_K,
)
# Build context
context = build_context(reranked_docs, show_scores=DEBUG_RETRIEVAL)
return {
"question": question,
"processed_query": processed_query,
"context": context,
"conversation_context": conversation_context,
"reranked_docs": reranked_docs,
"scores": scores,
}
def generate_answer(x: dict) -> dict:
"""Generate answer using LLM."""
question = x["question"]
context = x["context"]
conversation_context = x.get("conversation_context", "")
reranked_docs = x["reranked_docs"]
# Select prompt based on memory usage
if use_memory and conversation_context:
prompt = get_multi_turn_prompt()
prompt_input = {
"question": question,
"context": context,
"conversation_history": conversation_context,
}
else:
prompt = get_single_turn_prompt()
prompt_input = {
"question": question,
"context": context,
}
# Generate answer
raw_answer = (prompt | llm | StrOutputParser()).invoke(prompt_input)
# Add citations
cited_answer = sentence_citations(
raw_answer,
reranked_docs,
embeddings,
min_sim=0.25,
)
return {
**x,
"raw_answer": raw_answer,
"final_answer": cited_answer,
}
def post_process(x: dict) -> str:
"""Post-process answer and update memory."""
final_answer = x["final_answer"]
question = x["question"]
context = x["context"]
# Update conversation memory
if use_memory and memory.enabled:
memory.add_turn(question, final_answer, context)
return final_answer
# Build chain
chain = (
RunnablePassthrough()
.assign(prep=RunnableLambda(retrieve_and_prepare))
.assign(
question=lambda x: x["question"],
context=lambda x: x["prep"]["context"],
conversation_context=lambda x: x["prep"]["conversation_context"],
reranked_docs=lambda x: x["prep"]["reranked_docs"],
scores=lambda x: x["prep"]["scores"],
)
.assign(answer=RunnableLambda(generate_answer))
.assign(final_answer=lambda x: x["answer"]["final_answer"])
| RunnableLambda(post_process)
)
return chain
# ============================================================================
# STREAMLIT UI
# ============================================================================
def main():
"""Main Streamlit application."""
st.set_page_config(
page_title="IntelliQuery RAG",
page_icon="π",
layout="wide"
)
st.title("π IntelliQuery - Intelligent RAG System")
st.caption(
"Fully local RAG with conversation memory, hybrid search, and citation-grounded answers."
)
initialize_session_state()
# Sidebar configuration
with st.sidebar:
st.header("βοΈ Settings")
model_embed = st.text_input("Embedding model", EMBEDDING_MODEL)
model_llm = st.text_input("Chat model", LANGUAGE_MODEL)
top_k = st.slider("Retriever k", 4, 20, RETRIEVAL_K)
rerank_k = st.slider("Rerank top_k", 2, 10, RERANK_K)
use_memory = st.checkbox("Enable conversation memory", ENABLE_CONVERSATION_MEMORY)
use_hybrid = st.checkbox("Enable hybrid search", USE_HYBRID)
st.divider()
st.header("π Document Upload")
uploaded_files = st.file_uploader(
"Upload PDFs",
type=["pdf"],
accept_multiple_files=True
)
if uploaded_files and st.button("π¨ Build Index", type="primary"):
with st.spinner("Building vector index..."):
try:
# Close any existing database connections first
if st.session_state.get("vectordb") is not None:
try:
# Chroma doesn't have explicit close, but we can delete the reference
del st.session_state.vectordb
except:
pass
st.session_state.vectordb = None
# Give Windows time to release file handles
time.sleep(0.5)
# Check Ollama connection first
st.info("π Checking Ollama connection...")
if not check_ollama_connection(model_embed):
st.error(
f"β Cannot connect to Ollama. Please ensure:\n"
f"1. Ollama is running (check with `ollama serve`)\n"
f"2. Model '{model_embed}' is available (pull with `ollama pull {model_embed}`)\n"
f"3. Your firewall/antivirus isn't blocking the connection"
)
return
# Create embeddings with retry
st.info("π Initializing embeddings model...")
embeddings = create_embeddings_with_retry(model_embed)
# Create LLM with retry
st.info("π€ Initializing language model...")
llm = create_llm_with_retry(model_llm, temperature=LLM_TEMPERATURE)
# Build vector database
st.info("π Processing documents and building index...")
vectordb, documents = build_vectordb_from_uploads(uploaded_files, embeddings)
# Initialize retrieval system
retrieval_system = RetrievalSystem(
vectordb=vectordb,
embeddings=embeddings,
documents=documents,
use_hybrid=use_hybrid,
)
# Build chain
chain = make_rag_chain(
retrieval_system,
embeddings,
llm,
st.session_state.memory,
use_memory=use_memory,
)
# Update session state
st.session_state.chain = chain
st.session_state.embeddings = embeddings
st.session_state.llm = llm
st.session_state.retrieval_system = retrieval_system
st.session_state.vectordb = vectordb
st.session_state.documents = documents
st.session_state.memory.enabled = use_memory
st.success(f"β
Index ready! {len(documents)} chunks indexed.")
except ConnectionError as e:
st.error(f"β Connection Error: {str(e)}")
st.info("π‘ Troubleshooting tips:\n"
"1. Ensure Ollama is running: `ollama serve`\n"
"2. Check if models are available: `ollama list`\n"
"3. Pull missing models: `ollama pull <model_name>`\n"
"4. Check network/firewall settings")
except Exception as e:
st.error(f"β Error building index: {str(e)}")
import traceback
with st.expander("π Show detailed error"):
st.code(traceback.format_exc())
# Memory management
if st.session_state.memory.enabled:
st.divider()
st.header("π¬ Conversation")
if st.button("ποΈ Clear History"):
st.session_state.memory.clear()
st.session_state.messages = []
st.rerun()
history_summary = st.session_state.memory.get_history_summary()
st.caption(f"Turns: {history_summary['total_turns']}")
# Main chat interface
if not st.session_state.get("chain"):
st.info("π Upload PDFs and click 'Build Index' to start.")
return
# Display chat history
for message in st.session_state.messages:
with st.chat_message(message["role"]):
st.markdown(message["content"])
# Chat input
if prompt := st.chat_input("Ask a question about your documents..."):
# Add user message
st.session_state.messages.append({"role": "user", "content": prompt})
with st.chat_message("user"):
st.markdown(prompt)
# Generate response
with st.chat_message("assistant"):
with st.spinner("Thinking..."):
try:
answer = st.session_state.chain.invoke({"question": prompt})
st.markdown(answer)
st.session_state.messages.append({"role": "assistant", "content": answer})
except Exception as e:
error_msg = f"β Error: {str(e)}"
st.error(error_msg)
st.session_state.messages.append({"role": "assistant", "content": error_msg})
if __name__ == "__main__":
main()