mirror of
https://github.com/MODSetter/SurfSense.git
synced 2026-04-25 00:36:31 +02:00
fix: mentioned documents xml structure
This commit is contained in:
parent
3660b91e63
commit
ef9e9b65df
1 changed files with 34 additions and 31 deletions
|
|
@ -52,38 +52,41 @@ def format_mentioned_documents_as_context(documents: list[Document]) -> str:
|
|||
"""Format mentioned documents as context for the agent."""
|
||||
if not documents:
|
||||
return ""
|
||||
import json
|
||||
|
||||
context_parts = ["<mentioned_documents>"]
|
||||
context_parts.append(
|
||||
"The user has explicitly mentioned the following documents from their knowledge base. "
|
||||
"These documents are directly relevant to the query and should be prioritized as primary sources."
|
||||
)
|
||||
for i, doc in enumerate(documents, 1):
|
||||
# Prepare retriever-style structure
|
||||
doc_metadata = doc.document_metadata if doc.document_metadata else {}
|
||||
xml_doc = f"""
|
||||
<document index='{i}'>
|
||||
<document_id>{doc.id}</document_id>
|
||||
<content><![CDATA[{doc.content}]]></content>
|
||||
<score>1.0</score>
|
||||
<chunks>
|
||||
<chunk>
|
||||
<chunk_id>{doc.id}-full</chunk_id>
|
||||
<content><![CDATA[{doc.content}]]></content>
|
||||
</chunk>
|
||||
</chunks>
|
||||
<document_info>
|
||||
<id>{doc.id}</id>
|
||||
<title><![CDATA[{doc.title}]]></title>
|
||||
<document_type>{doc.document_type.value}</document_type>
|
||||
<metadata><![CDATA[{json.dumps(doc_metadata)}]]></metadata>
|
||||
</document_info>
|
||||
<source>{doc.document_type.value}</source>
|
||||
</document>"""
|
||||
context_parts.append(xml_doc.strip())
|
||||
context_parts.append("</mentioned_documents>")
|
||||
|
||||
return "\n".join(context_parts)
|
||||
parts = []
|
||||
for doc in documents:
|
||||
metadata = doc.document_metadata or {}
|
||||
chunks = (
|
||||
[
|
||||
{"chunk_id": c.id, "content": c.content}
|
||||
for c in getattr(doc, "chunks", [])
|
||||
]
|
||||
if hasattr(doc, "chunks") and doc.chunks
|
||||
else [{"chunk_id": doc.id, "content": doc.content}]
|
||||
)
|
||||
metadata_json = json.dumps(metadata, ensure_ascii=False)
|
||||
parts.append("<document>")
|
||||
parts.append("<document_metadata>")
|
||||
parts.append(f" <document_id>{doc.id}</document_id>")
|
||||
parts.append(f" <document_type>{doc.document_type.value}</document_type>")
|
||||
parts.append(f" <title><![CDATA[{doc.title}]]></title>")
|
||||
parts.append(" <url><![CDATA[]]></url>")
|
||||
parts.append(f" <metadata_json><![CDATA[{metadata_json}]]></metadata_json>")
|
||||
parts.append("</document_metadata>")
|
||||
parts.append("")
|
||||
parts.append("<document_content>")
|
||||
for ch in chunks:
|
||||
ch_content = ch["content"]
|
||||
ch_id = ch["chunk_id"]
|
||||
if ch_id is None:
|
||||
parts.append(f" <chunk><![CDATA[{ch_content}]]></chunk>")
|
||||
else:
|
||||
parts.append(f" <chunk id='{ch_id}'><![CDATA[{ch_content}]]></chunk>")
|
||||
parts.append("</document_content>")
|
||||
parts.append("</document>")
|
||||
parts.append("")
|
||||
return "\n".join(parts).strip()
|
||||
|
||||
|
||||
async def stream_new_chat(
|
||||
|
|
|
|||
Loading…
Add table
Add a link
Reference in a new issue