Skip to content

Commit a0e359a

Browse files
authored
[ENG-337] - Create Similarity Ranking Route (#185)
* Add similarity rank API route with error handling and input validation * Enhance input validation in similarity rank API route to ensure embedding and subsetRoamUids are arrays before processing
1 parent 2d836c3 commit a0e359a

1 file changed

Lines changed: 45 additions & 0 deletions

File tree

  • apps/website/app/api/supabase/similarity-rank
Lines changed: 45 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,45 @@
1+
import { NextResponse, NextRequest } from "next/server";
2+
import { createClient } from "~/utils/supabase/server";
3+
import {
4+
createApiResponse,
5+
handleRouteError,
6+
defaultOptionsHandler,
7+
asPostgrestFailure,
8+
} from "~/utils/supabase/apiUtils";
9+
10+
type SimilarityRankInput = {
11+
embedding: number[];
12+
subsetRoamUids: string[];
13+
};
14+
15+
export const POST = async (request: NextRequest): Promise<NextResponse> => {
16+
try {
17+
const body: SimilarityRankInput = await request.json();
18+
const { embedding, subsetRoamUids } = body;
19+
20+
if (
21+
!Array.isArray(embedding) ||
22+
!Array.isArray(subsetRoamUids) ||
23+
!subsetRoamUids.length
24+
) {
25+
return createApiResponse(
26+
request,
27+
asPostgrestFailure(
28+
"Missing required fields: embedding and subsetRoamUids",
29+
"invalid",
30+
),
31+
);
32+
}
33+
const supabase = await createClient();
34+
const response = await supabase.rpc("match_embeddings_for_subset_nodes", {
35+
p_query_embedding: JSON.stringify(embedding),
36+
p_subset_roam_uids: subsetRoamUids,
37+
});
38+
39+
return createApiResponse(request, response);
40+
} catch (e: unknown) {
41+
return handleRouteError(request, e, "/api/supabase/similarity-rank");
42+
}
43+
};
44+
45+
export const OPTIONS = defaultOptionsHandler;

0 commit comments

Comments
 (0)