| """ |
| main.py β Knowledge Agent entry point |
| ====================================== |
| Starts: |
| 1. File watcher (background thread) β auto-indexes new/changed documents |
| 2. FastAPI web server (foreground) β CLI and web UI |
| |
| Run: |
| python main.py # web UI on http://localhost:8000 |
| python main.py --port 8080 # custom port |
| python main.py --no-watcher # disable file watcher |
| python main.py --index-on-start # index all docs before starting |
| """ |
|
|
| import argparse |
| import os |
| import sys |
| import uvicorn |
|
|
| import os as _os |
| from dotenv import load_dotenv |
| load_dotenv(_os.path.join(_os.path.dirname(_os.path.abspath(__file__)), "..", ".env")) |
|
|
| DOCS_DIR = os.getenv("DOCS_DIR", "./documents") |
|
|
|
|
| def main(): |
| parser = argparse.ArgumentParser( |
| description="Knowledge Agent β Personal RAG server" |
| ) |
| parser.add_argument("--host", default="0.0.0.0", |
| help="Bind host (default: 0.0.0.0)") |
| parser.add_argument("--port", type=int, default=8000, |
| help="Port (default: 8000)") |
| parser.add_argument("--no-watcher", action="store_true", |
| help="Disable the file-system watcher") |
| parser.add_argument("--index-on-start", action="store_true", |
| help="Re-index all documents before starting server") |
| parser.add_argument("--reload", action="store_true", |
| help="Enable uvicorn auto-reload (development)") |
| args = parser.parse_args() |
|
|
| |
| if args.index_on_start: |
| print("[Main] Indexing documents on startup β¦") |
| from knowledge_api import index_docs |
| index_docs(docs_dir=DOCS_DIR) |
|
|
| |
| if not args.no_watcher: |
| from watcher import start_watcher_thread |
| wt = start_watcher_thread(docs_dir=DOCS_DIR) |
| print(f"[Main] File watcher started for: {DOCS_DIR}") |
|
|
| |
| print(f"\n[Main] Starting Knowledge Agent on http://{args.host}:{args.port}") |
| print("[Main] Web UI: http://localhost:{port}".format(port=args.port)) |
| print("[Main] API docs: http://localhost:{port}/docs\n".format(port=args.port)) |
|
|
| uvicorn.run( |
| "web_app:app", |
| host=args.host, |
| port=args.port, |
| reload=args.reload, |
| log_level="info", |
| ) |
|
|
|
|
| if __name__ == "__main__": |
| main() |