init: RAG 知识库服务初始提交
- 后端 API(Flask + Gunicorn) - RAG 引擎(混合检索 + 云端 Reranker + 引用溯源) - 文档解析(MinerU + 多格式支持) - Docker 生产部署配置 - 排除前端项目、敏感配置、模型文件
This commit is contained in:
137
core/agentic_quality.py
Normal file
137
core/agentic_quality.py
Normal file
@@ -0,0 +1,137 @@
|
||||
"""
|
||||
Agentic RAG - 质量评估 Mixin
|
||||
|
||||
包含置信度门控、质量评估、推理反思等方法
|
||||
"""
|
||||
|
||||
import logging
|
||||
|
||||
from .agentic_base import logger
|
||||
|
||||
logger = logging.getLogger(__name__)
|
||||
|
||||
|
||||
class QualityMixin:
|
||||
"""质量评估方法"""
|
||||
|
||||
def _check_confidence_gate(self, query: str, docs: list, verbose: bool = True,
|
||||
precomputed_scores: list = None):
|
||||
"""检查置信度门控
|
||||
|
||||
Args:
|
||||
query: 用户查询
|
||||
docs: 文档列表
|
||||
verbose: 是否详细输出
|
||||
precomputed_scores: 预计算的 Rerank 分数(可选,避免重复推理)
|
||||
"""
|
||||
if not self.confidence_gate:
|
||||
return {"passed": True, "reason": "no_gate"}
|
||||
|
||||
try:
|
||||
result = self.confidence_gate.evaluate(query, docs,
|
||||
precomputed_scores=precomputed_scores)
|
||||
return result
|
||||
except Exception as e:
|
||||
logger.warning(f"置信度门控检查失败: {e}")
|
||||
return {"passed": True, "reason": "error"}
|
||||
|
||||
def _assess_quality(self, query: str, docs: list, metas: list = None,
|
||||
verbose: bool = True) -> dict:
|
||||
"""多维质量评估"""
|
||||
if not self.quality_assessor:
|
||||
return {"overall_score": 0.5, "dimensions": {}}
|
||||
|
||||
try:
|
||||
result = self.quality_assessor.assess(query, docs, metas)
|
||||
return result
|
||||
except Exception as e:
|
||||
logger.warning(f"质量评估失败: {e}")
|
||||
return {"overall_score": 0.5, "dimensions": {}}
|
||||
|
||||
def _reflect_on_answer(self, query: str, answer: str, contexts: list,
|
||||
verbose: bool = True) -> dict:
|
||||
"""推理反思"""
|
||||
if not self.reasoning_reflector:
|
||||
return {"needs_reflection": False, "issues": []}
|
||||
|
||||
try:
|
||||
result = self.reasoning_reflector.reflect(query, answer, contexts)
|
||||
return result
|
||||
except Exception as e:
|
||||
logger.warning(f"推理反思失败: {e}")
|
||||
return {"needs_reflection": False, "issues": []}
|
||||
|
||||
def _think(self, original_query: str, current_query: str,
|
||||
iteration: int, contexts: list, verbose: bool = True) -> dict:
|
||||
"""
|
||||
Agent 思考:决定下一步行动
|
||||
|
||||
Returns:
|
||||
{
|
||||
"action": "answer" | "rewrite" | "search_web" | "decompose",
|
||||
"reason": "...",
|
||||
"rewrite_query": "..." # 如果 action == "rewrite"
|
||||
}
|
||||
"""
|
||||
from core.llm_utils import call_llm, parse_json_from_response
|
||||
from .agentic_base import MODEL
|
||||
|
||||
# 构建思考提示
|
||||
context_summary = ""
|
||||
if contexts:
|
||||
for i, ctx in enumerate(contexts[:3], 1):
|
||||
meta = ctx.get('meta', {})
|
||||
source = meta.get('source', '未知')
|
||||
doc_preview = ctx.get('doc', '')[:100]
|
||||
context_summary += f"{i}. [{source}] {doc_preview}...\n"
|
||||
|
||||
prompt = f"""你是一个 RAG 系统的决策 Agent,需要判断下一步行动。
|
||||
|
||||
【原始问题】
|
||||
{original_query}
|
||||
|
||||
【当前问题】
|
||||
{current_query}
|
||||
|
||||
【迭代轮次】
|
||||
{iteration} / {self.max_iterations}
|
||||
|
||||
【已检索到的上下文】
|
||||
{context_summary if context_summary else "(无)"}
|
||||
|
||||
【可选行动】
|
||||
1. answer - 已有足够信息,可以回答
|
||||
2. rewrite - 查询不够清晰,需要重写
|
||||
3. search_web - 知识库信息不足,需要网络搜索
|
||||
4. decompose - 问题太复杂,需要分解
|
||||
|
||||
【决策要求】
|
||||
- 如果上下文足够回答问题,选择 answer
|
||||
- 如果上下文不足且迭代未超限,选择 search_web 或 rewrite
|
||||
- 返回 JSON 格式
|
||||
|
||||
请决策:"""
|
||||
|
||||
try:
|
||||
result = call_llm(
|
||||
self.client, prompt, MODEL,
|
||||
temperature=0.3,
|
||||
max_tokens=200
|
||||
)
|
||||
|
||||
decision = parse_json_from_response(result) if result else {}
|
||||
|
||||
# 默认决策
|
||||
if not decision or "action" not in decision:
|
||||
if contexts and len(contexts) >= 2:
|
||||
decision = {"action": "answer", "reason": "有足够上下文"}
|
||||
else:
|
||||
decision = {"action": "rewrite", "reason": "上下文不足"}
|
||||
|
||||
return decision
|
||||
|
||||
except Exception as e:
|
||||
logger.warning(f"Agent 思考失败: {e}")
|
||||
if contexts:
|
||||
return {"action": "answer", "reason": "默认回答"}
|
||||
return {"action": "rewrite", "reason": "默认重写"}
|
||||
Reference in New Issue
Block a user