ChristopherMarais commited on
Commit
bdf6636
·
verified ·
1 Parent(s): 06b3311

Update app.py

Browse files
Files changed (1) hide show
  1. app.py +14 -6
app.py CHANGED
@@ -36,7 +36,7 @@ PRE_PROMPT = load_decrypted_preprompt()
36
  # Default parameters for the QA chain
37
  DEFAULT_TEMPERATURE = 0.7
38
  DEFAULT_MAX_TOKENS = 1024
39
- DEFAULT_TOP_K = 3
40
  DEFAULT_TOP_P = 0.95
41
 
42
  def load_vector_db(index_path="faiss_index", model_name="sentence-transformers/all-MiniLM-L6-v2"):
@@ -154,16 +154,21 @@ def get_assistant_response(message, history, max_tokens, temperature, top_p, qa_
154
  qa_chain = qa_chain_state_dict.get("qa_chain")
155
 
156
  if qa_chain is not None:
 
157
  formatted_history = format_chat_history(history)
 
158
  combined_question = PRE_PROMPT + "\n" + message
159
- print("Combined Question:", combined_question) # Debug print
160
  response = qa_chain.invoke({"question": combined_question, "chat_history": formatted_history})
161
- print("QA Chain Response:", response) # Debug print
162
- answer = response.get("answer", "")
 
 
 
 
163
  history.append({"role": "assistant", "content": answer})
164
  return history, {"qa_chain": qa_chain}
165
 
166
- # Fallback: Plain Chat Mode using the InferenceClient (pre-prompt already included here)
167
  messages = [{"role": "system", "content": PRE_PROMPT}] + history
168
  response = ""
169
  result = client.chat_completion(
@@ -173,10 +178,13 @@ def get_assistant_response(message, history, max_tokens, temperature, top_p, qa_
173
  temperature=temperature,
174
  top_p=top_p,
175
  )
176
- print("Chat Completion Result:", result) # Debug print
177
  for token_message in result:
178
  token = token_message.choices[0].delta.content
179
  response += token
 
 
 
 
180
 
181
  history.append({"role": "assistant", "content": response})
182
  return history, {"qa_chain": qa_chain}
 
36
  # Default parameters for the QA chain
37
  DEFAULT_TEMPERATURE = 0.7
38
  DEFAULT_MAX_TOKENS = 1024
39
+ DEFAULT_TOP_K = 10
40
  DEFAULT_TOP_P = 0.95
41
 
42
  def load_vector_db(index_path="faiss_index", model_name="sentence-transformers/all-MiniLM-L6-v2"):
 
154
  qa_chain = qa_chain_state_dict.get("qa_chain")
155
 
156
  if qa_chain is not None:
157
+ # Format history to the plain-text format expected by the QA chain
158
  formatted_history = format_chat_history(history)
159
+ # Prepend the pre-prompt to the current question
160
  combined_question = PRE_PROMPT + "\n" + message
 
161
  response = qa_chain.invoke({"question": combined_question, "chat_history": formatted_history})
162
+ answer = response.get("answer", "").strip()
163
+
164
+ # Check if the answer is empty and apply a fallback response if needed.
165
+ if not answer:
166
+ answer = "I'm sorry, I couldn't retrieve a clear answer. Could you please rephrase your question?"
167
+
168
  history.append({"role": "assistant", "content": answer})
169
  return history, {"qa_chain": qa_chain}
170
 
171
+ # Fallback: Plain Chat Mode using the InferenceClient
172
  messages = [{"role": "system", "content": PRE_PROMPT}] + history
173
  response = ""
174
  result = client.chat_completion(
 
178
  temperature=temperature,
179
  top_p=top_p,
180
  )
 
181
  for token_message in result:
182
  token = token_message.choices[0].delta.content
183
  response += token
184
+
185
+ response = response.strip()
186
+ if not response:
187
+ response = "I'm sorry, I couldn't generate a response. Please try asking in a different way. Alterantively, consider contacting Christopher directly: https://gcmarais.com/contact/"
188
 
189
  history.append({"role": "assistant", "content": response})
190
  return history, {"qa_chain": qa_chain}