test_knowledge_document_parsers.py 1.8 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253
  1. from __future__ import annotations
  2. import sys
  3. from pathlib import Path
  4. REPO_ROOT = Path(__file__).resolve().parents[1]
  5. for module_name in list(sys.modules):
  6. if module_name == "app" or module_name.startswith("app."):
  7. del sys.modules[module_name]
  8. for path in [
  9. REPO_ROOT / "libs" / "core-shared" / "src",
  10. REPO_ROOT / "services" / "knowledge-service",
  11. ]:
  12. sys.path.insert(0, str(path))
  13. from app.application.document_parsers import parse_document_content
  14. from app.application.retrieval import rerank_score
  15. def test_parse_markdown_html_json_csv_documents() -> None:
  16. markdown = parse_document_content(
  17. source_type="markdown",
  18. content_text="# Title\n\nUse [docs](https://example.com) and `code`.")
  19. html = parse_document_content(
  20. source_type="html",
  21. content_text="<h1>Title</h1><script>hidden()</script><p>Hello <b>world</b></p>")
  22. json_doc = parse_document_content(
  23. source_type="json",
  24. content_text='{"order":{"id":"A1","status":"paid"}}')
  25. csv_doc = parse_document_content(
  26. source_type="csv",
  27. content_text="id,status\nA1,paid\nA2,refunded\n")
  28. assert "Title" in markdown.content_text
  29. assert "docs" in markdown.content_text
  30. assert "hidden" not in html.content_text
  31. assert "Hello world" in html.content_text
  32. assert "order.id: A1" in json_doc.content_text
  33. assert "row 2: id: A2; status: refunded" in csv_doc.content_text
  34. def test_rerank_score_prefers_title_and_phrase_matches() -> None:
  35. strong = rerank_score(
  36. query="refund policy",
  37. chunk_text="The refund policy allows refunds within seven days.",
  38. document_title="Refund Policy")
  39. weak = rerank_score(
  40. query="refund policy",
  41. chunk_text="Shipping times are usually three days.",
  42. document_title="Shipping")
  43. assert strong > weak
  44. assert strong > 0.5