forked from run-llama/llama_index
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathprepare_for_build.py
461 lines (418 loc) · 17.8 KB
/
prepare_for_build.py
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
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
"""Prepare the docs folder for building the documentation.
This file will:
1. Update the mkdocs.yml file to include all example notebooks
2. Create API reference pages for all integration folders
3. Update the search paths for the mkdocstrings plugin
4. Copy over the latest CHANGELOG.md/CONTRIBUTING.md/DOCS_README.md
to the docs/docs folder
"""
import toml
import yaml
import os
MKDOCS_YML = "mkdocs.yml"
# examples config
EXAMPLES_DIR = "docs/examples"
FOLDER_NAME_TO_LABEL = {
"./examples/agent": "Agents",
"./examples/cookbooks": "Cookbooks",
"./examples/chat_engine": "Chat Engines",
"./examples/customization": "Customization",
"./examples/data_connectors": "Data Connectors",
"./examples/discover_llamaindex": "Discover LlamaIndex",
"./examples/docstore": "Docstores",
"./examples/embeddings": "Embeddings",
"./examples/evaluation": "Evaluation",
"./examples/finetuning": "Finetuning",
"./examples/ingestion": "Ingestion",
"./examples/llama_dataset": "Llama Datasets",
"./examples/llama_hub": "Llama Hub",
"./examples/llm": "LLMs",
"./examples/low_level": "Low Level",
"./examples/managed": "Managed Indexes",
"./examples/metadata_extraction": "Metadata Extractors",
"./examples/multi_modal": "Multi-Modal",
"./examples/multi_tenancy": "Multi-Tenancy",
"./examples/node_parsers": "Node Parsers & Text Splitters",
"./examples/node_postprocessor": "Node Postprocessors",
"./examples/objects": "Object Stores",
"./examples/observability": "Observability",
"./examples/output_parsing": "Output Parsers",
"./examples/param_optimizer": "Param Optimizer",
"./examples/pipeline": "Query Pipeline",
"./examples/prompts": "Prompts",
"./examples/query_engine": "Query Engines",
"./examples/query_transformations": "Query Transformations",
"./examples/response_synthesizers": "Response Synthesizers",
"./examples/retrievers": "Retrievers",
"./examples/tools": "Tools",
"./examples/transforms": "Transforms",
"./examples/usecases": "Use Cases",
"./examples/vector_stores": "Vector Stores",
"./examples/workflow": "Workflow",
}
# integration config
INTEGRATION_FOLDERS = [
# "../llama-index-networks",
# "../llama-index-finetuning",
"../llama-index-packs",
"../llama-index-integrations",
# "../llama-index-cli",
]
INTEGRATION_FOLDER_TO_LABEL = {
"finetuning": "Fine-tuning",
"llms": "LLMs",
"agent": "Agents",
"callbacks": "Callbacks",
"chat_engines": "Chat Engines",
"embeddings": "Embeddings",
"evaluation": "Evaluation",
"extractors": "Metadata Extractors",
"graph_rag": "Graph RAG",
"indices": "Indexes",
"ingestion": "Ingestion",
"instrumentation": "Instrumentation",
"llama_dataset": "Llama Datasets",
"packs": "Llama Packs",
"memory": "Memory",
"multi_modal_llms": "Multi-Modal LLMs",
"node_parsers": "Node Parsers & Text Splitters",
"node_parser": "Node Parsers & Text Splitters",
"objects": "Object Stores",
"output_parsers": "Output Parsers",
"postprocessor": "Node Postprocessors",
"program": "Programs",
"prompts": "Prompts",
"query_engine": "Query Engines",
"query_pipeline": "Query Pipeline",
"question_gen": "Question Generators",
"readers": "Readers",
"response_synthesizers": "Response Synthesizers",
"retrievers": "Retrievers",
"schema": "Schema",
"selectors": "Selectors",
"sparse_embeddings": "Sparse Embeddings",
"storage": "Storage",
"tools": "Tools",
"workflow": "Workflow",
}
API_REF_TEMPLATE = """::: {import_path}
options:
members:
{members}
"""
API_REF_MEMBER_TEMPLATE = """ - {member}"""
def main():
with open(MKDOCS_YML) as f:
mkdocs = yaml.safe_load(f)
# get all example notebooks
notebooks = []
for root, dirs, files in os.walk(EXAMPLES_DIR):
for file in files:
if file.endswith(".ipynb"):
notebooks.append(os.path.join(root, file))
# update the mkdocs.yml nav section
examples_idx = -1
for idx, item in enumerate(mkdocs["nav"]):
if "Examples" in item:
examples_idx = idx
break
for path_name, label in FOLDER_NAME_TO_LABEL.items():
path_name = os.path.join(
EXAMPLES_DIR.replace("examples", ""), path_name.replace("./", "")
)
label_idx = -1
for idx, item in enumerate(mkdocs["nav"][examples_idx]["Examples"]):
if label in item:
label_idx = idx
break
# Add or clear the label
if label_idx == -1:
mkdocs["nav"][examples_idx]["Examples"].append({label: []})
else:
mkdocs["nav"][examples_idx]["Examples"][label_idx][label] = []
for file_name in os.listdir(path_name):
if file_name.endswith(".ipynb"):
toc_path_name = "./" + os.path.join(
path_name.replace("docs/", ""), file_name
)
if (
toc_path_name
not in mkdocs["nav"][examples_idx]["Examples"][label_idx][
label
]
):
mkdocs["nav"][examples_idx]["Examples"][label_idx][
label
].append(toc_path_name)
if os.path.isdir(os.path.join(path_name, file_name)):
for root, dirs, files in os.walk(
os.path.join(path_name, file_name)
):
for file in files:
if file.endswith(".ipynb"):
toc_path_name = "./" + os.path.join(
root.replace("docs/", ""), file
)
if (
toc_path_name
not in mkdocs["nav"][examples_idx]["Examples"][
label_idx
][label]
):
mkdocs["nav"][examples_idx]["Examples"][
label_idx
][label].append(toc_path_name)
# find all pyproject.toml files in the integration folders
# each toml file has a toml['tool']['llamahub']['import_path'] key that we need
# toml['tool']['llamahub']['class_authors'] contains a list of exposed classes
# For each class, we need to create an API reference page
search_paths = []
for folder in INTEGRATION_FOLDERS:
for root, dirs, files in os.walk(folder):
for file in files:
if file == "pyproject.toml":
toml_path = os.path.join(root, file)
with open(toml_path) as f:
toml_data = toml.load(f)
import_path = toml_data["tool"]["llamahub"]["import_path"]
class_authors = toml_data["tool"]["llamahub"][
"class_authors"
]
members = "\n".join(
[
API_REF_MEMBER_TEMPLATE.format(member=member)
for member in class_authors
]
)
api_ref = API_REF_TEMPLATE.format(
import_path=import_path, members=members
)
folder_name = "/".join(import_path.split(".")[1:-1])
search_paths.append(root)
# special cases
if folder_name == "vector_stores":
folder_name = "storage/vector_store"
elif folder_name == "indices/managed":
folder_name = "indices"
elif folder_name == "graph_stores":
folder_name = "storage/graph_stores"
full_path = os.path.join("docs/api_reference", folder_name)
module_name = import_path.split(".")[-1] + ".md"
os.makedirs(full_path, exist_ok=True)
with open(os.path.join(full_path, module_name), "w") as f:
f.write(api_ref)
# update the mkdocs.yml nav section
api_ref_idx = -1
for idx, item in enumerate(mkdocs["nav"]):
if "API Reference" in item:
api_ref_idx = idx
break
if "storage" in folder_name:
label = "Storage"
else:
label = INTEGRATION_FOLDER_TO_LABEL[
import_path.split(".")[1]
]
label_idx = -1
for idx, item in enumerate(
mkdocs["nav"][api_ref_idx]["API Reference"]
):
if label in item:
label_idx = idx
break
if label_idx == -1:
mkdocs["nav"][api_ref_idx]["API Reference"].append(
{label: []}
)
toc_path_name = "./" + os.path.join(
"api_reference", folder_name, module_name
)
if (
toc_path_name
not in mkdocs["nav"][api_ref_idx]["API Reference"][
label_idx
][label]
):
# storage is a special case, multi-level
if label == "Storage":
sub_path = folder_name.split("/")[-1]
sub_label = sub_path.replace("_", " ").title()
sub_label_idx = -1
for (
existing_sub_label_idx,
existing_sub_label,
) in enumerate(
mkdocs["nav"][api_ref_idx]["API Reference"][
label_idx
][label]
):
if sub_label in existing_sub_label:
sub_label_idx = existing_sub_label_idx
break
if sub_label_idx == -1:
mkdocs["nav"][api_ref_idx]["API Reference"][
label_idx
][label].append({sub_label: []})
if (
toc_path_name
not in mkdocs["nav"][api_ref_idx][
"API Reference"
][label_idx][label][sub_label_idx][sub_label]
):
mkdocs["nav"][api_ref_idx]["API Reference"][
label_idx
][label][sub_label_idx][sub_label].append(
toc_path_name
)
# sort per sub-label
mkdocs["nav"][api_ref_idx]["API Reference"][
label_idx
][label][sub_label_idx][sub_label] = sorted(
mkdocs["nav"][api_ref_idx]["API Reference"][
label_idx
][label][sub_label_idx][sub_label],
key=lambda x: next(iter(x.keys()))
if isinstance(x, dict)
else x,
)
else:
mkdocs["nav"][api_ref_idx]["API Reference"][
label_idx
][label].append(toc_path_name)
# maintain sorting per label
mkdocs["nav"][api_ref_idx]["API Reference"][label_idx][
label
] = sorted(
mkdocs["nav"][api_ref_idx]["API Reference"][label_idx][
label
],
key=lambda x: next(iter(x.keys()))
if isinstance(x, dict)
else x,
)
# add existing api reference pages to nav
api_ref_idx = -1
for idx, item in enumerate(mkdocs["nav"]):
if "API Reference" in item:
api_ref_idx = idx
break
for root, _, files in os.walk("docs/api_reference"):
for file in files:
if file.endswith(".md"):
toc_path_name = os.path.join(
root.replace("docs/api_reference", "./api_reference"),
file,
)
if toc_path_name == "./api_reference/index.md":
continue
if "storage" in root:
label = "Storage"
else:
label = INTEGRATION_FOLDER_TO_LABEL[root.split("/")[-1]]
label_idx = -1
for idx, item in enumerate(
mkdocs["nav"][api_ref_idx]["API Reference"]
):
if label in item:
label_idx = idx
break
if label_idx == -1:
mkdocs["nav"][api_ref_idx]["API Reference"].append(
{label: []}
)
if "storage" in root:
sub_path = root.split("/")[-1]
sub_label = sub_path.replace("_", " ").title()
sub_label_idx = -1
for (
existing_sub_label_idx,
existing_sub_label,
) in enumerate(
mkdocs["nav"][api_ref_idx]["API Reference"][label_idx][
label
]
):
if sub_label in existing_sub_label:
sub_label_idx = existing_sub_label_idx
break
if sub_label_idx == -1:
mkdocs["nav"][api_ref_idx]["API Reference"][label_idx][
label
].append({sub_label: []})
if (
toc_path_name
not in mkdocs["nav"][api_ref_idx]["API Reference"][
label_idx
][label][sub_label_idx][sub_label]
):
mkdocs["nav"][api_ref_idx]["API Reference"][label_idx][
label
][sub_label_idx][sub_label].append(toc_path_name)
# sort per sub-label
mkdocs["nav"][api_ref_idx]["API Reference"][label_idx][
label
][sub_label_idx][sub_label] = sorted(
mkdocs["nav"][api_ref_idx]["API Reference"][label_idx][
label
][sub_label_idx][sub_label],
key=lambda x: next(iter(x.keys()))
if isinstance(x, dict)
else x,
)
elif (
toc_path_name
not in mkdocs["nav"][api_ref_idx]["API Reference"][
label_idx
][label]
):
mkdocs["nav"][api_ref_idx]["API Reference"][label_idx][
label
].append(toc_path_name)
# sort per label
mkdocs["nav"][api_ref_idx]["API Reference"][label_idx][
label
] = sorted(
mkdocs["nav"][api_ref_idx]["API Reference"][label_idx][
label
],
key=lambda x: next(iter(x.keys()))
if isinstance(x, dict)
else x,
)
# sort the API Reference nav section
mkdocs["nav"][api_ref_idx]["API Reference"] = sorted(
mkdocs["nav"][api_ref_idx]["API Reference"],
key=lambda x: next(iter(x.keys())) if isinstance(x, dict) else x,
)
# sort the examples
for idx, item in enumerate(mkdocs["nav"][examples_idx]["Examples"]):
if isinstance(item, dict):
for key in item:
mkdocs["nav"][examples_idx]["Examples"][idx][key] = sorted(
mkdocs["nav"][examples_idx]["Examples"][idx][key],
key=lambda x: next(iter(x.keys()))
if isinstance(x, dict)
else x,
)
# update search paths
for i, plugin in enumerate(mkdocs["plugins"]):
if "mkdocstrings" in plugin:
for search_path in search_paths:
if (
search_path
not in mkdocs["plugins"][i]["mkdocstrings"]["handlers"][
"python"
]["paths"]
):
mkdocs["plugins"][i]["mkdocstrings"]["handlers"]["python"][
"paths"
].append(search_path)
# write the updated mkdocs.yml
with open(MKDOCS_YML, "w") as f:
yaml.dump(mkdocs, f)
# copy over extra files
os.system("cp ../CHANGELOG.md ./docs/CHANGELOG.md")
os.system("cp ../CONTRIBUTING.md ./docs/CONTRIBUTING.md")
os.system("cp ./README.md ./docs/DOCS_README.md")
if __name__ == "__main__":
main()