build out morpheus-docs MCP stack, mirroring hvm-docs through Phases 1-13
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>
This commit is contained in:
@@ -0,0 +1,4 @@
|
||||
{"query": "what's the per-socket licensing model for Morpheus Enterprise", "expected": [{"bundle_id": "morpheus_quickspecs", "page_id": "a50009231enw"}], "tags": ["licensing", "skus"]}
|
||||
{"query": "add an AWS cloud integration", "expected": [], "tags": ["cloud", "TODO-populate-after-first-scrape"]}
|
||||
{"query": "Plugin API version compatibility", "expected": [], "tags": ["api", "TODO"]}
|
||||
{"query": "Morpheus Enterprise 8.1.2 what's new", "expected": [{"bundle_id": "morpheus_release_notes_8_1_2", "page_id": "sd00007733en_us"}], "tags": ["release-notes"]}
|
||||
+118
-28
@@ -10,7 +10,7 @@ to one entry; the highest-ranked chunk's position wins).
|
||||
"""
|
||||
from __future__ import annotations
|
||||
|
||||
from typing import Protocol, Iterable
|
||||
from typing import Iterable, Protocol
|
||||
|
||||
|
||||
class Retriever(Protocol):
|
||||
@@ -21,12 +21,17 @@ class Retriever(Protocol):
|
||||
...
|
||||
|
||||
|
||||
def _collapse_to_pages(chunk_ids: Iterable[tuple[str, str, str]], k: int) -> list[tuple[str, str]]:
|
||||
"""Take a stream of (bundle_id, page_id, chunk_ordinal) and return
|
||||
the first k unique pages in their first-seen order."""
|
||||
def _split_chunk_id(chunk_id: str) -> tuple[str, str, int]:
|
||||
"""`bundle::page::ordinal` -> (bundle, page, int(ordinal))."""
|
||||
bid, pid, ordinal = chunk_id.split("::")
|
||||
return bid, pid, int(ordinal)
|
||||
|
||||
|
||||
def _collapse_to_pages(chunk_ids: Iterable[str], k: int) -> list[tuple[str, str]]:
|
||||
seen: set[tuple[str, str]] = set()
|
||||
out: list[tuple[str, str]] = []
|
||||
for bid, pid, _ord in chunk_ids:
|
||||
for cid in chunk_ids:
|
||||
bid, pid, _ord = _split_chunk_id(cid)
|
||||
key = (bid, pid)
|
||||
if key in seen:
|
||||
continue
|
||||
@@ -37,26 +42,111 @@ def _collapse_to_pages(chunk_ids: Iterable[tuple[str, str, str]], k: int) -> lis
|
||||
return out
|
||||
|
||||
|
||||
# TODO Phase 2/3 — implement these once Chroma + the bm25 module are
|
||||
# in place. Each one is small (15-30 LOC). The eval harness imports
|
||||
# from this module by class name.
|
||||
#
|
||||
# class DenseRetriever:
|
||||
# name = "dense"
|
||||
# def __init__(self, collection): self.col = collection
|
||||
# def retrieve(self, query, k=10): ...
|
||||
#
|
||||
# class RerankedRetriever:
|
||||
# name = "dense+rerank"
|
||||
# def __init__(self, collection, rerank_url, pool=200): ...
|
||||
# def retrieve(self, query, k=10): ...
|
||||
#
|
||||
# class BM25Retriever:
|
||||
# name = "bm25"
|
||||
# def __init__(self, bm25_index): ...
|
||||
# def retrieve(self, query, k=10): ...
|
||||
#
|
||||
# class HybridRetriever:
|
||||
# name = "bm25+dense+rrf"
|
||||
# def __init__(self, dense, bm25, k_rrf=60): ...
|
||||
# def retrieve(self, query, k=10): ...
|
||||
class DenseRetriever:
|
||||
"""Chroma cosine search via the live embedding function."""
|
||||
name = "dense"
|
||||
|
||||
def __init__(self, collection, pool: int = 50):
|
||||
self.col = collection
|
||||
self.pool = pool
|
||||
|
||||
def retrieve(self, query: str, k: int = 10) -> list[tuple[str, str]]:
|
||||
res = self.col.query(query_texts=[query], n_results=self.pool)
|
||||
ids = (res.get("ids") or [[]])[0]
|
||||
return _collapse_to_pages(ids, k)
|
||||
|
||||
|
||||
class BM25Retriever:
|
||||
"""SQLite FTS5 lexical search."""
|
||||
name = "bm25"
|
||||
|
||||
def __init__(self, bm25_index, pool: int = 200):
|
||||
self.bm = bm25_index
|
||||
self.pool = pool
|
||||
|
||||
def retrieve(self, query: str, k: int = 10) -> list[tuple[str, str]]:
|
||||
hits = self.bm.query(query, n=self.pool)
|
||||
return _collapse_to_pages((cid for cid, _score in hits), k)
|
||||
|
||||
|
||||
class HybridRetriever:
|
||||
"""Reciprocal Rank Fusion of dense + BM25 rankings."""
|
||||
name = "hybrid_rrf"
|
||||
|
||||
def __init__(self, dense: DenseRetriever, bm25: BM25Retriever, k_rrf: int = 60, pool: int = 100):
|
||||
self.dense = dense
|
||||
self.bm25 = bm25
|
||||
self.k_rrf = k_rrf
|
||||
self.pool = pool
|
||||
|
||||
def retrieve(self, query: str, k: int = 10) -> list[tuple[str, str]]:
|
||||
dense_pages = self.dense.retrieve(query, k=self.pool)
|
||||
bm25_pages = self.bm25.retrieve(query, k=self.pool)
|
||||
scores: dict[tuple[str, str], float] = {}
|
||||
for rank, page in enumerate(dense_pages, start=1):
|
||||
scores[page] = scores.get(page, 0.0) + 1.0 / (self.k_rrf + rank)
|
||||
for rank, page in enumerate(bm25_pages, start=1):
|
||||
scores[page] = scores.get(page, 0.0) + 1.0 / (self.k_rrf + rank)
|
||||
ranked = sorted(scores.items(), key=lambda kv: -kv[1])
|
||||
return [page for page, _s in ranked[:k]]
|
||||
|
||||
|
||||
def _rerank_pool(rerank_url: str, query: str, ids_and_texts: list[tuple[str, str]],
|
||||
timeout: float = 30.0) -> list[str] | None:
|
||||
"""POST to /v1/rerank, return ids in reranked order. None on failure."""
|
||||
if not ids_and_texts:
|
||||
return []
|
||||
import httpx
|
||||
try:
|
||||
with httpx.Client(timeout=timeout) as c:
|
||||
r = c.post(f"{rerank_url}/v1/rerank", json={
|
||||
"query": query,
|
||||
"documents": [(t or "")[:2000] for _i, t in ids_and_texts],
|
||||
"top_n": len(ids_and_texts),
|
||||
})
|
||||
r.raise_for_status()
|
||||
results = r.json().get("results") or []
|
||||
return [ids_and_texts[item["index"]][0] for item in results
|
||||
if isinstance(item.get("index"), int)
|
||||
and 0 <= item["index"] < len(ids_and_texts)]
|
||||
except Exception:
|
||||
return None
|
||||
|
||||
|
||||
class RerankedRetriever:
|
||||
"""Pull a candidate pool via a base retriever, then cross-encoder re-rank."""
|
||||
|
||||
def __init__(self, base: Retriever, collection, rerank_url: str, name_suffix: str = "rerank",
|
||||
pool: int = 50, timeout: float = 30.0):
|
||||
self.base = base
|
||||
self.col = collection
|
||||
self.url = rerank_url
|
||||
self.name = f"{base.name}+{name_suffix}"
|
||||
self.pool = pool
|
||||
self.timeout = timeout
|
||||
|
||||
def retrieve(self, query: str, k: int = 10) -> list[tuple[str, str]]:
|
||||
# Base returns deduplicated page-level tuples; rerank needs CHUNK-level
|
||||
# texts to be informative. Pull each page's chunk 0 text from Chroma.
|
||||
pages = self.base.retrieve(query, k=self.pool)
|
||||
if not pages:
|
||||
return []
|
||||
chunk_ids = [f"{bid}::{pid}::0" for bid, pid in pages]
|
||||
g = self.col.get(ids=chunk_ids, include=["documents"])
|
||||
by_id = dict(zip(g["ids"], g["documents"]))
|
||||
ids_and_texts = [(cid, by_id.get(cid, "")) for cid in chunk_ids]
|
||||
order = _rerank_pool(self.url, query, ids_and_texts, timeout=self.timeout)
|
||||
if order is None:
|
||||
return pages[:k]
|
||||
out: list[tuple[str, str]] = []
|
||||
seen: set[tuple[str, str]] = set()
|
||||
for cid in order:
|
||||
bid, pid, _ = cid.split("::")
|
||||
key = (bid, pid)
|
||||
if key in seen:
|
||||
continue
|
||||
seen.add(key)
|
||||
out.append(key)
|
||||
if len(out) >= k:
|
||||
break
|
||||
return out
|
||||
|
||||
+81
-9
@@ -76,15 +76,87 @@ def main() -> int:
|
||||
queries = load_queries(args.queries)
|
||||
print(f"loaded {len(queries)} queries")
|
||||
|
||||
# TODO Phase 7: instantiate the retrievers you implemented in
|
||||
# eval/retrievers.py and run each one against each query.
|
||||
# Aggregate MRR / Recall@K / nDCG@K per retriever. Emit a
|
||||
# markdown table to args.output. Commit the file alongside the
|
||||
# PR that changes retrieval.
|
||||
raise NotImplementedError(
|
||||
"Wire up the retrievers in eval/retrievers.py first, then "
|
||||
"fill in this evaluation loop. See PLAN.md Phase 7."
|
||||
)
|
||||
import os
|
||||
import chromadb
|
||||
from chromadb.config import Settings
|
||||
from rag.embeddings import embedding_function
|
||||
from rag.bm25 import BM25Index
|
||||
from eval.retrievers import DenseRetriever, BM25Retriever, HybridRetriever
|
||||
|
||||
product = os.environ.get("PRODUCT_NAME", "hvm")
|
||||
repo_root = Path(__file__).resolve().parent.parent
|
||||
client = chromadb.PersistentClient(path=str(repo_root / "chroma"),
|
||||
settings=Settings(anonymized_telemetry=False))
|
||||
col = client.get_collection(f"{product}_docs", embedding_function=embedding_function())
|
||||
bm = BM25Index(str(repo_root / "bm25" / f"{product}_docs.db"))
|
||||
|
||||
from eval.retrievers import RerankedRetriever
|
||||
|
||||
dense = DenseRetriever(col)
|
||||
bm25 = BM25Retriever(bm)
|
||||
hybrid = HybridRetriever(DenseRetriever(col, pool=100), BM25Retriever(bm, pool=100))
|
||||
|
||||
retrievers = [dense, bm25, hybrid]
|
||||
|
||||
rerank_url = os.environ.get("RERANK_URL", "").rstrip("/")
|
||||
if rerank_url:
|
||||
retrievers += [
|
||||
RerankedRetriever(bm25, col, rerank_url, name_suffix="rerank", pool=50),
|
||||
RerankedRetriever(hybrid, col, rerank_url, name_suffix="rerank", pool=50),
|
||||
]
|
||||
print(f"reranker enabled: {rerank_url}")
|
||||
|
||||
rows: dict[str, dict[str, float]] = {}
|
||||
per_query: list[dict] = []
|
||||
for r in retrievers:
|
||||
mrr_sum = recall_sum = ndcg_sum = 0.0
|
||||
elapsed_sum = 0.0
|
||||
for q in queries:
|
||||
expected = [(e["bundle_id"], e["page_id"]) for e in q["expected"]]
|
||||
t0 = time.time()
|
||||
retrieved = r.retrieve(q["query"], k=max(args.k, 10))
|
||||
elapsed = time.time() - t0
|
||||
mrr = reciprocal_rank(retrieved, expected)
|
||||
recall = recall_at_k(retrieved, expected, args.k)
|
||||
ndcg = ndcg_at_k(retrieved, expected, args.k)
|
||||
mrr_sum += mrr
|
||||
recall_sum += recall
|
||||
ndcg_sum += ndcg
|
||||
elapsed_sum += elapsed
|
||||
per_query.append({
|
||||
"retriever": r.name, "query": q["query"],
|
||||
"mrr": mrr, "recall@k": recall, "ndcg@k": ndcg,
|
||||
"top1": list(retrieved[0]) if retrieved else None,
|
||||
"elapsed_s": round(elapsed, 3),
|
||||
})
|
||||
n = len(queries)
|
||||
rows[r.name] = {
|
||||
"MRR": mrr_sum / n,
|
||||
f"Recall@{args.k}": recall_sum / n,
|
||||
f"nDCG@{args.k}": ndcg_sum / n,
|
||||
"avg_latency_s": elapsed_sum / n,
|
||||
}
|
||||
print(f" {r.name}: MRR={rows[r.name]['MRR']:.3f} "
|
||||
f"Recall@{args.k}={rows[r.name][f'Recall@{args.k}']:.3f} "
|
||||
f"nDCG@{args.k}={rows[r.name][f'nDCG@{args.k}']:.3f} "
|
||||
f"avg={rows[r.name]['avg_latency_s']*1000:.0f}ms")
|
||||
|
||||
args.output.parent.mkdir(parents=True, exist_ok=True)
|
||||
md = [f"# Retrieval eval — k={args.k}", "",
|
||||
f"_{len(queries)} hand-curated queries, generated {time.strftime('%Y-%m-%d %H:%M:%S')}_", "",
|
||||
"| Retriever | MRR | Recall@{k} | nDCG@{k} | avg latency |".replace("{k}", str(args.k)),
|
||||
"| --- | ---: | ---: | ---: | ---: |"]
|
||||
for name, m in rows.items():
|
||||
md.append(f"| `{name}` | {m['MRR']:.3f} | {m[f'Recall@{args.k}']:.3f} "
|
||||
f"| {m[f'nDCG@{args.k}']:.3f} | {m['avg_latency_s']*1000:.0f}ms |")
|
||||
md += ["", "## Per-query results", "",
|
||||
"| Retriever | Query | MRR | top-1 |", "| --- | --- | ---: | --- |"]
|
||||
for r in per_query:
|
||||
top1 = f"`{r['top1'][0]}/{r['top1'][1][:24]}...`" if r["top1"] else "—"
|
||||
md.append(f"| `{r['retriever']}` | {r['query'][:60]} | {r['mrr']:.3f} | {top1} |")
|
||||
args.output.write_text("\n".join(md) + "\n")
|
||||
print(f"wrote {args.output}")
|
||||
return 0
|
||||
|
||||
|
||||
if __name__ == "__main__":
|
||||
|
||||
Reference in New Issue
Block a user