Skip to content

Commit 545e5a7

Browse files
committed
Model schema changes
1 parent d1e9d16 commit 545e5a7

File tree

2 files changed

+14
-10
lines changed

2 files changed

+14
-10
lines changed

docs/customize_data.md

+1
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,7 @@
11
# Customizing the data
22

33
This guide shows you how to bring in a table with a different schema than the sample table.
4+
For a full example of code changes needed, check out [this branch](https://github.com/Azure-Samples/rag-postgres-openai-python/compare/main...otherdata).
45

56
## Define the table schema
67

src/backend/fastapi_app/postgres_models.py

+13-10
Original file line numberDiff line numberDiff line change
@@ -1,14 +1,12 @@
11
from __future__ import annotations
22

3-
from dataclasses import asdict
4-
53
from pgvector.sqlalchemy import Vector
64
from sqlalchemy import Index
7-
from sqlalchemy.orm import DeclarativeBase, Mapped, MappedAsDataclass, mapped_column
5+
from sqlalchemy.orm import DeclarativeBase, Mapped, mapped_column
86

97

108
# Define the models
11-
class Base(DeclarativeBase, MappedAsDataclass):
9+
class Base(DeclarativeBase):
1210
pass
1311

1412

@@ -20,11 +18,12 @@ class Item(Base):
2018
name: Mapped[str] = mapped_column()
2119
description: Mapped[str] = mapped_column()
2220
price: Mapped[float] = mapped_column()
23-
embedding_ada002: Mapped[Vector] = mapped_column(Vector(1536)) # ada-002
24-
embedding_nomic: Mapped[Vector] = mapped_column(Vector(768)) # nomic-embed-text
21+
# Embeddings for different models:
22+
embedding_ada002: Mapped[Vector] = mapped_column(Vector(1536), nullable=True) # ada-002
23+
embedding_nomic: Mapped[Vector] = mapped_column(Vector(768), nullable=True) # nomic-embed-text
2524

2625
def to_dict(self, include_embedding: bool = False):
27-
model_dict = asdict(self)
26+
model_dict = {column.name: getattr(self, column.name) for column in self.__table__.columns}
2827
if include_embedding:
2928
model_dict["embedding_ada002"] = model_dict.get("embedding_ada002", [])
3029
model_dict["embedding_nomic"] = model_dict.get("embedding_nomic", [])
@@ -40,17 +39,21 @@ def to_str_for_embedding(self):
4039
return f"Name: {self.name} Description: {self.description} Type: {self.type}"
4140

4241

43-
# Define HNSW index to support vector similarity search through the vector_cosine_ops access method (cosine distance).
42+
# Define HNSW index to support vector similarity search
43+
# Use the vector_ip_ops access method (inner product) since these embeddings are normalized
44+
45+
table_name = Item.__tablename__
46+
4447
index_ada002 = Index(
45-
"hnsw_index_for_innerproduct_item_embedding_ada002",
48+
"hnsw_index_for_innerproduct_{table_name}_embedding_ada002",
4649
Item.embedding_ada002,
4750
postgresql_using="hnsw",
4851
postgresql_with={"m": 16, "ef_construction": 64},
4952
postgresql_ops={"embedding_ada002": "vector_ip_ops"},
5053
)
5154

5255
index_nomic = Index(
53-
"hnsw_index_for_innerproduct_item_embedding_nomic",
56+
f"hnsw_index_for_innerproduct_{table_name}_embedding_nomic",
5457
Item.embedding_nomic,
5558
postgresql_using="hnsw",
5659
postgresql_with={"m": 16, "ef_construction": 64},

0 commit comments

Comments
 (0)