You signed in with another tab or window. Reload to refresh your session.You signed out in another tab or window. Reload to refresh your session.You switched accounts on another tab or window. Reload to refresh your session.Dismiss alert
Copy file name to clipboardExpand all lines: docs/source/developer_notes/component.rst
+82-39
Original file line number
Diff line number
Diff line change
@@ -13,27 +13,23 @@ Component
13
13
14
14
15
15
: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``.
22
18
23
19
24
20
25
21
Design
26
22
---------------------------------------
27
23
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.
31
27
32
28
29
+
Here is the comparison of writing a PyTorch model and a LightRAG task pipeline.
33
30
34
-
Here is the comparison of writing a PyTorch model and a LightRAG task component.
35
31
36
-
.. grid:: 2
32
+
.. grid:: 1
37
33
:gutter: 1
38
34
39
35
.. grid-item-card:: PyTorch
@@ -75,28 +71,49 @@ Here is the comparison of writing a PyTorch model and a LightRAG task component.
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.
78
78
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.
80
81
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.
86
84
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.
88
92
By subclassing from the `Component` class, you will get most of these features out of the box.
89
93
90
94
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
+
91
107
Component in Action
92
108
---------------------------------------
93
109
94
-
.. Transparency
95
-
.. ~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
110
+
111
+
96
112
97
113
In this note, we are creating an AI doctor to answer medical questions.
98
114
Run the ``DocQA`` on a query:
99
115
116
+
100
117
.. code-block:: python
101
118
102
119
doc = DocQA()
@@ -143,6 +160,7 @@ Configure from file
143
160
As the above example shows, we added subcomponent via attributes.
144
161
We can also use methods to add more subcomponnents or parameters.
145
162
163
+
146
164
.. code-block:: python
147
165
148
166
from lightrag.core.parameter import Parameter
@@ -151,8 +169,12 @@ We can also use methods to add more subcomponnents or parameters.
151
169
# list all parameters
152
170
for param in doc.named_parameters():
153
171
print(param)
154
-
# output
155
-
# ('demo', Parameter: demo)
172
+
173
+
The output:
174
+
175
+
.. code-block::
176
+
177
+
('demo', Parameter: demo)
156
178
157
179
You can easily save the detailed states:
158
180
@@ -162,21 +184,25 @@ You can easily save the detailed states:
162
184
163
185
save_json(doc.to_dict(), "doc.json")
164
186
187
+
To add even more flexibility, we provide :class:`FunComponent<core.component.FunComponent>` and :class:`Sequential<core.container.Sequential>` for more advanced use cases.
165
188
166
-
To adds even more flexibility, we provide :class:`core.component.FunComponent` and :class:`core.component.Sequential` for more advanced use cases.
167
189
168
190
169
191
Searalization and deserialization
170
192
~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
171
193
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
+
174
200
175
201
FunComponent
176
202
--------------
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.
178
204
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.
180
206
You can directly use this class as:
181
207
182
208
.. code-block:: python
@@ -190,16 +216,21 @@ You can directly use this class as:
190
216
print(fun_component(1))
191
217
print(type(fun_component))
192
218
193
-
# output:
194
-
# 2
195
-
# <class 'core.component.FunComponent'>
219
+
The printout:
220
+
221
+
.. code-block::
222
+
223
+
2
224
+
<class 'core.component.FunComponent'>
196
225
197
226
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.
199
229
This approach gives you a unique component converted from the function name.
200
230
201
231
Via direct call:
202
232
233
+
203
234
.. code-block:: python
204
235
205
236
from lightrag.core.component import fun_to_component
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:
217
253
218
254
.. code-block:: python
219
255
@@ -230,8 +266,12 @@ Via decorator will be even more convenient to have a component from a function:
230
266
231
267
Sequential
232
268
--------------
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:
235
275
236
276
.. code-block:: python
237
277
@@ -246,9 +286,12 @@ Let's put the FunComponent and DocQA together in a sequence:
246
286
query ="What is the best treatment for headache?"
247
287
print(seq(query))
248
288
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.
250
290
The output is:
251
291
292
+
293
+
294
+
252
295
.. code-block::
253
296
254
297
1. Over-the-counter pain relievers like acetaminophen, ibuprofen, or aspirin
@@ -279,4 +322,4 @@ The structure of the sequence using ``print(seq)``:
279
322
- :func:`core.component.fun_to_component`
280
323
281
324
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