itsmehardawood commited on
Commit
f7111ce
·
1 Parent(s): 8eb9727
Files changed (2) hide show
  1. App/main.py +17 -2
  2. App/utils.py +11 -0
App/main.py CHANGED
@@ -449,8 +449,23 @@ async def query_rag_endpoint(payload: QueryModel):
449
  else:
450
  user_lan = language
451
  question_tokens = count_tokens(payload.question)
452
- print(f"Question tokens: {question_tokens}")
453
- retrieved_docs = app.state.retriever.get_relevant_documents(payload.question)
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
454
  context_text = "\n\n".join([doc.page_content for doc in retrieved_docs])
455
  context_tokens = count_tokens(context_text)
456
  chat_history = await chatbot_history_collection.find_one({"userId": payload.user_id})
 
449
  else:
450
  user_lan = language
451
  question_tokens = count_tokens(payload.question)
452
+
453
+
454
+
455
+
456
+ # In your query_rag_endpoint, before rag_chain.invoke():
457
+ try:
458
+ retrieved_docs = app.state.retriever.get_relevant_documents(payload.question)
459
+ except Exception as e:
460
+ # Rebuild retriever and try again
461
+ app.state.retriever = get_existing_retriever()
462
+ if not app.state.retriever:
463
+ raise HTTPException(400, "Vector store corrupted. Please rebuild.")
464
+ retrieved_docs = app.state.retriever.get_relevant_documents(payload.question)
465
+
466
+
467
+
468
+ # retrieved_docs = app.state.retriever.get_relevant_documents(payload.question)
469
  context_text = "\n\n".join([doc.page_content for doc in retrieved_docs])
470
  context_tokens = count_tokens(context_text)
471
  chat_history = await chatbot_history_collection.find_one({"userId": payload.user_id})
App/utils.py CHANGED
@@ -19,6 +19,17 @@ import tiktoken
19
  from googleapiclient.discovery import build
20
  import numpy as np
21
 
 
 
 
 
 
 
 
 
 
 
 
22
  # MongoDB connection
23
  MONGODB_URI = os.getenv(
24
  "MONGODB_URI",
 
19
  from googleapiclient.discovery import build
20
  import numpy as np
21
 
22
+
23
+
24
+ os.environ['TRANSFORMERS_CACHE'] = '/tmp/transformers_cache'
25
+ os.environ['HF_HOME'] = '/tmp/huggingface_home'
26
+ os.environ['HUGGINGFACE_HUB_CACHE'] = '/tmp/huggingface_cache'
27
+
28
+ # Create the directories
29
+ os.makedirs('/tmp/transformers_cache', exist_ok=True)
30
+ os.makedirs('/tmp/huggingface_home', exist_ok=True)
31
+ os.makedirs('/tmp/huggingface_cache', exist_ok=True)
32
+
33
  # MongoDB connection
34
  MONGODB_URI = os.getenv(
35
  "MONGODB_URI",