-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathapp.py
More file actions
193 lines (155 loc) · 6.84 KB
/
Copy pathapp.py
File metadata and controls
193 lines (155 loc) · 6.84 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
import os
import logging
import warnings
from typing import Optional, List
from fastapi import FastAPI, HTTPException
from fastapi.staticfiles import StaticFiles
from fastapi.responses import JSONResponse, RedirectResponse
from fastapi.concurrency import run_in_threadpool
from pydantic import BaseModel
import uvicorn
# LangChain & Ollama Imports
from langchain_community.document_loaders import PyPDFLoader, DirectoryLoader, TextLoader
from langchain_text_splitters import RecursiveCharacterTextSplitter
from langchain_ollama import OllamaLLM, OllamaEmbeddings
from langchain_community.vectorstores import FAISS
# Suppress Warnings
warnings.filterwarnings("ignore", category=UserWarning, module="langchain_core._api.deprecation")
# Configure Logging
logging.basicConfig(level=logging.INFO, format='%(asctime)s - %(levelname)s - %(message)s')
logger = logging.getLogger(__name__)
# --- Configuration ---
DATA_PATH = "data/"
DB_FAISS_PATH = "vectorstore/db_faiss"
MODEL_NAME = "llama3"
EMBEDDING_MODEL = "nomic-embed-text"
DOCUMIND_SYSTEM_PROMPT = """You are DocuMind, a helpful and knowledgeable document assistant.
Your goal is to answer the user's questions based on the provided document context.
────────────────────────────────────
INSTRUCTIONS
────────────────────────────────────
1. **Prioritize the Document**: Use the provided document context as your primary source of truth.
2. **Be Helpful**: If the document mentions a term but doesn't define it fully, you MAY use your general knowledge to briefly explain it.
3. **Admit Gaps**: If the answer is completely missing from the document, state "I couldn't find that specific information in the document."
4. **Context Support**: Use the provided chat history to understand follow-up questions.
Context:
{context}
Chat History:
{history}
Question: {question}
Answer:"""
# --- Logic: Ingestion ---
def create_vector_db():
"""
Ingests PDF and TXT documents, splits them, generates embeddings, and saves to FAISS.
"""
if not os.path.exists(DATA_PATH):
os.makedirs(DATA_PATH)
logger.info(f"Directory {DATA_PATH} created.")
return False, "Data directory created. Please add files."
logger.info("Loading documents from storage...")
try:
pdf_loader = DirectoryLoader(DATA_PATH, glob='*.pdf', loader_cls=PyPDFLoader)
txt_loader = DirectoryLoader(DATA_PATH, glob='*.txt', loader_cls=TextLoader)
documents = pdf_loader.load() + txt_loader.load()
except Exception as e:
logger.error(f"Failed to load documents: {e}")
return False, str(e)
if not documents:
logger.warning(f"No compatible documents found in {DATA_PATH}.")
return False, "No documents found."
# Split text
text_splitter = RecursiveCharacterTextSplitter(chunk_size=1000, chunk_overlap=150)
texts = text_splitter.split_documents(documents)
logger.info(f"Processed {len(documents)} documents into {len(texts)} chunks.")
# Embed and Store
try:
logger.info(f"Initializing embeddings model ({EMBEDDING_MODEL})...")
embeddings = OllamaEmbeddings(model=EMBEDDING_MODEL)
logger.info("Building vector index...")
db = FAISS.from_documents(texts, embeddings)
db.save_local(DB_FAISS_PATH)
logger.info(f"Vector store successfully persisted to {DB_FAISS_PATH}")
return True, "Ingestion successful!"
except Exception as e:
logger.error(f"Failed to create vector store: {e}")
return False, str(e)
# --- Logic: Retrieval & RAG ---
class DocuMindError(Exception):
pass
class DocuMind:
def __init__(self):
self.db = None
self.llm = OllamaLLM(model=MODEL_NAME)
self.chat_history = []
self._initialize_vector_store()
def _initialize_vector_store(self):
print(f"Initializing embeddings ({EMBEDDING_MODEL})...")
embeddings = OllamaEmbeddings(model=EMBEDDING_MODEL)
try:
if os.path.exists(DB_FAISS_PATH):
self.db = FAISS.load_local(DB_FAISS_PATH, embeddings, allow_dangerous_deserialization=True)
print("Vector store loaded successfully.")
else:
print("Warning: Vector store not found. Please run ingestion.")
self.db = None
except Exception as e:
print(f"Error loading vector store: {e}")
self.db = None
def get_response(self, query: str) -> str:
if not self.db:
return "System Error: Vector store not loaded. Please ingest documents first."
try:
# Increase k to 5 for better context
docs = self.db.similarity_search(query, k=5)
context = "\n---\n".join([doc.page_content for doc in docs])
except Exception as e:
return f"Retrieval Error: {e}"
# Format History (last 3 turns)
history_str = "\n".join([f"User: {q}\nDocuMind: {a}" for q, a in self.chat_history[-3:]])
prompt = DOCUMIND_SYSTEM_PROMPT.format(context=context, history=history_str, question=query)
try:
response = self.llm.invoke(prompt)
self.chat_history.append((query, response.strip()))
return response.strip()
except Exception as e:
return f"Error calling LLM: {e}"
# --- FastAPI App ---
app = FastAPI()
app.mount("/static", StaticFiles(directory="static"), name="static")
# Initialize Chatbot Global Instance
bot = DocuMind()
class ChatRequest(BaseModel):
message: str
@app.post("/api/chat")
async def chat(request: ChatRequest):
global bot
if bot is None or bot.db is None:
# Try to re-initialize
try:
bot = DocuMind()
except:
pass
if bot is None or bot.db is None:
return JSONResponse(status_code=503, content={"message": "System not ready. Please ingest documents first."})
# Run blocking inference in threadpool
response = await run_in_threadpool(bot.get_response, request.message)
return {"response": response}
@app.post("/api/ingest")
async def ingest_endpoint():
global bot
try:
# Run blocking ingestion in threadpool
success, message = await run_in_threadpool(create_vector_db)
if not success:
return JSONResponse(status_code=500, content={"message": f"Ingestion failed: {message}"})
# Re-initialize bot to load new vector store
bot = DocuMind()
return {"message": "Ingestion successful! You can now ask questions."}
except Exception as e:
return JSONResponse(status_code=500, content={"message": str(e)})
@app.get("/")
async def root():
return RedirectResponse(url="/static/index.html")
if __name__ == "__main__":
uvicorn.run(app, host="127.0.0.1", port=8000)