feat: unify scoring and chat evidence pages
This commit is contained in:
@@ -0,0 +1,53 @@
|
||||
"""Canonical page content and links shared by retrieval, scoring, and citations."""
|
||||
from pathlib import Path
|
||||
|
||||
from .config import get_settings
|
||||
from .models import KnowledgeDocument, KnowledgePage
|
||||
|
||||
|
||||
def text_url(document_id: int, page: int) -> str:
|
||||
return f"/api/knowledge/documents/{document_id}/pages/{page}"
|
||||
|
||||
|
||||
def image_url(document_id: int, page: int) -> str:
|
||||
return f"{text_url(document_id, page)}/image"
|
||||
|
||||
|
||||
def page_text(source: dict) -> str:
|
||||
blocks = [str(source.get("text") or "").strip()]
|
||||
blocks.extend("表格: " + str(table.get("rows", table)) for table in source.get("tables", []))
|
||||
return "\n".join(block for block in blocks if block)
|
||||
|
||||
|
||||
def relative_image_path(value: str | None) -> str | None:
|
||||
if not value:
|
||||
return None
|
||||
root = get_settings().processed_path.resolve()
|
||||
path = Path(value).resolve()
|
||||
if not path.is_relative_to(root):
|
||||
raise ValueError("页面图像路径不在处理目录内")
|
||||
return str(path.relative_to(root))
|
||||
|
||||
|
||||
def page_image_path(db, doc: KnowledgeDocument, page: int) -> Path | None:
|
||||
root = get_settings().processed_path.resolve()
|
||||
record = db.get(KnowledgePage, (doc.id, page))
|
||||
path = (root / record.image_path).resolve() if record and record.image_path else None
|
||||
return path if path and path.is_relative_to(root) and path.is_file() else None
|
||||
|
||||
|
||||
def page_links(db, document_id: int | None, page: int | None) -> dict[str, str | None]:
|
||||
if not document_id or not page:
|
||||
return {"url": None, "image_url": None}
|
||||
doc = db.get(KnowledgeDocument, document_id)
|
||||
if not doc:
|
||||
return {"url": None, "image_url": None}
|
||||
record = db.get(KnowledgePage, (document_id, page))
|
||||
if not record:
|
||||
return {"url": None, "image_url": None}
|
||||
return {"url": text_url(document_id, page),
|
||||
"image_url": image_url(document_id, page) if page_image_path(db, doc, page) else None}
|
||||
|
||||
|
||||
def evidence_links(db, evidence) -> dict[str, str | None]:
|
||||
return page_links(db, evidence.document_id, evidence.page)
|
||||
Reference in New Issue
Block a user