Lhumpal commited on
Commit
1f5682b
·
verified ·
1 Parent(s): 08a4aab

Update app.py

Browse files
Files changed (1) hide show
  1. app.py +8 -9
app.py CHANGED
@@ -36,18 +36,17 @@ def build_faiss_vectorstore(chunks):
36
  print(f"Total number of documents: {num_documents}")
37
  return vectorstore
38
 
39
- # Function to retrieve similar text
40
  # Function to retrieve similar text
41
  def retrieve(query, vectorstore, top_k=5):
42
  docs_and_scores = vectorstore.similarity_search_with_score(query=query, k=top_k)
43
 
44
- # Return (page_content, score) for docs that meet the score threshold
45
- return [
46
- (doc.page_content, float(score)) # Ensure score is a standard float
47
- for doc, score in docs_and_scores
48
- if float(score) <= 0.75
49
- ]
50
 
 
51
 
52
  class ChatRequest(BaseModel):
53
  message: str
@@ -99,7 +98,7 @@ async def chat(request: ChatRequest):
99
 
100
 
101
  # Retrieve relevant text
102
- docs, scores = retrieve(request.message, vectorstore, top_k=5)
103
  docs = "\n\n".join(docs)
104
 
105
  rag_prompt = f"""Use the following information to answer the user's query. You do not have to use all the information, just the pieces that directly
@@ -142,7 +141,7 @@ async def chat(request: ChatRequest):
142
  del request.chat_history[-1]
143
  request.chat_history.append({"role": "user", "parts": [{"text": request.message}]})
144
 
145
- return {"response": response.text, "dataset_str": concise_text_string, "docs": docs, "history": request.chat_history, "RAG_prompt": rag_prompt, "chunks": chunks}
146
 
147
  if request.model_choice == "HF":
148
  if hf_token:
 
36
  print(f"Total number of documents: {num_documents}")
37
  return vectorstore
38
 
 
39
  # Function to retrieve similar text
40
  def retrieve(query, vectorstore, top_k=5):
41
  docs_and_scores = vectorstore.similarity_search_with_score(query=query, k=top_k)
42
 
43
+ # Filter results based on score threshold
44
+ filtered_docs_and_scores = [(doc.page_content, float(score)) for doc, score in docs_and_scores if float(score) <= 0.75]
45
+
46
+ # Separate docs from the (doc, score) tuples
47
+ docs_content = [doc for doc, _ in filtered_docs_and_scores]
 
48
 
49
+ return docs_content, filtered_docs_and_scores
50
 
51
  class ChatRequest(BaseModel):
52
  message: str
 
98
 
99
 
100
  # Retrieve relevant text
101
+ docs, filtered_docs_and_scores = retrieve(request.message, vectorstore, top_k=5)
102
  docs = "\n\n".join(docs)
103
 
104
  rag_prompt = f"""Use the following information to answer the user's query. You do not have to use all the information, just the pieces that directly
 
141
  del request.chat_history[-1]
142
  request.chat_history.append({"role": "user", "parts": [{"text": request.message}]})
143
 
144
+ return {"response": response.text, "dataset_str": concise_text_string, "docs": docs, "filtered_docs_and_scores": filtered_docs_and_scores, "history": request.chat_history, "RAG_prompt": rag_prompt, "chunks": chunks}
145
 
146
  if request.model_choice == "HF":
147
  if hf_token: