From 306635d2b70b662c38f10697c62063ca684108dd Mon Sep 17 00:00:00 2001 From: William Fu-Hinthorn <13333726+hinthornw@users.noreply.github.com> Date: Tue, 25 Jul 2023 17:46:13 -0700 Subject: [PATCH] attempt --- docs/api_reference/conf.py | 8 +++++++- docs/docs_skeleton/link_generator.py | 11 +++++++---- 2 files changed, 14 insertions(+), 5 deletions(-) diff --git a/docs/api_reference/conf.py b/docs/api_reference/conf.py index 35b8212d0a4..6928da19c02 100644 --- a/docs/api_reference/conf.py +++ b/docs/api_reference/conf.py @@ -13,14 +13,19 @@ # import os import sys +import json +from pathlib import Path import toml +_DIR = Path(__file__).parent.absolute() sys.path.insert(0, os.path.abspath(".")) sys.path.insert(0, os.path.abspath("../../libs/langchain")) -with open("../../libs/langchain/pyproject.toml") as f: +with (_DIR.parents[1] / "libs" / "langchain" / "pyproject.toml").open("r") as f: data = toml.load(f) +with (_DIR / "example_imports.json").open("r") as f: + imported_classes = json.load(f) # -- Project information ----------------------------------------------------- @@ -105,6 +110,7 @@ html_context = { "github_version": "master", # Version "conf_py_path": "/docs/api_reference", # Path in the checkout to the docs root "redirects": redirects, + "imported_classes": imported_classes } # Add any paths that contain custom static files (such as style sheets) here, diff --git a/docs/docs_skeleton/link_generator.py b/docs/docs_skeleton/link_generator.py index 1917393ed3a..0936f00cbff 100644 --- a/docs/docs_skeleton/link_generator.py +++ b/docs/docs_skeleton/link_generator.py @@ -16,12 +16,10 @@ code_block_re = re.compile(r"^(```python\n)(.*?)(```\n)", re.DOTALL | re.MULTILI # Regular expression to match langchain import lines _IMPORT_RE = re.compile(r"(from\s+(langchain\.\w+(\.\w+)*?)\s+import\s+)(\w+)") -_CODEBLOCK_PATH = "src/theme/CodeBlock/" _CURRENT_PATH = Path(__file__).parent.absolute() # Directory where generated markdown files are stored _DOCS_DIR = _CURRENT_PATH / "docs" -# Will dump to the codeblock directory / imports.json -_JSON_PATH = _CURRENT_PATH / _CODEBLOCK_PATH / "imports.json" +_JSON_PATH = _CURRENT_PATH.parent / 'api_reference' / "example_imports.json" def find_files(path): @@ -52,7 +50,12 @@ def main(): if file_imports: # Use relative file path as key relative_path = os.path.relpath(file, _DOCS_DIR) - global_imports[relative_path] = file_imports + doc_url = f"https://python.langchain.com/docs/{relative_path.replace('.mdx', '').replace('.md', '')}" + for import_info in file_imports: + class_name = import_info["imported"] + if class_name not in global_imports: + global_imports[class_name] = [] + global_imports[class_name].append(doc_url) # Write the global imports information to a JSON file with _JSON_PATH.open("w") as f: