Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

DOC-11959: Vector Search Code Snippets - iOS #850

Merged
merged 13 commits into from
Mar 25, 2024
17 changes: 0 additions & 17 deletions .github/workflows/xcodebuild.yml

This file was deleted.

57 changes: 20 additions & 37 deletions modules/objc/examples/code_snippets.xcodeproj/project.pbxproj
Original file line number Diff line number Diff line change
Expand Up @@ -3,12 +3,10 @@
archiveVersion = 1;
classes = {
};
objectVersion = 52;
objectVersion = 54;
objects = {

/* Begin PBXBuildFile section */
1A26D3BA27F76B35007C7FEE /* CouchbaseLite.xcframework in Frameworks */ = {isa = PBXBuildFile; fileRef = 1A26D3B827F76B30007C7FEE /* CouchbaseLite.xcframework */; };
1A26D3BB27F76B35007C7FEE /* CouchbaseLite.xcframework in Embed Frameworks */ = {isa = PBXBuildFile; fileRef = 1A26D3B827F76B30007C7FEE /* CouchbaseLite.xcframework */; settings = {ATTRIBUTES = (CodeSignOnCopy, RemoveHeadersOnCopy, ); }; };
1A508E74273A3887007EABB2 /* Assets.xcassets in Resources */ = {isa = PBXBuildFile; fileRef = 1A508E66273A3887007EABB2 /* Assets.xcassets */; };
1A508E76273A3887007EABB2 /* ViewController.m in Sources */ = {isa = PBXBuildFile; fileRef = 1A508E68273A3887007EABB2 /* ViewController.m */; };
1A508E77273A3887007EABB2 /* LaunchScreen.storyboard in Resources */ = {isa = PBXBuildFile; fileRef = 1A508E69273A3887007EABB2 /* LaunchScreen.storyboard */; };
Expand All @@ -17,24 +15,12 @@
1A508E7A273A3887007EABB2 /* main.m in Sources */ = {isa = PBXBuildFile; fileRef = 1A508E6E273A3887007EABB2 /* main.m */; };
1A508E7B273A3887007EABB2 /* AppDelegate.m in Sources */ = {isa = PBXBuildFile; fileRef = 1A508E6F273A3887007EABB2 /* AppDelegate.m */; };
1A508E7C273A3887007EABB2 /* SampleCodeTest.m in Sources */ = {isa = PBXBuildFile; fileRef = 1A508E70273A3887007EABB2 /* SampleCodeTest.m */; };
AEE304D22BADC4F000C12A19 /* VectorSeach.m in Sources */ = {isa = PBXBuildFile; fileRef = AEE304D12BADC4F000C12A19 /* VectorSeach.m */; };
AEE304DD2BADC7FE00C12A19 /* CouchbaseLite.xcframework in Frameworks */ = {isa = PBXBuildFile; fileRef = AEE304D32BADC54200C12A19 /* CouchbaseLite.xcframework */; };
AEE304E02BADC80000C12A19 /* CouchbaseLiteVectorSearch.xcframework in Frameworks */ = {isa = PBXBuildFile; fileRef = AEE304D52BADC55B00C12A19 /* CouchbaseLiteVectorSearch.xcframework */; };
/* End PBXBuildFile section */

/* Begin PBXCopyFilesBuildPhase section */
BEC78AF8221A9D7D00FC586A /* Embed Frameworks */ = {
isa = PBXCopyFilesBuildPhase;
buildActionMask = 2147483647;
dstPath = "";
dstSubfolderSpec = 10;
files = (
1A26D3BB27F76B35007C7FEE /* CouchbaseLite.xcframework in Embed Frameworks */,
);
name = "Embed Frameworks";
runOnlyForDeploymentPostprocessing = 0;
};
/* End PBXCopyFilesBuildPhase section */

/* Begin PBXFileReference section */
1A26D3B827F76B30007C7FEE /* CouchbaseLite.xcframework */ = {isa = PBXFileReference; lastKnownFileType = wrapper.xcframework; name = CouchbaseLite.xcframework; path = Frameworks/CouchbaseLite.xcframework; sourceTree = "<group>"; };
1A354CEB273B5996001A20BE /* GettingStarted.h */ = {isa = PBXFileReference; lastKnownFileType = sourcecode.c.h; path = GettingStarted.h; sourceTree = "<group>"; };
1A354CEC273B5A54001A20BE /* Info.plist */ = {isa = PBXFileReference; lastKnownFileType = text.plist.xml; path = Info.plist; sourceTree = "<group>"; };
1A508E64273A3887007EABB2 /* AppDelegate.h */ = {isa = PBXFileReference; fileEncoding = 4; lastKnownFileType = sourcecode.c.h; path = AppDelegate.h; sourceTree = "<group>"; };
Expand All @@ -47,15 +33,19 @@
1A508E6F273A3887007EABB2 /* AppDelegate.m */ = {isa = PBXFileReference; fileEncoding = 4; lastKnownFileType = sourcecode.c.objc; path = AppDelegate.m; sourceTree = "<group>"; };
1A508E70273A3887007EABB2 /* SampleCodeTest.m */ = {isa = PBXFileReference; fileEncoding = 4; lastKnownFileType = sourcecode.c.objc; path = SampleCodeTest.m; sourceTree = "<group>"; };
1A508E72273A3887007EABB2 /* ViewController.h */ = {isa = PBXFileReference; fileEncoding = 4; lastKnownFileType = sourcecode.c.h; path = ViewController.h; sourceTree = "<group>"; };
BEC78ADA221A9A6500FC586A /* code-snippets.app */ = {isa = PBXFileReference; explicitFileType = wrapper.application; includeInIndex = 0; path = "code-snippets.app"; sourceTree = BUILT_PRODUCTS_DIR; };
AEE304D12BADC4F000C12A19 /* VectorSeach.m */ = {isa = PBXFileReference; lastKnownFileType = sourcecode.c.objc; path = VectorSeach.m; sourceTree = "<group>"; };
AEE304D32BADC54200C12A19 /* CouchbaseLite.xcframework */ = {isa = PBXFileReference; lastKnownFileType = wrapper.xcframework; name = CouchbaseLite.xcframework; path = "../../../../../../Downloads/couchbase-lite-objc_xc_enterprise_3.2.0-91/CouchbaseLite.xcframework"; sourceTree = "<group>"; };
AEE304D52BADC55B00C12A19 /* CouchbaseLiteVectorSearch.xcframework */ = {isa = PBXFileReference; lastKnownFileType = wrapper.xcframework; name = CouchbaseLiteVectorSearch.xcframework; path = ../../../../../../Downloads/CouchbaseLiteVectorSearch.xcframework; sourceTree = "<group>"; };
AEE304DC2BADC7FE00C12A19 /* code-snippets.app */ = {isa = PBXFileReference; explicitFileType = wrapper.application; includeInIndex = 0; name = "code-snippets.app"; path = "/Users/vladvelicu/Documents/Git/docs-couchbase-lite/modules/objc/examples/build/Debug-iphoneos/code-snippets.app"; sourceTree = "<absolute>"; };
/* End PBXFileReference section */

/* Begin PBXFrameworksBuildPhase section */
BEC78AD7221A9A6500FC586A /* Frameworks */ = {
isa = PBXFrameworksBuildPhase;
buildActionMask = 2147483647;
files = (
1A26D3BA27F76B35007C7FEE /* CouchbaseLite.xcframework in Frameworks */,
AEE304E02BADC80000C12A19 /* CouchbaseLiteVectorSearch.xcframework in Frameworks */,
AEE304DD2BADC7FE00C12A19 /* CouchbaseLite.xcframework in Frameworks */,
);
runOnlyForDeploymentPostprocessing = 0;
};
Expand All @@ -77,14 +67,16 @@
1A508E70273A3887007EABB2 /* SampleCodeTest.m */,
1A508E72273A3887007EABB2 /* ViewController.h */,
1A354CEB273B5996001A20BE /* GettingStarted.h */,
AEE304D12BADC4F000C12A19 /* VectorSeach.m */,
);
path = code_snippets;
sourceTree = "<group>";
};
49095594276D0EA3002A394C /* Frameworks */ = {
isa = PBXGroup;
children = (
1A26D3B827F76B30007C7FEE /* CouchbaseLite.xcframework */,
AEE304D32BADC54200C12A19 /* CouchbaseLite.xcframework */,
AEE304D52BADC55B00C12A19 /* CouchbaseLiteVectorSearch.xcframework */,
);
name = Frameworks;
sourceTree = "<group>";
Expand All @@ -93,19 +85,10 @@
isa = PBXGroup;
children = (
1A508E63273A3887007EABB2 /* code_snippets */,
BEC78ADB221A9A6500FC586A /* Products */,
49095594276D0EA3002A394C /* Frameworks */,
);
sourceTree = "<group>";
};
BEC78ADB221A9A6500FC586A /* Products */ = {
isa = PBXGroup;
children = (
BEC78ADA221A9A6500FC586A /* code-snippets.app */,
);
name = Products;
sourceTree = "<group>";
};
/* End PBXGroup section */

/* Begin PBXNativeTarget section */
Expand All @@ -116,7 +99,6 @@
BEC78AD6221A9A6500FC586A /* Sources */,
BEC78AD7221A9A6500FC586A /* Frameworks */,
BEC78AD8221A9A6500FC586A /* Resources */,
BEC78AF8221A9D7D00FC586A /* Embed Frameworks */,
);
buildRules = (
);
Expand All @@ -126,7 +108,7 @@
packageProductDependencies = (
);
productName = "code-snippets";
productReference = BEC78ADA221A9A6500FC586A /* code-snippets.app */;
productReference = AEE304DC2BADC7FE00C12A19 /* code-snippets.app */;
productType = "com.apple.product-type.application";
};
/* End PBXNativeTarget section */
Expand Down Expand Up @@ -154,7 +136,7 @@
mainGroup = BEC78AD1221A9A6500FC586A;
packageReferences = (
);
productRefGroup = BEC78ADB221A9A6500FC586A /* Products */;
productRefGroup = BEC78AD1221A9A6500FC586A;
projectDirPath = "";
projectRoot = "";
targets = (
Expand Down Expand Up @@ -182,6 +164,7 @@
buildActionMask = 2147483647;
files = (
1A508E7A273A3887007EABB2 /* main.m in Sources */,
AEE304D22BADC4F000C12A19 /* VectorSeach.m in Sources */,
1A508E79273A3887007EABB2 /* GettingStarted.m in Sources */,
1A508E7B273A3887007EABB2 /* AppDelegate.m in Sources */,
1A508E7C273A3887007EABB2 /* SampleCodeTest.m in Sources */,
Expand Down Expand Up @@ -263,7 +246,7 @@
GCC_WARN_UNINITIALIZED_AUTOS = YES_AGGRESSIVE;
GCC_WARN_UNUSED_FUNCTION = YES;
GCC_WARN_UNUSED_VARIABLE = YES;
IPHONEOS_DEPLOYMENT_TARGET = 12.1;
IPHONEOS_DEPLOYMENT_TARGET = 13.0;
MTL_ENABLE_DEBUG_INFO = INCLUDE_SOURCE;
MTL_FAST_MATH = YES;
ONLY_ACTIVE_ARCH = YES;
Expand Down Expand Up @@ -317,7 +300,7 @@
GCC_WARN_UNINITIALIZED_AUTOS = YES_AGGRESSIVE;
GCC_WARN_UNUSED_FUNCTION = YES;
GCC_WARN_UNUSED_VARIABLE = YES;
IPHONEOS_DEPLOYMENT_TARGET = 12.1;
IPHONEOS_DEPLOYMENT_TARGET = 13.0;
MTL_ENABLE_DEBUG_INFO = NO;
MTL_FAST_MATH = YES;
SDKROOT = iphoneos;
Expand All @@ -330,7 +313,7 @@
buildSettings = {
ASSETCATALOG_COMPILER_APPICON_NAME = AppIcon;
CODE_SIGN_STYLE = Automatic;
DEVELOPMENT_TEAM = 863R969N56;
DEVELOPMENT_TEAM = N2Q372V7W2;
FRAMEWORK_SEARCH_PATHS = (
"$(inherited)",
"$(PROJECT_DIR)/Frameworks",
Expand All @@ -351,7 +334,7 @@
buildSettings = {
ASSETCATALOG_COMPILER_APPICON_NAME = AppIcon;
CODE_SIGN_STYLE = Automatic;
DEVELOPMENT_TEAM = 863R969N56;
DEVELOPMENT_TEAM = N2Q372V7W2;
FRAMEWORK_SEARCH_PATHS = (
"$(inherited)",
"$(PROJECT_DIR)/Frameworks",
Expand Down
142 changes: 142 additions & 0 deletions modules/objc/examples/code_snippets/VectorSeach.m
Original file line number Diff line number Diff line change
@@ -0,0 +1,142 @@
//
// VectorSeach.m
// CouchbaseLite
//
// Copyright © 2024 couchbase. All rights reserved.
//


#import <Foundation/Foundation.h>
#import <CouchbaseLite/CouchbaseLite.h>
#import <NaturalLanguage/NLEmbedding.h>

CBLDatabase* database;
CBLCollection* collection;
NLEmbedding* model;
// placeholder
NSArray* vectorArray;

@interface VectorSearchSnippets : NSObject

@end

@implementation VectorSearchSnippets

// MARK: Configuring a project to use Vector Search.

/*/
Download Couchbase Lite iOS Obj-C and Couchbase Lite Vector Search libraries
Extract the xcframeworks and copy them into the App Project Directory

Add both libraries to the *Frameworks, Libraries and Embedded Content* of your desired target
*/

- (void) createDefaultIndex {

velicuvlad marked this conversation as resolved.
Show resolved Hide resolved
// MARK: Create a default Vector Index Configuration
CBLVectorIndexConfiguration* config = [[CBLVectorIndexConfiguration alloc] initWithExpression: @"vector" dimensions: 300 centroids: 8];

// MARK: Set custom settings
config.encoding = [CBLVectorEncoding none];
config.metric = kCBLDistanceMetricCosine;
config.minTrainingSize = 50;
config.maxTrainingSize = 300;
}

- (CBLQueryResultSet*) vectorIndexEmbedding {

velicuvlad marked this conversation as resolved.
Show resolved Hide resolved
// MARK: Create Vector Index with Embedding
CBLVectorIndexConfiguration* config = [[CBLVectorIndexConfiguration alloc] initWithExpression: @"vector" dimensions: 300 centroids: 8];

NSError* error;
[collection createIndexWithName: @"vector_index" config: config error: &error];

NSArray<NSNumber*>* vectorArray = [model vectorForString: @"word"];

NSString* sql = @"select meta().id, word from _default.words where vector_match(vector_index, $vector, 20)";
CBLQuery* query = [database createQuery: sql error: &error];

CBLQueryParameters* parameters = [[CBLQueryParameters alloc] init];
[parameters setValue: vectorArray forName: @"vector"];
[query setParameters: parameters];

return [query execute: &error];
}

- (CBLQueryResultSet*) useVectorMatch {

velicuvlad marked this conversation as resolved.
Show resolved Hide resolved
// MARK: Use vector_match
CBLVectorIndexConfiguration* config = [[CBLVectorIndexConfiguration alloc] initWithExpression: @"vector" dimensions: 300 centroids: 8];

NSError* error;
[collection createIndexWithName: @"vector_index" config: config error: &error];

NSString* sql = @"select meta().id, word from _default.words where vector_match(vector_index, $vector, 20)";
CBLQuery* query = [database createQuery: sql error: &error];

CBLQueryParameters* parameters = [[CBLQueryParameters alloc] init];
[parameters setValue: vectorArray forName: @"vector"];
[query setParameters: parameters];

return [query execute: &error];
}

- (CBLQueryResultSet*) useVectorDistance {

velicuvlad marked this conversation as resolved.
Show resolved Hide resolved
// MARK: Use vector_distance
CBLVectorIndexConfiguration* config = [[CBLVectorIndexConfiguration alloc] initWithExpression: @"vector" dimensions: 300 centroids: 8];

NSError* error;
[collection createIndexWithName: @"vector_index" config: config error: &error];

NSString* sql = @"select meta().id, word, vector_distance(vector_index) from _default.words where vector_match(vector_index, $vector, 20)";
CBLQuery* q = [database createQuery: sql error: &error];

CBLQueryParameters* parameters = [[CBLQueryParameters alloc] init];
[parameters setValue: vectorArray forName: @"vector"];
[q setParameters: parameters];

return [q execute: &error];
}

@end


// MARK: Create Vector Index with Predictive Model

@interface WordModel : NSObject <CBLPredictiveModel>
@end

@implementation WordModel


- (CBLDictionary*) predict: (CBLDictionary*)input {
model = [NLEmbedding wordEmbeddingForLanguage: @"english"];

NSString* word = [input stringForKey: @"word"];
if (!word) {
NSLog(@"No word found !!!");
return nil;
}

NSArray* vector = [model vectorForString: @"word"];
CBLMutableDictionary* output = [[CBLMutableDictionary alloc] init];
[output setValue: vector forKey: @"vector"];

return output;
}

- (void) createVectorIndex {
WordModel* model = [[WordModel alloc] init];
[[CBLDatabase prediction] registerModel: model withName: @"WordEmbedding"];

NSString* expression = @"prediction(WordEmbedding,{\"word\": word}).vector";
CBLVectorIndexConfiguration* config = [[CBLVectorIndexConfiguration alloc] initWithExpression: expression dimensions: 300 centroids: 8];

NSError* error;
[collection createIndexWithName: @"vector_pred_index" config: config error: &error];

[[CBLDatabase prediction] unregisterModelWithName: @"WordEmbedding"];
}

@end
Loading