-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathpdf_search_engine.py
More file actions
125 lines (99 loc) · 4.47 KB
/
pdf_search_engine.py
File metadata and controls
125 lines (99 loc) · 4.47 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
from typing import List, Dict, Any, Optional
import logging
from pdf_processor import PDFProcessor
from vector_store import VectorStore
from qa_system import QASystem
from config import Config
logging.basicConfig(
level=logging.INFO,
format='%(asctime)s - %(name)s - %(levelname)s - %(message)s'
)
logger = logging.getLogger(__name__)
class PDFSearchEngine:
def __init__(self):
logger.info("Initializing PDF Search Engine...")
self.pdf_processor = PDFProcessor()
self.vector_store = VectorStore()
self.qa_system = QASystem(self.vector_store)
logger.info("PDF Search Engine initialized successfully!")
def upload_pdf(self, pdf_content: bytes, filename: str, metadata: Optional[Dict] = None) -> Dict[str, Any]:
try:
documents = self.pdf_processor.process_pdf(pdf_content, filename)
if metadata:
for doc in documents:
doc.metadata.update(metadata)
result = self.vector_store.add_documents(documents)
logger.info(f"Successfully processed {filename}: {len(documents)} chunks created")
return result
except Exception as e:
error_msg = f"Error processing {filename}: {str(e)}"
logger.error(error_msg)
return {"status": "error", "message": error_msg}
def upload_multiple_pdfs(self, pdf_files: List[tuple]) -> Dict[str, Any]:
results = []
total_success = 0
total_failed = 0
for pdf_content, filename in pdf_files:
try:
result = self.upload_pdf(pdf_content, filename)
if result["status"] == "success":
total_success += 1
else:
total_failed += 1
results.append({"filename": filename, "result": result})
except Exception as e:
total_failed += 1
results.append({
"filename": filename,
"result": {"status": "error", "message": str(e)}
})
return {
"status": "completed",
"total_processed": len(pdf_files),
"successful": total_success,
"failed": total_failed,
"detailed_results": results
}
def ask_question(self, question: str, k: int = 5, use_memory: bool = False, session_id: Optional[str] = None) -> Dict[str, Any]:
try:
if use_memory:
return self.qa_system.ask_conversational_question(question, session_id)
else:
return self.qa_system.ask_question(question, k)
except Exception as e:
logger.error(f"Error answering question: {e}")
raise
def search_documents(self, query: str, k: int = 5, with_scores: bool = False) -> List[Dict[str, Any]]:
return self.qa_system.search_documents(query, k, with_scores)
def get_stats(self) -> Dict[str, Any]:
return self.vector_store.get_stats()
def check_existing_documents(self) -> Dict[str, Any]:
"""Check for existing documents in the vector store"""
try:
stats = self.get_stats()
sources = self.vector_store.get_document_sources()
return {
"has_documents": stats.get("has_documents", False),
"total_vectors": stats.get("total_vectors", 0),
"document_sources": sources,
"document_count": len(sources)
}
except Exception as e:
logger.error(f"Error checking existing documents: {e}")
return {
"has_documents": False,
"total_vectors": 0,
"document_sources": [],
"document_count": 0
}
def clear_all_documents(self):
self.vector_store.delete_documents()
logger.info("All documents cleared from vector store")
def start_conversation(self, session_id: Optional[str] = None) -> str:
return self.qa_system.start_conversation(session_id)
def get_conversation_history(self, session_id: Optional[str] = None) -> List[Dict[str, str]]:
return self.qa_system.get_conversation_history(session_id)
def clear_conversation(self, session_id: Optional[str] = None):
self.qa_system.clear_conversation(session_id)
def get_all_sessions(self) -> List[Dict[str, Any]]:
return self.qa_system.get_all_sessions()