From 0fd1352737506e3c46ac34124d3ae8caf95192e4 Mon Sep 17 00:00:00 2001 From: Preston Vasquez Date: Fri, 8 Nov 2024 15:03:58 -0700 Subject: [PATCH] Update search index lookup --- examples/mongovector-vectorstore-example/README.md | 4 ++-- .../mongovector_vectorstore_example.go | 12 ++++++++++++ 2 files changed, 14 insertions(+), 2 deletions(-) diff --git a/examples/mongovector-vectorstore-example/README.md b/examples/mongovector-vectorstore-example/README.md index f94d2d2ff..8ff357cfc 100644 --- a/examples/mongovector-vectorstore-example/README.md +++ b/examples/mongovector-vectorstore-example/README.md @@ -32,6 +32,6 @@ For more information on getting started with MongoDB Atlas, visit the [MongoDB A export MONGODB_URI= export OPENAI_API_KEY= -2. If you want to run this using docker-compose.yml, `MONGODB_URI` should be `localhost:27017`: `docker-compose up -d` +2. If you want to run this using docker-compose.yml, `MONGODB_URI` should be `mongodb://localhost:27017/?directConnection=true`: `docker-compose up -d` -3. Run the program: `go run main.go` +3. Run the program: `go run mongovector_vectorstore_example.go` diff --git a/examples/mongovector-vectorstore-example/mongovector_vectorstore_example.go b/examples/mongovector-vectorstore-example/mongovector_vectorstore_example.go index dbf3af67e..dd3f14198 100644 --- a/examples/mongovector-vectorstore-example/mongovector_vectorstore_example.go +++ b/examples/mongovector-vectorstore-example/mongovector_vectorstore_example.go @@ -61,12 +61,20 @@ func main() { Similarity: similarityAlgorithm, }) + // Create the vectorstore collection + err = client.Database(databaseName).CreateCollection(context.Background(), collectionName) + if err != nil { + log.Fatalf("failed to create vector store collection: %w", err) + } + _, err = createVectorSearchIndex(context.Background(), coll, indexDP1536, fields...) if err != nil { log.Fatalf("faield to create index: %v", err) } } + return + // Create an embeddings client using the OpenAI API. Requires environment // variable OPENAI_API_KEY to be set. llm, err := openai.New(openai.WithEmbeddingModel(openAIEmbeddingModel)) @@ -235,6 +243,10 @@ func searchIndexExists(ctx context.Context, coll *mongo.Collection, idx string) return false, fmt.Errorf("failed to list search indexes: %w", err) } + if cursor == nil || cursor.Current == nil { + return false, nil + } + name := cursor.Current.Lookup("name").StringValue() queryable := cursor.Current.Lookup("queryable").Boolean()