mirror of
https://github.com/MODSetter/SurfSense.git
synced 2026-05-12 17:22:38 +02:00
Merge pull request #622 from CREDO23/documents-mentions
[Fix] Documents mentions | Use the same structure of document returned from retriever
This commit is contained in:
commit
fb719faa0d
1 changed files with 33 additions and 13 deletions
|
|
@ -52,21 +52,41 @@ def format_mentioned_documents_as_context(documents: list[Document]) -> str:
|
||||||
"""Format mentioned documents as context for the agent."""
|
"""Format mentioned documents as context for the agent."""
|
||||||
if not documents:
|
if not documents:
|
||||||
return ""
|
return ""
|
||||||
|
import json
|
||||||
|
|
||||||
context_parts = ["<mentioned_documents>"]
|
parts = []
|
||||||
context_parts.append(
|
for doc in documents:
|
||||||
"The user has explicitly mentioned the following documents from their knowledge base. "
|
metadata = doc.document_metadata or {}
|
||||||
"These documents are directly relevant to the query and should be prioritized as primary sources."
|
chunks = (
|
||||||
)
|
[
|
||||||
for i, doc in enumerate(documents, 1):
|
{"chunk_id": c.id, "content": c.content}
|
||||||
context_parts.append(
|
for c in getattr(doc, "chunks", [])
|
||||||
f"<document index='{i}' id='{doc.id}' title='{doc.title}' type='{doc.document_type.value}'>"
|
]
|
||||||
|
if hasattr(doc, "chunks") and doc.chunks
|
||||||
|
else [{"chunk_id": doc.id, "content": doc.content}]
|
||||||
)
|
)
|
||||||
context_parts.append(f"<![CDATA[{doc.content}]]>")
|
metadata_json = json.dumps(metadata, ensure_ascii=False)
|
||||||
context_parts.append("</document>")
|
parts.append("<document>")
|
||||||
context_parts.append("</mentioned_documents>")
|
parts.append("<document_metadata>")
|
||||||
|
parts.append(f" <document_id>{doc.id}</document_id>")
|
||||||
return "\n".join(context_parts)
|
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(
|
async def stream_new_chat(
|
||||||
|
|
|
||||||
Loading…
Add table
Add a link
Reference in a new issue