chunker different methods

This commit is contained in:
Jonas Braus
2024-12-12 15:35:32 +01:00
parent 3c59120444
commit 8d83d5cfec
2 changed files with 30 additions and 2 deletions
+28 -1
View File
@@ -1,8 +1,11 @@
import os
from llama_index.core import SimpleDirectoryReader
from llama_index.core.node_parser import SemanticSplitterNodeParser
from llama_index.embeddings.ollama import OllamaEmbedding
import json
def generate_chunks(content):
def generate_chunks_semantic(content):
with open("./temp.txt", "wb") as file:
file.write(content.encode("utf-8"))
file.flush()
@@ -21,3 +24,27 @@ def generate_chunks(content):
output.append(node.to_dict()["text"])
return output
def generate_chunks_line_split(content):
output = []
sp = content.split(".")
while len(sp) > 0:
output.append("".join(sp[:10]))
sp = sp[10:]
return output
def generate_chunks(content):
chunking_method = os.environ.get("CHUNKING_METHOD")
print("CHUNKING_METHOD:", chunking_method)
if chunking_method is None:
chunking_method = "none"
return {
"semantic": lambda: generate_chunks_semantic(content),
"lines": lambda: generate_chunks_line_split(content),
"none": lambda: [content]
}[chunking_method.lower().strip()]()