From 8e92a99b46158e683cb8811a71e58157cacf3421 Mon Sep 17 00:00:00 2001 From: buhe Date: Mon, 12 Feb 2024 13:36:25 +0800 Subject: [PATCH] "Refactor SimilaritySearchKit to include autoLoad functionality and writeToFile method." --- .../vectorstores/SimilaritySearchKit.swift | 14 +++++++++++++- 1 file changed, 13 insertions(+), 1 deletion(-) diff --git a/Sources/LangChain/vectorstores/SimilaritySearchKit.swift b/Sources/LangChain/vectorstores/SimilaritySearchKit.swift index f48e87b..871b37a 100644 --- a/Sources/LangChain/vectorstores/SimilaritySearchKit.swift +++ b/Sources/LangChain/vectorstores/SimilaritySearchKit.swift @@ -40,11 +40,18 @@ private struct LangChainEmbeddingBridge: EmbeddingsProtocol { public class SimilaritySearchKit: VectorStore { let vs: SimilarityIndex - public init(embeddings: Embeddings) async { + public init(embeddings: Embeddings, autoLoad: Bool = false) async { self.vs = await SimilarityIndex( model: LangChainEmbeddingBridge(embeddings: embeddings), metric: CosineSimilarity() ) + if #available(macOS 13.0, *) { + if autoLoad { + try? vs.loadIndex() + } + } else { + // Fallback on earlier versions + } } override func similaritySearch(query: String, k: Int) async -> [MatchedModel] { @@ -54,5 +61,10 @@ public class SimilaritySearchKit: VectorStore { override func addText(text: String, metadata: [String: String]) async { await vs.addItem(id: UUID().uuidString, text: text, metadata: metadata) } + + @available(macOS 13.0, *) + public func writeToFile() { + try? vs.saveIndex() + } } #endif