@@ -39,23 +39,30 @@ def to_str_for_embedding(self):
39
39
return f"Name: { self .name } Description: { self .description } Type: { self .type } "
40
40
41
41
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
+ """
44
51
45
52
table_name = Item .__tablename__
46
53
47
54
index_ada002 = Index (
48
- "hnsw_index_for_innerproduct_ {table_name}_embedding_ada002" ,
55
+ "hnsw_index_for_cosine_ {table_name}_embedding_ada002" ,
49
56
Item .embedding_ada002 ,
50
57
postgresql_using = "hnsw" ,
51
58
postgresql_with = {"m" : 16 , "ef_construction" : 64 },
52
- postgresql_ops = {"embedding_ada002" : "vector_ip_ops " },
59
+ postgresql_ops = {"embedding_ada002" : "vector_cosine_ops " },
53
60
)
54
61
55
62
index_nomic = Index (
56
- f"hnsw_index_for_innerproduct_ { table_name } _embedding_nomic" ,
63
+ f"hnsw_index_for_cosine_ { table_name } _embedding_nomic" ,
57
64
Item .embedding_nomic ,
58
65
postgresql_using = "hnsw" ,
59
66
postgresql_with = {"m" : 16 , "ef_construction" : 64 },
60
- postgresql_ops = {"embedding_nomic" : "vector_ip_ops " },
67
+ postgresql_ops = {"embedding_nomic" : "vector_cosine_ops " },
61
68
)
0 commit comments