feat: rerank prompt

This commit is contained in:
李如威 2025-07-10 00:23:11 +08:00
parent c8b1754e73
commit 2a9654a65f
1 changed files with 24 additions and 19 deletions

View File

@ -24,6 +24,7 @@ class AsyncRAGService:
openai_api_key=self.openai_api_key,
openai_api_base=self.openai_api_base,
)
self.rerank_llm = ChatOpenAI(
model="deepseek-r1:8b",
temperature=0.7,
@ -31,17 +32,27 @@ class AsyncRAGService:
openai_api_base=self.openai_api_base,
)
self.rerank_prompt_template = PromptTemplate(
input_variables=["question", "content"],
template=(
"你是一个智能评分助手,请判断以下“文档片段”与“用户问题”的相关程度。\n"
"请只输出一个介于 0 到 1 之间的分数,数值越高表示相关性越强。\n\n"
"用户问题:\n"
"{question}\n\n"
"文档片段:\n"
"{content}"
),
)
self.prompt_template = PromptTemplate(
input_variables=["context", "question"],
template="""
基于以下上下文回答问题如果上下文中没有相关信息请说明无法从提供的文档中找到答案
上下文
{context}
问题{question}
答案""",
template=(
"基于以下上下文回答问题。如果上下文中没有相关信息,请说明无法从提供的文档中找到答案。\n\n"
"上下文:\n"
"{context}\n\n"
"问题:{question}\n\n"
"答案:"
),
)
self.logger.info("RAG服务初始化完成")
@ -216,16 +227,10 @@ class AsyncRAGService:
"""使用 rerank LLM 对搜索结果重新排序"""
async def score_result(result: Dict[str, Any]) -> float:
prompt = f"""
你是一个智能评分助手请判断以下文档片段用户问题的相关程度
请只输出一个介于 0 1 之间的分数数值越高表示相关性越强
用户问题
{question}
文档片段
{result['content'][:1000]}
"""
prompt = self.rerank_prompt_template.format(
content=result["content"][:1000],
question=question
)
try:
response = await asyncio.to_thread(self.rerank_llm.invoke, prompt)
score = float(response.content.strip())