[
  {
    "path": ".gitignore",
    "content": ".DS_Store\n/.build\n/Packages\n/*.xcodeproj\nxcuserdata/\nDerivedData/\n.swiftpm/config/registries.json\n.swiftpm/xcode/package.xcworkspace/contents.xcworkspacedata\n.netrc"
  },
  {
    "path": ".swiftpm/xcode/package.xcworkspace/xcshareddata/IDEWorkspaceChecks.plist",
    "content": "<?xml version=\"1.0\" encoding=\"UTF-8\"?>\n<!DOCTYPE plist PUBLIC \"-//Apple//DTD PLIST 1.0//EN\" \"http://www.apple.com/DTDs/PropertyList-1.0.dtd\">\n<plist version=\"1.0\">\n<dict>\n\t<key>IDEDidComputeMac32BitWarning</key>\n\t<true/>\n</dict>\n</plist>\n"
  },
  {
    "path": "LICENSE",
    "content": "MIT License\n\nCopyright (c) 2025 Dripfarm\n\nPermission is hereby granted, free of charge, to any person obtaining a copy\nof this software and associated documentation files (the \"Software\"), to deal\nin the Software without restriction, including without limitation the rights\nto use, copy, modify, merge, publish, distribute, sublicense, and/or sell\ncopies of the Software, and to permit persons to whom the Software is\nfurnished to do so, subject to the following conditions:\n\nThe above copyright notice and this permission notice shall be included in all\ncopies or substantial portions of the Software.\n\nTHE SOFTWARE IS PROVIDED \"AS IS\", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR\nIMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,\nFITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE\nAUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER\nLIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,\nOUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE\nSOFTWARE.\n"
  },
  {
    "path": "Package.swift",
    "content": "// swift-tools-version: 5.8\n// The swift-tools-version declares the minimum version of Swift required to build this package.\n\nimport PackageDescription\n\nlet package = Package(\n    name: \"SVDB\",\n    products: [\n        // Products define the executables and libraries a package produces, and make them visible to other packages.\n        .library(\n            name: \"SVDB\",\n            targets: [\"SVDB\"]),\n    ],\n    dependencies: [\n        // Dependencies declare other packages that this package depends on.\n        // .package(url: /* package url */, from: \"1.0.0\"),\n    ],\n    targets: [\n        // Targets are the basic building blocks of a package. A target can define a module or a test suite.\n        // Targets can depend on other targets in this package, and on products in packages this package depends on.\n        .target(\n            name: \"SVDB\",\n            dependencies: []),\n        .testTarget(\n            name: \"SVDBTests\",\n            dependencies: [\"SVDB\"]),\n    ]\n)\n"
  },
  {
    "path": "README.md",
    "content": "# Swift Vector Database (SVDB)\n\nA new fast local on-device vector database for Swift Apps.\n\nBuilt for those building the next-generation of user experiences only possible with on-device intelligence. \n\nLocal on-device vector databases are just the beginning. \n\n## Installation\nTo install it using the Swift Package Manager, either directly add it to your project using Xcode 11, or specify it as dependency in the Package.swift file:\n\n```\n// ...\ndependencies: [\n    .package(url: \"https://github.com/Dripfarm/SVDB.git\", from: \"1.0.0\"),\n],\n//...\n```\n\n\n## Usage\n\n### 1. Create Embeddings\n```\nlet document = \"cat\"\n```\n\n**ChatGPT:**\n\nI find [This Swift OpenAI package to be the best](https://github.com/MacPaw/OpenAI)\n\n```\nimport OpenAI\n\nfunc embed(text: String) async -> [Double]? {\n\tlet query = EmbeddingsQuery(model: .textEmbeddingAda, input: text)\n\n\tlet result = try! await openAI.embeddings(query: query)\n\n\treturn result.data.first?.embedding\n}\n\nlet wordEmbedding = embed(text: document)\n```\n\n**NLEmbeddings**\n\n```\nimport NaturalLanguage\n\nlet embedding: NLEmbedding? = NLEmbedding.wordEmbedding(for: .english)\n\nlet wordEmedding = embedding?.vector(for: document) //returns double array\n```\n\n### 2. Add Documents\n\n```\nlet animalCollection = SVDB.shared.collection(\"animals\")\n\nSVDB.shared.addDocument(text: document, embedding: wordEmbedding)\n\n```\n\n### 3. Search\n\n```\nlet dogEmedding = embedding?.vector(for: \"dog\")\n\nlet results = animalCollection.search(query: dogEmedding)\n```\n\n## Demo\n\nCheck out the demo [Demo](https://github.com/Dripfarm/SVDB/tree/master/SVDBDemo)\n\n## Todo\nNot sure. I want to make it easier to add documents and take care of the embeddings for you. Any suggestions?"
  },
  {
    "path": "SVDBDemo/SVDBDemo/Assets.xcassets/AccentColor.colorset/Contents.json",
    "content": "{\n  \"colors\" : [\n    {\n      \"idiom\" : \"universal\"\n    }\n  ],\n  \"info\" : {\n    \"author\" : \"xcode\",\n    \"version\" : 1\n  }\n}\n"
  },
  {
    "path": "SVDBDemo/SVDBDemo/Assets.xcassets/AppIcon.appiconset/Contents.json",
    "content": "{\n  \"images\" : [\n    {\n      \"idiom\" : \"universal\",\n      \"platform\" : \"ios\",\n      \"size\" : \"1024x1024\"\n    }\n  ],\n  \"info\" : {\n    \"author\" : \"xcode\",\n    \"version\" : 1\n  }\n}\n"
  },
  {
    "path": "SVDBDemo/SVDBDemo/Assets.xcassets/Contents.json",
    "content": "{\n  \"info\" : {\n    \"author\" : \"xcode\",\n    \"version\" : 1\n  }\n}\n"
  },
  {
    "path": "SVDBDemo/SVDBDemo/ContentView.swift",
    "content": "//\n//  ContentView.swift\n//  SVDBDemo\n//\n//  Created by Jordan Howlett on 8/4/23.\n//\n\nimport Accelerate\nimport CoreML\nimport NaturalLanguage\nimport SVDB\nimport SwiftUI\n\nstruct EmbeddingEntry: Codable {\n    let id: UUID\n    let text: String\n    let embedding: [Double]\n    let magnitude: Double\n}\n\nfunc generateRandomSentence() -> String {\n    var sentence = \"\"\n    for _ in 1...5 {\n        if let randomWord = words.randomElement() {\n            sentence += randomWord + \" \"\n        }\n    }\n    return sentence.trimmingCharacters(in: .whitespaces)\n}\n\nstruct ContentView: View {\n    let collectionName: String = \"testCollection\"\n    @State private var collection: Collection?\n    @State private var query: String = \"emotions\"\n    @State private var newEntry: String = \"\"\n    @State private var neighbors: [(String, Double)] = []\n\n    var body: some View {\n        VStack {\n            TextField(\"Enter query\", text: $query)\n                .padding()\n                .textFieldStyle(RoundedBorderTextFieldStyle())\n\n            HStack {\n                TextField(\"New Entry\", text: $newEntry)\n                    .textFieldStyle(RoundedBorderTextFieldStyle())\n                Button(\"Add Entry\") {\n                    Task {\n                        await addEntry(newEntry)\n                    }\n                }\n            }\n            .padding()\n\n            Button(\"Find Neighbors\") {\n                self.neighbors.removeAll()\n                Task {\n                    await findNeighbors()\n                }\n            }\n\n            Button(\"Generate Random Embeddings\") {\n                Task {\n                    await generateRandomEmbeddings()\n                }\n            }\n            .padding()\n\n            List(neighbors, id: \\.0) { neighbor in\n                Text(\"\\(neighbor.0) - \\(neighbor.1)\")\n            }\n        }\n        .padding()\n        .onAppear {\n            Task {\n                await loadCollection()\n            }\n        }\n    }\n\n    func loadCollection() async {\n        do {\n            collection = try SVDB.shared.collection(collectionName)\n        } catch {\n            print(\"Failed to load collection:\", error)\n        }\n    }\n\n    func generateRandomEmbeddings() async {\n        var randomSentences: [String] = []\n        for _ in 1...100 {\n            let sentence = generateRandomSentence()\n            randomSentences.append(sentence)\n        }\n\n        for sentence in randomSentences {\n            await addEntry(sentence)\n        }\n\n        print(\"Done creating\")\n    }\n\n    func addEntry(_ entry: String) async {\n        guard let collection = collection else { return }\n        guard let embedding = generateEmbedding(for: entry) else {\n            return\n        }\n\n        collection.addDocument(text: entry, embedding: embedding)\n    }\n\n    func generateEmbedding(for sentence: String) -> [Double]? {\n        guard let embedding = NLEmbedding.wordEmbedding(for: .english) else {\n            return nil\n        }\n\n        let words = sentence.lowercased().split(separator: \" \")\n        guard let firstVector = embedding.vector(for: String(words.first!)) else {\n            return nil\n        }\n\n        var vectorSum = [Double](firstVector)\n\n        for word in words.dropFirst() {\n            if let vector = embedding.vector(for: String(word)) {\n                vDSP_vaddD(vectorSum, 1, vector, 1, &vectorSum, 1, vDSP_Length(vectorSum.count))\n            }\n        }\n\n        var vectorAverage = [Double](repeating: 0, count: vectorSum.count)\n        var divisor = Double(words.count)\n        vDSP_vsdivD(vectorSum, 1, &divisor, &vectorAverage, 1, vDSP_Length(vectorAverage.count))\n\n        return vectorAverage\n    }\n\n    func findNeighbors() async {\n        guard let collection = collection else { return }\n        guard let queryEmbedding = generateEmbedding(for: query) else {\n            return\n        }\n\n        let results = collection.search(query: queryEmbedding)\n        neighbors = results.map { ($0.text, $0.score) }\n    }\n}\n\nstruct ContentView_Previews: PreviewProvider {\n    static var previews: some View {\n        ContentView()\n    }\n}\n"
  },
  {
    "path": "SVDBDemo/SVDBDemo/Preview Content/Preview Assets.xcassets/Contents.json",
    "content": "{\n  \"info\" : {\n    \"author\" : \"xcode\",\n    \"version\" : 1\n  }\n}\n"
  },
  {
    "path": "SVDBDemo/SVDBDemo/SVDBDemoApp.swift",
    "content": "//\n//  SVDBDemoApp.swift\n//  SVDBDemo\n//\n//  Created by Jordan Howlett on 8/4/23.\n//\n\nimport SwiftUI\n\n@main\nstruct SVDBDemoApp: App {\n    var body: some Scene {\n        WindowGroup {\n            ContentView()\n        }\n    }\n}\n"
  },
  {
    "path": "SVDBDemo/SVDBDemo/WORDS.swift",
    "content": "//\n//  WORDS.swift\n//  SVDBDemo\n//\n//  Created by Jordan Howlett on 8/4/23.\n//\n\nlet words = [\n    \"apple\", \"banana\", \"cherry\", \"date\", \"elderberry\", \"fig\", \"grape\", \"honeydew\", \"kiwi\", \"lemon\",\n\n    \"mango\", \"nectarine\", \"orange\", \"papaya\", \"quince\", \"raspberry\", \"strawberry\", \"tangerine\", \"watermelon\", \"blueberry\",\n\n    \"dog\", \"cat\", \"mouse\", \"bird\", \"fish\", \"elephant\", \"giraffe\", \"tiger\", \"lion\", \"bear\",\n\n    \"happy\", \"sad\", \"excited\", \"angry\", \"bored\", \"confused\", \"calm\", \"elated\", \"frustrated\", \"nervous\",\n\n    \"love\", \"hate\", \"trust\", \"fear\", \"joy\", \"surprise\", \"anticipation\", \"disgust\", \"sadness\", \"acceptance\",\n\n    \"run\", \"walk\", \"jump\", \"dance\", \"sing\", \"read\", \"write\", \"draw\", \"play\", \"swim\",\n\n    \"sun\", \"moon\", \"star\", \"sky\", \"cloud\", \"rain\", \"snow\", \"wind\", \"storm\", \"thunder\",\n\n    \"red\", \"blue\", \"green\", \"yellow\", \"purple\", \"pink\", \"brown\", \"black\", \"white\", \"gray\",\n\n    \"spring\", \"summer\", \"fall\", \"winter\", \"morning\", \"noon\", \"evening\", \"night\", \"dawn\", \"dusk\",\n\n    \"car\", \"truck\", \"boat\", \"plane\", \"train\", \"bicycle\", \"skateboard\", \"rollerblades\", \"scooter\", \"unicycle\",\n\n    \"tree\", \"flower\", \"bush\", \"cactus\", \"fern\", \"moss\", \"mushroom\", \"vine\", \"grass\", \"weed\",\n\n    \"shirt\", \"pants\", \"skirt\", \"dress\", \"socks\", \"shoes\", \"hat\", \"scarf\", \"gloves\", \"coat\",\n\n    \"eat\", \"drink\", \"sleep\", \"wake\", \"talk\", \"listen\", \"learn\", \"teach\", \"laugh\", \"cry\",\n\n    \"big\", \"small\", \"fast\", \"slow\", \"old\", \"young\", \"tall\", \"short\", \"loud\", \"quiet\",\n\n    \"ocean\", \"lake\", \"river\", \"stream\", \"pond\", \"puddle\", \"fountain\", \"waterfall\", \"wave\", \"ripple\",\n\n    \"rock\", \"stone\", \"pebble\", \"boulder\", \"gravel\", \"sand\", \"dirt\", \"mud\", \"clay\", \"silt\",\n\n    \"Monday\", \"Tuesday\", \"Wednesday\", \"Thursday\", \"Friday\", \"Saturday\", \"Sunday\",\n\n    \"January\", \"February\", \"March\", \"April\", \"May\", \"June\", \"July\", \"August\", \"September\", \"October\", \"November\", \"December\",\n\n    \"breakfast\", \"lunch\", \"dinner\", \"snack\", \"appetizer\", \"dessert\", \"meal\", \"feast\", \"picnic\", \"buffet\"\n]\n"
  },
  {
    "path": "SVDBDemo/SVDBDemo.xcodeproj/project.pbxproj",
    "content": "// !$*UTF8*$!\n{\n\tarchiveVersion = 1;\n\tclasses = {\n\t};\n\tobjectVersion = 56;\n\tobjects = {\n\n/* Begin PBXBuildFile section */\n\t\t177872D12A7DF0FE00D52548 /* SVDBDemoApp.swift in Sources */ = {isa = PBXBuildFile; fileRef = 177872D02A7DF0FE00D52548 /* SVDBDemoApp.swift */; };\n\t\t177872D32A7DF0FE00D52548 /* ContentView.swift in Sources */ = {isa = PBXBuildFile; fileRef = 177872D22A7DF0FE00D52548 /* ContentView.swift */; };\n\t\t177872D52A7DF10000D52548 /* Assets.xcassets in Resources */ = {isa = PBXBuildFile; fileRef = 177872D42A7DF10000D52548 /* Assets.xcassets */; };\n\t\t177872D82A7DF10000D52548 /* Preview Assets.xcassets in Resources */ = {isa = PBXBuildFile; fileRef = 177872D72A7DF10000D52548 /* Preview Assets.xcassets */; };\n\t\t17C2FE882A7DF61B00A3D246 /* WORDS.swift in Sources */ = {isa = PBXBuildFile; fileRef = 17C2FE872A7DF61B00A3D246 /* WORDS.swift */; };\n\t\t17C2FE8B2A7DF79300A3D246 /* SVDB in Frameworks */ = {isa = PBXBuildFile; productRef = 17C2FE8A2A7DF79300A3D246 /* SVDB */; };\n/* End PBXBuildFile section */\n\n/* Begin PBXFileReference section */\n\t\t177872CD2A7DF0FE00D52548 /* SVDBDemo.app */ = {isa = PBXFileReference; explicitFileType = wrapper.application; includeInIndex = 0; path = SVDBDemo.app; sourceTree = BUILT_PRODUCTS_DIR; };\n\t\t177872D02A7DF0FE00D52548 /* SVDBDemoApp.swift */ = {isa = PBXFileReference; lastKnownFileType = sourcecode.swift; path = SVDBDemoApp.swift; sourceTree = \"<group>\"; };\n\t\t177872D22A7DF0FE00D52548 /* ContentView.swift */ = {isa = PBXFileReference; lastKnownFileType = sourcecode.swift; path = ContentView.swift; sourceTree = \"<group>\"; };\n\t\t177872D42A7DF10000D52548 /* Assets.xcassets */ = {isa = PBXFileReference; lastKnownFileType = folder.assetcatalog; path = Assets.xcassets; sourceTree = \"<group>\"; };\n\t\t177872D72A7DF10000D52548 /* Preview Assets.xcassets */ = {isa = PBXFileReference; lastKnownFileType = folder.assetcatalog; path = \"Preview Assets.xcassets\"; sourceTree = \"<group>\"; };\n\t\t17C2FE872A7DF61B00A3D246 /* WORDS.swift */ = {isa = PBXFileReference; lastKnownFileType = sourcecode.swift; path = WORDS.swift; sourceTree = \"<group>\"; };\n/* End PBXFileReference section */\n\n/* Begin PBXFrameworksBuildPhase section */\n\t\t177872CA2A7DF0FE00D52548 /* Frameworks */ = {\n\t\t\tisa = PBXFrameworksBuildPhase;\n\t\t\tbuildActionMask = 2147483647;\n\t\t\tfiles = (\n\t\t\t\t17C2FE8B2A7DF79300A3D246 /* SVDB in Frameworks */,\n\t\t\t);\n\t\t\trunOnlyForDeploymentPostprocessing = 0;\n\t\t};\n/* End PBXFrameworksBuildPhase section */\n\n/* Begin PBXGroup section */\n\t\t177872C42A7DF0FE00D52548 = {\n\t\t\tisa = PBXGroup;\n\t\t\tchildren = (\n\t\t\t\t177872CF2A7DF0FE00D52548 /* SVDBDemo */,\n\t\t\t\t177872CE2A7DF0FE00D52548 /* Products */,\n\t\t\t);\n\t\t\tsourceTree = \"<group>\";\n\t\t};\n\t\t177872CE2A7DF0FE00D52548 /* Products */ = {\n\t\t\tisa = PBXGroup;\n\t\t\tchildren = (\n\t\t\t\t177872CD2A7DF0FE00D52548 /* SVDBDemo.app */,\n\t\t\t);\n\t\t\tname = Products;\n\t\t\tsourceTree = \"<group>\";\n\t\t};\n\t\t177872CF2A7DF0FE00D52548 /* SVDBDemo */ = {\n\t\t\tisa = PBXGroup;\n\t\t\tchildren = (\n\t\t\t\t177872D02A7DF0FE00D52548 /* SVDBDemoApp.swift */,\n\t\t\t\t177872D22A7DF0FE00D52548 /* ContentView.swift */,\n\t\t\t\t177872D42A7DF10000D52548 /* Assets.xcassets */,\n\t\t\t\t177872D62A7DF10000D52548 /* Preview Content */,\n\t\t\t\t17C2FE872A7DF61B00A3D246 /* WORDS.swift */,\n\t\t\t);\n\t\t\tpath = SVDBDemo;\n\t\t\tsourceTree = \"<group>\";\n\t\t};\n\t\t177872D62A7DF10000D52548 /* Preview Content */ = {\n\t\t\tisa = PBXGroup;\n\t\t\tchildren = (\n\t\t\t\t177872D72A7DF10000D52548 /* Preview Assets.xcassets */,\n\t\t\t);\n\t\t\tpath = \"Preview Content\";\n\t\t\tsourceTree = \"<group>\";\n\t\t};\n/* End PBXGroup section */\n\n/* Begin PBXNativeTarget section */\n\t\t177872CC2A7DF0FE00D52548 /* SVDBDemo */ = {\n\t\t\tisa = PBXNativeTarget;\n\t\t\tbuildConfigurationList = 177872DB2A7DF10000D52548 /* Build configuration list for PBXNativeTarget \"SVDBDemo\" */;\n\t\t\tbuildPhases = (\n\t\t\t\t177872C92A7DF0FE00D52548 /* Sources */,\n\t\t\t\t177872CA2A7DF0FE00D52548 /* Frameworks */,\n\t\t\t\t177872CB2A7DF0FE00D52548 /* Resources */,\n\t\t\t);\n\t\t\tbuildRules = (\n\t\t\t);\n\t\t\tdependencies = (\n\t\t\t);\n\t\t\tname = SVDBDemo;\n\t\t\tpackageProductDependencies = (\n\t\t\t\t17C2FE8A2A7DF79300A3D246 /* SVDB */,\n\t\t\t);\n\t\t\tproductName = SVDBDemo;\n\t\t\tproductReference = 177872CD2A7DF0FE00D52548 /* SVDBDemo.app */;\n\t\t\tproductType = \"com.apple.product-type.application\";\n\t\t};\n/* End PBXNativeTarget section */\n\n/* Begin PBXProject section */\n\t\t177872C52A7DF0FE00D52548 /* Project object */ = {\n\t\t\tisa = PBXProject;\n\t\t\tattributes = {\n\t\t\t\tBuildIndependentTargetsInParallel = 1;\n\t\t\t\tLastSwiftUpdateCheck = 1430;\n\t\t\t\tLastUpgradeCheck = 1430;\n\t\t\t\tTargetAttributes = {\n\t\t\t\t\t177872CC2A7DF0FE00D52548 = {\n\t\t\t\t\t\tCreatedOnToolsVersion = 14.3.1;\n\t\t\t\t\t};\n\t\t\t\t};\n\t\t\t};\n\t\t\tbuildConfigurationList = 177872C82A7DF0FE00D52548 /* Build configuration list for PBXProject \"SVDBDemo\" */;\n\t\t\tcompatibilityVersion = \"Xcode 14.0\";\n\t\t\tdevelopmentRegion = en;\n\t\t\thasScannedForEncodings = 0;\n\t\t\tknownRegions = (\n\t\t\t\ten,\n\t\t\t\tBase,\n\t\t\t);\n\t\t\tmainGroup = 177872C42A7DF0FE00D52548;\n\t\t\tpackageReferences = (\n\t\t\t\t17C2FE892A7DF79300A3D246 /* XCRemoteSwiftPackageReference \"SVDB\" */,\n\t\t\t);\n\t\t\tproductRefGroup = 177872CE2A7DF0FE00D52548 /* Products */;\n\t\t\tprojectDirPath = \"\";\n\t\t\tprojectRoot = \"\";\n\t\t\ttargets = (\n\t\t\t\t177872CC2A7DF0FE00D52548 /* SVDBDemo */,\n\t\t\t);\n\t\t};\n/* End PBXProject section */\n\n/* Begin PBXResourcesBuildPhase section */\n\t\t177872CB2A7DF0FE00D52548 /* Resources */ = {\n\t\t\tisa = PBXResourcesBuildPhase;\n\t\t\tbuildActionMask = 2147483647;\n\t\t\tfiles = (\n\t\t\t\t177872D82A7DF10000D52548 /* Preview Assets.xcassets in Resources */,\n\t\t\t\t177872D52A7DF10000D52548 /* Assets.xcassets in Resources */,\n\t\t\t);\n\t\t\trunOnlyForDeploymentPostprocessing = 0;\n\t\t};\n/* End PBXResourcesBuildPhase section */\n\n/* Begin PBXSourcesBuildPhase section */\n\t\t177872C92A7DF0FE00D52548 /* Sources */ = {\n\t\t\tisa = PBXSourcesBuildPhase;\n\t\t\tbuildActionMask = 2147483647;\n\t\t\tfiles = (\n\t\t\t\t177872D32A7DF0FE00D52548 /* ContentView.swift in Sources */,\n\t\t\t\t17C2FE882A7DF61B00A3D246 /* WORDS.swift in Sources */,\n\t\t\t\t177872D12A7DF0FE00D52548 /* SVDBDemoApp.swift in Sources */,\n\t\t\t);\n\t\t\trunOnlyForDeploymentPostprocessing = 0;\n\t\t};\n/* End PBXSourcesBuildPhase section */\n\n/* Begin XCBuildConfiguration section */\n\t\t177872D92A7DF10000D52548 /* Debug */ = {\n\t\t\tisa = XCBuildConfiguration;\n\t\t\tbuildSettings = {\n\t\t\t\tALWAYS_SEARCH_USER_PATHS = NO;\n\t\t\t\tCLANG_ANALYZER_NONNULL = YES;\n\t\t\t\tCLANG_ANALYZER_NUMBER_OBJECT_CONVERSION = YES_AGGRESSIVE;\n\t\t\t\tCLANG_CXX_LANGUAGE_STANDARD = \"gnu++20\";\n\t\t\t\tCLANG_ENABLE_MODULES = YES;\n\t\t\t\tCLANG_ENABLE_OBJC_ARC = YES;\n\t\t\t\tCLANG_ENABLE_OBJC_WEAK = YES;\n\t\t\t\tCLANG_WARN_BLOCK_CAPTURE_AUTORELEASING = YES;\n\t\t\t\tCLANG_WARN_BOOL_CONVERSION = YES;\n\t\t\t\tCLANG_WARN_COMMA = YES;\n\t\t\t\tCLANG_WARN_CONSTANT_CONVERSION = YES;\n\t\t\t\tCLANG_WARN_DEPRECATED_OBJC_IMPLEMENTATIONS = YES;\n\t\t\t\tCLANG_WARN_DIRECT_OBJC_ISA_USAGE = YES_ERROR;\n\t\t\t\tCLANG_WARN_DOCUMENTATION_COMMENTS = YES;\n\t\t\t\tCLANG_WARN_EMPTY_BODY = YES;\n\t\t\t\tCLANG_WARN_ENUM_CONVERSION = YES;\n\t\t\t\tCLANG_WARN_INFINITE_RECURSION = YES;\n\t\t\t\tCLANG_WARN_INT_CONVERSION = YES;\n\t\t\t\tCLANG_WARN_NON_LITERAL_NULL_CONVERSION = YES;\n\t\t\t\tCLANG_WARN_OBJC_IMPLICIT_RETAIN_SELF = YES;\n\t\t\t\tCLANG_WARN_OBJC_LITERAL_CONVERSION = YES;\n\t\t\t\tCLANG_WARN_OBJC_ROOT_CLASS = YES_ERROR;\n\t\t\t\tCLANG_WARN_QUOTED_INCLUDE_IN_FRAMEWORK_HEADER = YES;\n\t\t\t\tCLANG_WARN_RANGE_LOOP_ANALYSIS = YES;\n\t\t\t\tCLANG_WARN_STRICT_PROTOTYPES = YES;\n\t\t\t\tCLANG_WARN_SUSPICIOUS_MOVE = YES;\n\t\t\t\tCLANG_WARN_UNGUARDED_AVAILABILITY = YES_AGGRESSIVE;\n\t\t\t\tCLANG_WARN_UNREACHABLE_CODE = YES;\n\t\t\t\tCLANG_WARN__DUPLICATE_METHOD_MATCH = YES;\n\t\t\t\tCOPY_PHASE_STRIP = NO;\n\t\t\t\tDEBUG_INFORMATION_FORMAT = dwarf;\n\t\t\t\tENABLE_STRICT_OBJC_MSGSEND = YES;\n\t\t\t\tENABLE_TESTABILITY = YES;\n\t\t\t\tGCC_C_LANGUAGE_STANDARD = gnu11;\n\t\t\t\tGCC_DYNAMIC_NO_PIC = NO;\n\t\t\t\tGCC_NO_COMMON_BLOCKS = YES;\n\t\t\t\tGCC_OPTIMIZATION_LEVEL = 0;\n\t\t\t\tGCC_PREPROCESSOR_DEFINITIONS = (\n\t\t\t\t\t\"DEBUG=1\",\n\t\t\t\t\t\"$(inherited)\",\n\t\t\t\t);\n\t\t\t\tGCC_WARN_64_TO_32_BIT_CONVERSION = YES;\n\t\t\t\tGCC_WARN_ABOUT_RETURN_TYPE = YES_ERROR;\n\t\t\t\tGCC_WARN_UNDECLARED_SELECTOR = YES;\n\t\t\t\tGCC_WARN_UNINITIALIZED_AUTOS = YES_AGGRESSIVE;\n\t\t\t\tGCC_WARN_UNUSED_FUNCTION = YES;\n\t\t\t\tGCC_WARN_UNUSED_VARIABLE = YES;\n\t\t\t\tIPHONEOS_DEPLOYMENT_TARGET = 16.4;\n\t\t\t\tMTL_ENABLE_DEBUG_INFO = INCLUDE_SOURCE;\n\t\t\t\tMTL_FAST_MATH = YES;\n\t\t\t\tONLY_ACTIVE_ARCH = YES;\n\t\t\t\tSDKROOT = iphoneos;\n\t\t\t\tSWIFT_ACTIVE_COMPILATION_CONDITIONS = DEBUG;\n\t\t\t\tSWIFT_OPTIMIZATION_LEVEL = \"-Onone\";\n\t\t\t};\n\t\t\tname = Debug;\n\t\t};\n\t\t177872DA2A7DF10000D52548 /* Release */ = {\n\t\t\tisa = XCBuildConfiguration;\n\t\t\tbuildSettings = {\n\t\t\t\tALWAYS_SEARCH_USER_PATHS = NO;\n\t\t\t\tCLANG_ANALYZER_NONNULL = YES;\n\t\t\t\tCLANG_ANALYZER_NUMBER_OBJECT_CONVERSION = YES_AGGRESSIVE;\n\t\t\t\tCLANG_CXX_LANGUAGE_STANDARD = \"gnu++20\";\n\t\t\t\tCLANG_ENABLE_MODULES = YES;\n\t\t\t\tCLANG_ENABLE_OBJC_ARC = YES;\n\t\t\t\tCLANG_ENABLE_OBJC_WEAK = YES;\n\t\t\t\tCLANG_WARN_BLOCK_CAPTURE_AUTORELEASING = YES;\n\t\t\t\tCLANG_WARN_BOOL_CONVERSION = YES;\n\t\t\t\tCLANG_WARN_COMMA = YES;\n\t\t\t\tCLANG_WARN_CONSTANT_CONVERSION = YES;\n\t\t\t\tCLANG_WARN_DEPRECATED_OBJC_IMPLEMENTATIONS = YES;\n\t\t\t\tCLANG_WARN_DIRECT_OBJC_ISA_USAGE = YES_ERROR;\n\t\t\t\tCLANG_WARN_DOCUMENTATION_COMMENTS = YES;\n\t\t\t\tCLANG_WARN_EMPTY_BODY = YES;\n\t\t\t\tCLANG_WARN_ENUM_CONVERSION = YES;\n\t\t\t\tCLANG_WARN_INFINITE_RECURSION = YES;\n\t\t\t\tCLANG_WARN_INT_CONVERSION = YES;\n\t\t\t\tCLANG_WARN_NON_LITERAL_NULL_CONVERSION = YES;\n\t\t\t\tCLANG_WARN_OBJC_IMPLICIT_RETAIN_SELF = YES;\n\t\t\t\tCLANG_WARN_OBJC_LITERAL_CONVERSION = YES;\n\t\t\t\tCLANG_WARN_OBJC_ROOT_CLASS = YES_ERROR;\n\t\t\t\tCLANG_WARN_QUOTED_INCLUDE_IN_FRAMEWORK_HEADER = YES;\n\t\t\t\tCLANG_WARN_RANGE_LOOP_ANALYSIS = YES;\n\t\t\t\tCLANG_WARN_STRICT_PROTOTYPES = YES;\n\t\t\t\tCLANG_WARN_SUSPICIOUS_MOVE = YES;\n\t\t\t\tCLANG_WARN_UNGUARDED_AVAILABILITY = YES_AGGRESSIVE;\n\t\t\t\tCLANG_WARN_UNREACHABLE_CODE = YES;\n\t\t\t\tCLANG_WARN__DUPLICATE_METHOD_MATCH = YES;\n\t\t\t\tCOPY_PHASE_STRIP = NO;\n\t\t\t\tDEBUG_INFORMATION_FORMAT = \"dwarf-with-dsym\";\n\t\t\t\tENABLE_NS_ASSERTIONS = NO;\n\t\t\t\tENABLE_STRICT_OBJC_MSGSEND = YES;\n\t\t\t\tGCC_C_LANGUAGE_STANDARD = gnu11;\n\t\t\t\tGCC_NO_COMMON_BLOCKS = YES;\n\t\t\t\tGCC_WARN_64_TO_32_BIT_CONVERSION = YES;\n\t\t\t\tGCC_WARN_ABOUT_RETURN_TYPE = YES_ERROR;\n\t\t\t\tGCC_WARN_UNDECLARED_SELECTOR = YES;\n\t\t\t\tGCC_WARN_UNINITIALIZED_AUTOS = YES_AGGRESSIVE;\n\t\t\t\tGCC_WARN_UNUSED_FUNCTION = YES;\n\t\t\t\tGCC_WARN_UNUSED_VARIABLE = YES;\n\t\t\t\tIPHONEOS_DEPLOYMENT_TARGET = 16.4;\n\t\t\t\tMTL_ENABLE_DEBUG_INFO = NO;\n\t\t\t\tMTL_FAST_MATH = YES;\n\t\t\t\tSDKROOT = iphoneos;\n\t\t\t\tSWIFT_COMPILATION_MODE = wholemodule;\n\t\t\t\tSWIFT_OPTIMIZATION_LEVEL = \"-O\";\n\t\t\t\tVALIDATE_PRODUCT = YES;\n\t\t\t};\n\t\t\tname = Release;\n\t\t};\n\t\t177872DC2A7DF10000D52548 /* Debug */ = {\n\t\t\tisa = XCBuildConfiguration;\n\t\t\tbuildSettings = {\n\t\t\t\tASSETCATALOG_COMPILER_APPICON_NAME = AppIcon;\n\t\t\t\tASSETCATALOG_COMPILER_GLOBAL_ACCENT_COLOR_NAME = AccentColor;\n\t\t\t\tCODE_SIGN_STYLE = Automatic;\n\t\t\t\tCURRENT_PROJECT_VERSION = 1;\n\t\t\t\tDEVELOPMENT_ASSET_PATHS = \"\\\"SVDBDemo/Preview Content\\\"\";\n\t\t\t\tDEVELOPMENT_TEAM = PPVB2USUND;\n\t\t\t\tENABLE_PREVIEWS = YES;\n\t\t\t\tGENERATE_INFOPLIST_FILE = YES;\n\t\t\t\tINFOPLIST_KEY_UIApplicationSceneManifest_Generation = YES;\n\t\t\t\tINFOPLIST_KEY_UIApplicationSupportsIndirectInputEvents = YES;\n\t\t\t\tINFOPLIST_KEY_UILaunchScreen_Generation = YES;\n\t\t\t\tINFOPLIST_KEY_UISupportedInterfaceOrientations_iPad = \"UIInterfaceOrientationPortrait UIInterfaceOrientationPortraitUpsideDown UIInterfaceOrientationLandscapeLeft UIInterfaceOrientationLandscapeRight\";\n\t\t\t\tINFOPLIST_KEY_UISupportedInterfaceOrientations_iPhone = \"UIInterfaceOrientationPortrait UIInterfaceOrientationLandscapeLeft UIInterfaceOrientationLandscapeRight\";\n\t\t\t\tLD_RUNPATH_SEARCH_PATHS = (\n\t\t\t\t\t\"$(inherited)\",\n\t\t\t\t\t\"@executable_path/Frameworks\",\n\t\t\t\t);\n\t\t\t\tMARKETING_VERSION = 1.0;\n\t\t\t\tPRODUCT_BUNDLE_IDENTIFIER = co.beginagain.SVDBDemo;\n\t\t\t\tPRODUCT_NAME = \"$(TARGET_NAME)\";\n\t\t\t\tSWIFT_EMIT_LOC_STRINGS = YES;\n\t\t\t\tSWIFT_VERSION = 5.0;\n\t\t\t\tTARGETED_DEVICE_FAMILY = \"1,2\";\n\t\t\t};\n\t\t\tname = Debug;\n\t\t};\n\t\t177872DD2A7DF10000D52548 /* Release */ = {\n\t\t\tisa = XCBuildConfiguration;\n\t\t\tbuildSettings = {\n\t\t\t\tASSETCATALOG_COMPILER_APPICON_NAME = AppIcon;\n\t\t\t\tASSETCATALOG_COMPILER_GLOBAL_ACCENT_COLOR_NAME = AccentColor;\n\t\t\t\tCODE_SIGN_STYLE = Automatic;\n\t\t\t\tCURRENT_PROJECT_VERSION = 1;\n\t\t\t\tDEVELOPMENT_ASSET_PATHS = \"\\\"SVDBDemo/Preview Content\\\"\";\n\t\t\t\tDEVELOPMENT_TEAM = PPVB2USUND;\n\t\t\t\tENABLE_PREVIEWS = YES;\n\t\t\t\tGENERATE_INFOPLIST_FILE = YES;\n\t\t\t\tINFOPLIST_KEY_UIApplicationSceneManifest_Generation = YES;\n\t\t\t\tINFOPLIST_KEY_UIApplicationSupportsIndirectInputEvents = YES;\n\t\t\t\tINFOPLIST_KEY_UILaunchScreen_Generation = YES;\n\t\t\t\tINFOPLIST_KEY_UISupportedInterfaceOrientations_iPad = \"UIInterfaceOrientationPortrait UIInterfaceOrientationPortraitUpsideDown UIInterfaceOrientationLandscapeLeft UIInterfaceOrientationLandscapeRight\";\n\t\t\t\tINFOPLIST_KEY_UISupportedInterfaceOrientations_iPhone = \"UIInterfaceOrientationPortrait UIInterfaceOrientationLandscapeLeft UIInterfaceOrientationLandscapeRight\";\n\t\t\t\tLD_RUNPATH_SEARCH_PATHS = (\n\t\t\t\t\t\"$(inherited)\",\n\t\t\t\t\t\"@executable_path/Frameworks\",\n\t\t\t\t);\n\t\t\t\tMARKETING_VERSION = 1.0;\n\t\t\t\tPRODUCT_BUNDLE_IDENTIFIER = co.beginagain.SVDBDemo;\n\t\t\t\tPRODUCT_NAME = \"$(TARGET_NAME)\";\n\t\t\t\tSWIFT_EMIT_LOC_STRINGS = YES;\n\t\t\t\tSWIFT_VERSION = 5.0;\n\t\t\t\tTARGETED_DEVICE_FAMILY = \"1,2\";\n\t\t\t};\n\t\t\tname = Release;\n\t\t};\n/* End XCBuildConfiguration section */\n\n/* Begin XCConfigurationList section */\n\t\t177872C82A7DF0FE00D52548 /* Build configuration list for PBXProject \"SVDBDemo\" */ = {\n\t\t\tisa = XCConfigurationList;\n\t\t\tbuildConfigurations = (\n\t\t\t\t177872D92A7DF10000D52548 /* Debug */,\n\t\t\t\t177872DA2A7DF10000D52548 /* Release */,\n\t\t\t);\n\t\t\tdefaultConfigurationIsVisible = 0;\n\t\t\tdefaultConfigurationName = Release;\n\t\t};\n\t\t177872DB2A7DF10000D52548 /* Build configuration list for PBXNativeTarget \"SVDBDemo\" */ = {\n\t\t\tisa = XCConfigurationList;\n\t\t\tbuildConfigurations = (\n\t\t\t\t177872DC2A7DF10000D52548 /* Debug */,\n\t\t\t\t177872DD2A7DF10000D52548 /* Release */,\n\t\t\t);\n\t\t\tdefaultConfigurationIsVisible = 0;\n\t\t\tdefaultConfigurationName = Release;\n\t\t};\n/* End XCConfigurationList section */\n\n/* Begin XCRemoteSwiftPackageReference section */\n\t\t17C2FE892A7DF79300A3D246 /* XCRemoteSwiftPackageReference \"SVDB\" */ = {\n\t\t\tisa = XCRemoteSwiftPackageReference;\n\t\t\trepositoryURL = \"git@github.com:Dripfarm/SVDB.git\";\n\t\t\trequirement = {\n\t\t\t\tkind = upToNextMajorVersion;\n\t\t\t\tminimumVersion = 1.0.0;\n\t\t\t};\n\t\t};\n/* End XCRemoteSwiftPackageReference section */\n\n/* Begin XCSwiftPackageProductDependency section */\n\t\t17C2FE8A2A7DF79300A3D246 /* SVDB */ = {\n\t\t\tisa = XCSwiftPackageProductDependency;\n\t\t\tpackage = 17C2FE892A7DF79300A3D246 /* XCRemoteSwiftPackageReference \"SVDB\" */;\n\t\t\tproductName = SVDB;\n\t\t};\n/* End XCSwiftPackageProductDependency section */\n\t};\n\trootObject = 177872C52A7DF0FE00D52548 /* Project object */;\n}\n"
  },
  {
    "path": "SVDBDemo/SVDBDemo.xcodeproj/project.xcworkspace/contents.xcworkspacedata",
    "content": "<?xml version=\"1.0\" encoding=\"UTF-8\"?>\n<Workspace\n   version = \"1.0\">\n   <FileRef\n      location = \"self:\">\n   </FileRef>\n</Workspace>\n"
  },
  {
    "path": "SVDBDemo/SVDBDemo.xcodeproj/project.xcworkspace/xcshareddata/IDEWorkspaceChecks.plist",
    "content": "<?xml version=\"1.0\" encoding=\"UTF-8\"?>\n<!DOCTYPE plist PUBLIC \"-//Apple//DTD PLIST 1.0//EN\" \"http://www.apple.com/DTDs/PropertyList-1.0.dtd\">\n<plist version=\"1.0\">\n<dict>\n\t<key>IDEDidComputeMac32BitWarning</key>\n\t<true/>\n</dict>\n</plist>\n"
  },
  {
    "path": "SVDBDemo/SVDBDemo.xcodeproj/project.xcworkspace/xcshareddata/swiftpm/Package.resolved",
    "content": "{\n  \"pins\" : [\n    {\n      \"identity\" : \"svdb\",\n      \"kind\" : \"remoteSourceControl\",\n      \"location\" : \"git@github.com:Dripfarm/SVDB.git\",\n      \"state\" : {\n        \"revision\" : \"cd44e9db1786b811277db1b151825e714b1995ac\",\n        \"version\" : \"1.0.1\"\n      }\n    }\n  ],\n  \"version\" : 2\n}\n"
  },
  {
    "path": "Sources/SVDB/API/Collection.swift",
    "content": "//\n//  File.swift\n//\n//\n//  Created by Jordan Howlett on 8/4/23.\n//\n\nimport Accelerate\nimport CoreML\nimport NaturalLanguage\n\n@available(macOS 10.15, *)\n@available(iOS 13.0, *)\npublic class Collection {\n    private var documents: [UUID: Document] = [:]\n    private let name: String\n\n    init(name: String) {\n        self.name = name\n    }\n\n    public func addDocument(id: UUID? = nil, text: String, embedding: [Double]) {\n        let document = Document(\n            id: id ?? UUID(),\n            text: text,\n            embedding: embedding\n        )\n\n        documents[document.id] = document\n        save()\n    }\n\n    public func addDocuments(_ docs: [Document]) {\n        docs.forEach { documents[$0.id] = $0 }\n        save()\n    }\n\n    public func removeDocument(byId id: UUID) {\n        documents[id] = nil\n        save()\n    }\n\n    public func search(\n        query: [Double],\n        num_results: Int = 10,\n        threshold: Double? = nil\n    ) -> [SearchResult] {\n        let queryMagnitude = sqrt(query.reduce(0) { $0 + $1 * $1 })\n\n        var similarities: [SearchResult] = []\n        for document in documents.values {\n            let id = document.id\n            let text = document.text\n            let vector = document.embedding\n            let magnitude = sqrt(vector.reduce(0) { $0 + $1 * $1 })\n            let similarity = MathFunctions.cosineSimilarity(query, vector, magnitudeA: queryMagnitude, magnitudeB: magnitude)\n\n            if let thresholdValue = threshold, similarity < thresholdValue {\n                continue\n            }\n\n            similarities.append(SearchResult(id: id, text: text, score: similarity))\n        }\n\n        return Array(similarities.sorted(by: { $0.score > $1.score }).prefix(num_results))\n    }\n\n    private func save() {\n        let svdbDirectory = FileManager.default.urls(for: .documentDirectory, in: .userDomainMask).first!.appendingPathComponent(\"SVDB\")\n        try? FileManager.default.createDirectory(at: svdbDirectory, withIntermediateDirectories: true, attributes: nil)\n\n        let fileURL = svdbDirectory.appendingPathComponent(\"\\(name).json\")\n\n        do {\n            let encodedDocuments = try JSONEncoder().encode(documents)\n            let compressedData = try (encodedDocuments as NSData).compressed(using: .zlib)\n            try compressedData.write(to: fileURL)\n        } catch {\n            print(\"Failed to save documents: \\(error.localizedDescription)\")\n        }\n    }\n\n    public func load() throws {\n        let svdbDirectory = FileManager.default.urls(for: .documentDirectory, in: .userDomainMask).first!.appendingPathComponent(\"SVDB\")\n        let fileURL = svdbDirectory.appendingPathComponent(\"\\(name).json\")\n\n        // Check if file exists\n        guard FileManager.default.fileExists(atPath: fileURL.path) else {\n            print(\"File does not exist for collection \\(name), initializing with empty documents.\")\n            documents = [:]\n            return\n        }\n\n        do {\n            let compressedData = try Data(contentsOf: fileURL)\n\n            let decompressedData = try (compressedData as NSData).decompressed(using: .zlib)\n            documents = try JSONDecoder().decode([UUID: Document].self, from: decompressedData as Data)\n\n            print(\"Successfully loaded collection: \\(name)\")\n        } catch {\n            print(\"Failed to load collection \\(name): \\(error.localizedDescription)\")\n            throw CollectionError.loadFailed(error.localizedDescription)\n        }\n    }\n\n    public func clear() {\n        documents.removeAll()\n        save()\n    }\n}\n"
  },
  {
    "path": "Sources/SVDB/API/SVDB.swift",
    "content": "import Accelerate\nimport CoreML\nimport NaturalLanguage\n\n@available(macOS 10.15, *)\n@available(iOS 13.0, *)\npublic class SVDB {\n    public static let shared = SVDB()\n    private var collections: [String: Collection] = [:]\n\n    private init() {}\n\n    public func collection(_ name: String) throws -> Collection {\n        if collections[name] != nil {\n            throw SVDBError.collectionAlreadyExists\n        }\n\n        let collection = Collection(name: name)\n        collections[name] = collection\n        try collection.load()\n        return collection\n    }\n\n    public func getCollection(_ name: String) -> Collection? {\n        return collections[name]\n    }\n\n    public func releaseCollection(_ name: String) {\n        collections[name] = nil\n    }\n\n    public func reset() {\n        for (_, collection) in collections {\n            collection.clear()\n        }\n        collections.removeAll()\n    }\n}\n"
  },
  {
    "path": "Sources/SVDB/Internals/Models/Document.swift",
    "content": "//\n//  File.swift\n//\n//\n//  Created by Jordan Howlett on 8/4/23.\n//\n\nimport Foundation\n\npublic struct Document: Codable, Identifiable {\n    public let id: UUID\n    public let text: String\n    public let embedding: [Double]\n    public let magnitude: Double\n\n    public init(id: UUID? = nil, text: String, embedding: [Double]) {\n        self.id = id ?? UUID()\n        self.text = text\n        self.embedding = embedding\n        self.magnitude = sqrt(embedding.reduce(0) { $0 + $1 * $1 })\n    }\n}\n"
  },
  {
    "path": "Sources/SVDB/Internals/Models/Errors.swift",
    "content": "//\n//  File.swift\n//\n//\n//  Created by Jordan Howlett on 8/4/23.\n//\n\nimport Foundation\n\npublic enum SVDBError: Error {\n    case collectionAlreadyExists\n}\n\npublic enum CollectionError: Error {\n    case fileNotFound\n    case loadFailed(String)\n}\n"
  },
  {
    "path": "Sources/SVDB/Internals/Models/SearchResult.swift",
    "content": "//\n//  File.swift\n//\n//\n//  Created by Jordan Howlett on 8/4/23.\n//\n\nimport Foundation\n\npublic struct SearchResult {\n    public let id: UUID\n    public let text: String\n    public let score: Double\n}\n"
  },
  {
    "path": "Sources/SVDB/Internals/TheMathFile.swift",
    "content": "//\n//  MathFunctions.swift\n//\n\nimport Accelerate\n\nenum MathFunctions {\n    static func cosineSimilarity(_ a: [Double], _ b: [Double], magnitudeA: Double, magnitudeB: Double) -> Double {\n        var result = 0.0\n        vDSP_dotprD(a, 1, b, 1, &result, vDSP_Length(a.count))\n        return result / (magnitudeA * magnitudeB)\n    }\n\n    static func euclideanDistance(_ a: [Double], _ b: [Double]) -> Double {\n        var differences = [Double](repeating: 0.0, count: a.count)\n        vDSP_vsubD(a, 1, b, 1, &differences, 1, vDSP_Length(a.count))\n\n        var squaredDifferences = [Double](repeating: 0.0, count: a.count)\n        vDSP_vsqD(differences, 1, &squaredDifferences, 1, vDSP_Length(a.count))\n\n        var sumOfSquaredDifferences = 0.0\n        vDSP_sveD(squaredDifferences, 1, &sumOfSquaredDifferences, vDSP_Length(a.count))\n\n        return sqrt(sumOfSquaredDifferences)\n    }\n}\n"
  },
  {
    "path": "Tests/SVDBTests/CollectionTests.swift",
    "content": "//\n//  SwiftUIView.swift\n//\n//\n//  Created by Jordan Howlett on 8/4/23.\n//\n\n@testable import SVDB\nimport XCTest\n\nclass SVDBCollectionTests: XCTestCase {\n    override func setUp() {\n        super.setUp()\n        SVDB.shared.reset()\n    }\n\n    override func tearDown() {\n        super.tearDown()\n        SVDB.shared.reset()\n        try! SVDB.shared.collection(\"test\").clear()\n    }\n\n    func testAddDocument_WithProvidedID() {\n        let collection = Collection(name: \"test\")\n        let id = UUID()\n        let text = \"Test text awesome 2\"\n        let embedding = [1.0, 2.0, 3.0]\n\n        collection.addDocument(id: id, text: text, embedding: embedding)\n\n        let results = collection.search(query: embedding, num_results: 5)\n        XCTAssertEqual(results.count, 1)\n        XCTAssertEqual(results.first?.text, text)\n    }\n\n    func testAddDocument_Duplicate() {\n        let collection = Collection(name: \"test\")\n        let id = UUID()\n        let text = \"Test text awesome 2\"\n        let expected = \"Test text we expect\"\n        let embedding = [1.0, 2.0, 3.0]\n\n        collection.addDocument(id: id, text: text, embedding: embedding)\n        collection.addDocument(id: id, text: expected, embedding: embedding)\n\n        let results = collection.search(query: embedding, num_results: 5)\n        XCTAssertEqual(results.count, 1, \"Documents with duplicate ids should be overwritten\")\n        XCTAssertEqual(results.first?.text, expected)\n    }\n\n    func testAddDocument_WithoutProvidedID() {\n        let collection = Collection(name: \"test\")\n        let text = \"Test text Awesome\"\n        let embedding = [1.0, 2.0, 3.0]\n\n        collection.addDocument(id: nil, text: text, embedding: embedding)\n\n        let results = collection.search(query: embedding, num_results: 5)\n        XCTAssertEqual(results.count, 1)\n        XCTAssertEqual(results.first?.text, text)\n    }\n\n    func testAddDocument_MagnitudeCalculation() {\n        let collection = Collection(name: \"test\")\n        let embedding = [3.0, 4.0]\n\n        collection.addDocument(id: nil, text: \"text\", embedding: embedding)\n\n        let results = collection.search(query: embedding, num_results: 5)\n        XCTAssertEqual(results.count, 1)\n        XCTAssertEqual(results.first?.text, \"text\")\n    }\n\n    func testAddDocuments() {\n        let svdb = SVDB.shared\n        SVDB.shared.reset()\n        let collectionName = \"test\"\n        let collection = try! svdb.collection(collectionName)\n\n        let document1 = Document(id: UUID(), text: \"test1\", embedding: [1.0, 2.0, 3.0])\n        let document2 = Document(id: UUID(), text: \"test2\", embedding: [4.0, 5.0, 6.0])\n        let query = [2.5, 3.5, 4.5]\n\n        collection.addDocuments([document1, document2])\n\n        let searchResults = collection.search(query: query, num_results: 5)\n\n        let resultTexts = searchResults.map { $0.text }\n        XCTAssertTrue(resultTexts.contains(document1.text))\n        XCTAssertTrue(resultTexts.contains(document2.text))\n        XCTAssertTrue(resultTexts.count == 2)\n    }\n\n    func testRemoveDocument_Existing() {\n        let collection = Collection(name: \"test\")\n        let id1 = UUID()\n        let id2 = UUID()\n        let document1 = Document(id: id1, text: \"test1\", embedding: [1.0, 2.0, 3.0])\n        let document2 = Document(id: id2, text: \"test2\", embedding: [4.0, 5.0, 6.0])\n\n        collection.addDocuments([document1, document2])\n        XCTAssertFalse(collection.search(query: []).isEmpty, \"There should be a hit for this query\")\n\n        collection.removeDocument(byId: id1)\n        XCTAssertEqual(collection.search(query: []).count, 1)\n\n        collection.removeDocument(byId: id2)\n        XCTAssertEqual(collection.search(query: []).count, 0)\n    }\n}\n"
  },
  {
    "path": "Tests/SVDBTests/SVDBTests.swift",
    "content": "@testable import SVDB\nimport XCTest\n\nfinal class SVDBTests: XCTestCase {\n    override func setUp() {\n        super.setUp()\n        SVDB.shared.reset()\n    }\n\n    func testCreateCollection_Success() {\n        let name = \"uniqueCollectionName\"\n\n        do {\n            _ = try SVDB.shared.collection(name)\n        } catch {\n            XCTFail(\"Unexpected error: \\(error)\")\n        }\n    }\n\n    func testCreateCollection_CollectionAlreadyExists() {\n        let name = \"existingCollectionName\"\n        do {\n            _ = try SVDB.shared.collection(name)\n            print(\"created\")\n        } catch {\n            XCTFail(\"Unexpected error: \\(error)\")\n        }\n\n        do {\n            _ = try SVDB.shared.collection(name)\n            XCTFail(\"Expected collectionAlreadyExists error, but no error was thrown.\")\n        } catch let error as SVDBError {\n            XCTAssertEqual(error, SVDBError.collectionAlreadyExists)\n        } catch {\n            XCTFail(\"Unexpected error: \\(error)\")\n        }\n    }\n\n    func testRemoveDocument() throws {\n        let svdb = SVDB.shared\n        let collectionName = \"test\"\n        let collection = try svdb.collection(collectionName)\n\n        let document1 = Document(id: UUID(), text: \"test1\", embedding: [1.0, 2.0, 3.0])\n        let document2 = Document(id: UUID(), text: \"test2\", embedding: [4.0, 5.0, 6.0])\n\n        collection.addDocuments([document1, document2])\n\n        collection.removeDocument(byId: document1.id)\n\n        let query = [2.5, 3.5, 4.5]\n        let searchResults = collection.search(query: query, num_results: 2)\n        let resultIds = searchResults.map { $0.id }\n\n        XCTAssertFalse(resultIds.contains(document1.id))\n        XCTAssertTrue(resultIds.contains(document2.id))\n    }\n}\n"
  },
  {
    "path": "Tests/SVDBTests/SearchTests.swift",
    "content": "//\n//  SwiftUIView.swift\n//\n//\n//  Created by Jordan Howlett on 8/4/23.\n//\n\n@testable import SVDB\nimport XCTest\n\nclass SVDBSearchTests: XCTestCase {\n    override func setUp() {\n        super.setUp()\n        SVDB.shared.reset()\n    }\n\n    func testSearchWithDefaultParameters() throws {\n        let svdb = SVDB.shared\n        let collectionName = \"test\"\n        let collection = try svdb.collection(collectionName)\n\n        let documents = [\n            Document(id: UUID(), text: \"test1\", embedding: [1.0, 2.0, 3.0]),\n            Document(id: UUID(), text: \"test2\", embedding: [4.0, 5.0, 6.0])\n        ]\n\n        collection.addDocuments(documents)\n        let query = [2.5, 3.5, 4.5]\n\n        let results = collection.search(query: query)\n\n        XCTAssertEqual(results.count, 2)\n    }\n\n    func testSearchWithNumResults() throws {\n        let svdb = SVDB.shared\n        let collectionName = \"test\"\n        let collection = try svdb.collection(collectionName)\n\n        let documents = [\n            Document(id: UUID(), text: \"test1\", embedding: [1.0, 2.0, 3.0]),\n            Document(id: UUID(), text: \"test2\", embedding: [4.0, 5.0, 6.0])\n        ]\n\n        collection.addDocuments(documents)\n        let query = [2.5, 3.5, 4.5]\n\n        let results = collection.search(query: query, num_results: 1)\n\n        XCTAssertEqual(results.count, 1)\n    }\n\n    func testSearchWithThreshold() throws {\n        let svdb = SVDB.shared\n        let collectionName = \"test\"\n        let collection = try svdb.collection(collectionName)\n\n        let documents = [\n            Document(id: UUID(), text: \"test1\", embedding: [900.0, 5000.0, 13.0]),\n            Document(id: UUID(), text: \"test2\", embedding: [4.0, 5.0, 6.0])\n        ]\n\n        collection.addDocuments(documents)\n        let query = [4.0, 5.0, 6.0]\n\n        let results = collection.search(query: query, threshold: 0.9)\n\n        XCTAssertEqual(results.count, 1)\n    }\n}\n"
  }
]