Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Use parameter for reranker #177

Merged
merged 5 commits into from
Jun 19, 2024
Merged
Show file tree
Hide file tree
Changes from 3 commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
1 change: 1 addition & 0 deletions comps/cores/proto/docarray.py
Original file line number Diff line number Diff line change
Expand Up @@ -50,6 +50,7 @@ class EmbedDoc1024(BaseDoc):
class SearchedDoc(BaseDoc):
retrieved_docs: DocList[TextDoc]
initial_query: str
top_n: int = 1

class Config:
json_encoders = {np.ndarray: lambda x: x.tolist()}
Expand Down
9 changes: 9 additions & 0 deletions comps/reranks/README.md
Original file line number Diff line number Diff line change
Expand Up @@ -100,3 +100,12 @@ curl http://localhost:8000/v1/reranking \
-d '{"initial_query":"What is Deep Learning?", "retrieved_docs": [{"text":"Deep Learning is not..."}, {"text":"Deep learning is..."}]}' \
-H 'Content-Type: application/json'
```

You can add the parameter `top_n` to specify the return number of the reranker model, default value is 1.

```bash
curl http://localhost:8000/v1/reranking \
-X POST \
-d '{"initial_query":"What is Deep Learning?", "retrieved_docs": [{"text":"Deep Learning is not..."}, {"text":"Deep learning is..."}], "top_n":2}' \
-H 'Content-Type: application/json'
```
9 changes: 6 additions & 3 deletions comps/reranks/langchain/reranking_tei_xeon.py
Original file line number Diff line number Diff line change
@@ -1,6 +1,7 @@
# Copyright (C) 2024 Intel Corporation
# SPDX-License-Identifier: Apache-2.0

import heapq
import json
import os
import time
Expand Down Expand Up @@ -39,14 +40,16 @@ def reranking(input: SearchedDoc) -> LLMParamsDoc:
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"])
best_response_list = heapq.nlargest(input.top_n, response_data, key=lambda x: x["score"])
template = """Answer the question based only on the following context:
{context}
Question: {question}
"""
prompt = ChatPromptTemplate.from_template(template)
doc = input.retrieved_docs[best_response["index"]]
final_prompt = prompt.format(context=doc.text, question=input.initial_query)
context_str = ""
for best_response in best_response_list:
context_str = context_str + " " + input.retrieved_docs[best_response["index"]].text
final_prompt = prompt.format(context=context_str, question=input.initial_query)
statistics_dict["opea_service@reranking_tgi_gaudi"].append_latency(time.time() - start, None)
return LLMParamsDoc(query=final_prompt.strip())

Expand Down
Loading