Update app.py to support reranking using SweRankLLM-Small
Browse files
app.py
CHANGED
|
@@ -1,7 +1,44 @@
|
|
| 1 |
from fastapi import FastAPI
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 2 |
|
| 3 |
app = FastAPI()
|
| 4 |
|
| 5 |
@app.get("/")
|
| 6 |
def hello_world():
|
| 7 |
-
return {"msg": "Success"}
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 1 |
from fastapi import FastAPI
|
| 2 |
+
from reranker import RankLLM, RankListwiseOSLLM, Result, RankingExecInfo
|
| 3 |
+
from pydantic import BaseModel
|
| 4 |
+
from typing import Optional, List, Tuple
|
| 5 |
+
|
| 6 |
+
# load RankListwiseOSLLM
|
| 7 |
+
reranker = RankListwiseOSLLM("Salesforce/SweRankLLM-small")
|
| 8 |
+
|
| 9 |
+
class RerankRequest(BaseModel):
|
| 10 |
+
query: str
|
| 11 |
+
hits: List[Tuple[int, str]]
|
| 12 |
+
|
| 13 |
+
class RerankResponse(BaseModel):
|
| 14 |
+
hits: List[Tuple[int, str]]
|
| 15 |
|
| 16 |
app = FastAPI()
|
| 17 |
|
| 18 |
@app.get("/")
|
| 19 |
def hello_world():
|
| 20 |
+
return {"msg": "Success"}
|
| 21 |
+
|
| 22 |
+
|
| 23 |
+
@app.get("/rerank")
|
| 24 |
+
def rerank(request: RerankRequest):
|
| 25 |
+
hits = request.hits
|
| 26 |
+
sorted_hits = sorted(hits, key=lambda x: x[0]) # sort hits again for safety
|
| 27 |
+
|
| 28 |
+
result = Result(
|
| 29 |
+
query=request["query"],
|
| 30 |
+
hits = [{"content": hit} for hit in sorted_hits]
|
| 31 |
+
)
|
| 32 |
+
|
| 33 |
+
reranked_result = reranker.permutation_pipeline(
|
| 34 |
+
result,
|
| 35 |
+
1,
|
| 36 |
+
len(hits),
|
| 37 |
+
logging=True
|
| 38 |
+
)
|
| 39 |
+
|
| 40 |
+
response = [(i, item["content"]) for i, item in enumerate(reranked_result.hits)]
|
| 41 |
+
|
| 42 |
+
return {"reranked": response}
|
| 43 |
+
|
| 44 |
+
|