-
Notifications
You must be signed in to change notification settings - Fork 346
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
- Loading branch information
黄宇扬
committed
Sep 25, 2024
1 parent
e0e9a6b
commit c9ae3fa
Showing
3 changed files
with
51 additions
and
4 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,30 @@ | ||
import asyncio | ||
import logging | ||
import json | ||
import traceback | ||
from fastapi import Request | ||
|
||
from .protocal.openai_protocol import * | ||
from ftllm import llm | ||
|
||
class FastLLmReranker: | ||
def __init__(self, | ||
model_name, | ||
model): | ||
self.model_name = model_name | ||
self.model = model | ||
|
||
def rerank(self, request: RerankRequest, raw_request: Request): | ||
query = request.query | ||
pairs = [] | ||
for text in request.texts: | ||
pairs.append([query, text]) | ||
scores = self.model.reranker_compute_score(pairs = pairs) | ||
ret = [] | ||
for i in range(len(request.texts)): | ||
now = {'index': i, 'score': scores[i]} | ||
if (request.return_text): | ||
now['text'] = request.texts[i] | ||
ret.append(now) | ||
ret = sorted(ret, key = lambda x : -x['score']) | ||
return ret |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters