Spaces:
Sleeping
Sleeping
File size: 1,135 Bytes
305ef4d | 1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 | from langchain_text_splitters import RecursiveCharacterTextSplitter
from src.logger import logger
def split_text(docs, chunk_size=1000, chunk_overlap=200):
"""
Takes a list of LangChain Document objects and splits them into smaller,
manageable chunks for the vector database.
"""
logger.info(f"Starting text splitting: chunk_size={chunk_size}, overlap={chunk_overlap}")
try:
# Initialize the LangChain text splitter
text_splitter = RecursiveCharacterTextSplitter(
chunk_size=chunk_size,
chunk_overlap=chunk_overlap,
separators=["\n\n", "\n", " ", ""] # Splits by paragraph, then line, then word
)
# Split the documents
chunks = text_splitter.split_documents(docs)
logger.info(f"Successfully split the document into {len(chunks)} individual chunks.")
# Return the chunks so the embedding model can vectorize them
return chunks
except Exception as e:
logger.error(f"Text splitting failed: {str(e)}", exc_info=True)
raise e |