"tests/vscode:/vscode.git/clone" did not exist on "5ec9c0fb3c667c30117eb1fd743e0e7c13ccf997"
generate_examples.py 1.88 KB
Newer Older
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
import re
from pathlib import Path


def fix_case(text: str) -> str:
    subs = [
        ("api", "API"),
        ("llm", "LLM"),
        ("vllm", "vLLM"),
        ("openai", "OpenAI"),
        ("multilora", "MultiLoRA"),
    ]
    for sub in subs:
        text = re.sub(*sub, text, flags=re.IGNORECASE)
    return text


def generate_title(filename: str) -> str:
    # Turn filename into a title
    title = filename.replace("_", " ").title()
    # Handle acronyms and names
    title = fix_case(title)
23
    return f"# {title}"
24
25
26
27
28
29
30
31
32
33
34


def generate_examples():
    root_dir = Path(__file__).parent.parent.parent.resolve()

    # Source paths
    script_dir = root_dir / "examples"
    script_paths = sorted(script_dir.glob("*.py"))

    # Destination paths
    doc_dir = root_dir / "docs/source/getting_started/examples"
35
    doc_paths = [doc_dir / f"{path.stem}.md" for path in script_paths]
36
37
38
39
40
41
42

    # Generate the example docs for each example script
    for script_path, doc_path in zip(script_paths, doc_paths):
        script_url = f"https://github.com/vllm-project/vllm/blob/main/examples/{script_path.name}"
        # Make script_path relative to doc_path and call it include_path
        include_path = '../../../..' / script_path.relative_to(root_dir)
        content = (f"{generate_title(doc_path.stem)}\n\n"
43
44
45
46
                   f"Source: <{script_url}>.\n\n"
                   f"```{{literalinclude}} {include_path}\n"
                   ":language: python\n"
                   ":linenos:\n```")
47
48
49
50
        with open(doc_path, "w+") as f:
            f.write(content)

    # Generate the toctree for the example scripts
51
    with open(doc_dir / "examples_index.template.md") as f:
52
        examples_index = f.read()
53
54
    with open(doc_dir / "examples_index.md", "w+") as f:
        example_docs = "\n".join(path.stem + ".md" for path in script_paths)
55
        f.write(examples_index.replace(r"%EXAMPLE_DOCS%", example_docs))