Skip to content

Commit

Permalink
Update operator used in index
Browse files Browse the repository at this point in the history
  • Loading branch information
pamelafox committed Jan 7, 2025
1 parent 3b938d6 commit cae84d9
Showing 1 changed file with 13 additions and 6 deletions.
19 changes: 13 additions & 6 deletions src/backend/fastapi_app/postgres_models.py
Original file line number Diff line number Diff line change
Expand Up @@ -39,23 +39,30 @@ def to_str_for_embedding(self):
return f"Name: {self.name} Description: {self.description} Type: {self.type}"


# Define HNSW index to support vector similarity search
# Use the vector_ip_ops access method (inner product) since these embeddings are normalized
"""
**Define HNSW index to support vector similarity search**
We use the vector_cosine_ops access method (cosine distance)
since it works for both normalized and non-normalized vector embeddings
If you know your embeddings are normalized,
you can switch to inner product for potentially better performance.
The index operator should match the operator used in queries.
"""

table_name = Item.__tablename__

index_ada002 = Index(
"hnsw_index_for_innerproduct_{table_name}_embedding_ada002",
"hnsw_index_for_cosine_{table_name}_embedding_ada002",
Item.embedding_ada002,
postgresql_using="hnsw",
postgresql_with={"m": 16, "ef_construction": 64},
postgresql_ops={"embedding_ada002": "vector_ip_ops"},
postgresql_ops={"embedding_ada002": "vector_cosine_ops"},
)

index_nomic = Index(
f"hnsw_index_for_innerproduct_{table_name}_embedding_nomic",
f"hnsw_index_for_cosine_{table_name}_embedding_nomic",
Item.embedding_nomic,
postgresql_using="hnsw",
postgresql_with={"m": 16, "ef_construction": 64},
postgresql_ops={"embedding_nomic": "vector_ip_ops"},
postgresql_ops={"embedding_nomic": "vector_cosine_ops"},
)

0 comments on commit cae84d9

Please sign in to comment.