Skip to content

Commit

Permalink
update the python code
Browse files Browse the repository at this point in the history
  • Loading branch information
Alleria1809 committed May 31, 2024
1 parent 508a092 commit 4849548
Show file tree
Hide file tree
Showing 3 changed files with 93 additions and 135 deletions.
3 changes: 2 additions & 1 deletion docs/source/insert_labels.py
Original file line number Diff line number Diff line change
Expand Up @@ -3,7 +3,7 @@

def add_reference_labels(directory: str):
for filename in os.listdir(directory):
if filename.endswith(".rst"):
if filename.endswith(".rst") and "index" not in filename:
filepath = os.path.join(directory, filename)
with open(filepath, "r+") as file:
content = file.read()
Expand All @@ -19,3 +19,4 @@ def add_reference_labels(directory: str):
add_reference_labels("./source/apis/components")
add_reference_labels("./source/apis/eval")
add_reference_labels("./source/apis/prompts")
add_reference_labels("./source/apis/utils")
36 changes: 36 additions & 0 deletions docs/source/remove_string.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,36 @@
import os

def remove_unwanted_string(directory: str, target_string: str):
"""
Removes the specified string from the end of titles in .rst files within the specified directory.
Parameters:
- directory (str): The path to the directory containing .rst files.
"""
for filename in os.listdir(directory):
if filename.endswith(".rst"):
filepath = os.path.join(directory, filename)
with open(filepath, "r+") as file:
lines = file.readlines()
file.seek(0)
file.truncate()
for line in lines:
# Check if the line ends with 'module' and remove it
if line.strip().endswith(target_string):
line = line.replace(target_string, "")
file.write(line)

if __name__ == "__main__":
# Specify the directory or directories you want to process
directories = [
"./source/apis/core",
"./source/apis/components",
"./source/apis/utils",
"./source/apis/prompts",
"./source/apis/eval",
]
for directory in directories:
remove_unwanted_string(directory, 'module')
remove_unwanted_string(directory, 'package')
remove_unwanted_string(directory, 'Module contents')

189 changes: 55 additions & 134 deletions docs/source/resources/contributing.rst
Original file line number Diff line number Diff line change
@@ -1,194 +1,115 @@
Contributing
===============================================

Content Overview
----------------
.. contents::
:local:
:depth: 2

- `How the Documentation Works`_
- `Setup`_
- `File Structure`_
- `How to Edit the Documentation`_
.. _Writing documentation:

.. _How the Documentation Works:

How the Documentation Works
Writing Documentation
---------------------------

We use `Sphinx <https://www.sphinx-doc.org/en/master/>`_ as the documentation tool and `reStructuredText <https://www.sphinx-doc.org/en/master/usage/restructuredtext/basics.html>`_ as the language. Sphinx primarily reads configurations from a Python script (``conf.py``), pulls documentation from comments in the code (via the ``autodoc`` extension), and organizes content through its table of contents hierarchy defined in ``.rst`` files.

.. _Setup:

Setup
-----

**1. Clone the Github Project**

.. code-block:: bash
git clone https://github.com/SylphAI-Inc/LightRAG.git
- **User-Facing Documentation**: Found on the main docs site. These include tutorials, guides, and usage documentation meant for end users.
- **Developer Documentation**: Located within the repository's READMEs and the ``docs/`` directory. These documents are more technical and intended for contributors and maintainers.

**2. Install Necessary Packages**
This section is about user-facing documentation.

.. code-block:: bash
LightRAG uses `Sphinx <https://www.sphinx-doc.org/en/master/>`_ for documentation, leveraging both `reStructuredText <https://www.sphinx-doc.org/en/master/usage/restructuredtext/basics.html>`_ and Sphinx's `autodoc <https://www.sphinx-doc.org/en/master/usage/extensions/autodoc.html>`_ feature to pull docstrings from code and organize them through ``.rst`` files. Our documentation is split into:

pip install sphinx sphinx-rtd-theme
Souce Code Docstring Standard
~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
Sphinx automatically pulls docstrings from source code and uses them as the docs in API reference. For clarity and consistency, we have a standard for all the code contributors.

**3. Build the Documentation**
Aligning with Pytorch, LightRAG uses the `Google style with Sphinx <https://www.sphinx-doc.org/en/master/usage/extensions/example_google.html>`_ for formatting docstrings `(detailed styles) <https://google.github.io/styleguide/pyguide.html>`_, emphasizing **docstring** and **type control** to guarantee the document and code quality.

.. code-block:: python
cd docs
make html

**4. View the Documentation**
Setup & Build Documentation
~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~

After building the documentation, you can use any browser to view it by opening the ``index.html`` file located in ``docs/build/html``.

Some browsers restrict loading local resources like CSS for security reasons. In this case, try to use a local server to serve the file in the ``build/html`` directory. Run the following commands:
**1. Clone the GitHub Project**

.. code-block:: bash
cd docs/build/html
python -m http.server # Run this in your build/html directory
You will find the port shown in the terminal, e.g., ``Serving HTTP on :: port 8000 (http://[::]:8000/) ...``. In this case, you should open `http://127.0.0.1:8000/` in your browser and you will see the documentation.

.. _File Structure:

File Structure
--------------

**conf.py**

The ``docs/source/conf.py`` controls the configurations used by Sphinx to build the documentation, including the project-related information, `sphinx extensions <https://www.sphinx-doc.org/en/master/usage/extensions/index.html>`_, templates configuration, html theme, patterns to exclude, language configuration, project path setup, etc.

**index.rst**
git clone https://github.com/SylphAI-Inc/LightRAG.git
The ``docs/source/index.rst`` is the root document for Sphinx-generated documentation ("homepage" for the documentation site). It includes the ``toctree`` that defines the documentation hierarchical structure (sections/chapters). It also links to other ``.rst`` files that users can navigate through.
**2. Install Necessary Packages**

For example, in the ``index.rst``, the ``:caption: Get Started`` corresponds to the section name of the documentation site. ``installation`` and ``introduction`` are the detailed pages.
LightRAG's documentation style is `pydata_sphinx_theme <https://pydata-sphinx-theme.readthedocs.io/en/stable/>`_ (version: 0.15.2).

.. code-block:: rst
Install by ``pip``:

What is LightRAG?
=================
LightRAG comes from the best of AI research and engineering...
.. code-block:: bash
.. toctree::
:glob:
:maxdepth: 1
:caption: Get Started
cd docs
pip install -r requirements.txt
get_started/installation
get_started/introduction
Install by ``poetry`` along with all other dependencies for LightRAG:

**Existing Sections**
.. code-block:: bash
Existing sections include:
poetry install
- ``get_started/``: Includes installation and LightRAG introduction
- ``tutorials/``: Includes sample code and instructions
- ``apis/``: All the source-code-related documents will be included in this directory
- ``resources/``: Include all the LightRAG-relevant resources.
**3. Build the Documentation**

.. _How to Edit the Documentation:
.. code-block:: bash
How to Edit the Documentation
-----------------------------
cd docs
make html
Most of the documentation updates should be written as comments/doc-strings in your source code, which will be automatically converted to docs. Do manual editing when you add instructions to use your code, adjust the layout, etc.
The existing documentation is a combination of automatic generation and human editing.
**conf.py**

**Source Code Doc-string Update**
This file (``docs/source/conf.py``) contains configurations used by Sphinx, including extensions, templates, HTML theme, and language settings.

The ``autodoc`` extension in ``conf.py`` combined with ``.. automodule::`` in the ``.rst`` files makes it easy to update documents from the source code.
**Source Code Doc-string**

If you update the existing source code, you only need to run:
Follow `Google style docstrings <https://www.sphinx-doc.org/en/master/usage/extensions/example_google.html>`_ to update your source code docstrings. Limit lines to **80** characters for better readability in various environments.

.. code-block:: python
**RST Files**: Directly edit ``.rst`` files for broader changes or new sections. Use the ``.. toctree::`` directive to link documents.

cd docs
make clean
make html
The ``.rst`` files are in the ``docs/source``. The majority of ``.rst`` files in the ``docs/source/apis`` are generated automatically from the Python code docstrings using ``sphinx-apidoc``.

And your documentation will be updated.
To shorten the doc generating process, please remove the files that is not included in your project.

**Add New Code**
The Sphinx build will show warnings but the docs will still be completed.

If you add new modules or code to the project, sphinx has a `command <https://www.sphinx-doc.org/en/master/man/sphinx-apidoc.html#sphinx-apidoc>`_ to automatically generate the code docs.
If you have a module folder containing code, for example, ``components/``, please add the following line to the ``docs/Makefile`` in the ``apidoc:`` section.

.. code-block:: bash
@sphinx-apidoc -o $(APIDOCOUTDIR)/components ../components --separate --force
sphinx-apidoc [OPTIONS] -o <OUTPUT_PATH> <MODULE_PATH> [EXCLUDE_PATTERN …]
.. note::
If your new module is a folder, it should contain a ``__init__.py`` file.

Remember to exclude the code that you don’t need in the [EXCLUDE_PATTERN …], otherwise Sphinx will compile them all.
Example:
**4. View the Documentation Locally**

Located in the root directory, run:
After building, open ``docs/build/html/index.html`` in a web browser. If you face issues with local resources, such as the browser prohibits loading the web page correctly, run a local server:

.. code-block:: bash
sphinx-apidoc -o docs/source/tutorials ./use_cases **test**
(*test* is to exclude the files containing ``test`` in the filename)

cd docs/build/html
python -m http.server 8000 <path_to_html_output>
You will find a ``modules.rst`` and a ``use_cases.rst`` in the ``docs/source/tutorials``. The ``use_cases.rst`` contains all the packages included in your ``./use_cases``.
Then navigate to the corresbonding site in your browser.

Then you should add the link to the ``index.rst`` to show your source code and docs in the documentation. Find ``docs/source/index.rst`` and add the new section:

.. code-block:: rst

.. toctree::
:glob:
:maxdepth: 1
:caption: Use Cases
Adding Documentation Tests
~~~~~~~~~~~~~~~~~~~~~~~~~~~~~

tutorials/use_cases
To ensure the documentation remains up-to-date, LightRAG uses Sphinx's Doctest extension. Add ``.. testcode::`` to your ``.rst`` files or docstrings and run ``make doctest`` to test your documentation snippets.

Then run:
To manually run these tests, run:

.. code-block:: python
.. code-block:: bash
cd docs
make clean
make html
make doctest
And you will be able to find the newly added use cases module.
**Add New Docs**

If you want to add any written files such as README.md to the documentation, there is an easy way to transform the files to ``.rst`` files using `Pandoc`.

- First, install Pandoc with Homebrew:

.. code-block:: bash
brew install pandoc
- Then run `pandoc -s <input .md file> -o <path/to/target_rst_file>`. For example, in the root directory run:

.. code-block:: bash
pandoc -s README.md -o docs/source/get_started/introduction.rst
This command will take content from ``README.md`` and create an ``introduction.rst`` file in the specified directory.

After editing, run:

.. code-block:: python
cd docs
make clean
make html
**Commit the Edited Documentation**
Commit Changes
~~~~~~~~~~~~~~~~~~~~~~~~~

Remember to exclude any unnecessary files in ``.gitignore``. Please don’t commit files in ``docs/build``. We can dynamically build local documentation with the make files and ``source/``.
After making changes, commit the ``.rst`` and source files, avoiding the ``docs/build`` directory, and push them to your GitHub fork for review.

Please push your updates to the GitHub repo.

0 comments on commit 4849548

Please sign in to comment.