added RaG

This commit is contained in:
2026-09-09 13:03:41 +03:00
parent e2ee6c2a98
commit df72dc4b8e
27 changed files with 3291 additions and 70 deletions
+21 -6
View File
@@ -15,6 +15,7 @@ from docx.table import Table
from docxtpl import DocxTemplate
from app.services.espd.models import (
CONTENT_CONTEXT_KEYS,
DOC_TYPE_CONFIG,
CommonFields,
DocumentType,
@@ -23,6 +24,8 @@ from app.services.espd.models import (
SelectedDocument,
SpecItem,
)
from app.services.espd.html_util import html_to_plain
TEMPLATES_DIR = Path(__file__).resolve().parents[2] / "espd_templates"
@@ -60,6 +63,12 @@ class DocumentGenerator:
"DECIMAL_FULL": request.common.decimal + (config["suffix"] if config else ""),
"SPEC_TABLE_PLACEHOLDER": "",
"RO_CONTENT": (request.extra_context or {}).get("RO_CONTENT", ""),
"DOC_CONTENT": (request.extra_context or {}).get("DOC_CONTENT", ""),
"TEXT_CONTENT": (request.extra_context or {}).get("TEXT_CONTENT", ""),
"RSP_CONTENT": (request.extra_context or {}).get("RSP_CONTENT", ""),
"RP_CONTENT": (request.extra_context or {}).get("RP_CONTENT", ""),
"PMI_CONTENT": (request.extra_context or {}).get("PMI_CONTENT", ""),
"IMP_CONTENT": (request.extra_context or {}).get("IMP_CONTENT", ""),
}
if request.imp_zip_info:
context["FOLDER_TABLE_PLACEHOLDER"] = "%%FOLDER_TABLE%%"
@@ -220,8 +229,12 @@ class DocumentGenerator:
spec_items: list[SpecItem],
imp_zip_info: ImpZipInfo | None = None,
ro_ai_content: str | None = None,
doc_contents: dict[str, str] | None = None,
) -> io.BytesIO:
zip_buffer = io.BytesIO()
contents = dict(doc_contents or {})
if ro_ai_content and "ro" not in contents:
contents["ro"] = ro_ai_content
with zipfile.ZipFile(zip_buffer, "w", zipfile.ZIP_DEFLATED) as zip_file:
for selected in selected_docs:
doc_type = selected.doc_type
@@ -231,17 +244,19 @@ class DocumentGenerator:
else:
template_path = self.get_template_path(doc_type)
if template_path.exists():
extra = (
{"RO_CONTENT": ro_ai_content}
if (doc_type == DocumentType.RO and ro_ai_content)
else None
)
plain = contents.get(doc_type.value, "")
if plain and "<" in plain:
plain = html_to_plain(plain)
key = CONTENT_CONTEXT_KEYS.get(doc_type)
extra = {"DOC_CONTENT": plain}
if key:
extra[key] = plain
doc_request = GenerateRequest(
doc_type=doc_type,
common=common,
spec_items=[],
imp_zip_info=imp_zip_info if doc_type == DocumentType.IMP else None,
extra_context=extra if doc_type == DocumentType.RO else None,
extra_context=extra,
)
doc_content = self.generate(doc_request)
zip_file.writestr(self.generate_filename(doc_request), doc_content.read())