2023-08-04 20:10:58 +00:00
|
|
|
"""Script for auto-generating api_reference.rst."""
|
|
|
|
import importlib
|
|
|
|
import inspect
|
|
|
|
import typing
|
2023-06-30 16:23:32 +00:00
|
|
|
from pathlib import Path
|
2023-09-20 00:03:32 +00:00
|
|
|
from typing import TypedDict, Sequence, List, Dict, Literal, Union, Optional
|
2023-08-04 20:10:58 +00:00
|
|
|
from enum import Enum
|
|
|
|
|
|
|
|
from pydantic import BaseModel
|
2023-06-30 16:23:32 +00:00
|
|
|
|
|
|
|
ROOT_DIR = Path(__file__).parents[2].absolute()
|
2023-08-04 20:10:58 +00:00
|
|
|
HERE = Path(__file__).parent
|
|
|
|
|
2023-07-21 19:37:21 +00:00
|
|
|
PKG_DIR = ROOT_DIR / "libs" / "langchain" / "langchain"
|
2023-07-28 21:26:47 +00:00
|
|
|
EXP_DIR = ROOT_DIR / "libs" / "experimental" / "langchain_experimental"
|
2023-08-04 20:10:58 +00:00
|
|
|
WRITE_FILE = HERE / "api_reference.rst"
|
|
|
|
EXP_WRITE_FILE = HERE / "experimental_api_reference.rst"
|
|
|
|
|
|
|
|
|
|
|
|
ClassKind = Literal["TypedDict", "Regular", "Pydantic", "enum"]
|
|
|
|
|
|
|
|
|
|
|
|
class ClassInfo(TypedDict):
|
|
|
|
"""Information about a class."""
|
|
|
|
|
|
|
|
name: str
|
|
|
|
"""The name of the class."""
|
|
|
|
qualified_name: str
|
|
|
|
"""The fully qualified name of the class."""
|
|
|
|
kind: ClassKind
|
|
|
|
"""The kind of the class."""
|
|
|
|
is_public: bool
|
|
|
|
"""Whether the class is public or not."""
|
|
|
|
|
|
|
|
|
|
|
|
class FunctionInfo(TypedDict):
|
|
|
|
"""Information about a function."""
|
|
|
|
|
|
|
|
name: str
|
|
|
|
"""The name of the function."""
|
|
|
|
qualified_name: str
|
|
|
|
"""The fully qualified name of the function."""
|
|
|
|
is_public: bool
|
|
|
|
"""Whether the function is public or not."""
|
|
|
|
|
|
|
|
|
|
|
|
class ModuleMembers(TypedDict):
|
|
|
|
"""A dictionary of module members."""
|
|
|
|
|
|
|
|
classes_: Sequence[ClassInfo]
|
|
|
|
functions: Sequence[FunctionInfo]
|
|
|
|
|
|
|
|
|
|
|
|
def _load_module_members(module_path: str, namespace: str) -> ModuleMembers:
|
|
|
|
"""Load all members of a module.
|
|
|
|
|
|
|
|
Args:
|
|
|
|
module_path: Path to the module.
|
|
|
|
namespace: the namespace of the module.
|
|
|
|
|
|
|
|
Returns:
|
|
|
|
list: A list of loaded module objects.
|
|
|
|
"""
|
|
|
|
classes_: List[ClassInfo] = []
|
|
|
|
functions: List[FunctionInfo] = []
|
|
|
|
module = importlib.import_module(module_path)
|
|
|
|
for name, type_ in inspect.getmembers(module):
|
|
|
|
if not hasattr(type_, "__module__"):
|
|
|
|
continue
|
|
|
|
if type_.__module__ != module_path:
|
|
|
|
continue
|
|
|
|
|
|
|
|
if inspect.isclass(type_):
|
|
|
|
if type(type_) == typing._TypedDictMeta: # type: ignore
|
|
|
|
kind: ClassKind = "TypedDict"
|
|
|
|
elif issubclass(type_, Enum):
|
|
|
|
kind = "enum"
|
|
|
|
elif issubclass(type_, BaseModel):
|
|
|
|
kind = "Pydantic"
|
|
|
|
else:
|
|
|
|
kind = "Regular"
|
|
|
|
|
|
|
|
classes_.append(
|
|
|
|
ClassInfo(
|
|
|
|
name=name,
|
|
|
|
qualified_name=f"{namespace}.{name}",
|
|
|
|
kind=kind,
|
|
|
|
is_public=not name.startswith("_"),
|
|
|
|
)
|
|
|
|
)
|
|
|
|
elif inspect.isfunction(type_):
|
|
|
|
functions.append(
|
|
|
|
FunctionInfo(
|
|
|
|
name=name,
|
|
|
|
qualified_name=f"{namespace}.{name}",
|
|
|
|
is_public=not name.startswith("_"),
|
|
|
|
)
|
|
|
|
)
|
|
|
|
else:
|
|
|
|
continue
|
|
|
|
|
|
|
|
return ModuleMembers(
|
|
|
|
classes_=classes_,
|
|
|
|
functions=functions,
|
|
|
|
)
|
|
|
|
|
|
|
|
|
|
|
|
def _merge_module_members(
|
|
|
|
module_members: Sequence[ModuleMembers],
|
|
|
|
) -> ModuleMembers:
|
|
|
|
"""Merge module members."""
|
|
|
|
classes_: List[ClassInfo] = []
|
|
|
|
functions: List[FunctionInfo] = []
|
|
|
|
for module in module_members:
|
|
|
|
classes_.extend(module["classes_"])
|
|
|
|
functions.extend(module["functions"])
|
|
|
|
|
|
|
|
return ModuleMembers(
|
|
|
|
classes_=classes_,
|
|
|
|
functions=functions,
|
|
|
|
)
|
|
|
|
|
|
|
|
|
|
|
|
def _load_package_modules(
|
2023-09-20 00:03:32 +00:00
|
|
|
package_directory: Union[str, Path],
|
|
|
|
submodule: Optional[str] = None
|
2023-08-04 20:10:58 +00:00
|
|
|
) -> Dict[str, ModuleMembers]:
|
|
|
|
"""Recursively load modules of a package based on the file system.
|
|
|
|
|
|
|
|
Traversal based on the file system makes it easy to determine which
|
|
|
|
of the modules/packages are part of the package vs. 3rd party or built-in.
|
|
|
|
|
|
|
|
Parameters:
|
|
|
|
package_directory: Path to the package directory.
|
2023-09-20 00:03:32 +00:00
|
|
|
submodule: Optional name of submodule to load.
|
2023-08-04 20:10:58 +00:00
|
|
|
|
|
|
|
Returns:
|
|
|
|
list: A list of loaded module objects.
|
|
|
|
"""
|
|
|
|
package_path = (
|
|
|
|
Path(package_directory)
|
|
|
|
if isinstance(package_directory, str)
|
|
|
|
else package_directory
|
|
|
|
)
|
|
|
|
modules_by_namespace = {}
|
|
|
|
|
2023-09-20 00:03:32 +00:00
|
|
|
# Get the high level package name
|
2023-08-04 20:10:58 +00:00
|
|
|
package_name = package_path.name
|
|
|
|
|
2023-09-20 00:03:32 +00:00
|
|
|
# If we are loading a submodule, add it in
|
|
|
|
if submodule is not None:
|
|
|
|
package_path = package_path / submodule
|
|
|
|
|
2023-08-04 20:10:58 +00:00
|
|
|
for file_path in package_path.rglob("*.py"):
|
2023-08-11 22:58:14 +00:00
|
|
|
if file_path.name.startswith("_"):
|
|
|
|
continue
|
|
|
|
|
|
|
|
relative_module_name = file_path.relative_to(package_path)
|
|
|
|
|
2023-08-16 22:56:54 +00:00
|
|
|
# Skip if any module part starts with an underscore
|
|
|
|
if any(part.startswith("_") for part in relative_module_name.parts):
|
2023-08-11 22:58:14 +00:00
|
|
|
continue
|
|
|
|
|
|
|
|
# Get the full namespace of the module
|
|
|
|
namespace = str(relative_module_name).replace(".py", "").replace("/", ".")
|
|
|
|
# Keep only the top level namespace
|
|
|
|
top_namespace = namespace.split(".")[0]
|
|
|
|
|
|
|
|
try:
|
2023-09-20 00:03:32 +00:00
|
|
|
# If submodule is present, we need to construct the paths in a slightly
|
|
|
|
# different way
|
|
|
|
if submodule is not None:
|
|
|
|
module_members = _load_module_members(
|
|
|
|
f"{package_name}.{submodule}.{namespace}", f"{submodule}.{namespace}"
|
|
|
|
)
|
|
|
|
else:
|
|
|
|
module_members = _load_module_members(
|
|
|
|
f"{package_name}.{namespace}", namespace
|
|
|
|
)
|
2023-08-11 22:58:14 +00:00
|
|
|
# Merge module members if the namespace already exists
|
|
|
|
if top_namespace in modules_by_namespace:
|
|
|
|
existing_module_members = modules_by_namespace[top_namespace]
|
|
|
|
_module_members = _merge_module_members(
|
|
|
|
[existing_module_members, module_members]
|
2023-08-04 20:10:58 +00:00
|
|
|
)
|
2023-08-11 22:58:14 +00:00
|
|
|
else:
|
|
|
|
_module_members = module_members
|
2023-08-04 20:10:58 +00:00
|
|
|
|
2023-08-11 22:58:14 +00:00
|
|
|
modules_by_namespace[top_namespace] = _module_members
|
2023-08-04 20:10:58 +00:00
|
|
|
|
2023-08-11 22:58:14 +00:00
|
|
|
except ImportError as e:
|
|
|
|
print(f"Error: Unable to import module '{namespace}' with error: {e}")
|
2023-08-04 20:10:58 +00:00
|
|
|
|
|
|
|
return modules_by_namespace
|
|
|
|
|
|
|
|
|
|
|
|
def _construct_doc(pkg: str, members_by_namespace: Dict[str, ModuleMembers]) -> str:
|
|
|
|
"""Construct the contents of the reference.rst file for the given package.
|
|
|
|
|
|
|
|
Args:
|
|
|
|
pkg: The package name
|
|
|
|
members_by_namespace: The members of the package, dict organized by top level
|
|
|
|
module contains a list of classes and functions
|
|
|
|
inside of the top level namespace.
|
|
|
|
|
|
|
|
Returns:
|
|
|
|
The contents of the reference.rst file.
|
|
|
|
"""
|
2023-07-28 21:26:47 +00:00
|
|
|
full_doc = f"""\
|
2023-08-04 20:10:58 +00:00
|
|
|
=======================
|
2023-07-28 21:26:47 +00:00
|
|
|
``{pkg}`` API Reference
|
2023-08-04 20:10:58 +00:00
|
|
|
=======================
|
2023-06-30 16:23:32 +00:00
|
|
|
|
|
|
|
"""
|
2023-08-04 20:10:58 +00:00
|
|
|
namespaces = sorted(members_by_namespace)
|
|
|
|
|
|
|
|
for module in namespaces:
|
|
|
|
_members = members_by_namespace[module]
|
|
|
|
classes = _members["classes_"]
|
2023-06-30 16:23:32 +00:00
|
|
|
functions = _members["functions"]
|
|
|
|
if not (classes or functions):
|
|
|
|
continue
|
2023-07-28 21:26:47 +00:00
|
|
|
section = f":mod:`{pkg}.{module}`"
|
2023-08-04 20:10:58 +00:00
|
|
|
underline = "=" * (len(section) + 1)
|
2023-06-30 16:23:32 +00:00
|
|
|
full_doc += f"""\
|
|
|
|
{section}
|
2023-08-04 20:10:58 +00:00
|
|
|
{underline}
|
2023-06-30 16:23:32 +00:00
|
|
|
|
2023-07-28 21:26:47 +00:00
|
|
|
.. automodule:: {pkg}.{module}
|
2023-06-30 16:23:32 +00:00
|
|
|
:no-members:
|
|
|
|
:no-inherited-members:
|
|
|
|
|
|
|
|
"""
|
|
|
|
|
|
|
|
if classes:
|
|
|
|
full_doc += f"""\
|
|
|
|
Classes
|
|
|
|
--------------
|
2023-07-28 21:26:47 +00:00
|
|
|
.. currentmodule:: {pkg}
|
2023-06-30 16:23:32 +00:00
|
|
|
|
|
|
|
.. autosummary::
|
|
|
|
:toctree: {module}
|
2023-08-04 20:10:58 +00:00
|
|
|
"""
|
2023-06-30 16:23:32 +00:00
|
|
|
|
2023-08-24 20:53:50 +00:00
|
|
|
for class_ in sorted(classes, key=lambda c: c["qualified_name"]):
|
2023-08-11 22:58:14 +00:00
|
|
|
if not class_["is_public"]:
|
2023-08-04 20:10:58 +00:00
|
|
|
continue
|
2023-08-11 22:58:14 +00:00
|
|
|
|
2023-08-04 20:10:58 +00:00
|
|
|
if class_["kind"] == "TypedDict":
|
|
|
|
template = "typeddict.rst"
|
|
|
|
elif class_["kind"] == "enum":
|
|
|
|
template = "enum.rst"
|
|
|
|
elif class_["kind"] == "Pydantic":
|
|
|
|
template = "pydantic.rst"
|
|
|
|
else:
|
|
|
|
template = "class.rst"
|
2023-06-30 16:23:32 +00:00
|
|
|
|
2023-08-04 20:10:58 +00:00
|
|
|
full_doc += f"""\
|
|
|
|
:template: {template}
|
|
|
|
|
|
|
|
{class_["qualified_name"]}
|
|
|
|
|
2023-06-30 16:23:32 +00:00
|
|
|
"""
|
2023-08-04 20:10:58 +00:00
|
|
|
|
2023-06-30 16:23:32 +00:00
|
|
|
if functions:
|
2023-08-04 20:10:58 +00:00
|
|
|
_functions = [f["qualified_name"] for f in functions if f["is_public"]]
|
|
|
|
fstring = "\n ".join(sorted(_functions))
|
2023-06-30 16:23:32 +00:00
|
|
|
full_doc += f"""\
|
|
|
|
Functions
|
|
|
|
--------------
|
2023-07-28 21:26:47 +00:00
|
|
|
.. currentmodule:: {pkg}
|
2023-06-30 16:23:32 +00:00
|
|
|
|
|
|
|
.. autosummary::
|
|
|
|
:toctree: {module}
|
2023-07-26 19:38:58 +00:00
|
|
|
:template: function.rst
|
2023-06-30 16:23:32 +00:00
|
|
|
|
|
|
|
{fstring}
|
|
|
|
|
|
|
|
"""
|
|
|
|
return full_doc
|
|
|
|
|
|
|
|
|
|
|
|
def main() -> None:
|
2023-08-04 20:10:58 +00:00
|
|
|
"""Generate the reference.rst file for each package."""
|
|
|
|
lc_members = _load_package_modules(PKG_DIR)
|
2023-09-21 06:09:58 +00:00
|
|
|
# Put some packages at top level
|
2023-09-20 00:03:32 +00:00
|
|
|
tools = _load_package_modules(PKG_DIR, "tools")
|
|
|
|
lc_members['tools.render'] = tools['render']
|
2023-09-21 06:09:58 +00:00
|
|
|
agents = _load_package_modules(PKG_DIR, "agents")
|
|
|
|
lc_members['agents.output_parsers'] = agents['output_parsers']
|
|
|
|
lc_members['agents.format_scratchpad'] = agents['format_scratchpad']
|
2023-08-04 20:10:58 +00:00
|
|
|
lc_doc = ".. _api_reference:\n\n" + _construct_doc("langchain", lc_members)
|
2023-06-30 16:23:32 +00:00
|
|
|
with open(WRITE_FILE, "w") as f:
|
2023-07-28 21:26:47 +00:00
|
|
|
f.write(lc_doc)
|
2023-08-04 20:10:58 +00:00
|
|
|
exp_members = _load_package_modules(EXP_DIR)
|
|
|
|
exp_doc = ".. _experimental_api_reference:\n\n" + _construct_doc(
|
|
|
|
"langchain_experimental", exp_members
|
|
|
|
)
|
2023-07-28 21:26:47 +00:00
|
|
|
with open(EXP_WRITE_FILE, "w") as f:
|
|
|
|
f.write(exp_doc)
|
2023-06-30 16:23:32 +00:00
|
|
|
|
|
|
|
|
|
|
|
if __name__ == "__main__":
|
|
|
|
main()
|