mirror of
https://github.com/hwchase17/langchain
synced 2024-11-08 07:10:35 +00:00
ed24de8467
This change compacts the left-side Navbar (ToC) of the [API Reference](https://api.python.langchain.com/en/latest/api_reference.html). Now almost each namespace item is split into two lines. For example `langchain.chat_models: Chat Models` We remove the `Chat Models` and leave one the `langchain.chat_models`. This effectively compacts the navbar and increases the main page's usability. On my screen, it reduces # of lines in Toc from 28 t to 18, which is huge. Removing the namespace "title" (like `Chat Models`) does not remove any information because the title is composed directly from the namespace. API Reference users are developers. Usability for them is very important. We see less text => we find faster.
94 lines
2.3 KiB
Python
94 lines
2.3 KiB
Python
"""Script for auto-generating api_reference.rst"""
|
|
import glob
|
|
import re
|
|
from pathlib import Path
|
|
|
|
ROOT_DIR = Path(__file__).parents[2].absolute()
|
|
PKG_DIR = ROOT_DIR / "libs" / "langchain" / "langchain"
|
|
WRITE_FILE = Path(__file__).parent / "api_reference.rst"
|
|
|
|
|
|
def load_members() -> dict:
|
|
members: dict = {}
|
|
for py in glob.glob(str(PKG_DIR) + "/**/*.py", recursive=True):
|
|
module = py[len(str(PKG_DIR)) + 1 :].replace(".py", "").replace("/", ".")
|
|
top_level = module.split(".")[0]
|
|
if top_level not in members:
|
|
members[top_level] = {"classes": [], "functions": []}
|
|
with open(py, "r") as f:
|
|
for line in f.readlines():
|
|
cls = re.findall(r"^class ([^_].*)\(", line)
|
|
members[top_level]["classes"].extend([module + "." + c for c in cls])
|
|
func = re.findall(r"^def ([^_].*)\(", line)
|
|
afunc = re.findall(r"^async def ([^_].*)\(", line)
|
|
func_strings = [module + "." + f for f in func + afunc]
|
|
members[top_level]["functions"].extend(func_strings)
|
|
return members
|
|
|
|
|
|
def construct_doc(members: dict) -> str:
|
|
full_doc = """\
|
|
.. _api_reference:
|
|
|
|
=============
|
|
API Reference
|
|
=============
|
|
|
|
"""
|
|
for module, _members in sorted(members.items(), key=lambda kv: kv[0]):
|
|
classes = _members["classes"]
|
|
functions = _members["functions"]
|
|
if not (classes or functions):
|
|
continue
|
|
section = f":mod:`langchain.{module}`"
|
|
full_doc += f"""\
|
|
{section}
|
|
{'=' * (len(section) + 1)}
|
|
|
|
.. automodule:: langchain.{module}
|
|
:no-members:
|
|
:no-inherited-members:
|
|
|
|
"""
|
|
|
|
if classes:
|
|
cstring = "\n ".join(sorted(classes))
|
|
full_doc += f"""\
|
|
Classes
|
|
--------------
|
|
.. currentmodule:: langchain
|
|
|
|
.. autosummary::
|
|
:toctree: {module}
|
|
:template: class.rst
|
|
|
|
{cstring}
|
|
|
|
"""
|
|
if functions:
|
|
fstring = "\n ".join(sorted(functions))
|
|
full_doc += f"""\
|
|
Functions
|
|
--------------
|
|
.. currentmodule:: langchain
|
|
|
|
.. autosummary::
|
|
:toctree: {module}
|
|
:template: function.rst
|
|
|
|
{fstring}
|
|
|
|
"""
|
|
return full_doc
|
|
|
|
|
|
def main() -> None:
|
|
members = load_members()
|
|
full_doc = construct_doc(members)
|
|
with open(WRITE_FILE, "w") as f:
|
|
f.write(full_doc)
|
|
|
|
|
|
if __name__ == "__main__":
|
|
main()
|