forked from intitni/CopilotForXcode
-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathRecursiveCharacterTextSplitter.swift
More file actions
39 lines (35 loc) · 1.3 KB
/
RecursiveCharacterTextSplitter.swift
File metadata and controls
39 lines (35 loc) · 1.3 KB
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
33
34
35
36
37
38
import Foundation
import PythonHelper
import PythonKit
public struct RecursiveCharacterTextSplitter: TextSplitter {
public var separators: [String]
public var chunkSize: Int
public var chunkOverlap: Int
public init(
separators: [String] = ["\n\n", "\n", " ", ""],
chunkSize: Int = 4000,
chunkOverlap: Int = 200
) {
self.separators = separators
self.chunkSize = chunkSize
self.chunkOverlap = chunkOverlap
}
public func split(text: String) async throws -> [String] {
try await runPython {
let text_splitter = try Python.attemptImportOnPythonThread("langchain.text_splitter")
let PythonRecursiveCharacterTextSplitter = text_splitter.RecursiveCharacterTextSplitter
let splitter = PythonRecursiveCharacterTextSplitter(
separators: separators,
chunk_size: chunkSize,
chunk_overlap: chunkOverlap
// length_function: PythonFunction({ object in
// if let string = String(object) { return string.count }
// return 0
// })
)
let result = splitter.split_text(text)
guard let array = [String](result) else { return [] }
return array
}
}
}