geqintan commited on
Commit
408f2cf
·
1 Parent(s): 67ec176
Files changed (1) hide show
  1. app.py +8 -3
app.py CHANGED
@@ -30,9 +30,14 @@ async def rerank(request: RerankerRequest):
30
  if not query or not documents:
31
  raise HTTPException(status_code=400, detail="Query and documents must be provided.")
32
 
33
- # Rerank the documents based on the query
34
- pairs = [[query, doc] for doc in documents]
35
- scores = model.predict(pairs)
 
 
 
 
 
36
 
37
  # Create a list of dictionaries containing the document and its score
38
  results = [{"document": doc, "score": score} for doc, score in zip(documents, scores)]
 
30
  if not query or not documents:
31
  raise HTTPException(status_code=400, detail="Query and documents must be provided.")
32
 
33
+ from sentence_transformers import util
34
+
35
+ # Calculate embeddings for the query and documents
36
+ query_embedding = model.encode(query, convert_to_tensor=True)
37
+ document_embeddings = model.encode(documents, convert_to_tensor=True)
38
+
39
+ # Calculate cosine similarity between the query and each document
40
+ scores = util.cos_sim(query_embedding, document_embeddings)[0].tolist()
41
 
42
  # Create a list of dictionaries containing the document and its score
43
  results = [{"document": doc, "score": score} for doc, score in zip(documents, scores)]