Skip to content

Model schema changes #81

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Merged
merged 1 commit into from
Sep 11, 2024
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
1 change: 1 addition & 0 deletions docs/customize_data.md
Original file line number Diff line number Diff line change
@@ -1,6 +1,7 @@
# Customizing the data

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

## Define the table schema

Expand Down
23 changes: 13 additions & 10 deletions src/backend/fastapi_app/postgres_models.py
Original file line number Diff line number Diff line change
@@ -1,14 +1,12 @@
from __future__ import annotations

from dataclasses import asdict

from pgvector.sqlalchemy import Vector
from sqlalchemy import Index
from sqlalchemy.orm import DeclarativeBase, Mapped, MappedAsDataclass, mapped_column
from sqlalchemy.orm import DeclarativeBase, Mapped, mapped_column


# Define the models
class Base(DeclarativeBase, MappedAsDataclass):
class Base(DeclarativeBase):
pass


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

def to_dict(self, include_embedding: bool = False):
model_dict = asdict(self)
model_dict = {column.name: getattr(self, column.name) for column in self.__table__.columns}
if include_embedding:
model_dict["embedding_ada002"] = model_dict.get("embedding_ada002", [])
model_dict["embedding_nomic"] = model_dict.get("embedding_nomic", [])
Expand All @@ -40,17 +39,21 @@ 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 through the vector_cosine_ops access method (cosine distance).
# Define HNSW index to support vector similarity search
# Use the vector_ip_ops access method (inner product) since these embeddings are normalized

table_name = Item.__tablename__

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

index_nomic = Index(
"hnsw_index_for_innerproduct_item_embedding_nomic",
f"hnsw_index_for_innerproduct_{table_name}_embedding_nomic",
Item.embedding_nomic,
postgresql_using="hnsw",
postgresql_with={"m": 16, "ef_construction": 64},
Expand Down
Loading