-
Notifications
You must be signed in to change notification settings - Fork 156
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
📂 feat: load full context of files via
/documents/{id}/context
(#7)
- Loading branch information
1 parent
d7a7b38
commit 90970a7
Showing
2 changed files
with
53 additions
and
1 deletion.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,29 @@ | ||
from typing import List, Optional | ||
from langchain.schema import Document | ||
from config import CHUNK_OVERLAP | ||
|
||
def process_documents(documents: List[Document]) -> str: | ||
processed_text = "" | ||
last_page: Optional[int] = None | ||
doc_basename = "" | ||
|
||
for doc in documents: | ||
if 'source' in doc.metadata: | ||
doc_basename = doc.metadata['source'].split('/')[-1] | ||
break | ||
|
||
processed_text += f"{doc_basename}\n" | ||
|
||
for doc in documents: | ||
current_page = doc.metadata.get('page') | ||
if current_page and current_page != last_page: | ||
processed_text += f"\n# PAGE {doc.metadata['page']}\n\n" | ||
last_page = current_page | ||
|
||
new_content = doc.page_content | ||
if processed_text.endswith(new_content[:CHUNK_OVERLAP]): | ||
processed_text += new_content[CHUNK_OVERLAP:] | ||
else: | ||
processed_text += new_content | ||
|
||
return processed_text.strip() |