File size: 1,037 Bytes
7b7cab6
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
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
30
from langchain_text_splitters import RecursiveCharacterTextSplitter

class TextSplitter:
    def __init__(self, chunk_size=1024, chunk_overlap=100):
        """
        Initialize the TextSplitter with a specific chunk size and overlap.

        Args:
            chunk_size (int): The size of each text chunk.
            chunk_overlap (int): The overlap size between chunks.
        """
        self.text_splitter = RecursiveCharacterTextSplitter(chunk_size=chunk_size, chunk_overlap=chunk_overlap)

    def split_documents(self, documents):
        """
        Split the provided documents into chunks based on the chunk size and overlap.

        Args:
            documents (list): A list of documents to be split.

        Returns:
            A list of split documents.

        Exceptions:
            Prints an error message if splitting documents fails.
        """
        try:
            return self.text_splitter.split_documents(documents)
        except Exception as e:
            print(f"Error splitting documents: {e}")