Skip to content

Commit d19e813

Browse files
authored
Merge pull request #174 from cuappdev/feature/embedding-pipeline
Handle empty/null post embeddings for similar posts and createPost
2 parents dc75067 + 6065a4d commit d19e813

5 files changed

Lines changed: 37 additions & 9 deletions

File tree

src/models/PostModel.ts

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -49,7 +49,7 @@ export class PostModel {
4949
archive: boolean;
5050

5151
@Column("float", { array: true, nullable: true })
52-
embedding: number[];
52+
embedding: number[] | null;
5353

5454
@ManyToOne(() => UserModel, (user) => user.posts, { onDelete: "CASCADE" })
5555
@JoinColumn({ name: "userId" })

src/repositories/PostRepository.ts

Lines changed: 13 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -90,7 +90,7 @@ export class PostRepository extends AbstractRepository<PostModel> {
9090
price: number,
9191
images: string[],
9292
user: UserModel,
93-
embedding: number[],
93+
embedding: number[] | null,
9494
): Promise<PostModel> {
9595
const post = new PostModel();
9696
post.title = title;
@@ -561,12 +561,16 @@ export class PostRepository extends AbstractRepository<PostModel> {
561561
excludeUserId: string,
562562
limit = 20,
563563
): Promise<PostModel[]> {
564+
if (!Array.isArray(queryEmbedding) || queryEmbedding.length === 0) {
565+
return [];
566+
}
564567
const lit = `[${queryEmbedding.join(",")}]`;
565568
return await this.repository
566569
.createQueryBuilder("post")
567570
.leftJoinAndSelect("post.user", "user")
568571
.where("post.id != :excludePostId", { excludePostId })
569572
.andWhere("post.embedding IS NOT NULL")
573+
.andWhere("CARDINALITY(post.embedding) > 0")
570574
.andWhere("post.archive = false")
571575
.andWhere("post.sold = false")
572576
.andWhere("user.firebaseUid != :excludeUserId", { excludeUserId })
@@ -583,11 +587,15 @@ export class PostRepository extends AbstractRepository<PostModel> {
583587
excludeUserId: string,
584588
limit = 10,
585589
): Promise<PostModel[]> {
590+
if (!Array.isArray(embedding) || embedding.length === 0) {
591+
return [];
592+
}
586593
const lit = `[${embedding.join(",")}]`;
587594
return await this.repository
588595
.createQueryBuilder("post")
589596
.leftJoinAndSelect("post.user", "user")
590597
.where("post.embedding IS NOT NULL")
598+
.andWhere("CARDINALITY(post.embedding) > 0")
591599
.andWhere("user.firebaseUid != :excludeUserId", { excludeUserId })
592600
.orderBy(`post.embedding::vector <-> CAST('${lit}' AS vector(512))`)
593601
.limit(limit)
@@ -703,11 +711,15 @@ export class PostRepository extends AbstractRepository<PostModel> {
703711
excludeUserId: string,
704712
limit = 10,
705713
): Promise<PostModel[]> {
714+
if (!Array.isArray(avgEmbedding) || avgEmbedding.length === 0) {
715+
return [];
716+
}
706717
const lit = `[${avgEmbedding.join(",")}]`;
707718
return await this.repository
708719
.createQueryBuilder("post")
709720
.leftJoinAndSelect("post.user", "user")
710721
.where("post.embedding IS NOT NULL")
722+
.andWhere("CARDINALITY(post.embedding) > 0")
711723
.andWhere("post.archive = false")
712724
.andWhere("post.sold = false")
713725
.andWhere("user.firebaseUid != :excludeUserId", { excludeUserId })

src/repositories/RequestRepository.ts

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -128,6 +128,7 @@ export class RequestRepository extends AbstractRepository<RequestModel> {
128128
"distance",
129129
)
130130
.where("request.embedding IS NOT NULL")
131+
.andWhere("CARDINALITY(request.embedding) > 0")
131132
.andWhere("user.firebaseUid != :excludeUserId", { excludeUserId })
132133
// 3. Safely pass the embedding string as a parameter
133134
.setParameter("embedding", embeddingString)

src/services/PostService.ts

Lines changed: 20 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -114,14 +114,26 @@ export class PostService {
114114
console.error("Error computing embedding:", error);
115115
embedding = null;
116116
}
117-
const freshPost = await postRepository.createPost(post.title, post.description, categories, eventTags, post.condition, post.originalPrice, images, user, (embedding ?? []) as number[]);
118-
if (embedding && Array.isArray(embedding) && embedding.length > 0) {
117+
const validEmbedding =
118+
Array.isArray(embedding) && embedding.length > 0 ? embedding : null;
119+
const freshPost = await postRepository.createPost(
120+
post.title,
121+
post.description,
122+
categories,
123+
eventTags,
124+
post.condition,
125+
post.originalPrice,
126+
images,
127+
user,
128+
validEmbedding,
129+
);
130+
if (validEmbedding) {
119131
const requestRepository = Repositories.request(
120132
transactionalEntityManager,
121133
);
122134
// TODO: how many should we get?
123135
const similarRequests = await requestRepository.findSimilarRequests(
124-
embedding,
136+
validEmbedding,
125137
user.firebaseUid,
126138
10,
127139
);
@@ -584,9 +596,12 @@ export class PostService {
584596
const postRepository = Repositories.post(transactionalEntityManager);
585597
const post = await postRepository.getPostById(params.id);
586598
if (!post) throw new NotFoundError("Post not found!");
587-
const embedding = post.embedding;
599+
const embedding =
600+
Array.isArray(post.embedding) && post.embedding.length > 0
601+
? post.embedding
602+
: null;
588603

589-
if (embedding == null) {
604+
if (!embedding) {
590605
let fallback = await postRepository.getSimilarPostsFallback(
591606
post.id,
592607
user.firebaseUid,

src/types/ApiResponses.ts

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -93,7 +93,7 @@ export interface Post {
9393
categories: Category[];
9494
eventTags: EventTag[];
9595
sold: boolean;
96-
embedding: number[];
96+
embedding: number[] | null;
9797
}
9898

9999
export interface GetPostsResponse {
@@ -157,7 +157,7 @@ export interface Request {
157157
title: string;
158158
description: string;
159159
archive: boolean;
160-
embedding: number[];
160+
embedding: number[] | null;
161161
user: PrivateProfile;
162162
matches: PostModel[];
163163
}

0 commit comments

Comments
 (0)