forked from intitni/CopilotForXcode
-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathDocumentLoader.swift
More file actions
31 lines (26 loc) · 841 Bytes
/
DocumentLoader.swift
File metadata and controls
31 lines (26 loc) · 841 Bytes
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
import Foundation
import JSONRPC
public struct Document: Codable {
public typealias Metadata = [String: JSONValue]
public var pageContent: String
public var metadata: Metadata
public init(pageContent: String, metadata: Metadata) {
self.pageContent = pageContent
self.metadata = metadata
}
public func metadata<Key>(_ keyPath: KeyPath<Key.Type, String>) -> JSONValue? {
let key = Key.self[keyPath: keyPath]
return metadata[key]
}
}
public protocol DocumentLoader {
func load() async throws -> [Document]
}
extension DocumentLoader {
func loadAndSplit(
with textSplitter: TextSplitter = RecursiveCharacterTextSplitter()
) async throws -> [Document] {
let docs = try await load()
return try await textSplitter.splitDocuments(docs)
}
}