- Add /api/admin/embeddings/dimension (GET column dim, POST test model dim) - Add /api/admin/embeddings/migrate (alter column, clear, re-index) - Admin form warns on dimension mismatch after save, offers migrate button - Remove hardcoded 1536 from validate endpoint and embedding service - Add validateDimension() utility to EmbeddingService - Fix health route: import prisma correctly, use router instead of missing registry - i18n keys for dimension warning (EN/FR)
67 lines
2.1 KiB
TypeScript
67 lines
2.1 KiB
TypeScript
import { NextResponse } from 'next/server'
|
|
import { prisma } from '@/lib/prisma'
|
|
import { auth } from '@/auth'
|
|
|
|
/**
|
|
* Admin endpoint to validate all pgvector embeddings in the database.
|
|
* Uses native SQL to check for valid vector format.
|
|
*/
|
|
export async function GET() {
|
|
try {
|
|
const session = await auth()
|
|
if (!session?.user?.id) {
|
|
return NextResponse.json({ error: 'Unauthorized' }, { status: 401 })
|
|
}
|
|
|
|
const user = await prisma.user.findUnique({
|
|
where: { id: session.user.id },
|
|
select: { role: true }
|
|
})
|
|
|
|
if (!user || user.role !== 'ADMIN') {
|
|
return NextResponse.json({ error: 'Forbidden - Admin only' }, { status: 403 })
|
|
}
|
|
|
|
const totalResult: Array<{ total: bigint }> = await prisma.$queryRawUnsafe(
|
|
`SELECT COUNT(*)::bigint as total FROM "Note" WHERE "trashedAt" IS NULL`
|
|
)
|
|
const total = Number(totalResult[0]?.total ?? 0)
|
|
|
|
const withEmbedding: Array<{ count: bigint }> = await prisma.$queryRawUnsafe(
|
|
`SELECT COUNT(*)::bigint as count FROM "NoteEmbedding"`
|
|
)
|
|
const validCount = Number(withEmbedding[0]?.count ?? 0)
|
|
|
|
const dimResult: Array<{ dim: number | null }> = await prisma.$queryRawUnsafe(
|
|
`SELECT a.atttypmod AS dim FROM pg_attribute a JOIN pg_class c ON a.attrelid = c.oid WHERE c.relname = 'NoteEmbedding' AND a.attname = 'embedding'`
|
|
)
|
|
const dbDim = dimResult[0]?.dim ?? 1536
|
|
|
|
const invalidResult: Array<{ count: bigint }> = await prisma.$queryRawUnsafe(
|
|
`SELECT COUNT(*)::bigint as count FROM "NoteEmbedding" e
|
|
WHERE e."embedding" IS NULL`
|
|
)
|
|
const invalidCount = Number(invalidResult[0]?.count ?? 0)
|
|
|
|
const missingCount = total - validCount
|
|
|
|
return NextResponse.json({
|
|
success: true,
|
|
dbDimension: dbDim,
|
|
summary: {
|
|
total,
|
|
valid: validCount - invalidCount,
|
|
missing: missingCount > 0 ? missingCount : 0,
|
|
invalid: invalidCount
|
|
},
|
|
invalidNotes: []
|
|
})
|
|
} catch (error) {
|
|
console.error('[EMBEDDING_VALIDATION] Error:', error)
|
|
return NextResponse.json(
|
|
{ success: false, error: String(error) },
|
|
{ status: 500 }
|
|
)
|
|
}
|
|
}
|