2023-11-03 20:37:29 +00:00
|
|
|
from string import Formatter
|
|
|
|
from typing import List
|
|
|
|
|
2024-02-22 23:58:44 +00:00
|
|
|
from langchain_core.documents import Document
|
2023-11-03 20:37:29 +00:00
|
|
|
|
|
|
|
document_template = """
|
|
|
|
PASSAGE: {page_content}
|
|
|
|
METADATA: {metadata}
|
|
|
|
"""
|
|
|
|
|
|
|
|
|
|
|
|
def combine_documents(documents: List[Document]) -> str:
|
|
|
|
"""
|
|
|
|
Combine a list of documents into a single string that might be passed further down
|
|
|
|
to a language model.
|
|
|
|
:param documents: list of documents to combine
|
|
|
|
:return:
|
|
|
|
"""
|
|
|
|
formatter = Formatter()
|
|
|
|
return "\n\n".join(
|
|
|
|
formatter.format(
|
|
|
|
document_template,
|
|
|
|
page_content=document.page_content,
|
|
|
|
metadata=document.metadata,
|
|
|
|
)
|
|
|
|
for document in documents
|
|
|
|
)
|