78 lines
2.8 KiB
Python
78 lines
2.8 KiB
Python
"""One-command startup for the GraphRAG teacher application."""
|
|
from __future__ import annotations
|
|
|
|
import argparse
|
|
import os
|
|
from pathlib import Path
|
|
|
|
from dotenv import load_dotenv
|
|
|
|
ROOT = Path(__file__).resolve().parent
|
|
os.chdir(ROOT)
|
|
load_dotenv(ROOT / ".env")
|
|
|
|
from graph_builder import build_graph_from_documents, load_graph, save_graph, sync_graph_to_neo4j
|
|
from vector_store import ChromaVectorStore
|
|
|
|
|
|
def build_or_load_graph(rebuild: bool):
|
|
graph_path = ROOT / "graph" / "kg.graphml"
|
|
if not rebuild and graph_path.exists():
|
|
graph = load_graph(str(graph_path))
|
|
print(f"[OK] Loaded graph: {graph.number_of_nodes()} nodes, {graph.number_of_edges()} edges")
|
|
return graph
|
|
print("[INFO] Building graph from documents...")
|
|
graph = build_graph_from_documents(str(ROOT / "documents"))
|
|
save_graph(graph, str(graph_path))
|
|
print(f"[OK] Built graph: {graph.number_of_nodes()} nodes, {graph.number_of_edges()} edges")
|
|
return graph
|
|
|
|
|
|
def sync_neo4j(graph, skip: bool) -> None:
|
|
if skip:
|
|
print("[INFO] Neo4j synchronization skipped")
|
|
return
|
|
if not os.getenv("NEO4J_URI") or not os.getenv("NEO4J_PASSWORD"):
|
|
raise RuntimeError("NEO4J_URI and NEO4J_PASSWORD must be configured in .env")
|
|
edge_count = sync_graph_to_neo4j(graph)
|
|
print(f"[OK] Synchronized Neo4j: {edge_count} relationships")
|
|
|
|
|
|
def index_chroma(force: bool, skip: bool) -> None:
|
|
if skip:
|
|
print("[INFO] ChromaDB indexing skipped")
|
|
return
|
|
store = ChromaVectorStore(str(ROOT / "data" / "chroma"))
|
|
if store.collection.count() and not force:
|
|
print(f"[OK] ChromaDB ready: {store.collection.count()} indexed chunks")
|
|
return
|
|
count = store.index_documents(str(ROOT / "documents"))
|
|
print(f"[OK] ChromaDB indexed: {count} chunks")
|
|
|
|
|
|
def main() -> None:
|
|
parser = argparse.ArgumentParser(description="Start the GraphRAG teacher application")
|
|
parser.add_argument("--rebuild", action="store_true", help="Re-extract documents and rebuild the graph")
|
|
parser.add_argument("--reindex", action="store_true", help="Re-index all documents in ChromaDB")
|
|
parser.add_argument("--skip-neo4j", action="store_true", help="Do not synchronize Neo4j")
|
|
parser.add_argument("--skip-chroma", action="store_true", help="Do not initialize or index ChromaDB")
|
|
parser.add_argument("--no-ui", action="store_true", help="Prepare services without launching Gradio")
|
|
args = parser.parse_args()
|
|
|
|
graph = build_or_load_graph(args.rebuild)
|
|
sync_neo4j(graph, args.skip_neo4j)
|
|
index_chroma(args.reindex, args.skip_chroma)
|
|
|
|
if args.no_ui:
|
|
print("[OK] Project services are ready")
|
|
return
|
|
|
|
print("[INFO] Starting Gradio at http://127.0.0.1:7860")
|
|
from gradio_app import create_demo
|
|
|
|
create_demo().launch()
|
|
|
|
|
|
if __name__ == "__main__":
|
|
main()
|