"""Enable pgvector schema objects with a PostgreSQL administrator account. Run once after the pgvector package is installed on the server: .venv/Scripts/python -m scripts.enable_pgvector Then the normal application account can run ``scripts.backfill_embeddings``. """ from __future__ import annotations import asyncio from app.database.session import SessionFactory from sqlalchemy import text async def main() -> None: async with SessionFactory() as session: await session.execute(text("CREATE EXTENSION IF NOT EXISTS vector")) await session.execute( text( "ALTER TABLE anomaly_cases " "ADD COLUMN IF NOT EXISTS embedding_vector halfvec(3072)" ) ) await session.execute( text( "CREATE INDEX IF NOT EXISTS ix_anomaly_cases_embedding_vector " "ON anomaly_cases USING hnsw (embedding_vector halfvec_cosine_ops)" ) ) await session.commit() print("pgvector 已启用,embedding_vector 和 HNSW 索引已准备完成。") if __name__ == "__main__": asyncio.run(main())