# -*- coding: utf-8 -*- """verify 端点测试:L2→cosine 换算 + 阈值判定。""" import pytest from httpx import AsyncClient, ASGITransport from app.main import app class FakeDoc: def __init__(self, content, metadata): self.page_content = content self.metadata = metadata class FakeCollection: def __init__(self, emb_by_id): self._emb = emb_by_id def get(self, ids=None, include=None): return {"ids": ids, "embeddings": [self._emb[i] for i in ids]} class FakeStore: def __init__(self, emb_by_id, candidates): self._collection = FakeCollection(emb_by_id) self._candidates = candidates def similarity_search_with_score(self, query, k=None, filter=None): return self._candidates[:k] class FakeEmbeddings: def __init__(self, q): self.q = q def embed_query(self, text): return self.q def _patch_kb(monkeypatch, query_vec, emb_by_id, candidates): import app.api.knowledge_base as kb fake_store = FakeStore(emb_by_id, candidates) monkeypatch.setattr(kb, "Chroma", lambda *a, **kw: fake_store) monkeypatch.setattr(kb, "get_embeddings", lambda: FakeEmbeddings(query_vec)) return fake_store @pytest.mark.asyncio async def test_verify_converts_l2_to_cosine(monkeypatch): """关键:L2 距离≠相似度。候选向量 [0.5,0.5,0,0] 与 query [1,0,0,0] 真实 cosine=0.7071;若误用 L2(1-d^2/2) 会得 0.75。断言用真实 cosine。""" q = [1.0, 0.0, 0.0, 0.0] # 非归一化候选:真实 cos = 0.5 / sqrt(0.5) = 0.70710678 emb = {"cand1": [0.5, 0.5, 0.0, 0.0]} cands = [(FakeDoc("AKK菌相关内容...", {"id": "cand1", "title": "AKK菌——下一代有益菌", "source": "microbiome"}), 0.7071)] _patch_kb(monkeypatch, q, emb, cands) transport = ASGITransport(app=app) async with AsyncClient(transport=transport, base_url="http://test") as client: resp = await client.post("/api/v1/knowledge-base/verify", json={ "sentences": [{"id": "s1", "text": "双歧杆菌有助于维持肠道菌群平衡"}], "source": "microbiome", "k": 3, "threshold": 0.7, }) assert resp.status_code == 200 data = resp.json() assert data["code"] == 200 r = data["data"]["results"][0] assert r["id"] == "s1" got = r.get("max_similarity") assert got == pytest.approx(0.70710678, abs=1e-3), ( "应为真实 cosine 约 0.7071,若返回约 0.75 说明误用 L2。got=" + str(got) ) assert r["passed"] is True assert r["matched_title"] == "AKK菌——下一代有益菌" @pytest.mark.asyncio async def test_verify_passed_when_cosine_ge_threshold(monkeypatch): q = [1.0, 0.0, 0.0, 0.0] emb = {"cand1": [0.9, 0.43589, 0.0, 0.0]} # norm≈1,cos=0.9 >=0.7 cands = [(FakeDoc("高相似内容", {"id": "cand1", "title": "T", "source": "microbiome"}), 0.3)] _patch_kb(monkeypatch, q, emb, cands) transport = ASGITransport(app=app) async with AsyncClient(transport=transport, base_url="http://test") as client: resp = await client.post("/api/v1/knowledge-base/verify", json={ "sentences": [{"id": "s1", "text": "双歧杆菌"}], "source": "microbiome", }) data = resp.json() r = data["data"]["results"][0] assert r["passed"] is True assert data["data"]["all_passed"] is True @pytest.mark.asyncio async def test_verify_failed_below_threshold(monkeypatch): q = [1.0, 0.0, 0.0, 0.0] emb = {"cand1": [0.3, 0.9, 0.0, 0.0]} # cos 约 0.316 < 0.7 cands = [(FakeDoc("低相似内容", {"id": "cand1", "title": "T", "source": "microbiome"}), 0.9)] _patch_kb(monkeypatch, q, emb, cands) transport = ASGITransport(app=app) async with AsyncClient(transport=transport, base_url="http://test") as client: resp = await client.post("/api/v1/knowledge-base/verify", json={ "sentences": [{"id": "s1", "text": "随便一句话"}], "source": "microbiome", }) data = resp.json() r = data["data"]["results"][0] assert r["passed"] is False assert data["data"]["all_passed"] is False @pytest.mark.asyncio async def test_verify_handles_numpy_array_embeddings(monkeypatch): """真实 chromadb 的 collection.get 返回 numpy 数组而非 list: _cosine 必须兼容 numpy(if not a 对数组会抛 ValueError)。""" import numpy as np q = np.array([1.0, 0.0, 0.0, 0.0], dtype=np.float32) emb = {"cand1": np.array([0.5, 0.5, 0.0, 0.0], dtype=np.float32)} cands = [(FakeDoc("AKK菌相关内容...", {"id": "cand1", "title": "AKK菌——下一代有益菌", "source": "microbiome"}), 0.7)] _patch_kb(monkeypatch, q, emb, cands) transport = ASGITransport(app=app) async with AsyncClient(transport=transport, base_url="http://test") as client: resp = await client.post("/api/v1/knowledge-base/verify", json={ "sentences": [{"id": "s1", "text": "双歧杆菌有助于维持肠道菌群平衡"}], "source": "microbiome", "k": 3, "threshold": 0.7, }) assert resp.status_code == 200, resp.text data = resp.json() r = data["data"]["results"][0] assert r["id"] == "s1" assert r["max_similarity"] == pytest.approx(0.70710678, abs=1e-3), "numpy 数组下 cosine 计算错误: " + str(r) assert r["passed"] is True