cleanup: 删除旧的 rag/reranker.py
Some checks failed
构建并部署 AI Agent 服务 / deploy (push) Failing after 6m52s
Some checks failed
构建并部署 AI Agent 服务 / deploy (push) Failing after 6m52s
- rag/reranker.py 是旧代码,不再使用 - 现在使用 model_services/rerank_services.py 提供重排服务 - pipeline.py 已经通过 get_rerank_service() 使用服务层
This commit is contained in:
@@ -271,7 +271,7 @@ class TemplateManager:
|
||||
name: 模板名称
|
||||
|
||||
Returns:
|
||||
模板对象,如果不存在返回 None
|
||||
模板对象,如果不存在返回 None
|
||||
"""
|
||||
return self._templates.get(name)
|
||||
|
||||
@@ -479,3 +479,7 @@ class OutputRenderer:
|
||||
格式化后的字符串
|
||||
"""
|
||||
return self.markdown.format(data)
|
||||
"""
|
||||
格式化后的字符串
|
||||
"""
|
||||
return str(data)
|
||||
|
||||
@@ -1,75 +0,0 @@
|
||||
"""
|
||||
重排序器模块 (适配版)
|
||||
使用远程 llama.cpp 服务 (兼容 OpenAI Rerank API) 替代本地 Cross-Encoder
|
||||
"""
|
||||
import requests
|
||||
from typing import List
|
||||
from langchain_core.documents import Document
|
||||
|
||||
class LLaMaCPPReranker:
|
||||
"""使用远程 llama.cpp 服务对检索结果重排序。"""
|
||||
|
||||
def __init__(self,
|
||||
base_url: str,
|
||||
api_key: str,
|
||||
top_n: int = 5,
|
||||
timeout: int = 60):
|
||||
"""
|
||||
初始化远程重排序器
|
||||
|
||||
Args:
|
||||
base_url: llama.cpp 服务的地址和端口,默认为环境变量 LLAMACPP_RERANKER_URL 或 "http://127.0.0.1:8083"。
|
||||
top_n: 返回前 N 个结果。
|
||||
api_key: API 密钥,默认为环境变量 LLAMACPP_API_KEY 。
|
||||
timeout: 请求超时时间(秒)。
|
||||
"""
|
||||
self.base_url = base_url
|
||||
self.api_key = api_key
|
||||
self.top_n = top_n
|
||||
self.timeout = timeout
|
||||
self.endpoint = f"{self.base_url}/rerank"
|
||||
|
||||
def compress_documents(
|
||||
self, documents: List[Document], query: str
|
||||
) -> List[Document]:
|
||||
"""
|
||||
对文档进行重排序
|
||||
|
||||
Args:
|
||||
documents: 待排序的文档列表
|
||||
query: 查询字符串
|
||||
|
||||
Returns:
|
||||
排序后的文档列表
|
||||
"""
|
||||
if not documents:
|
||||
return []
|
||||
|
||||
# 准备请求体
|
||||
# 根据 llama.cpp 的 OpenAI 兼容性,文档是一个字符串列表
|
||||
payload = {
|
||||
"model": "bge-reranker-v2-m3",
|
||||
"query": query,
|
||||
"documents": [doc.page_content for doc in documents],
|
||||
"top_n": self.top_n
|
||||
}
|
||||
headers = {
|
||||
"Content-Type": "application/json",
|
||||
"Authorization": f"Bearer {self.api_key}"
|
||||
}
|
||||
|
||||
try:
|
||||
response = requests.post(self.endpoint, json=payload, headers=headers, timeout=self.timeout)
|
||||
response.raise_for_status() # 检查请求是否成功
|
||||
results = response.json()
|
||||
|
||||
# 解析返回结果
|
||||
# 返回格式: {"results": [{"index": 0, "document": "...", "relevance_score": 0.8}, ...]}
|
||||
# 按相关性得分降序排列
|
||||
sorted_indices = [item["index"] for item in results["results"]]
|
||||
sorted_docs = [documents[idx] for idx in sorted_indices]
|
||||
return sorted_docs
|
||||
|
||||
except Exception as e:
|
||||
print(f"警告: 远程重排序过程出错,将使用原始排序。错误: {e}")
|
||||
return documents[:self.top_n]
|
||||
Reference in New Issue
Block a user