66//
77
88import Foundation
9- import OSLog
109
1110// MARK: - Type Aliases
1211
@@ -19,20 +18,18 @@ public typealias VectorStoreType = SimilarityIndex.VectorStoreType
1918
2019@available ( macOS 11 . 0 , iOS 15 . 0 , * )
2120public class SimilarityIndex : Identifiable , Hashable {
22-
23- public static func == ( lhs: SimilarityIndex , rhs: SimilarityIndex ) -> Bool {
24- return lhs. id == rhs. id
25- }
26-
27- public func hash( into hasher: inout Hasher ) {
28- hasher. combine ( id)
29- }
30-
3121 // MARK: - Properties
3222
33- /// A unique identifier
34- public var id : UUID = UUID ( )
35-
23+ /// Unique identifier for this index instance
24+ public var id : UUID = . init( )
25+ public static func == ( lhs: SimilarityIndex , rhs: SimilarityIndex ) -> Bool {
26+ return lhs. id == rhs. id
27+ }
28+
29+ public func hash( into hasher: inout Hasher ) {
30+ hasher. combine ( id)
31+ }
32+
3633 /// The items stored in the index.
3734 public var indexItems : [ IndexItem ] = [ ]
3835
@@ -161,7 +158,7 @@ public class SimilarityIndex: Identifiable, Hashable {
161158 var indexIds : [ String ] = [ ]
162159 var indexEmbeddings : [ [ Float ] ] = [ ]
163160
164- indexItems . forEach { item in
161+ for item in indexItems {
165162 indexIds. append ( item. id)
166163 indexEmbeddings. append ( item. embedding)
167164 }
@@ -220,18 +217,18 @@ public class SimilarityIndex: Identifiable, Hashable {
220217// MARK: - CRUD
221218
222219@available ( macOS 11 . 0 , iOS 15 . 0 , * )
223- extension SimilarityIndex {
220+ public extension SimilarityIndex {
224221 // MARK: Create
225222
226- // Add an item with optional pre-computed embedding
227- public func addItem( id: String , text: String , metadata: [ String : String ] , embedding: [ Float ] ? = nil ) async {
223+ /// Add an item with optional pre-computed embedding
224+ func addItem( id: String , text: String , metadata: [ String : String ] , embedding: [ Float ] ? = nil ) async {
228225 let embeddingResult = await getEmbedding ( for: text, embedding: embedding)
229226
230227 let item = IndexItem ( id: id, text: text, embedding: embeddingResult, metadata: metadata)
231228 indexItems. append ( item)
232229 }
233230
234- public func addItems( ids: [ String ] , texts: [ String ] , metadata: [ [ String : String ] ] , embeddings: [ [ Float ] ? ] ? = nil , onProgress: ( ( String ) -> Void ) ? = nil ) async {
231+ func addItems( ids: [ String ] , texts: [ String ] , metadata: [ [ String : String ] ] , embeddings: [ [ Float ] ? ] ? = nil , onProgress: ( ( String ) -> Void ) ? = nil ) async {
235232 // Check if all input arrays have the same length
236233 guard ids. count == texts. count, texts. count == metadata. count else {
237234 fatalError ( " Input arrays must have the same length. " )
@@ -258,7 +255,7 @@ extension SimilarityIndex {
258255 }
259256 }
260257
261- public func addItems( _ items: [ IndexItem ] , completion: ( ( ) -> Void ) ? = nil ) {
258+ func addItems( _ items: [ IndexItem ] , completion: ( ( ) -> Void ) ? = nil ) {
262259 Task {
263260 for item in items {
264261 await self . addItem ( id: item. id, text: item. text, metadata: item. metadata, embedding: item. embedding)
@@ -269,17 +266,17 @@ extension SimilarityIndex {
269266
270267 // MARK: Read
271268
272- public func getItem( id: String ) -> IndexItem ? {
269+ func getItem( id: String ) -> IndexItem ? {
273270 return indexItems. first { $0. id == id }
274271 }
275272
276- public func sample( _ count: Int ) -> [ IndexItem ] ? {
273+ func sample( _ count: Int ) -> [ IndexItem ] ? {
277274 return Array ( indexItems. prefix ( upTo: count) )
278275 }
279276
280277 // MARK: Update
281278
282- public func updateItem( id: String , text: String ? = nil , embedding: [ Float ] ? = nil , metadata: [ String : String ] ? = nil ) {
279+ func updateItem( id: String , text: String ? = nil , embedding: [ Float ] ? = nil , metadata: [ String : String ] ? = nil ) {
283280 // Check if the provided embedding has the correct dimension
284281 if let embedding = embedding, embedding. count != dimension {
285282 print ( " Dimension mismatch, expected \( dimension) , saw \( embedding. count) " )
@@ -306,21 +303,20 @@ extension SimilarityIndex {
306303
307304 // MARK: Delete
308305
309- public func removeItem( id: String ) {
306+ func removeItem( id: String ) {
310307 indexItems. removeAll { $0. id == id }
311308 }
312309
313- public func removeAll( ) {
310+ func removeAll( ) {
314311 indexItems. removeAll ( )
315312 }
316313}
317314
318315// MARK: - Persistence
319316
320317@available ( macOS 13 . 0 , iOS 16 . 0 , * )
321- extension SimilarityIndex {
322-
323- public func saveIndex( toDirectory path: URL ? = nil , name: String ? = nil ) throws -> URL {
318+ public extension SimilarityIndex {
319+ func saveIndex( toDirectory path: URL ? = nil , name: String ? = nil ) throws -> URL {
324320 let indexName = name ?? self . indexName
325321 let basePath : URL
326322
@@ -333,13 +329,12 @@ extension SimilarityIndex {
333329
334330 let savedVectorStore = try vectorStore. saveIndex ( items: indexItems, to: basePath, as: indexName)
335331
336- let bundleId : String = Bundle . main. bundleIdentifier ?? " com.similarity-search-kit.logger "
337- let logger : Logger = Logger ( subsystem: bundleId, category: " similarityIndexSave " )
332+ print ( " Saved \( indexItems. count) index items to \( savedVectorStore. absoluteString) " )
338333
339334 return savedVectorStore
340335 }
341336
342- public func loadIndex( fromDirectory path: URL ? = nil , name: String ? = nil ) throws -> [ IndexItem ] ? {
337+ func loadIndex( fromDirectory path: URL ? = nil , name: String ? = nil ) throws -> [ IndexItem ] ? {
343338 if let indexPath = try getIndexPath ( fromDirectory: path, name: name) {
344339 indexItems = try vectorStore. loadIndex ( from: indexPath)
345340 return indexItems
@@ -355,7 +350,7 @@ extension SimilarityIndex {
355350 /// - name: optional name
356351 ///
357352 /// - Returns: an optional URL
358- public func getIndexPath( fromDirectory path: URL ? = nil , name: String ? = nil ) throws -> URL ? {
353+ func getIndexPath( fromDirectory path: URL ? = nil , name: String ? = nil ) throws -> URL ? {
359354 let indexName = name ?? self . indexName
360355 let basePath : URL
361356
@@ -382,7 +377,7 @@ extension SimilarityIndex {
382377 return appSpecificDirectory
383378 }
384379
385- public func estimatedSizeInBytes( ) -> Int {
380+ func estimatedSizeInBytes( ) -> Int {
386381 var totalSize = 0
387382
388383 for item in indexItems {
@@ -397,7 +392,7 @@ extension SimilarityIndex {
397392 let embeddingSize = item. embedding. count * floatSize
398393
399394 // Calculate the size of 'metadata' property
400- let metadataSize = item. metadata. reduce ( 0 ) { ( size, keyValue) -> Int in
395+ let metadataSize = item. metadata. reduce ( 0 ) { size, keyValue -> Int in
401396 let keySize = keyValue. key. utf8. count
402397 let valueSize = keyValue. value. utf8. count
403398 return size + keySize + valueSize
0 commit comments