test_knowledge_verify.py 5.2 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133
  1. # -*- coding: utf-8 -*-
  2. """verify 端点测试:L2→cosine 换算 + 阈值判定。"""
  3. import pytest
  4. from httpx import AsyncClient, ASGITransport
  5. from app.main import app
  6. class FakeDoc:
  7. def __init__(self, content, metadata):
  8. self.page_content = content
  9. self.metadata = metadata
  10. class FakeCollection:
  11. def __init__(self, emb_by_id):
  12. self._emb = emb_by_id
  13. def get(self, ids=None, include=None):
  14. return {"ids": ids, "embeddings": [self._emb[i] for i in ids]}
  15. class FakeStore:
  16. def __init__(self, emb_by_id, candidates):
  17. self._collection = FakeCollection(emb_by_id)
  18. self._candidates = candidates
  19. def similarity_search_with_score(self, query, k=None, filter=None):
  20. return self._candidates[:k]
  21. class FakeEmbeddings:
  22. def __init__(self, q):
  23. self.q = q
  24. def embed_query(self, text):
  25. return self.q
  26. def _patch_kb(monkeypatch, query_vec, emb_by_id, candidates):
  27. import app.api.knowledge_base as kb
  28. fake_store = FakeStore(emb_by_id, candidates)
  29. monkeypatch.setattr(kb, "Chroma", lambda *a, **kw: fake_store)
  30. monkeypatch.setattr(kb, "get_embeddings", lambda: FakeEmbeddings(query_vec))
  31. return fake_store
  32. @pytest.mark.asyncio
  33. async def test_verify_converts_l2_to_cosine(monkeypatch):
  34. """关键:L2 距离≠相似度。候选向量 [0.5,0.5,0,0] 与 query [1,0,0,0]
  35. 真实 cosine=0.7071;若误用 L2(1-d^2/2) 会得 0.75。断言用真实 cosine。"""
  36. q = [1.0, 0.0, 0.0, 0.0]
  37. # 非归一化候选:真实 cos = 0.5 / sqrt(0.5) = 0.70710678
  38. emb = {"cand1": [0.5, 0.5, 0.0, 0.0]}
  39. cands = [(FakeDoc("AKK菌相关内容...", {"id": "cand1", "title": "AKK菌——下一代有益菌", "source": "microbiome"}), 0.7071)]
  40. _patch_kb(monkeypatch, q, emb, cands)
  41. transport = ASGITransport(app=app)
  42. async with AsyncClient(transport=transport, base_url="http://test") as client:
  43. resp = await client.post("/api/v1/knowledge-base/verify", json={
  44. "sentences": [{"id": "s1", "text": "双歧杆菌有助于维持肠道菌群平衡"}],
  45. "source": "microbiome", "k": 3, "threshold": 0.7,
  46. })
  47. assert resp.status_code == 200
  48. data = resp.json()
  49. assert data["code"] == 200
  50. r = data["data"]["results"][0]
  51. assert r["id"] == "s1"
  52. got = r.get("max_similarity")
  53. assert got == pytest.approx(0.70710678, abs=1e-3), (
  54. "应为真实 cosine 约 0.7071,若返回约 0.75 说明误用 L2。got=" + str(got)
  55. )
  56. assert r["passed"] is True
  57. assert r["matched_title"] == "AKK菌——下一代有益菌"
  58. @pytest.mark.asyncio
  59. async def test_verify_passed_when_cosine_ge_threshold(monkeypatch):
  60. q = [1.0, 0.0, 0.0, 0.0]
  61. emb = {"cand1": [0.9, 0.43589, 0.0, 0.0]} # norm≈1,cos=0.9 >=0.7
  62. cands = [(FakeDoc("高相似内容", {"id": "cand1", "title": "T", "source": "microbiome"}), 0.3)]
  63. _patch_kb(monkeypatch, q, emb, cands)
  64. transport = ASGITransport(app=app)
  65. async with AsyncClient(transport=transport, base_url="http://test") as client:
  66. resp = await client.post("/api/v1/knowledge-base/verify", json={
  67. "sentences": [{"id": "s1", "text": "双歧杆菌"}], "source": "microbiome",
  68. })
  69. data = resp.json()
  70. r = data["data"]["results"][0]
  71. assert r["passed"] is True
  72. assert data["data"]["all_passed"] is True
  73. @pytest.mark.asyncio
  74. async def test_verify_failed_below_threshold(monkeypatch):
  75. q = [1.0, 0.0, 0.0, 0.0]
  76. emb = {"cand1": [0.3, 0.9, 0.0, 0.0]} # cos 约 0.316 < 0.7
  77. cands = [(FakeDoc("低相似内容", {"id": "cand1", "title": "T", "source": "microbiome"}), 0.9)]
  78. _patch_kb(monkeypatch, q, emb, cands)
  79. transport = ASGITransport(app=app)
  80. async with AsyncClient(transport=transport, base_url="http://test") as client:
  81. resp = await client.post("/api/v1/knowledge-base/verify", json={
  82. "sentences": [{"id": "s1", "text": "随便一句话"}], "source": "microbiome",
  83. })
  84. data = resp.json()
  85. r = data["data"]["results"][0]
  86. assert r["passed"] is False
  87. assert data["data"]["all_passed"] is False
  88. @pytest.mark.asyncio
  89. async def test_verify_handles_numpy_array_embeddings(monkeypatch):
  90. """真实 chromadb 的 collection.get 返回 numpy 数组而非 list:
  91. _cosine 必须兼容 numpy(if not a 对数组会抛 ValueError)。"""
  92. import numpy as np
  93. q = np.array([1.0, 0.0, 0.0, 0.0], dtype=np.float32)
  94. emb = {"cand1": np.array([0.5, 0.5, 0.0, 0.0], dtype=np.float32)}
  95. cands = [(FakeDoc("AKK菌相关内容...", {"id": "cand1", "title": "AKK菌——下一代有益菌", "source": "microbiome"}), 0.7)]
  96. _patch_kb(monkeypatch, q, emb, cands)
  97. transport = ASGITransport(app=app)
  98. async with AsyncClient(transport=transport, base_url="http://test") as client:
  99. resp = await client.post("/api/v1/knowledge-base/verify", json={
  100. "sentences": [{"id": "s1", "text": "双歧杆菌有助于维持肠道菌群平衡"}],
  101. "source": "microbiome", "k": 3, "threshold": 0.7,
  102. })
  103. assert resp.status_code == 200, resp.text
  104. data = resp.json()
  105. r = data["data"]["results"][0]
  106. assert r["id"] == "s1"
  107. assert r["max_similarity"] == pytest.approx(0.70710678, abs=1e-3), "numpy 数组下 cosine 计算错误: " + str(r)
  108. assert r["passed"] is True