Spaces:
Runtime error
Runtime error
Update app.py
Browse files
app.py
CHANGED
@@ -1,6 +1,5 @@
|
|
1 |
-
from helper import download_hugging_face_embeddings
|
2 |
from url import md_files_url
|
3 |
-
from get_data import extract_repo_details, fetch_md_file_via_api, data_loader, chunk_text
|
4 |
from langchain_community.llms import Ollama
|
5 |
from langchain.chains import create_retrieval_chain
|
6 |
from langchain.chains.combine_documents import create_stuff_documents_chain
|
@@ -28,7 +27,7 @@ logging.basicConfig(level=logging.INFO)
|
|
28 |
logger = logging.getLogger(__name__)
|
29 |
|
30 |
base = {}
|
31 |
-
last_messages =
|
32 |
documents = []
|
33 |
HF_ORG_NAME = 'HumbleBeeAI'
|
34 |
DATASET_NAME = 'faiss_index'
|
@@ -118,7 +117,11 @@ else:
|
|
118 |
# ---- Database part ----- #
|
119 |
# Database Connection
|
120 |
def connect_db():
|
121 |
-
|
|
|
|
|
|
|
|
|
122 |
|
123 |
def create_tables():
|
124 |
with connect_db() as conn:
|
@@ -239,11 +242,11 @@ docsearch = PineconeVectorStore.from_existing_index(
|
|
239 |
embedding=download_hugging_face_embeddings()
|
240 |
)
|
241 |
|
242 |
-
retriever = docsearch.as_retriever(search_type='mmr', search_kwargs={'k':5})
|
243 |
|
244 |
llm = Ollama(model='llama3.2', base_url=BASE_URL)
|
245 |
|
246 |
-
|
247 |
[
|
248 |
('system', system_prompt),
|
249 |
('human', '{input}'),
|
@@ -260,6 +263,7 @@ context_prompt = ChatPromptTemplate.from_messages(
|
|
260 |
('human', '{input}'),
|
261 |
]
|
262 |
)
|
|
|
263 |
history_aware_retriever = create_history_aware_retriever(llm, retriever, context_prompt)
|
264 |
|
265 |
qa_prompt = ChatPromptTemplate.from_messages(
|
@@ -269,6 +273,7 @@ qa_prompt = ChatPromptTemplate.from_messages(
|
|
269 |
('human', '{input}'),
|
270 |
]
|
271 |
)
|
|
|
272 |
question_answer_chain = create_stuff_documents_chain(llm, prompt)
|
273 |
rag_chain = create_retrieval_chain(history_aware_retriever, question_answer_chain)
|
274 |
|
@@ -291,15 +296,36 @@ chat_with_msg_history = RunnableWithMessageHistory(
|
|
291 |
input_messages_key='input',
|
292 |
history_messages_key='chat_history'
|
293 |
)
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
294 |
|
295 |
def get_response(message, chat_history, session_id, user_id):
|
296 |
if not session_id or not user_id:
|
297 |
return "Session expired. Please log in again.", []
|
298 |
|
299 |
-
|
300 |
|
301 |
response = chat_with_msg_history.invoke(
|
302 |
-
{
|
|
|
|
|
|
|
303 |
{'configurable': {'session_id': session_id}},
|
304 |
)
|
305 |
|
@@ -323,7 +349,6 @@ def logout(session_id):
|
|
323 |
del base[session_id] # Clear session history
|
324 |
return None, "Logged out successfully.", None
|
325 |
|
326 |
-
## ... [Keep all previous code up to the Gradio UI section] ...
|
327 |
# Gradio UI
|
328 |
with gr.Blocks() as demo:
|
329 |
gr.Markdown("## HumblebeeAI Customer Support Chatbot")
|
@@ -358,7 +383,7 @@ with gr.Blocks() as demo:
|
|
358 |
if session_id:
|
359 |
return session_id, user_id, message, "", "", get_chat_history(user_id), gr.update(visible=True)
|
360 |
return None, None, message, username, password, [], gr.update(visible=False)
|
361 |
-
|
362 |
login_button.click(
|
363 |
login_user,
|
364 |
[username, password],
|
@@ -390,18 +415,12 @@ with gr.Blocks() as demo:
|
|
390 |
)
|
391 |
|
392 |
# 🔹 Logout Logic (Clears Chat and Resets UI)
|
393 |
-
def logout_user():
|
394 |
-
return None, "", "", [], gr.update(visible=False)
|
395 |
-
|
396 |
-
# 🔹 Logout Function (Clears Status, Session, and Chat History)
|
397 |
def logout_user():
|
398 |
return None, "", "", "", [], gr.update(visible=False)
|
399 |
-
|
400 |
logout_button.click(
|
401 |
logout_user,
|
402 |
None,
|
403 |
[session_state, username, password, login_status, chatbot, chat_interface]
|
404 |
)
|
405 |
-
|
406 |
-
|
407 |
-
demo.launch(share=True)
|
|
|
|
|
1 |
from url import md_files_url
|
2 |
+
from get_data import extract_repo_details, fetch_md_file_via_api, data_loader, chunk_text, download_hugging_face_embeddings
|
3 |
from langchain_community.llms import Ollama
|
4 |
from langchain.chains import create_retrieval_chain
|
5 |
from langchain.chains.combine_documents import create_stuff_documents_chain
|
|
|
27 |
logger = logging.getLogger(__name__)
|
28 |
|
29 |
base = {}
|
30 |
+
last_messages = 10
|
31 |
documents = []
|
32 |
HF_ORG_NAME = 'HumbleBeeAI'
|
33 |
DATASET_NAME = 'faiss_index'
|
|
|
117 |
# ---- Database part ----- #
|
118 |
# Database Connection
|
119 |
def connect_db():
|
120 |
+
try:
|
121 |
+
return sqlite3.connect(DB_PATH)
|
122 |
+
except sqlite3.Error as e:
|
123 |
+
logger.error(f"Database connection failed: {e}")
|
124 |
+
return None # Or raise a custom exception
|
125 |
|
126 |
def create_tables():
|
127 |
with connect_db() as conn:
|
|
|
242 |
embedding=download_hugging_face_embeddings()
|
243 |
)
|
244 |
|
245 |
+
retriever = docsearch.as_retriever(search_type='mmr', search_kwargs={'k': 10, 'lambda_mult': 0.5})
|
246 |
|
247 |
llm = Ollama(model='llama3.2', base_url=BASE_URL)
|
248 |
|
249 |
+
= ChatPromptTemplate.from_messages(
|
250 |
[
|
251 |
('system', system_prompt),
|
252 |
('human', '{input}'),
|
|
|
263 |
('human', '{input}'),
|
264 |
]
|
265 |
)
|
266 |
+
|
267 |
history_aware_retriever = create_history_aware_retriever(llm, retriever, context_prompt)
|
268 |
|
269 |
qa_prompt = ChatPromptTemplate.from_messages(
|
|
|
273 |
('human', '{input}'),
|
274 |
]
|
275 |
)
|
276 |
+
|
277 |
question_answer_chain = create_stuff_documents_chain(llm, prompt)
|
278 |
rag_chain = create_retrieval_chain(history_aware_retriever, question_answer_chain)
|
279 |
|
|
|
296 |
input_messages_key='input',
|
297 |
history_messages_key='chat_history'
|
298 |
)
|
299 |
+
from langchain.schema import AIMessage, HumanMessage
|
300 |
+
|
301 |
+
def get_combined_history(session_id, user_id):
|
302 |
+
persistent_history = get_chat_history(user_id) # Retrieve long-term memory
|
303 |
+
|
304 |
+
# Convert stored chat history into messages
|
305 |
+
long_term_messages = [
|
306 |
+
HumanMessage(content=msg) if i % 2 == 0 else AIMessage(content=resp)
|
307 |
+
for i, (msg, resp) in enumerate(persistent_history)
|
308 |
+
]
|
309 |
+
|
310 |
+
# Collect session-based recent messages
|
311 |
+
recent_messages = base[session_id].messages if session_id in base else []
|
312 |
+
|
313 |
+
# Merge and return the combined context
|
314 |
+
combined_history = long_term_messages + recent_messages
|
315 |
+
print("Combined History for Debugging:", combined_history) # 🔹 Debugging step
|
316 |
+
return combined_history[-last_messages:] # Optionally limit
|
317 |
|
318 |
def get_response(message, chat_history, session_id, user_id):
|
319 |
if not session_id or not user_id:
|
320 |
return "Session expired. Please log in again.", []
|
321 |
|
322 |
+
combined_history = get_combined_history(session_id, user_id)
|
323 |
|
324 |
response = chat_with_msg_history.invoke(
|
325 |
+
{
|
326 |
+
'input': message,
|
327 |
+
'chat_history': combined_history # This should be a list of LangChain message objects
|
328 |
+
},
|
329 |
{'configurable': {'session_id': session_id}},
|
330 |
)
|
331 |
|
|
|
349 |
del base[session_id] # Clear session history
|
350 |
return None, "Logged out successfully.", None
|
351 |
|
|
|
352 |
# Gradio UI
|
353 |
with gr.Blocks() as demo:
|
354 |
gr.Markdown("## HumblebeeAI Customer Support Chatbot")
|
|
|
383 |
if session_id:
|
384 |
return session_id, user_id, message, "", "", get_chat_history(user_id), gr.update(visible=True)
|
385 |
return None, None, message, username, password, [], gr.update(visible=False)
|
386 |
+
|
387 |
login_button.click(
|
388 |
login_user,
|
389 |
[username, password],
|
|
|
415 |
)
|
416 |
|
417 |
# 🔹 Logout Logic (Clears Chat and Resets UI)
|
|
|
|
|
|
|
|
|
418 |
def logout_user():
|
419 |
return None, "", "", "", [], gr.update(visible=False)
|
420 |
+
|
421 |
logout_button.click(
|
422 |
logout_user,
|
423 |
None,
|
424 |
[session_state, username, password, login_status, chatbot, chat_interface]
|
425 |
)
|
426 |
+
demo.launch(share=True)
|
|
|
|