2023-08-04 20:10:58 +00:00
|
|
|
"""Script for auto-generating api_reference.rst."""
|
2024-02-10 00:13:30 +00:00
|
|
|
|
2023-08-04 20:10:58 +00:00
|
|
|
import importlib
|
|
|
|
import inspect
|
2023-12-13 21:37:27 +00:00
|
|
|
import os
|
2024-02-26 19:04:22 +00:00
|
|
|
import sys
|
2023-08-04 20:10:58 +00:00
|
|
|
import typing
|
|
|
|
from enum import Enum
|
2023-10-29 22:50:09 +00:00
|
|
|
from pathlib import Path
|
|
|
|
from typing import Dict, List, Literal, Optional, Sequence, TypedDict, Union
|
2023-08-04 20:10:58 +00:00
|
|
|
|
2023-12-13 22:24:50 +00:00
|
|
|
import toml
|
2024-06-20 15:35:00 +00:00
|
|
|
import typing_extensions
|
|
|
|
from langchain_core.runnables import Runnable, RunnableSerializable
|
2023-08-04 20:10:58 +00:00
|
|
|
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
|
|
|
|
|
2024-06-20 15:35:00 +00:00
|
|
|
ClassKind = Literal[
|
|
|
|
"TypedDict",
|
|
|
|
"Regular",
|
|
|
|
"Pydantic",
|
|
|
|
"enum",
|
|
|
|
"RunnablePydantic",
|
|
|
|
"RunnableNonPydantic",
|
|
|
|
]
|
2023-08-04 20:10:58 +00:00
|
|
|
|
|
|
|
|
|
|
|
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."""
|
2024-08-03 00:12:47 +00:00
|
|
|
is_deprecated: bool
|
|
|
|
"""Whether the class is deprecated."""
|
2023-08-04 20:10:58 +00:00
|
|
|
|
|
|
|
|
|
|
|
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."""
|
2024-08-03 00:12:47 +00:00
|
|
|
is_deprecated: bool
|
|
|
|
"""Whether the function is deprecated."""
|
2023-08-04 20:10:58 +00:00
|
|
|
|
|
|
|
|
|
|
|
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_):
|
2024-07-15 15:51:43 +00:00
|
|
|
# The type of the class is used to select a template
|
2024-06-20 15:35:00 +00:00
|
|
|
# for the object when rendering the documentation.
|
|
|
|
# See `templates` directory for defined templates.
|
|
|
|
# This is a hacky solution to distinguish between different
|
|
|
|
# kinds of thing that we want to render.
|
|
|
|
if type(type_) is typing_extensions._TypedDictMeta: # type: ignore
|
2023-08-04 20:10:58 +00:00
|
|
|
kind: ClassKind = "TypedDict"
|
2024-06-20 15:35:00 +00:00
|
|
|
elif type(type_) is typing._TypedDictMeta: # type: ignore
|
|
|
|
kind: ClassKind = "TypedDict"
|
|
|
|
elif (
|
|
|
|
issubclass(type_, Runnable)
|
|
|
|
and issubclass(type_, BaseModel)
|
|
|
|
and type_ is not Runnable
|
|
|
|
):
|
|
|
|
# RunnableSerializable subclasses from Pydantic which
|
|
|
|
# for which we use autodoc_pydantic for rendering.
|
|
|
|
# We need to distinguish these from regular Pydantic
|
|
|
|
# classes so we can hide inherited Runnable methods
|
|
|
|
# and provide a link to the Runnable interface from
|
|
|
|
# the template.
|
|
|
|
kind = "RunnablePydantic"
|
|
|
|
elif (
|
|
|
|
issubclass(type_, Runnable)
|
|
|
|
and not issubclass(type_, BaseModel)
|
|
|
|
and type_ is not Runnable
|
|
|
|
):
|
|
|
|
# These are not pydantic classes but are Runnable.
|
|
|
|
# We'll hide all the inherited methods from Runnable
|
|
|
|
# but use a regular class template to render.
|
|
|
|
kind = "RunnableNonPydantic"
|
2023-08-04 20:10:58 +00:00
|
|
|
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("_"),
|
2024-08-03 00:12:47 +00:00
|
|
|
is_deprecated=".. deprecated::" in (type_.__doc__ or ""),
|
2023-08-04 20:10:58 +00:00
|
|
|
)
|
|
|
|
)
|
|
|
|
elif inspect.isfunction(type_):
|
|
|
|
functions.append(
|
|
|
|
FunctionInfo(
|
|
|
|
name=name,
|
|
|
|
qualified_name=f"{namespace}.{name}",
|
|
|
|
is_public=not name.startswith("_"),
|
2024-08-03 00:12:47 +00:00
|
|
|
is_deprecated=".. deprecated::" in (type_.__doc__ or ""),
|
2023-08-04 20:10:58 +00:00
|
|
|
)
|
|
|
|
)
|
|
|
|
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-10-17 15:46:08 +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:
|
2024-06-06 14:51:46 +00:00
|
|
|
package_directory (Union[str, Path]): Path to the package directory.
|
|
|
|
submodule (Optional[str]): Optional name of submodule to load.
|
2023-08-04 20:10:58 +00:00
|
|
|
|
|
|
|
Returns:
|
2024-06-06 14:51:46 +00:00
|
|
|
Dict[str, ModuleMembers]: A dictionary where keys are module names and values are ModuleMembers objects.
|
2023-08-04 20:10:58 +00:00
|
|
|
"""
|
|
|
|
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(
|
2023-10-17 15:46:08 +00:00
|
|
|
f"{package_name}.{submodule}.{namespace}",
|
|
|
|
f"{submodule}.{namespace}",
|
2023-09-20 00:03:32 +00:00
|
|
|
)
|
|
|
|
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:
|
2024-05-22 22:21:08 +00:00
|
|
|
print(f"Error: Unable to import module '{namespace}' with error: {e}")
|
2023-08-04 20:10:58 +00:00
|
|
|
|
|
|
|
return modules_by_namespace
|
|
|
|
|
|
|
|
|
2023-12-07 19:43:42 +00:00
|
|
|
def _construct_doc(
|
2023-12-13 22:24:50 +00:00
|
|
|
package_namespace: str,
|
|
|
|
members_by_namespace: Dict[str, ModuleMembers],
|
|
|
|
package_version: str,
|
2024-08-14 14:00:17 +00:00
|
|
|
) -> List[typing.Tuple[str, str]]:
|
2023-08-04 20:10:58 +00:00
|
|
|
"""Construct the contents of the reference.rst file for the given package.
|
|
|
|
|
|
|
|
Args:
|
2023-12-07 19:43:42 +00:00
|
|
|
package_namespace: The package top level namespace
|
2023-08-04 20:10:58 +00:00
|
|
|
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.
|
|
|
|
"""
|
2024-08-14 14:00:17 +00:00
|
|
|
docs = []
|
|
|
|
index_doc = f"""\
|
|
|
|
:html_theme.sidebar_secondary.remove:
|
2023-06-30 16:23:32 +00:00
|
|
|
|
2024-08-14 14:00:17 +00:00
|
|
|
.. currentmodule:: {package_namespace}
|
|
|
|
|
|
|
|
.. _{package_namespace}:
|
|
|
|
|
|
|
|
======================================
|
|
|
|
{package_namespace.replace('_', '-')}: {package_version}
|
|
|
|
======================================
|
|
|
|
|
|
|
|
.. automodule:: {package_namespace}
|
|
|
|
:no-members:
|
|
|
|
:no-inherited-members:
|
|
|
|
|
|
|
|
.. toctree::
|
|
|
|
:hidden:
|
|
|
|
:maxdepth: 2
|
|
|
|
|
|
|
|
"""
|
|
|
|
index_autosummary = """
|
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:
|
2024-08-14 14:00:17 +00:00
|
|
|
index_doc += f" {module}\n"
|
|
|
|
module_doc = f"""\
|
|
|
|
.. currentmodule:: {package_namespace}
|
|
|
|
|
2024-08-15 16:52:12 +00:00
|
|
|
.. _{package_namespace}_{module}:
|
2024-08-14 14:00:17 +00:00
|
|
|
"""
|
2023-08-04 20:10:58 +00:00
|
|
|
_members = members_by_namespace[module]
|
2024-08-03 00:12:47 +00:00
|
|
|
classes = [
|
|
|
|
el
|
|
|
|
for el in _members["classes_"]
|
|
|
|
if el["is_public"] and not el["is_deprecated"]
|
|
|
|
]
|
|
|
|
functions = [
|
|
|
|
el
|
|
|
|
for el in _members["functions"]
|
|
|
|
if el["is_public"] and not el["is_deprecated"]
|
|
|
|
]
|
|
|
|
deprecated_classes = [
|
|
|
|
el for el in _members["classes_"] if el["is_public"] and el["is_deprecated"]
|
|
|
|
]
|
|
|
|
deprecated_functions = [
|
|
|
|
el
|
|
|
|
for el in _members["functions"]
|
|
|
|
if el["is_public"] and el["is_deprecated"]
|
|
|
|
]
|
2023-06-30 16:23:32 +00:00
|
|
|
if not (classes or functions):
|
|
|
|
continue
|
2024-08-14 14:00:17 +00:00
|
|
|
section = f":mod:`{module}`"
|
2023-08-04 20:10:58 +00:00
|
|
|
underline = "=" * (len(section) + 1)
|
2024-08-14 14:00:17 +00:00
|
|
|
module_doc += f"""
|
2023-06-30 16:23:32 +00:00
|
|
|
{section}
|
2023-08-04 20:10:58 +00:00
|
|
|
{underline}
|
2023-06-30 16:23:32 +00:00
|
|
|
|
2023-12-07 19:43:42 +00:00
|
|
|
.. automodule:: {package_namespace}.{module}
|
2023-06-30 16:23:32 +00:00
|
|
|
:no-members:
|
|
|
|
:no-inherited-members:
|
|
|
|
|
2024-08-14 14:00:17 +00:00
|
|
|
"""
|
|
|
|
|
|
|
|
index_autosummary += f"""
|
2024-08-15 16:52:12 +00:00
|
|
|
:ref:`{package_namespace}_{module}`
|
2024-08-14 14:00:17 +00:00
|
|
|
{'^' * (len(module) + 5)}
|
2023-06-30 16:23:32 +00:00
|
|
|
"""
|
|
|
|
|
|
|
|
if classes:
|
2024-08-14 14:00:17 +00:00
|
|
|
module_doc += f"""\
|
|
|
|
**Classes**
|
|
|
|
|
2023-12-07 19:43:42 +00:00
|
|
|
.. currentmodule:: {package_namespace}
|
2023-06-30 16:23:32 +00:00
|
|
|
|
|
|
|
.. autosummary::
|
|
|
|
:toctree: {module}
|
2024-08-14 17:40:16 +00:00
|
|
|
"""
|
|
|
|
index_autosummary += """
|
|
|
|
**Classes**
|
|
|
|
|
|
|
|
.. autosummary::
|
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-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"
|
2024-06-20 15:35:00 +00:00
|
|
|
elif class_["kind"] == "RunnablePydantic":
|
|
|
|
template = "runnable_pydantic.rst"
|
|
|
|
elif class_["kind"] == "RunnableNonPydantic":
|
|
|
|
template = "runnable_non_pydantic.rst"
|
2023-08-04 20:10:58 +00:00
|
|
|
else:
|
|
|
|
template = "class.rst"
|
2023-06-30 16:23:32 +00:00
|
|
|
|
2024-08-14 14:00:17 +00:00
|
|
|
module_doc += f"""\
|
2023-08-04 20:10:58 +00:00
|
|
|
:template: {template}
|
|
|
|
|
|
|
|
{class_["qualified_name"]}
|
|
|
|
|
2024-08-14 14:00:17 +00:00
|
|
|
"""
|
|
|
|
index_autosummary += f"""
|
|
|
|
{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:
|
2024-02-21 20:59:35 +00:00
|
|
|
_functions = [f["qualified_name"] for f in functions]
|
2023-08-04 20:10:58 +00:00
|
|
|
fstring = "\n ".join(sorted(_functions))
|
2024-08-14 14:00:17 +00:00
|
|
|
module_doc += f"""\
|
|
|
|
**Functions**
|
|
|
|
|
2023-12-07 19:43:42 +00:00
|
|
|
.. currentmodule:: {package_namespace}
|
2023-06-30 16:23:32 +00:00
|
|
|
|
2024-08-03 00:12:47 +00:00
|
|
|
.. autosummary::
|
|
|
|
:toctree: {module}
|
|
|
|
:template: function.rst
|
|
|
|
|
|
|
|
{fstring}
|
|
|
|
|
2024-08-14 17:40:16 +00:00
|
|
|
"""
|
|
|
|
|
|
|
|
index_autosummary += f"""
|
|
|
|
**Functions**
|
|
|
|
|
|
|
|
.. autosummary::
|
|
|
|
|
|
|
|
{fstring}
|
2024-08-03 00:12:47 +00:00
|
|
|
"""
|
|
|
|
if deprecated_classes:
|
2024-08-14 14:00:17 +00:00
|
|
|
module_doc += f"""\
|
|
|
|
**Deprecated classes**
|
2024-08-03 00:12:47 +00:00
|
|
|
|
|
|
|
.. currentmodule:: {package_namespace}
|
|
|
|
|
|
|
|
.. autosummary::
|
|
|
|
:toctree: {module}
|
|
|
|
"""
|
|
|
|
|
2024-08-14 17:40:16 +00:00
|
|
|
index_autosummary += """
|
2024-08-14 19:25:39 +00:00
|
|
|
**Deprecated classes**
|
2024-08-14 17:40:16 +00:00
|
|
|
|
|
|
|
.. autosummary::
|
|
|
|
"""
|
|
|
|
|
2024-08-03 00:12:47 +00:00
|
|
|
for class_ in sorted(deprecated_classes, key=lambda c: c["qualified_name"]):
|
|
|
|
if class_["kind"] == "TypedDict":
|
|
|
|
template = "typeddict.rst"
|
|
|
|
elif class_["kind"] == "enum":
|
|
|
|
template = "enum.rst"
|
|
|
|
elif class_["kind"] == "Pydantic":
|
|
|
|
template = "pydantic.rst"
|
|
|
|
elif class_["kind"] == "RunnablePydantic":
|
|
|
|
template = "runnable_pydantic.rst"
|
|
|
|
elif class_["kind"] == "RunnableNonPydantic":
|
|
|
|
template = "runnable_non_pydantic.rst"
|
|
|
|
else:
|
|
|
|
template = "class.rst"
|
|
|
|
|
2024-08-14 14:00:17 +00:00
|
|
|
module_doc += f"""\
|
2024-08-03 00:12:47 +00:00
|
|
|
:template: {template}
|
|
|
|
|
|
|
|
{class_["qualified_name"]}
|
|
|
|
|
2024-08-14 17:40:16 +00:00
|
|
|
"""
|
|
|
|
index_autosummary += f"""
|
|
|
|
{class_['qualified_name']}
|
2024-08-03 00:12:47 +00:00
|
|
|
"""
|
|
|
|
|
|
|
|
if deprecated_functions:
|
|
|
|
_functions = [f["qualified_name"] for f in deprecated_functions]
|
|
|
|
fstring = "\n ".join(sorted(_functions))
|
2024-08-14 14:00:17 +00:00
|
|
|
module_doc += f"""\
|
|
|
|
**Deprecated functions**
|
2024-08-03 00:12:47 +00:00
|
|
|
|
|
|
|
.. currentmodule:: {package_namespace}
|
|
|
|
|
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}
|
|
|
|
|
2024-08-14 17:40:16 +00:00
|
|
|
"""
|
2024-08-14 19:25:39 +00:00
|
|
|
index_autosummary += f"""
|
2024-08-14 17:40:16 +00:00
|
|
|
**Deprecated functions**
|
|
|
|
|
|
|
|
.. autosummary::
|
|
|
|
|
|
|
|
{fstring}
|
|
|
|
|
2023-06-30 16:23:32 +00:00
|
|
|
"""
|
2024-08-14 14:00:17 +00:00
|
|
|
docs.append((f"{module}.rst", module_doc))
|
|
|
|
docs.append(("index.rst", index_doc + index_autosummary))
|
|
|
|
|
|
|
|
return docs
|
2023-06-30 16:23:32 +00:00
|
|
|
|
|
|
|
|
2023-12-07 19:43:42 +00:00
|
|
|
def _build_rst_file(package_name: str = "langchain") -> None:
|
|
|
|
"""Create a rst file for building of documentation.
|
2023-06-30 16:23:32 +00:00
|
|
|
|
2023-12-07 19:43:42 +00:00
|
|
|
Args:
|
|
|
|
package_name: Can be either "langchain" or "core" or "experimental".
|
|
|
|
"""
|
2023-12-13 22:24:50 +00:00
|
|
|
package_dir = _package_dir(package_name)
|
|
|
|
package_members = _load_package_modules(package_dir)
|
|
|
|
package_version = _get_package_version(package_dir)
|
2024-08-14 14:00:17 +00:00
|
|
|
output_dir = _out_file_path(package_name)
|
|
|
|
os.mkdir(output_dir)
|
|
|
|
rsts = _construct_doc(
|
|
|
|
_package_namespace(package_name), package_members, package_version
|
|
|
|
)
|
|
|
|
for name, rst in rsts:
|
|
|
|
with open(output_dir / name, "w") as f:
|
|
|
|
f.write(rst)
|
2023-06-30 16:23:32 +00:00
|
|
|
|
2023-10-17 15:46:08 +00:00
|
|
|
|
2023-12-13 21:37:27 +00:00
|
|
|
def _package_namespace(package_name: str) -> str:
|
|
|
|
return (
|
|
|
|
package_name
|
|
|
|
if package_name == "langchain"
|
|
|
|
else f"langchain_{package_name.replace('-', '_')}"
|
|
|
|
)
|
2023-12-07 19:43:42 +00:00
|
|
|
|
|
|
|
|
|
|
|
def _package_dir(package_name: str = "langchain") -> Path:
|
|
|
|
"""Return the path to the directory containing the documentation."""
|
2024-03-01 03:04:44 +00:00
|
|
|
if package_name in (
|
|
|
|
"langchain",
|
|
|
|
"experimental",
|
|
|
|
"community",
|
|
|
|
"core",
|
|
|
|
"cli",
|
|
|
|
"text-splitters",
|
|
|
|
):
|
2023-12-13 21:37:27 +00:00
|
|
|
return ROOT_DIR / "libs" / package_name / _package_namespace(package_name)
|
|
|
|
else:
|
|
|
|
return (
|
|
|
|
ROOT_DIR
|
|
|
|
/ "libs"
|
|
|
|
/ "partners"
|
|
|
|
/ package_name
|
|
|
|
/ _package_namespace(package_name)
|
|
|
|
)
|
2023-12-07 19:43:42 +00:00
|
|
|
|
|
|
|
|
2023-12-13 22:24:50 +00:00
|
|
|
def _get_package_version(package_dir: Path) -> str:
|
2024-02-14 22:55:09 +00:00
|
|
|
"""Return the version of the package."""
|
|
|
|
try:
|
|
|
|
with open(package_dir.parent / "pyproject.toml", "r") as f:
|
|
|
|
pyproject = toml.load(f)
|
|
|
|
except FileNotFoundError as e:
|
|
|
|
print(
|
|
|
|
f"pyproject.toml not found in {package_dir.parent}.\n"
|
|
|
|
"You are either attempting to build a directory which is not a package or "
|
|
|
|
"the package is missing a pyproject.toml file which should be added."
|
|
|
|
"Aborting the build."
|
|
|
|
)
|
|
|
|
exit(1)
|
2023-12-13 22:24:50 +00:00
|
|
|
return pyproject["tool"]["poetry"]["version"]
|
|
|
|
|
|
|
|
|
2024-02-14 22:55:09 +00:00
|
|
|
def _out_file_path(package_name: str) -> Path:
|
2023-12-07 19:43:42 +00:00
|
|
|
"""Return the path to the file containing the documentation."""
|
2024-08-14 14:00:17 +00:00
|
|
|
return HERE / f"{package_name.replace('-', '_')}"
|
|
|
|
|
|
|
|
|
|
|
|
def _build_index(dirs: List[str]) -> None:
|
|
|
|
custom_names = {
|
|
|
|
"airbyte": "Airbyte",
|
|
|
|
"aws": "AWS",
|
|
|
|
"ai21": "AI21",
|
|
|
|
}
|
|
|
|
ordered = ["core", "langchain", "text-splitters", "community", "experimental"]
|
|
|
|
main_ = [dir_ for dir_ in ordered if dir_ in dirs]
|
|
|
|
integrations = sorted(dir_ for dir_ in dirs if dir_ not in main_)
|
|
|
|
main_headers = [
|
|
|
|
" ".join(custom_names.get(x, x.title()) for x in dir_.split("-"))
|
|
|
|
for dir_ in main_
|
|
|
|
]
|
|
|
|
integration_headers = [
|
|
|
|
" ".join(
|
|
|
|
custom_names.get(x, x.title().replace("ai", "AI").replace("db", "DB"))
|
|
|
|
for x in dir_.split("-")
|
|
|
|
)
|
|
|
|
for dir_ in integrations
|
|
|
|
]
|
|
|
|
main_tree = "\n".join(
|
|
|
|
f"{header_name}<{dir_.replace('-', '_')}/index>"
|
|
|
|
for header_name, dir_ in zip(main_headers, main_)
|
|
|
|
)
|
|
|
|
main_grid = "\n".join(
|
|
|
|
f'- header: "**{header_name}**"\n content: "{_package_namespace(dir_).replace("_", "-")}: {_get_package_version(_package_dir(dir_))}"\n link: {dir_.replace("-", "_")}/index.html'
|
|
|
|
for header_name, dir_ in zip(main_headers, main_)
|
|
|
|
)
|
|
|
|
integration_tree = "\n".join(
|
|
|
|
f"{header_name}<{dir_.replace('-', '_')}/index>"
|
|
|
|
for header_name, dir_ in zip(integration_headers, integrations)
|
|
|
|
)
|
2023-12-07 19:43:42 +00:00
|
|
|
|
2024-08-14 14:00:17 +00:00
|
|
|
integration_grid = ""
|
|
|
|
integrations_to_show = [
|
|
|
|
"openai",
|
|
|
|
"anthropic",
|
|
|
|
"google-vertexai",
|
|
|
|
"aws",
|
|
|
|
"huggingface",
|
|
|
|
"mistralai",
|
|
|
|
]
|
|
|
|
for header_name, dir_ in sorted(
|
|
|
|
zip(integration_headers, integrations),
|
|
|
|
key=lambda h_d: integrations_to_show.index(h_d[1])
|
|
|
|
if h_d[1] in integrations_to_show
|
|
|
|
else len(integrations_to_show),
|
|
|
|
)[: len(integrations_to_show)]:
|
|
|
|
integration_grid += f'\n- header: "**{header_name}**"\n content: {_package_namespace(dir_).replace("_", "-")} {_get_package_version(_package_dir(dir_))}\n link: {dir_.replace("-", "_")}/index.html'
|
|
|
|
doc = f"""# LangChain Python API Reference
|
2023-10-17 15:46:08 +00:00
|
|
|
|
2024-08-14 14:00:17 +00:00
|
|
|
Welcome to the LangChain Python API reference. This is a reference for all
|
|
|
|
`langchain-x` packages.
|
|
|
|
|
|
|
|
For user guides see [https://python.langchain.com](https://python.langchain.com).
|
|
|
|
|
|
|
|
For the legacy API reference hosted on ReadTheDocs see [https://api.python.langchain.com/](https://api.python.langchain.com/).
|
|
|
|
|
|
|
|
## Base packages
|
|
|
|
|
|
|
|
```{{gallery-grid}}
|
|
|
|
:grid-columns: "1 2 2 3"
|
|
|
|
|
|
|
|
{main_grid}
|
|
|
|
```
|
|
|
|
|
|
|
|
```{{toctree}}
|
|
|
|
:maxdepth: 2
|
|
|
|
:hidden:
|
|
|
|
:caption: Base packages
|
|
|
|
|
|
|
|
{main_tree}
|
|
|
|
```
|
|
|
|
|
|
|
|
## Integrations
|
|
|
|
|
|
|
|
```{{gallery-grid}}
|
|
|
|
:grid-columns: "1 2 2 3"
|
|
|
|
|
|
|
|
{integration_grid}
|
|
|
|
```
|
|
|
|
|
|
|
|
See the full list of integrations in the Section Navigation.
|
|
|
|
|
|
|
|
```{{toctree}}
|
|
|
|
:maxdepth: 2
|
|
|
|
:hidden:
|
|
|
|
:caption: Integrations
|
|
|
|
|
|
|
|
{integration_tree}
|
|
|
|
```
|
|
|
|
|
|
|
|
"""
|
|
|
|
with open(HERE / "reference.md", "w") as f:
|
|
|
|
f.write(doc)
|
|
|
|
|
|
|
|
dummy_index = """\
|
|
|
|
# API reference
|
|
|
|
|
|
|
|
```{toctree}
|
|
|
|
:maxdepth: 3
|
|
|
|
:hidden:
|
|
|
|
|
|
|
|
Reference<reference>
|
|
|
|
```
|
|
|
|
"""
|
|
|
|
with open(HERE / "index.md", "w") as f:
|
|
|
|
f.write(dummy_index)
|
2023-10-17 15:46:08 +00:00
|
|
|
|
|
|
|
|
2024-02-26 19:04:22 +00:00
|
|
|
def main(dirs: Optional[list] = None) -> None:
|
2023-12-07 19:43:42 +00:00
|
|
|
"""Generate the api_reference.rst file for each package."""
|
2024-02-14 22:55:09 +00:00
|
|
|
print("Starting to build API reference files.")
|
2024-02-26 19:04:22 +00:00
|
|
|
if not dirs:
|
|
|
|
dirs = [
|
|
|
|
dir_
|
|
|
|
for dir_ in os.listdir(ROOT_DIR / "libs")
|
2024-05-13 13:49:50 +00:00
|
|
|
if dir_ not in ("cli", "partners", "standard-tests")
|
|
|
|
]
|
|
|
|
dirs += [
|
|
|
|
dir_
|
|
|
|
for dir_ in os.listdir(ROOT_DIR / "libs" / "partners")
|
2024-05-22 20:16:07 +00:00
|
|
|
if os.path.isdir(ROOT_DIR / "libs" / "partners" / dir_)
|
2024-05-13 13:49:50 +00:00
|
|
|
and "pyproject.toml" in os.listdir(ROOT_DIR / "libs" / "partners" / dir_)
|
2024-02-26 19:04:22 +00:00
|
|
|
]
|
|
|
|
for dir_ in dirs:
|
2024-02-14 22:55:09 +00:00
|
|
|
# Skip any hidden directories
|
|
|
|
# Some of these could be present by mistake in the code base
|
|
|
|
# e.g., .pytest_cache from running tests from the wrong location.
|
2024-02-26 19:04:22 +00:00
|
|
|
if dir_.startswith("."):
|
|
|
|
print("Skipping dir:", dir_)
|
2023-12-13 21:37:27 +00:00
|
|
|
continue
|
|
|
|
else:
|
2024-02-26 19:04:22 +00:00
|
|
|
print("Building package:", dir_)
|
|
|
|
_build_rst_file(package_name=dir_)
|
2024-08-14 14:00:17 +00:00
|
|
|
|
|
|
|
_build_index(dirs)
|
2024-02-14 22:55:09 +00:00
|
|
|
print("API reference files built.")
|
2023-10-17 15:46:08 +00:00
|
|
|
|
|
|
|
|
2023-06-30 16:23:32 +00:00
|
|
|
if __name__ == "__main__":
|
2024-02-26 19:04:22 +00:00
|
|
|
dirs = sys.argv[1:] or None
|
|
|
|
main(dirs=dirs)
|