arjunanand13 commited on
Commit
4ad946f
1 Parent(s): 8b077e8

Update app.py

Browse files
Files changed (1) hide show
  1. app.py +23 -5
app.py CHANGED
@@ -187,6 +187,7 @@ def initialize_llmchain(llm_choice, temperature, max_tokens, top_k, vector_db, p
187
  return qa_chain, "LLM initialized successfully!"
188
 
189
  def conversation(qa_chain, message, history):
 
190
  response = qa_chain.invoke({
191
  "question": message,
192
  "chat_history": [(hist[0], hist[1]) for hist in history]
@@ -196,16 +197,33 @@ def conversation(qa_chain, message, history):
196
  if "Helpful Answer:" in response_answer:
197
  response_answer = response_answer.split("Helpful Answer:")[-1]
198
 
 
199
  sources = response["source_documents"][:3]
200
- source_contents = [s.page_content.strip() for s in sources]
201
- source_pages = [s.metadata.get("page", 0) + 1 for s in sources]
202
 
203
- while len(sources) < 3:
 
 
 
 
 
 
204
  source_contents.append("")
205
  source_pages.append(0)
206
 
207
- return (qa_chain, gr.update(value=""), history + [(message, response_answer)] +
208
- source_contents + source_pages)
 
 
 
 
 
 
 
 
 
 
209
 
210
  def demo():
211
  evaluator = RAGEvaluator()
 
187
  return qa_chain, "LLM initialized successfully!"
188
 
189
  def conversation(qa_chain, message, history):
190
+ """Fixed conversation function returning all required outputs"""
191
  response = qa_chain.invoke({
192
  "question": message,
193
  "chat_history": [(hist[0], hist[1]) for hist in history]
 
197
  if "Helpful Answer:" in response_answer:
198
  response_answer = response_answer.split("Helpful Answer:")[-1]
199
 
200
+ # Get source documents, ensure we have exactly 3
201
  sources = response["source_documents"][:3]
202
+ source_contents = []
203
+ source_pages = []
204
 
205
+ # Process available sources
206
+ for source in sources:
207
+ source_contents.append(source.page_content.strip())
208
+ source_pages.append(source.metadata.get("page", 0) + 1)
209
+
210
+ # Pad with empty values if we have fewer than 3 sources
211
+ while len(source_contents) < 3:
212
  source_contents.append("")
213
  source_pages.append(0)
214
 
215
+ # Return all required outputs in correct order
216
+ return (
217
+ qa_chain, # State
218
+ gr.update(value=""), # Clear message box
219
+ history + [(message, response_answer)], # Updated chat history
220
+ source_contents[0], # First source
221
+ source_pages[0], # First page
222
+ source_contents[1], # Second source
223
+ source_pages[1], # Second page
224
+ source_contents[2], # Third source
225
+ source_pages[2] # Third page
226
+ )
227
 
228
  def demo():
229
  evaluator = RAGEvaluator()