From e37a4e32e84365c8bd8a78c1b1256cdc7047ee85 Mon Sep 17 00:00:00 2001 From: Liangyx2 Date: Fri, 14 Jun 2024 10:34:05 +0800 Subject: [PATCH 1/2] Use parameter for reranker Signed-off-by: Liangyx2 --- comps/cores/proto/docarray.py | 2 +- comps/reranks/README.md | 8 ++++++++ comps/reranks/langchain/reranking_tei_xeon.py | 10 ++++++---- 3 files changed, 15 insertions(+), 5 deletions(-) diff --git a/comps/cores/proto/docarray.py b/comps/cores/proto/docarray.py index c657d0c2e4..a3abc75a45 100644 --- a/comps/cores/proto/docarray.py +++ b/comps/cores/proto/docarray.py @@ -50,7 +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()} diff --git a/comps/reranks/README.md b/comps/reranks/README.md index f8f12251ee..dd93c007c7 100644 --- a/comps/reranks/README.md +++ b/comps/reranks/README.md @@ -100,3 +100,11 @@ 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' +``` diff --git a/comps/reranks/langchain/reranking_tei_xeon.py b/comps/reranks/langchain/reranking_tei_xeon.py index 394264743f..f61e77a84f 100644 --- a/comps/reranks/langchain/reranking_tei_xeon.py +++ b/comps/reranks/langchain/reranking_tei_xeon.py @@ -8,7 +8,7 @@ import requests from langchain_core.prompts import ChatPromptTemplate from langsmith import traceable - +import heapq from comps import ( LLMParamsDoc, SearchedDoc, @@ -39,14 +39,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()) From e0c449f3f4be0abe2ca414d2085455631e0e73be Mon Sep 17 00:00:00 2001 From: "pre-commit-ci[bot]" <66853113+pre-commit-ci[bot]@users.noreply.github.com> Date: Fri, 14 Jun 2024 02:36:23 +0000 Subject: [PATCH 2/2] [pre-commit.ci] auto fixes from pre-commit.com hooks for more information, see https://pre-commit.ci --- comps/cores/proto/docarray.py | 1 + comps/reranks/README.md | 3 ++- comps/reranks/langchain/reranking_tei_xeon.py | 9 +++++---- 3 files changed, 8 insertions(+), 5 deletions(-) diff --git a/comps/cores/proto/docarray.py b/comps/cores/proto/docarray.py index a3abc75a45..f74f8a8c06 100644 --- a/comps/cores/proto/docarray.py +++ b/comps/cores/proto/docarray.py @@ -51,6 +51,7 @@ class SearchedDoc(BaseDoc): retrieved_docs: DocList[TextDoc] initial_query: str top_n: int = 1 + class Config: json_encoders = {np.ndarray: lambda x: x.tolist()} diff --git a/comps/reranks/README.md b/comps/reranks/README.md index dd93c007c7..0d90cb2f22 100644 --- a/comps/reranks/README.md +++ b/comps/reranks/README.md @@ -101,7 +101,8 @@ curl http://localhost:8000/v1/reranking \ -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. +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 \ diff --git a/comps/reranks/langchain/reranking_tei_xeon.py b/comps/reranks/langchain/reranking_tei_xeon.py index f61e77a84f..c3535e9772 100644 --- a/comps/reranks/langchain/reranking_tei_xeon.py +++ b/comps/reranks/langchain/reranking_tei_xeon.py @@ -1,6 +1,7 @@ # Copyright (C) 2024 Intel Corporation # SPDX-License-Identifier: Apache-2.0 +import heapq import json import os import time @@ -8,7 +9,7 @@ import requests from langchain_core.prompts import ChatPromptTemplate from langsmith import traceable -import heapq + from comps import ( LLMParamsDoc, SearchedDoc, @@ -39,15 +40,15 @@ 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_list=heapq.nlargest(input.top_n, response_data, key=lambda x: x['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) - context_str='' + context_str = "" for best_response in best_response_list: - context_str=context_str+' '+input.retrieved_docs[best_response["index"]].text + 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())