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

fix bugs of rerank model with xinference #1481

Merged
merged 1 commit into from
Jul 12, 2024
Merged
Show file tree
Hide file tree
Changes from all 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
11 changes: 11 additions & 0 deletions api/apps/llm_app.py
Original file line number Diff line number Diff line change
Expand Up @@ -165,6 +165,17 @@ def add_llm():
except Exception as e:
msg += f"\nFail to access model({llm['llm_name']})." + str(
e)
elif llm["model_type"] == LLMType.RERANK:
mdl = RerankModel[factory](
key=None, model_name=llm["llm_name"], base_url=llm["api_base"]
)
try:
arr, tc = mdl.similarity("Hello~ Ragflower!", ["Hi, there!"])
if len(arr) == 0 or tc == 0:
raise Exception("Not known.")
except Exception as e:
msg += f"\nFail to access model({llm['llm_name']})." + str(
e)
else:
# TODO: check other type of models
pass
Expand Down
16 changes: 9 additions & 7 deletions rag/llm/rerank_model.py
Original file line number Diff line number Diff line change
Expand Up @@ -136,22 +136,24 @@ def similarity(self, query: str, texts: list):
else: res.extend(scores)
return np.array(res), token_count


class XInferenceRerank(Base):
def __init__(self,model_name="",base_url=""):
self.model_name=model_name
self.base_url=base_url
def __init__(self, key="xxxxxxx", model_name="", base_url=""):
self.model_name = model_name
self.base_url = base_url
self.headers = {
"Content-Type": "application/json",
"accept": "application/json"
}

def similarity(self, query: str, texts: list):
data = {
"model":self.model_name,
"query":query,
"model": self.model_name,
"query": query,
"return_documents": "true",
"return_len": "true",
"documents":texts
"documents": texts
}
res = requests.post(self.base_url, headers=self.headers, json=data).json()
return np.array([d["relevance_score"] for d in res["results"]]),res["tokens"]["input_tokens"]+res["tokens"]["output_tokens"]
return np.array([d["relevance_score"] for d in res["results"]]), res["tokens"]["input_tokens"] + res["tokens"][
"output_tokens"]