forked from YassKhazzan/openperplex_backend_os
-
Notifications
You must be signed in to change notification settings - Fork 0
/
semantic_chunking.py
32 lines (22 loc) · 889 Bytes
/
semantic_chunking.py
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
31
32
import os
from semantic_router.encoders import CohereEncoder
from semantic_chunkers import StatisticalChunker
COHERE_API_KEY = os.getenv("COHERE_API_KEY")
encoder = CohereEncoder(cohere_api_key=COHERE_API_KEY, input_type='search_document',
name='embed-multilingual-v3.0')
chunker = StatisticalChunker(encoder=encoder, max_split_tokens=200)
def get_chunking(text):
"""
Splits the provided text into meaningful chunks using a predefined chunker.
Args:
text (str): The text to be chunked.
Returns:
list: A list of chunks if the text is sufficiently long and non-empty; otherwise, an empty list.
"""
try:
chunks = chunker(docs=[text])
values = [c.content for chunk in chunks for c in chunk]
return values
except Exception as e:
print(f"Error during chunking process: {e}")
return []