This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| WITH index_fields AS ( | |
| -- Parse each field from the index definitions | |
| SELECT | |
| vectorindexes__name as index_name, | |
| path as field_path, | |
| numdimensions, | |
| type, | |
| CASE | |
| WHEN numdimensions IS NOT NULL THEN 'vector' | |
| ELSE 'filter' |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| import math | |
| from pymongo import MongoClient, UpdateOne | |
| from bson.binary import Binary | |
| def normalize(vector): | |
| """Normalize a vector to unit length.""" | |
| magnitude = math.sqrt(sum(x**2 for x in vector)) | |
| return [x / magnitude for x in vector] if magnitude > 0 else vector | |
| def normalize_vectors(vectors=None, db_uri="mongodb://localhost:27017", db_name="vectors", collection_name="data", batch_size=1000): |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| `CREATE OR REPLACE FUNCTION two_stage_vector_search_optimized_prefiltered( | |
| query_embedding vector, | |
| match_limit int, | |
| num_candidates int, | |
| filter_category text DEFAULT NULL::text, | |
| max_price double precision DEFAULT NULL::double precision | |
| ) | |
| RETURNS TABLE ( | |
| id int8, |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| db.createView( | |
| "512_embeddings", | |
| "wikipedia-22-12-en 2", | |
| [ | |
| { | |
| $addFields: { | |
| "512_embedding": { $slice: ["$embedding", 512] } | |
| } | |
| } | |
| ] |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| import pymongo | |
| import json | |
| from bson.binary import Binary, BinaryVectorDtype | |
| connection_str = "" | |
| client = pymongo.MongoClient(connection_str) # mongodb cluster URI | |
| db = client['bsontestdb'] | |
| coll = db['embedded_mflix_bson'] |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| import os | |
| import openai | |
| embedding = openai.Embedding.create(input="test query", model="text-embedding-ada-002").data[0].embedding | |
| pipeline = [ | |
| { | |
| "$vectorSearch": { | |
| "index":'bson_vector_index', | |
| "path": "plot_embedding", | |
| "queryVector": embedding, |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| # deps | |
| # %pip install pymongo openai numpy | |
| import numpy as np | |
| import os | |
| os.environ["OPENAI_API_KEY"] = '<my-openai-key>' | |
| import pymongo | |
| import openai |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| "knnBeta": { | |
| "filter": { | |
| "compound": { | |
| "must": [ | |
| { | |
| "queryString": { | |
| "query": "TEST_NAME_1 AND TEST_NAME_2~2 NOT TEST_NAME_3", | |
| "defaultPath": "descriptions" | |
| } | |
| }, |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| searchQ.filter = filter; | |
| } | |
| else if(beds || rooms) { | |
| let filter = { "$and" : []} | |
| if (beds) { | |
| filter.$and.push({"beds" : {"$gte" : parseInt(beds) }}) | |
| } | |
| if (rooms) | |
| { |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| {$vectorSearch:{ | |
| "index": "default", | |
| "queryVector": embedding, | |
| "path": "doc_embedding", | |
| "k": 100, | |
| "numCandidates": 1000 | |
| } | |
| } |
NewerOlder