-
Notifications
You must be signed in to change notification settings - Fork 7
Expand file tree
/
Copy pathbuild_copilot_corpus.py
More file actions
221 lines (195 loc) · 7.33 KB
/
Copy pathbuild_copilot_corpus.py
File metadata and controls
221 lines (195 loc) · 7.33 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
"""Build the Copilot widget corpus.
Reads:
- Every page's visible text in docs/**/*.html (strips scripts/styles/tags)
- The TOOLS registry inline-defined in docs/find-a-tool/app.js
- Every sibling repo's README.md (in the parent workspace folder)
- Every email template body in Analytics-Hub/Email Templates/*.txt
Writes:
- docs/copilot/corpus.json
{ "version": iso8601, "docs": [ {id, title, url, source, text}, ... ] }
The widget loads corpus.json once on first open, then runs an in-browser
BM25 search to pick the top-k chunks to ground each answer.
Run before commit:
python build_copilot_corpus.py
"""
from __future__ import annotations
import json
import re
import sys
from datetime import datetime, timezone
from pathlib import Path
ROOT = Path(__file__).parent.resolve() # .../Analytics-Hub
WORKSPACE = ROOT.parent # .../AI Analytics Hub
DOCS = ROOT / "docs"
OUT = DOCS / "copilot" / "corpus.json"
SIBLING_REPOS = [
"AI-in-One-Dashboard",
"AI-Solutions-Intelligence-Dashboard",
"copilot-adoption-sentiment-report",
"CopilotChatAnalytics",
"customizecopilot",
"DecodingSuperUsage",
"GitHubCopilotImpact",
"M365UsageAnalytics",
"pax",
"superuserimpact",
]
SCRIPT_OR_STYLE = re.compile(r"<(script|style)\b[^>]*>.*?</\1>", re.DOTALL | re.IGNORECASE)
TAG = re.compile(r"<[^>]+>")
WS = re.compile(r"\s+")
def strip_html(html: str) -> str:
html = SCRIPT_OR_STYLE.sub(" ", html)
html = TAG.sub(" ", html)
# Decode the handful of entities we actually use
html = (html.replace(" ", " ")
.replace("&", "&")
.replace("<", "<")
.replace(">", ">")
.replace(""", '"')
.replace("'", "'"))
return WS.sub(" ", html).strip()
def title_of(html: str, fallback: str) -> str:
m = re.search(r"<title[^>]*>(.*?)</title>", html, re.DOTALL | re.IGNORECASE)
if m:
return WS.sub(" ", m.group(1)).strip()
h1 = re.search(r"<h1[^>]*>(.*?)</h1>", html, re.DOTALL | re.IGNORECASE)
if h1:
return strip_html(h1.group(1))
return fallback
def page_url(rel: Path) -> str:
parts = list(rel.parts)
if parts[-1] == "index.html":
parts = parts[:-1]
base = "https://microsoft.github.io/Analytics-Hub/"
return base + ("/".join(parts) + "/" if parts else "")
def collect_pages() -> list[dict]:
docs: list[dict] = []
for html_path in sorted(DOCS.rglob("*.html")):
# Skip generated / non-page assets
rel = html_path.relative_to(DOCS)
if rel.parts and rel.parts[0] in {"copilot", "demos"}:
continue
if html_path.name in {"google3493de09080400a0.html"}:
continue
html = html_path.read_text(encoding="utf-8", errors="ignore")
text = strip_html(html)
if len(text) < 80:
continue
docs.append({
"id": f"page::{rel.as_posix()}",
"title": title_of(html, rel.as_posix()),
"url": page_url(rel),
"source": "Site page",
"text": text[:12000],
})
return docs
def collect_tool_registry() -> list[dict]:
"""Parse the TOOLS array out of find-a-tool/app.js (regex, not a JS parser)."""
js = (DOCS / "find-a-tool" / "app.js").read_text(encoding="utf-8")
# Match each { ... }, between `const TOOLS = [` and the next `];`
block_m = re.search(r"const TOOLS\s*=\s*\[(.*?)\n\];", js, re.DOTALL)
if not block_m:
return []
body = block_m.group(1)
# Crude split on `},\n {` boundaries inside the array
items = re.split(r"\n \},\s*\n \{", body)
out: list[dict] = []
for raw in items:
def grab(key: str) -> str | None:
m = re.search(
rf"\b{key}\s*:\s*(['\"`])(.*?)\1",
raw,
re.DOTALL,
)
return m.group(2) if m else None
tid = grab("id")
title = grab("title")
if not tid or not title:
continue
meta_audience = re.search(r"audience:\s*['\"](.*?)['\"]", raw, re.DOTALL)
meta_license = re.search(r"license:\s*['\"](.*?)['\"]", raw, re.DOTALL)
meta_time = re.search(r"time:\s*['\"](.*?)['\"]", raw, re.DOTALL)
parts = [
f"Tool: {title}",
f"Use case: {grab('question') or ''}",
f"Data source: {grab('source') or ''}",
f"Repository: {grab('repo') or ''}",
f"Summary: {grab('blurb') or ''}",
f"Audience: {meta_audience.group(1) if meta_audience else ''}",
f"License / permissions: {meta_license.group(1) if meta_license else ''}",
f"Time to set up: {meta_time.group(1) if meta_time else ''}",
]
out.append({
"id": f"tool::{tid}",
"title": title,
"url": grab("repo") or "",
"source": "Tool catalog",
"text": "\n".join(p for p in parts if p.split(": ", 1)[1]),
})
return out
def collect_readmes() -> list[dict]:
out: list[dict] = []
for repo in SIBLING_REPOS:
readme = WORKSPACE / repo / "README.md"
if not readme.exists():
continue
text = readme.read_text(encoding="utf-8", errors="ignore")
# Truncate aggressively — keeping the first ~20 KB per README is plenty
out.append({
"id": f"readme::{repo}",
"title": f"{repo} — README",
"url": f"https://github.com/microsoft/{repo}",
"source": "Repository README",
"text": text[:20000],
})
# Analytics-Hub itself
self_readme = WORKSPACE / "Analytics-Hub" / "README.md"
if self_readme.exists():
out.append({
"id": "readme::Analytics-Hub",
"title": "Analytics-Hub — README",
"url": "https://github.com/microsoft/Analytics-Hub",
"source": "Repository README",
"text": self_readme.read_text(encoding="utf-8", errors="ignore")[:20000],
})
return out
def collect_email_templates() -> list[dict]:
out: list[dict] = []
email_dir = WORKSPACE / "Email Templates"
if not email_dir.exists():
return out
for txt in sorted(email_dir.glob("*.txt")):
body = txt.read_text(encoding="utf-8", errors="ignore")
if len(body) < 80:
continue
out.append({
"id": f"email::{txt.stem}",
"title": f"Admin email template — {txt.stem}",
"url": "",
"source": "Email template",
"text": body[:8000],
})
return out
def main() -> int:
OUT.parent.mkdir(parents=True, exist_ok=True)
docs: list[dict] = []
docs += collect_pages()
docs += collect_tool_registry()
docs += collect_readmes()
docs += collect_email_templates()
payload = {
"version": datetime.now(timezone.utc).isoformat(timespec="seconds"),
"doc_count": len(docs),
"docs": docs,
}
OUT.write_text(json.dumps(payload, ensure_ascii=False, indent=2), encoding="utf-8")
kb = OUT.stat().st_size / 1024
print(f"Wrote {OUT.relative_to(ROOT)} ({len(docs)} docs, {kb:.1f} KB)")
by_source = {}
for d in docs:
by_source[d["source"]] = by_source.get(d["source"], 0) + 1
for s, n in sorted(by_source.items()):
print(f" {n:3d} {s}")
return 0
if __name__ == "__main__":
sys.exit(main())