itsskofficial commited on
Commit
f0298af
·
1 Parent(s): 9e84deb

added error handling

Browse files
Files changed (1) hide show
  1. agent.py +11 -1
agent.py CHANGED
@@ -190,12 +190,22 @@ def build_graph(provider: str = "google"):
190
 
191
  def retriever(state: MessagesState):
192
  query = state["messages"][-1].content
193
- similar_doc = vector_store.similarity_search(query, k=1)[0]
194
 
 
 
 
 
 
 
 
 
195
  content = similar_doc.page_content
 
196
  if "Final answer :" in content:
197
  answer = content.split("Final answer :")[-1].strip()
198
  else:
 
199
  answer = content.strip()
200
 
201
  return {"messages": [AIMessage(content=answer)]}
 
190
 
191
  def retriever(state: MessagesState):
192
  query = state["messages"][-1].content
193
+ similar_docs = vector_store.similarity_search(query, k=1)
194
 
195
+ if not similar_docs:
196
+ # If no similar documents are found, return a default message.
197
+ # This prevents the "list index out of range" error.
198
+ answer = "I could not find a similar question in my knowledge base."
199
+ return {"messages": [AIMessage(content=answer)]}
200
+
201
+ # If we found documents, proceed as before.
202
+ similar_doc = similar_docs[0]
203
  content = similar_doc.page_content
204
+
205
  if "Final answer :" in content:
206
  answer = content.split("Final answer :")[-1].strip()
207
  else:
208
+ # Fallback if the document format is unexpected
209
  answer = content.strip()
210
 
211
  return {"messages": [AIMessage(content=answer)]}