chaithanyashaji commited on
Commit
f7254ad
·
verified ·
1 Parent(s): 114e142

Update main.py

Browse files
Files changed (1) hide show
  1. main.py +8 -3
main.py CHANGED
@@ -59,7 +59,7 @@ except Exception as e:
59
  # ==========================
60
  try:
61
  db = FAISS.load_local("ipc_vector_db", embeddings, allow_dangerous_deserialization=True)
62
- db_retriever = db.as_retriever(search_type="similarity", search_kwargs={"k": 2, "max-length": 512})
63
  logger.info("Vectorstore successfully loaded.")
64
  except Exception as e:
65
  logger.error(f"Error loading FAISS vectorstore: {e}")
@@ -68,13 +68,15 @@ except Exception as e:
68
  # ==========================
69
  # Define Prompt Template
70
  # ==========================
71
- prompt_template = """<s>[INST]As a legal chatbot specializing in the Indian Penal Code, provide a concise and accurate answer based on the given context. Avoid unnecessary details or unrelated content. Only respond if the answer can be derived from the provided context; otherwise, say \"The information is not available in the provided context.\"
 
72
  CONTEXT: {context}
73
  CHAT HISTORY: {chat_history}
74
  QUESTION: {question}
75
  ANSWER:
76
  </s>[INST]"""
77
 
 
78
  prompt = PromptTemplate(template=prompt_template, input_variables=["context", "question", "chat_history"])
79
 
80
  # ==========================
@@ -124,12 +126,15 @@ async def chat(request: ChatRequest):
124
  try:
125
  logger.debug(f"Received user question: {request.question}")
126
  result = qa.invoke(input=request.question)
127
- answer = result.get("answer", "I'm sorry, but I couldn't generate a response to your query. Please try rephrasing or providing more details.")
 
 
128
  return ChatResponse(answer=answer)
129
  except Exception as e:
130
  logger.error(f"Error during chat invocation: {e}")
131
  raise HTTPException(status_code=500, detail="Oops! Something went wrong on our end. Please try again later.")
132
 
 
133
  # ==========================
134
  # Run Uvicorn Server
135
  # ==========================
 
59
  # ==========================
60
  try:
61
  db = FAISS.load_local("ipc_vector_db", embeddings, allow_dangerous_deserialization=True)
62
+ db_retriever = db.as_retriever(search_type="similarity", search_kwargs={"k": 4, "max-length": 512})
63
  logger.info("Vectorstore successfully loaded.")
64
  except Exception as e:
65
  logger.error(f"Error loading FAISS vectorstore: {e}")
 
68
  # ==========================
69
  # Define Prompt Template
70
  # ==========================
71
+ prompt_template = """<s>[INST]You are a legal chatbot specializing in the Indian Penal Code. Provide concise, context-aware answers in a conversational tone. Avoid presenting the response as a question-answer format unless explicitly required.
72
+ If the answer cannot be derived from the given context, respond with: "I'm sorry, I couldn't find relevant information for your query."
73
  CONTEXT: {context}
74
  CHAT HISTORY: {chat_history}
75
  QUESTION: {question}
76
  ANSWER:
77
  </s>[INST]"""
78
 
79
+
80
  prompt = PromptTemplate(template=prompt_template, input_variables=["context", "question", "chat_history"])
81
 
82
  # ==========================
 
126
  try:
127
  logger.debug(f"Received user question: {request.question}")
128
  result = qa.invoke(input=request.question)
129
+ answer = result.get("answer")
130
+ if not answer or "The information is not available in the provided context" in answer:
131
+ answer = "I'm sorry, I couldn't find relevant information for your query. Please try rephrasing or providing more details."
132
  return ChatResponse(answer=answer)
133
  except Exception as e:
134
  logger.error(f"Error during chat invocation: {e}")
135
  raise HTTPException(status_code=500, detail="Oops! Something went wrong on our end. Please try again later.")
136
 
137
+
138
  # ==========================
139
  # Run Uvicorn Server
140
  # ==========================