Files
DEMO-AGENT/review_agent/management/commands/regulatory_rag_build.py

34 lines
1.3 KiB
Python
Raw 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.
from __future__ import annotations
from pathlib import Path
from django.conf import settings
from django.core.management.base import BaseCommand, CommandError
from review_agent.regulatory_review.services.rag_embedding import get_embedding_provider
from review_agent.regulatory_review.services.rag_index import build_chroma_index
from review_agent.regulatory_review.services.rule_loader import load_rule_file
class Command(BaseCommand):
help = "构建 NMPA 法规材料本地 ChromaDB RAG 索引。"
def add_arguments(self, parser):
parser.add_argument("--provider", default=None, help="覆盖 REGULATORY_RAG_PROVIDER。")
def handle(self, *args, **options):
rule_set = load_rule_file()
source_dir = Path(settings.BASE_DIR) / rule_set["source_material_dir"]
if not source_dir.exists():
raise CommandError(f"法规材料目录不存在:{source_dir}")
try:
provider = get_embedding_provider(options["provider"])
count = build_chroma_index(source_dir=source_dir, embedding_provider=provider)
except Exception as exc:
raise CommandError(str(exc)) from exc
self.stdout.write(
self.style.SUCCESS(
f"已构建法规 RAG 索引collection={settings.REGULATORY_RAG_COLLECTION}, chunks={count}"
)
)