abdullahtahir commited on
Commit
ca169fc
·
verified ·
1 Parent(s): fe8bb71

Update app.py

Browse files
Files changed (1) hide show
  1. app.py +10 -13
app.py CHANGED
@@ -127,30 +127,27 @@ def ask_bot_alternative(question):
127
  question = question[:500]
128
  print(f"\nProcessing question: {question}")
129
 
130
- # Retrieve and log context
131
- retriever = db.as_retriever(search_kwargs={"k": 3})
132
  context_docs = retriever.get_relevant_documents(question)
 
133
  print("\n--- Retrieved Context ---")
134
  for i, d in enumerate(context_docs, 1):
135
- print(f"[Doc {i}] {d.page_content[:200]}...\n")
136
 
137
- # Get answer from chain
138
- response = qa_chain.invoke({"query": question})
139
- if isinstance(response, dict):
140
- answer = response.get("result") or response.get("answer") or str(response)
141
- else:
142
- answer = str(response)
143
 
144
- answer = answer.strip()
145
- if len(answer) > 1000:
146
- answer = answer[:1000] + "..."
147
 
148
- return answer or "I could not find an answer in the portfolio content."
149
 
150
  except Exception as e:
151
  print(f"Error in ask_bot_alternative: {e}")
152
  return f"Sorry, I encountered an error: {str(e)[:200]}"
153
 
 
154
  # -------------------------------
155
  # 9. Gradio Interface
156
  # -------------------------------
 
127
  question = question[:500]
128
  print(f"\nProcessing question: {question}")
129
 
130
+ # Retrieve top documents
131
+ retriever = db.as_retriever(search_kwargs={"k": 1}) # only fetch the best match
132
  context_docs = retriever.get_relevant_documents(question)
133
+
134
  print("\n--- Retrieved Context ---")
135
  for i, d in enumerate(context_docs, 1):
136
+ print(f"[Doc {i}] {d.page_content[:300]}...\n")
137
 
138
+ if not context_docs:
139
+ return "I could not find an answer in the portfolio content."
 
 
 
 
140
 
141
+ # Always return the first match
142
+ answer = context_docs[0].page_content.strip()
 
143
 
144
+ return answer
145
 
146
  except Exception as e:
147
  print(f"Error in ask_bot_alternative: {e}")
148
  return f"Sorry, I encountered an error: {str(e)[:200]}"
149
 
150
+
151
  # -------------------------------
152
  # 9. Gradio Interface
153
  # -------------------------------