kx_guo b0b6ef014f 保留RAG检索并修复相似案例调用
- 保留 pgvector 与混合检索能力,返回 TOP 5 相似案例\n- 将相似案例上下文传入 AI 预防建议生成\n- 修复风险评估调用缺少 similar_cases 参数导致的运行报错\n- 保留 Embedding 配置、数据库迁移、回填脚本及 RAG 测试\n- 恢复基础字符串数组格式的 AI 预防建议协议
2026-09-05 19:00:25 +08:00

39 lines
1.1 KiB
Python
Raw Permalink Blame History

This file contains ambiguous Unicode characters

This file contains Unicode characters that might be confused with other characters. If you think that this is intentional, you can safely ignore this warning. Use the Escape button to reveal them.

"""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())