Harrison/add submodule to docs (#10803)

This commit is contained in:
Harrison Chase 2023-09-19 17:03:32 -07:00 committed by GitHub
parent c15bbaac31
commit 1dae3c383e
No known key found for this signature in database
GPG Key ID: 4AEE18F83AFDEB23
2 changed files with 32 additions and 13 deletions

View File

@ -1,9 +1,10 @@
"""Script for auto-generating api_reference.rst.""" """Script for auto-generating api_reference.rst."""
import importlib import importlib
import inspect import inspect
import os
import typing import typing
from pathlib import Path from pathlib import Path
from typing import TypedDict, Sequence, List, Dict, Literal, Union from typing import TypedDict, Sequence, List, Dict, Literal, Union, Optional
from enum import Enum from enum import Enum
from pydantic import BaseModel from pydantic import BaseModel
@ -122,7 +123,8 @@ def _merge_module_members(
def _load_package_modules( def _load_package_modules(
package_directory: Union[str, Path] package_directory: Union[str, Path],
submodule: Optional[str] = None
) -> Dict[str, ModuleMembers]: ) -> Dict[str, ModuleMembers]:
"""Recursively load modules of a package based on the file system. """Recursively load modules of a package based on the file system.
@ -131,6 +133,7 @@ def _load_package_modules(
Parameters: Parameters:
package_directory: Path to the package directory. package_directory: Path to the package directory.
submodule: Optional name of submodule to load.
Returns: Returns:
list: A list of loaded module objects. list: A list of loaded module objects.
@ -142,8 +145,13 @@ def _load_package_modules(
) )
modules_by_namespace = {} modules_by_namespace = {}
# Get the high level package name
package_name = package_path.name package_name = package_path.name
# If we are loading a submodule, add it in
if submodule is not None:
package_path = package_path / submodule
for file_path in package_path.rglob("*.py"): for file_path in package_path.rglob("*.py"):
if file_path.name.startswith("_"): if file_path.name.startswith("_"):
continue continue
@ -160,6 +168,13 @@ def _load_package_modules(
top_namespace = namespace.split(".")[0] top_namespace = namespace.split(".")[0]
try: try:
# 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( module_members = _load_module_members(
f"{package_name}.{namespace}", namespace f"{package_name}.{namespace}", namespace
) )
@ -269,6 +284,9 @@ Functions
def main() -> None: def main() -> None:
"""Generate the reference.rst file for each package.""" """Generate the reference.rst file for each package."""
lc_members = _load_package_modules(PKG_DIR) lc_members = _load_package_modules(PKG_DIR)
# Put tools.render at the top level
tools = _load_package_modules(PKG_DIR, "tools")
lc_members['tools.render'] = tools['render']
lc_doc = ".. _api_reference:\n\n" + _construct_doc("langchain", lc_members) lc_doc = ".. _api_reference:\n\n" + _construct_doc("langchain", lc_members)
with open(WRITE_FILE, "w") as f: with open(WRITE_FILE, "w") as f:
f.write(lc_doc) f.write(lc_doc)

View File

@ -8,10 +8,10 @@ def render_text_description(tools: List[BaseTool]) -> str:
Output will be in the format of: Output will be in the format of:
``` .. code-block:: markdown
search: This tool is used for search search: This tool is used for search
calculator: This tool is used for math calculator: This tool is used for math
```
""" """
return "\n".join([f"{tool.name}: {tool.description}" for tool in tools]) return "\n".join([f"{tool.name}: {tool.description}" for tool in tools])
@ -21,10 +21,11 @@ def render_text_description_and_args(tools: List[BaseTool]) -> str:
Output will be in the format of: Output will be in the format of:
``` .. code-block:: markdown
search: This tool is used for search, args: {"query": {"type": "string"}} search: This tool is used for search, args: {"query": {"type": "string"}}
calculator: This tool is used for math, args: {"expression": {"type": "string"}} calculator: This tool is used for math, \
``` args: {"expression": {"type": "string"}}
""" """
tool_strings = [] tool_strings = []
for tool in tools: for tool in tools: