forked from opea-project/GenAIComps
-
Notifications
You must be signed in to change notification settings - Fork 0
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Add reranking microservice (opea-project#21)
* Add reranking microservice Signed-off-by: lvliang-intel <[email protected]>
- Loading branch information
1 parent
1f6c1a5
commit 3bc899b
Showing
7 changed files
with
104 additions
and
15 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
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
Empty file.
This file was deleted.
Oops, something went wrong.
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,37 @@ | ||
# Copyright (c) 2024 Intel Corporation | ||
# | ||
# Licensed under the Apache License, Version 2.0 (the "License"); | ||
# you may not use this file except in compliance with the License. | ||
# You may obtain a copy of the License at | ||
# | ||
# http://www.apache.org/licenses/LICENSE-2.0 | ||
# | ||
# Unless required by applicable law or agreed to in writing, software | ||
# distributed under the License is distributed on an "AS IS" BASIS, | ||
# WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. | ||
# See the License for the specific language governing permissions and | ||
# limitations under the License. | ||
|
||
from sentence_transformers import CrossEncoder | ||
|
||
from comps import RerankingInputDoc, RerankingOutputDoc, opea_microservices, register_microservice | ||
|
||
|
||
@register_microservice( | ||
name="opea_service@local_reranking", | ||
expose_endpoint="/v1/reranking", | ||
port=8040, | ||
input_datatype=RerankingInputDoc, | ||
output_datatype=RerankingOutputDoc, | ||
) | ||
def reranking(input: RerankingInputDoc) -> RerankingOutputDoc: | ||
query_and_docs = [(input.query, doc.text) for doc in input.passages] | ||
scores = reranker_model.predict(query_and_docs) | ||
first_passage = sorted(list(zip(input.passages, scores)), key=lambda x: x[1], reverse=True)[0][0] | ||
res = RerankingOutputDoc(query=input.query, doc=first_passage) | ||
return res | ||
|
||
|
||
if __name__ == "__main__": | ||
reranker_model = CrossEncoder(model_name="BAAI/bge-reranker-large", max_length=512) | ||
opea_microservices["opea_service@local_reranking"].start() |
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,2 @@ | ||
docarray[full] | ||
sentence_transformers |
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,44 @@ | ||
# Copyright (c) 2024 Intel Corporation | ||
# | ||
# Licensed under the Apache License, Version 2.0 (the "License"); | ||
# you may not use this file except in compliance with the License. | ||
# You may obtain a copy of the License at | ||
# | ||
# http://www.apache.org/licenses/LICENSE-2.0 | ||
# | ||
# Unless required by applicable law or agreed to in writing, software | ||
# distributed under the License is distributed on an "AS IS" BASIS, | ||
# WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. | ||
# See the License for the specific language governing permissions and | ||
# limitations under the License. | ||
|
||
import json | ||
import os | ||
|
||
import requests | ||
|
||
from comps import RerankingInputDoc, RerankingOutputDoc, opea_microservices, register_microservice | ||
|
||
|
||
@register_microservice( | ||
name="opea_service@reranking_tgi_gaudi", | ||
expose_endpoint="/v1/reranking", | ||
port=8040, | ||
input_datatype=RerankingInputDoc, | ||
output_datatype=RerankingOutputDoc, | ||
) | ||
def reranking(input: RerankingInputDoc) -> RerankingOutputDoc: | ||
docs = [doc.text for doc in input.passages] | ||
url = tei_reranking_endpoint + "/rerank" | ||
data = {"query": input.query, "texts": docs} | ||
headers = {"Content-Type": "application/json"} | ||
response = requests.post(url, data=json.dumps(data), headers=headers) | ||
response_data = response.json() | ||
best_response = max(response_data, key=lambda response: response["score"]) | ||
res = RerankingOutputDoc(query=input.query, doc=input.passages[best_response["index"]]) | ||
return res | ||
|
||
|
||
if __name__ == "__main__": | ||
tei_reranking_endpoint = os.getenv("TEI_RERANKING_ENDPOINT", "http://localhost:8080") | ||
opea_microservices["opea_service@reranking_tgi_gaudi"].start() |