fa448f94e1
Initial scaffold: the docs-mcp-template clone with all the
HVM-validated stack ported across, customized for Morpheus
Enterprise (PRODUCT_NAME=morpheus, server name morpheus-docs).
Bundles (live-discovered 2026-05-22; 1710 cataloged pages total):
* morpheus_user_manual_8_1_0 sd00007510en_us 568 pages (Feb 2026)
* morpheus_user_manual_8_1_1 sd00007621en_us 569 pages (Mar 2026)
* morpheus_user_manual_8_1_2 sd00007732en_us 569 pages (Apr 2026)
* morpheus_release_notes_8_1_0 sd00007496en_us single-doc
* morpheus_release_notes_8_1_1 sd00007610en_us single-doc
* morpheus_release_notes_8_1_2 sd00007733en_us single-doc
* morpheus_quickspecs a50009231enw html-file (live
curl_cffi against www.hpe.com; all 12+ Enterprise SKUs captured —
S6E64..S6E73AAE for new/renewal/upgrade × 1/3/5-yr terms, plus
services SKUs HA124A1#V38/V39 and H46SBA1).
No Deployment Guide or Qualification Matrix on HPE Support for
Morpheus Enterprise specifically — the only QM (sd00006551en_us)
covers HVM clusters managed by Morpheus and lives in hvm-docs.
Stack carried forward from hvm-docs:
* rag/{index,chunk,embeddings,bm25}.py — including the
MAX_CHARS=4000 chunk-cap fix for table-dense content
* docs_mcp/{server,usage}.py — 11 MCP tools, BM25-default search,
cross-encoder rerank, hybrid behind HYBRID_SEARCH=true,
morpheus_api_lessons (renamed from hvm_api_lessons), env-gated
submit_doc_bug
* docs_mcp/api_lessons.md — Morpheus-specific scaffold covering
licensing model, HVM elevation path, REST vs Plugin API, with
TODO markers for sections to flesh out from real ops experience
* scrape/{runner,quickspecs,changelog,bundles}.py — TOC + single-doc
+ html-file modes, curl_cffi Chrome120 for www.hpe.com edge bypass
* eval/{retrievers,run_eval}.py + queries.jsonl scaffold (4 placeholder
queries; populate after first scrape)
* scripts/{rerank_server,usage_report,registry_gc}.py
* .gitea/workflows/{refresh,image-only}.yml — same Gitea Actions
setup zerto-docs uses (push LAN, pull public-URL, GPU Ollama pool)
* deploy/docker-compose.yml — morpheus-docs-mcp service definition,
shared jina-rerank sidecar, Watchtower-labeled
* Dockerfile, requirements.txt, requirements-rerank.txt
Verified locally: scrape produced 1599 .md pages (some TOC entries
are parent-only and yield no body), 6353 chunks all under the 4 KB
cap, MCP server boots and lists 11 tools cleanly.
Co-Authored-By: Claude Opus 4.7 (1M context) <noreply@anthropic.com>
135 lines
4.5 KiB
Python
135 lines
4.5 KiB
Python
"""Build Chroma (and optionally BM25) indexes from corpus on disk.
|
|
|
|
Reads `corpus/<bundle>/<page>.{md,json}`, chunks each page, upserts
|
|
into Chroma. With --rebuild, drops + recreates the collection (clean
|
|
state). With --bm25-only, skips Chroma and rebuilds only the FTS5
|
|
index — useful for fast iteration when chunking didn't change.
|
|
"""
|
|
from __future__ import annotations
|
|
|
|
import argparse
|
|
import json
|
|
import logging
|
|
import time
|
|
from pathlib import Path
|
|
from typing import Iterator
|
|
|
|
import chromadb
|
|
from chromadb.config import Settings
|
|
|
|
from .chunk import chunks_from_page
|
|
from .embeddings import embedding_function
|
|
|
|
log = logging.getLogger(__name__)
|
|
logging.basicConfig(level=logging.INFO, format="%(asctime)s %(message)s")
|
|
|
|
ROOT = Path(__file__).resolve().parent.parent
|
|
CORPUS = ROOT / "corpus"
|
|
CHROMA_DIR = ROOT / "chroma"
|
|
|
|
# Collection name — convention: <product>_docs. Override via env if needed.
|
|
import os
|
|
PRODUCT_NAME = os.environ.get("PRODUCT_NAME", "morpheus")
|
|
COLLECTION = f"{PRODUCT_NAME}_docs"
|
|
|
|
|
|
def page_records() -> Iterator[dict]:
|
|
"""Walk corpus/, yield chunks for every page."""
|
|
if not CORPUS.exists():
|
|
log.error("corpus/ doesn't exist; run the scraper first")
|
|
return
|
|
for bundle_dir in sorted(CORPUS.iterdir()):
|
|
if not bundle_dir.is_dir() or bundle_dir.name.startswith("."):
|
|
continue
|
|
for md_path in sorted(bundle_dir.glob("*.md")):
|
|
page_id = md_path.stem
|
|
sidecar = md_path.with_suffix(".json")
|
|
if not sidecar.exists():
|
|
log.warning("skipping %s — no JSON sidecar", md_path)
|
|
continue
|
|
md = md_path.read_text()
|
|
meta = json.loads(sidecar.read_text())
|
|
# Surface common filter fields at the chunk-metadata level
|
|
# so Chroma's `where` filter can use them.
|
|
base_meta = {
|
|
"bundle_id": bundle_dir.name,
|
|
"page_id": page_id,
|
|
"title": meta.get("title") or "",
|
|
"version": meta.get("version") or "",
|
|
"platform": meta.get("platform") or "",
|
|
"product": meta.get("product") or "",
|
|
}
|
|
yield from chunks_from_page(md, page_id, base_meta)
|
|
|
|
|
|
def upsert_to_chroma(records: list[dict]) -> int:
|
|
client = chromadb.PersistentClient(
|
|
path=str(CHROMA_DIR),
|
|
settings=Settings(anonymized_telemetry=False),
|
|
)
|
|
# Drop + recreate for --rebuild semantics
|
|
try:
|
|
client.delete_collection(COLLECTION)
|
|
except Exception:
|
|
pass
|
|
col = client.create_collection(COLLECTION, embedding_function=embedding_function())
|
|
|
|
BATCH = 64
|
|
total = 0
|
|
for i in range(0, len(records), BATCH):
|
|
chunk = records[i:i + BATCH]
|
|
col.upsert(
|
|
ids=[r["id"] for r in chunk],
|
|
documents=[r["text"] for r in chunk],
|
|
metadatas=[r["metadata"] for r in chunk],
|
|
)
|
|
total += len(chunk)
|
|
log.info("upserted %d / %d chunks", total, len(records))
|
|
return total
|
|
|
|
|
|
def main() -> int:
|
|
p = argparse.ArgumentParser()
|
|
p.add_argument("--rebuild", action="store_true",
|
|
help="Drop and recreate the Chroma collection.")
|
|
p.add_argument("--bm25-only", action="store_true",
|
|
help="Rebuild only the BM25 index, skip Chroma.")
|
|
p.add_argument("--bm25-db", type=Path,
|
|
default=ROOT / "bm25" / f"{PRODUCT_NAME}_docs.db",
|
|
help="Path to the BM25 sqlite db.")
|
|
args = p.parse_args()
|
|
|
|
log.info("reading corpus from %s", CORPUS)
|
|
t0 = time.time()
|
|
records = list(page_records())
|
|
log.info("loaded %d chunks in %.1fs", len(records), time.time() - t0)
|
|
|
|
if args.bm25_only:
|
|
from .bm25 import BM25Index
|
|
log.info("--bm25-only: building FTS5 only")
|
|
BM25Index(args.bm25_db).build(records)
|
|
return 0
|
|
|
|
if not args.rebuild:
|
|
log.info("no --rebuild; nothing to do. (Use --rebuild to upsert.)")
|
|
return 0
|
|
|
|
t_c = time.time()
|
|
n = upsert_to_chroma(records)
|
|
log.info("chroma: %d chunks in %.1fs", n, time.time() - t_c)
|
|
|
|
# Build BM25 too — see PLAN.md Phase 8. Safe to remove this block
|
|
# for products that don't need hybrid retrieval.
|
|
try:
|
|
from .bm25 import BM25Index
|
|
t_b = time.time()
|
|
BM25Index(args.bm25_db).build(records)
|
|
log.info("bm25 done in %.1fs", time.time() - t_b)
|
|
except ImportError:
|
|
log.info("rag.bm25 not available — skipping BM25 build")
|
|
return 0
|
|
|
|
|
|
if __name__ == "__main__":
|
|
raise SystemExit(main())
|