muse-agent-example/tests/集成/test_检索偏好重排.py

71 lines
3.3 KiB
Python
Raw Blame History

This file contains ambiguous Unicode characters

This file contains Unicode characters that might be confused with other characters. If you think that this is intentional, you can safely ignore this warning. Use the Escape button to reveal them.

"""检索偏好重排真实链路:挑选信号改变回执与排序;无信号时保持等权合同。"""
from uuid import uuid4
import pytest
import test_上下文快照与索引写入 as 方法测试
from muse.上下文.接口 import 混合检索
from muse.作者经验.接口 import 参考挑选反馈请求, 挑选候选
from muse.共享.调用身份 import 用途
from muse.基础设施.模型.嵌入 import 特征哈希嵌入器
pytestmark = pytest.mark.数据库
方法环境 = 方法测试.方法环境
def _确认方法(服务, 作者, 来源, 标题):
请求 = 方法测试._提案(str(uuid4()), 标题, str(来源.source_id), owner=作者.作者)
回执 = 服务.提出方法(作者, str(uuid4()), 请求)
方法测试._确认(服务, 作者, 回执["target_ref"])
版本 = 服务.读取方法详情(作者, 请求.method_id)["versions"][0]
return 请求.method_id, str(版本["version_id"])
@pytest.mark.case_id(
"NC-w32-a10019",
environment="隔离PG与真实检索索引;不证明文学效果",
given="两个已确认方法与新鲜索引,其一有真实挑选反馈",
when="经服务公开面检索",
then=["无信号回执保持等权合同;有信号启用重排并携带偏好分项与探索位"],
contract="docs/系统架构/新版设计/模块设计/B09-上下文.md#偏好重排通路",
)
def test_挑选信号改变检索回执且无信号保持等权__a10019(方法环境):
装配, 作者, 服务, 来源, _, 库 = 方法环境
_, 版本A = _确认方法(服务, 作者, 来源, "信息差打脸")
_, 版本B = _确认方法(服务, 作者, 来源, "信息差反转")
服务.重建索引(作者)
基线 = 服务.检索方法(作者, "信息差", work_id="method-work")
assert "重排权重版本" not in 基线
with 库[用途.生产].连接(只读=True) as 连:
裸 = 混合检索(
连, "信息差", 特征哈希嵌入器(), work_id="method-work", 数量=10, 作者=作者.作者
)
assert [(r["version_id"], r["得分"]) for r in 基线["结果"]] == [
(r["version_id"], r["得分"]) for r in 裸["结果"]
]
assert all("偏好" not in r for r in 基线["结果"])
经验 = 装配.要求作者经验()
经验.记录参考挑选反馈(
作者,
"rerank-sel-1",
参考挑选反馈请求(
query="信息差",
domain="combat",
candidates=(挑选候选(版本A, 1, 0.03), 挑选候选(版本B, 2, 0.02)),
selected=(版本A,),
index_generation=int(基线.get("index_generation", 0)),
),
)
重排 = 服务.检索方法(作者, "信息差", work_id="method-work", 领域="combat")
assert 重排["重排权重版本"] == "preference-rerank-v1"
行A = next(r for r in 重排["结果"] if r["version_id"] == 版本A)
行B = next(r for r in 重排["结果"] if r["version_id"] == 版本B)
基A = next(r for r in 基线["结果"] if r["version_id"] == 版本A)
assert 行A["偏好"]["选用次数"] == 1 and 行A["偏好"]["消费次数"] == 0
assert 行A["得分"] > 基A["得分"] # 正偏好第三路严格加分
# B 无任何挑选/消费信号:属未探索,标记探索位;A 已有信号不占探索位
assert 行B["探索位"] is True and 行A["探索位"] is False