@@ -39,23 +39,30 @@ def to_str_for_embedding(self):
3939 return f"Name: { self .name } Description: { self .description } Type: { self .type } "
4040
4141
42- # Define HNSW index to support vector similarity search
43- # Use the vector_ip_ops access method (inner product) since these embeddings are normalized
42+ """
43+ **Define HNSW index to support vector similarity search**
44+
45+ We use the vector_cosine_ops access method (cosine distance)
46+ since it works for both normalized and non-normalized vector embeddings
47+ If you know your embeddings are normalized,
48+ you can switch to inner product for potentially better performance.
49+ The index operator should match the operator used in queries.
50+ """
4451
4552table_name = Item .__tablename__
4653
4754index_ada002 = Index (
48- "hnsw_index_for_innerproduct_ {table_name}_embedding_ada002" ,
55+ "hnsw_index_for_cosine_ {table_name}_embedding_ada002" ,
4956 Item .embedding_ada002 ,
5057 postgresql_using = "hnsw" ,
5158 postgresql_with = {"m" : 16 , "ef_construction" : 64 },
52- postgresql_ops = {"embedding_ada002" : "vector_ip_ops " },
59+ postgresql_ops = {"embedding_ada002" : "vector_cosine_ops " },
5360)
5461
5562index_nomic = Index (
56- f"hnsw_index_for_innerproduct_ { table_name } _embedding_nomic" ,
63+ f"hnsw_index_for_cosine_ { table_name } _embedding_nomic" ,
5764 Item .embedding_nomic ,
5865 postgresql_using = "hnsw" ,
5966 postgresql_with = {"m" : 16 , "ef_construction" : 64 },
60- postgresql_ops = {"embedding_nomic" : "vector_ip_ops " },
67+ postgresql_ops = {"embedding_nomic" : "vector_cosine_ops " },
6168)
0 commit comments