Skip to content

Commit

Permalink
"Refactor SimilaritySearchKit to include autoLoad functionality and w…
Browse files Browse the repository at this point in the history
…riteToFile method."
  • Loading branch information
buhe committed Feb 12, 2024
1 parent a373249 commit 8e92a99
Showing 1 changed file with 13 additions and 1 deletion.
14 changes: 13 additions & 1 deletion Sources/LangChain/vectorstores/SimilaritySearchKit.swift
Original file line number Diff line number Diff line change
Expand Up @@ -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] {
Expand All @@ -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

0 comments on commit 8e92a99

Please sign in to comment.