Skip to content

Commit 4cc0106

Browse files
committed
simple editing of component tutorial
1 parent c1e9e40 commit 4cc0106

File tree

1 file changed

+82
-39
lines changed

1 file changed

+82
-39
lines changed

Diff for: docs/source/developer_notes/component.rst

+82-39
Original file line numberDiff line numberDiff line change
@@ -13,27 +13,23 @@ Component
1313
1414
1515
:ref:`Component<core-component>` is to LLM task pipelines what `nn.Module` is to PyTorch models.
16-
An LLM task pipeline in LightRAG consists of components, be it a `Prompt`, `ModelClient`, `Generator`, `Retriever`, `Agent`, or any other custom components.
17-
This pipeline can be ``Sequential`` or a Directed Acyclic Graph (DAG) of components.
18-
`Prompt` will work with ``DataClass`` to ease the data interaction with the LLM model.
19-
`Retriever` will work with database to retrieve context to overcome the halluciation and knowledge limitation of LLM, the paradigm of Retrieval-Augmented Generation (RAG).
20-
`Agent` will work with tools and LLM planner for enhanced ability to reason, plan, and to act real-world tasks.
21-
16+
It is the base class for components such as ``Prompt``, ``ModelClient``, ``Generator``, ``Retriever`` in LightRAG.
17+
Your task pipeline should also subclass from ``Component``.
2218

2319

2420

2521
Design
2622
---------------------------------------
2723

28-
It is the base class for components, such as ``Prompt``, ``ModelClient``, ``Generator``, ``Retriever`` in LightRAG.
29-
Your task pipeline should subclass from ``Component`` too. Instead of working with ``Tensor`` and ``Parameter`` to train models with weights and biases, our component works with any data, ``Parameter`` that can be any data type for LLM in-context learning, from manual to auto prompt engineering.
30-
We name it differently to avoid confusion and also for better compatibility with `PyTorch`.
24+
Different from PyTorch's nn.Module, which works exclusively with Tensor and Parameter to train models with weights and biases, our component can work with different types of data, from a string or a list of strings to a list of :class:`Document<core.types.Document>`.
25+
26+
.. `Parameter` that can be any data type for LLM in-context learning, from manual to auto prompt engineering.
3127
3228
29+
Here is the comparison of writing a PyTorch model and a LightRAG task pipeline.
3330

34-
Here is the comparison of writing a PyTorch model and a LightRAG task component.
3531

36-
.. grid:: 2
32+
.. grid:: 1
3733
:gutter: 1
3834

3935
.. grid-item-card:: PyTorch
@@ -75,28 +71,49 @@ Here is the comparison of writing a PyTorch model and a LightRAG task component.
7571
def call(self, query: str) -> str:
7672
return self.doc(prompt_kwargs={"input_str": query}).data
7773
74+
As the fundamental building block in LLM task pipelines, the component is designed to serve five main purposes:
75+
76+
1. **Standardize the interface for all components.**
77+
This includes the `__init__` method, the `call` method for synchronous calls, the `acall` method for asynchronous calls, and the `__call__` method, which by default calls the `call` method.
7878

79-
As the foundamental building block in LLM task pipeline, the component is designed to serve five main purposes:
79+
2. **Provide a unified way to visualize the structure of the task pipeline**
80+
via the `__repr__` method. Subclasses can additionally add the `_extra_repr` method to include more information than the default `__repr__` method.
8081

81-
1. **Standarize the interface for all components.** This includes the `__init__` method, the `call` method for synchronous call, the `acall` method for asynchronous call, and the `__call__` which in default calls the `call` method.
82-
2. **Provide a unified way to visualize the structure of the task pipeline** via `__repr__` method. And subclass can additional add `_extra_repr` method to add more information than the default `__repr__` method.
83-
3. **Tracks, adds all subcomponents and parameters automatically and recursively** to assistant the building and optimizing process of the task pipeline.
84-
4. **Manages the states and serialization**, with `state_dict` and `load_state_dict` methods in particular for parameters and `to_dict` method for serialization of all the states fall into the component's attributes, from subcomponents to parameters, to any other attributes of various data type.
85-
5. **Make all components configurable from using `json` or `yaml` files**. This is especially useful for experimenting or building data processing pipelines.
82+
3. **Track and add all subcomponents and parameters automatically and recursively**
83+
to assist in the building and optimizing process of the task pipeline.
8684

87-
These features are key to keep LightRAG pipeline transparent, flexible, and easy to use.
85+
4. **Manage the states and serialization**,
86+
with `state_dict` and `load_state_dict` methods specifically for parameters, and the `to_dict` method for serialization of all states within the component's attributes, from subcomponents to parameters, to any other attributes of various data types.
87+
88+
5. **Make all components configurable using `json` or `yaml` files**.
89+
This is especially useful for experimenting or building data processing pipelines.
90+
91+
These features are key to keeping the LightRAG pipeline transparent, flexible, and easy to use.
8892
By subclassing from the `Component` class, you will get most of these features out of the box.
8993

9094

95+
.. As the foundamental building block in LLM task pipeline, the component is designed to serve five main purposes:
96+
97+
.. 1. **Standarize the interface for all components.** This includes the `__init__` method, the `call` method for synchronous call, the `acall` method for asynchronous call, and the `__call__` which in default calls the `call` method.
98+
.. 2. **Provide a unified way to visualize the structure of the task pipeline** via `__repr__` method. And subclass can additional add `_extra_repr` method to add more information than the default `__repr__` method.
99+
.. 3. **Tracks, adds all subcomponents and parameters automatically and recursively** to assistant the building and optimizing process of the task pipeline.
100+
.. 4. **Manages the states and serialization**, with `state_dict` and `load_state_dict` methods in particular for parameters and `to_dict` method for serialization of all the states fall into the component's attributes, from subcomponents to parameters, to any other attributes of various data type.
101+
.. 5. **Make all components configurable from using `json` or `yaml` files**. This is especially useful for experimenting or building data processing pipelines.
102+
103+
.. These features are key to keep LightRAG pipeline transparent, flexible, and easy to use.
104+
.. By subclassing from the `Component` class, you will get most of these features out of the box.
105+
106+
91107
Component in Action
92108
---------------------------------------
93109

94-
.. Transparency
95-
.. ~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
110+
111+
96112

97113
In this note, we are creating an AI doctor to answer medical questions.
98114
Run the ``DocQA`` on a query:
99115

116+
100117
.. code-block:: python
101118
102119
doc = DocQA()
@@ -143,6 +160,7 @@ Configure from file
143160
As the above example shows, we added subcomponent via attributes.
144161
We can also use methods to add more subcomponnents or parameters.
145162

163+
146164
.. code-block:: python
147165
148166
from lightrag.core.parameter import Parameter
@@ -151,8 +169,12 @@ We can also use methods to add more subcomponnents or parameters.
151169
# list all parameters
152170
for param in doc.named_parameters():
153171
print(param)
154-
# output
155-
# ('demo', Parameter: demo)
172+
173+
The output:
174+
175+
.. code-block::
176+
177+
('demo', Parameter: demo)
156178
157179
You can easily save the detailed states:
158180

@@ -162,21 +184,25 @@ You can easily save the detailed states:
162184
163185
save_json(doc.to_dict(), "doc.json")
164186
187+
To add even more flexibility, we provide :class:`FunComponent<core.component.FunComponent>` and :class:`Sequential<core.container.Sequential>` for more advanced use cases.
165188

166-
To adds even more flexibility, we provide :class:`core.component.FunComponent` and :class:`core.component.Sequential` for more advanced use cases.
167189

168190

169191
Searalization and deserialization
170192
~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
171193

172-
We provide ``is_pickable`` method to check if the component is pickable.
173-
And any of your component, it is a good practise to ensure it is pickable.
194+
We provide the ``is_pickable`` method to check if the component is pickable.
195+
It is good practice to ensure that any of your components are pickable.
196+
197+
198+
199+
174200

175201
FunComponent
176202
--------------
177-
Use :func:`core.component.fun_to_component` as a decorator to convert any function to a Component with its unique class name.
203+
Use :func:`fun_to_component<core.component.fun_to_component>` as a decorator to convert any function to a Component with its unique class name.
178204

179-
:class:`core.component.FunComponent` is a subclass of :class:`core.component.Component` that allows you to define a component with a function.
205+
:class:`FunComponent<core.component.FunComponent>` is a subclass of :class:`Component<core.component.Component>` that allows you to define a component with a function.
180206
You can directly use this class as:
181207

182208
.. code-block:: python
@@ -190,16 +216,21 @@ You can directly use this class as:
190216
print(fun_component(1))
191217
print(type(fun_component))
192218
193-
# output:
194-
# 2
195-
# <class 'core.component.FunComponent'>
219+
The printout:
220+
221+
.. code-block::
222+
223+
2
224+
<class 'core.component.FunComponent'>
196225
197226
198-
We also have :func:`core.component.fun_to_component` to convert a function to a FunComponent via decorator or directly call the function.
227+
228+
We also have :func:`fun_to_component<core.component.fun_to_component>` to convert a function to a `FunComponent` via a decorator or by directly calling the function.
199229
This approach gives you a unique component converted from the function name.
200230

201231
Via direct call:
202232

233+
203234
.. code-block:: python
204235
205236
from lightrag.core.component import fun_to_component
@@ -208,12 +239,17 @@ Via direct call:
208239
print(fun_component(1))
209240
print(type(fun_component))
210241
211-
# output:
212-
# 2
213-
# <class 'lightrag.core.component.AddOneComponent'>
242+
The output:
243+
244+
.. code-block::
245+
246+
2
247+
<class 'lightrag.core.component.AddOneComponent'>
214248
215249
216-
Via decorator will be even more convenient to have a component from a function:
250+
251+
252+
Using a decorator is an even more convenient way to create a component from a function:
217253

218254
.. code-block:: python
219255
@@ -230,8 +266,12 @@ Via decorator will be even more convenient to have a component from a function:
230266
231267
Sequential
232268
--------------
233-
We have :class:`core.component.Sequential` class to PyTorch's ``nn.Sequential`` class. This is especially useful to chain together components in a sequence. Much like the concept of ``chain`` or ``pipeline`` in other LLM libraries.
234-
Let's put the FunComponent and DocQA together in a sequence:
269+
270+
271+
272+
We have the :class:`Sequential<core.container.Sequential>` class, which is similar to PyTorch's ``nn.Sequential`` class.
273+
This is especially useful for chaining together components in a sequence, much like the concept of ``chain`` or ``pipeline`` in other LLM libraries.
274+
Let's put the `FunComponent`` and `DocQA`` together in a sequence:
235275

236276
.. code-block:: python
237277
@@ -246,9 +286,12 @@ Let's put the FunComponent and DocQA together in a sequence:
246286
query = "What is the best treatment for headache?"
247287
print(seq(query))
248288
249-
We automatically enhance users' queries before passing them to the DocQA component.
289+
We automatically enhance users' queries before passing them to the `DocQA` component.
250290
The output is:
251291

292+
293+
294+
252295
.. code-block::
253296
254297
1. Over-the-counter pain relievers like acetaminophen, ibuprofen, or aspirin
@@ -279,4 +322,4 @@ The structure of the sequence using ``print(seq)``:
279322
- :func:`core.component.fun_to_component`
280323

281324

282-
We will have more advanced use cases in the upcoming tutorials.
325+
We will cover more advanced use cases in the upcoming tutorials.

0 commit comments

Comments
 (0)