INNER CODE UNIT · Swift

addItem

ZachNagengast/similarity-search-kit · Sources/SimilaritySearchKit/Core/Index/SimilarityIndex.swift:224

    func addItem(id: String, text: String, metadata: [String: String], embedding: [Float]? = nil) async {
        let embeddingResult = await getEmbedding(for: text, embedding: embedding)

        let item = IndexItem(id: id, text: text, embedding: embeddingResult, metadata: metadata)
        indexItems.append(item)
    }

    func addItems(ids: [String], texts: [String], metadata: [[String: String]], embeddings: [[Float]?]? = nil, onProgress: ((String) -> Void)? = nil) async {
        // Check if all input arrays have the same length
        guard ids.count == texts.count, texts.count == metadata.count else {
            fatalError("Input arrays must have the same length.")
        }

        if let embeddings = embeddings, embeddings.count != ids.count {
            print("Embeddings array length must be the same as ids array length. \(embeddings.count) vs \(ids.count)")
        }

        await withTaskGroup(of: Void.self) { taskGroup in

View source record →

📰 Research Paper
Loading…
⏳ Fetching content…