Second-Me/lpm_kernel/file_data/chunker.py
wuyuxiangX 7becdc4f84
Feature/new data (#393)
* new data pipiline

* new data pipiline

* shades and topic

---------

Co-authored-by: yanmuyuan <2216646664@qq.com>
2025-07-01 15:39:04 +08:00

51 lines
1.6 KiB
Python
Raw Permalink Blame History

This file contains ambiguous Unicode characters

This file contains Unicode characters that might be confused with other characters. If you think that this is intentional, you can safely ignore this warning. Use the Escape button to reveal them.

from typing import List
from lpm_kernel.stage2.bio import Chunk
import traceback
import time
from langchain.text_splitter import RecursiveCharacterTextSplitter
from lpm_kernel.configs.logging import get_train_process_logger
logger = get_train_process_logger()
class DocumentChunker:
def __init__(self, chunk_size: int = 1000, overlap: int = 200):
self.chunk_size = chunk_size
self.overlap = overlap
self.text_splitter = RecursiveCharacterTextSplitter(
chunk_size=self.chunk_size,
chunk_overlap=self.overlap,
length_function=len,
separators=["\n\n", "\n", "", "", "", ".", "!", "?", " ", ""],
)
def split(self, content: str) -> List[Chunk]:
try:
if not content:
logger.warning("Empty content provided")
return []
logger.info(f"Starting to split content of length {len(content)}")
# use LangChain splitter
texts = self.text_splitter.split_text(content)
chunks = [
Chunk(
id=None,
document_id=None,
content=text,
embedding=None,
tags=None,
topic=None,
)
for text in texts
]
logger.info(f"Split completed, created {len(chunks)} chunks")
return chunks
except Exception as e:
logger.error(f"Error in split method: {str(e)}")
logger.error(traceback.format_exc())
raise