forked from SaiAkhil066/DeepSeek-RAG-Chatbot
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathapp.py
164 lines (136 loc) · 6.36 KB
/
app.py
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
import streamlit as st
import requests
import json
from utils.retriever_pipeline import retrieve_documents
from utils.doc_handler import process_documents
from sentence_transformers import CrossEncoder
import torch
OLLAMA_BASE_URL = "http://localhost:11434"
OLLAMA_API_URL = f"{OLLAMA_BASE_URL}/api/generate"
MODEL="deepseek-r1:7b" #Make sure you have it installed in ollama
EMBEDDINGS_MODEL = "nomic-embed-text:latest"
CROSS_ENCODER_MODEL = "cross-encoder/ms-marco-MiniLM-L-6-v2"
device = "cuda" if torch.cuda.is_available() else "cpu"
reranker = None # 🚀 Initialize Cross-Encoder (Reranker) at the global level
try:
reranker = CrossEncoder(CROSS_ENCODER_MODEL, device=device)
except Exception as e:
st.error(f"Failed to load CrossEncoder model: {str(e)}")
st.set_page_config(page_title="DeepGraph RAG-Pro", layout="wide") # ✅ Streamlit configuration
# Custom CSS
st.markdown("""
<style>
.stApp { background-color: #f4f4f9; }
h1 { color: #00FF99; text-align: center; }
.stChatMessage { border-radius: 10px; padding: 10px; margin: 10px 0; }
.stChatMessage.user { background-color: #e8f0fe; }
.stChatMessage.assistant { background-color: #d1e7dd; }
.stButton>button { background-color: #00AAFF; color: white; }
</style>
""", unsafe_allow_html=True)
# Manage Session state
if "messages" not in st.session_state:
st.session_state.messages = []
if "retrieval_pipeline" not in st.session_state:
st.session_state.retrieval_pipeline = None
if "rag_enabled" not in st.session_state:
st.session_state.rag_enabled = False
if "documents_loaded" not in st.session_state:
st.session_state.documents_loaded = False
with st.sidebar: # 📁 Sidebar
st.header("📁 Document Management")
uploaded_files = st.file_uploader(
"Upload documents (PDF/DOCX/TXT)",
type=["pdf", "docx", "txt"],
accept_multiple_files=True
)
if uploaded_files and not st.session_state.documents_loaded:
with st.spinner("Processing documents..."):
process_documents(uploaded_files,reranker,EMBEDDINGS_MODEL, OLLAMA_BASE_URL)
st.success("Documents processed!")
st.markdown("---")
st.header("⚙️ RAG Settings")
st.session_state.rag_enabled = st.checkbox("Enable RAG", value=True)
st.session_state.enable_hyde = st.checkbox("Enable HyDE", value=True)
st.session_state.enable_reranking = st.checkbox("Enable Neural Reranking", value=True)
st.session_state.enable_graph_rag = st.checkbox("Enable GraphRAG", value=True)
st.session_state.temperature = st.slider("Temperature", 0.0, 1.0, 0.3, 0.05)
st.session_state.max_contexts = st.slider("Max Contexts", 1, 5, 3)
if st.button("Clear Chat History"):
st.session_state.messages = []
st.rerun()
# 🚀 Footer (Bottom Right in Sidebar) For some Credits :)
st.sidebar.markdown("""
<div style="position: absolute; top: 20px; right: 10px; font-size: 12px; color: gray;">
<b>Developed by:</b> N Sai Akhil © All Rights Reserved 2025
</div>
""", unsafe_allow_html=True)
# 💬 Chat Interface
st.title("🤖 DeepGraph RAG-Pro")
st.caption("Advanced RAG System with GraphRAG, Hybrid Retrieval, Neural Reranking and Chat History")
# Display messages
for message in st.session_state.messages:
with st.chat_message(message["role"]):
st.markdown(message["content"])
if prompt := st.chat_input("Ask about your documents..."):
chat_history = "\n".join([msg["content"] for msg in st.session_state.messages[-5:]]) # Last 5 messages
st.session_state.messages.append({"role": "user", "content": prompt})
with st.chat_message("user"):
st.markdown(prompt)
# Generate response
with st.chat_message("assistant"):
response_placeholder = st.empty()
full_response = ""
# 🚀 Build context
context = ""
if st.session_state.rag_enabled and st.session_state.retrieval_pipeline:
try:
docs = retrieve_documents(prompt, OLLAMA_API_URL, MODEL, chat_history)
context = "\n".join(
f"[Source {i+1}]: {doc.page_content}"
for i, doc in enumerate(docs)
)
except Exception as e:
st.error(f"Retrieval error: {str(e)}")
# 🚀 Structured Prompt
system_prompt = f"""Use the chat history to maintain context:
Chat History:
{chat_history}
Analyze the question and context through these steps:
1. Identify key entities and relationships
2. Check for contradictions between sources
3. Synthesize information from multiple contexts
4. Formulate a structured response
Context:
{context}
Question: {prompt}
Answer:"""
# Stream response
response = requests.post(
OLLAMA_API_URL,
json={
"model": MODEL,
"prompt": system_prompt,
"stream": True,
"options": {
"temperature": st.session_state.temperature, # Use dynamic user-selected value
"num_ctx": 4096
}
},
stream=True
)
try:
for line in response.iter_lines():
if line:
data = json.loads(line.decode())
token = data.get("response", "")
full_response += token
response_placeholder.markdown(full_response + "▌")
# Stop if we detect the end token
if data.get("done", False):
break
response_placeholder.markdown(full_response)
st.session_state.messages.append({"role": "assistant", "content": full_response})
except Exception as e:
st.error(f"Generation error: {str(e)}")
st.session_state.messages.append({"role": "assistant", "content": "Sorry, I encountered an error."})