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/base_data_class.rst
+1-1
Original file line number
Diff line number
Diff line change
@@ -300,7 +300,7 @@ The ``exclude`` parameter works the same across all methods.
300
300
301
301
**DataClassFormatType**
302
302
303
-
For data class format, we have :class:``core.base_data_class.DataClassFormatType`` along with ``format_class_str`` method to specify the format type for the data format methods.
303
+
For data class format, we have :class:`DataClassFormatType<core.base_data_class.DataClassFormatType>` along with ``format_class_str`` method to specify the format type for the data format methods.
Copy file name to clipboardexpand all lines: docs/source/developer_notes/index.rst
+5
Original file line number
Diff line number
Diff line change
@@ -52,6 +52,11 @@ A `Prompt` will work with `DataClass` to ease data interaction with the LLM mode
52
52
A `Retriever` will work with databases to retrieve context and overcome the hallucination and knowledge limitations of LLM, following the paradigm of Retrieval-Augmented Generation (RAG).
53
53
An `Agent` will work with tools and an LLM planner for enhanced ability to reason, plan, and act on real-world tasks.
54
54
55
+
56
+
Additionally, what shines in LightRAG is that all orchestrator components, like `Retriever`, `Embedder`, `Generator`, and `Agent`, are model-agnostic.
57
+
You can easily make each component work with different models from different providers by switching out the `ModelClient` and its `model_kwargs`.
58
+
59
+
55
60
We will introduce the libraries starting from the core base classes, then move to the RAG essentials, and finally to the agent essentials.
56
61
With these building blocks, we will further introduce optimizing, where the optimizer uses building blocks such as Generator for auto-prompting and retriever for dynamic few-shot in-context learning (ICL).
Copy file name to clipboardexpand all lines: docs/source/developer_notes/model_client.rst
+54-147
Original file line number
Diff line number
Diff line change
@@ -6,32 +6,37 @@ ModelClient
6
6
7
7
.. `Li Yin <https://github.com/liyin2015>`_
8
8
9
-
What you will learn?
9
+
..What you will learn?
10
10
11
-
1. What is ``ModelClient`` and why is it designed this way?
12
-
2. How to intergrate your own ``ModelClient``?
13
-
3. How to use ``ModelClient`` directly?
11
+
.. 1. What is ``ModelClient`` and why is it designed this way?
12
+
.. 2. How to intergrate your own ``ModelClient``?
13
+
.. 3. How to use ``ModelClient`` directly?
14
+
15
+
16
+
:ref:`ModelClient<core-model_client>` is the standardized protocol and base class for all model inference SDKs (either via APIs or local) to communicate with LightRAG internal components.
17
+
Therefore, by switching out the ``ModelClient`` in a ``Generator``, ``Embedder``, or ``Retriever`` (those components that take models), you can make these functional components model-agnostic.
14
18
15
-
:ref:`ModelClient<core-model_client>` is the standardized protocol and base class for all model inference SDKs (either via APIs or local) to communicate with LightRAG internal components/classes.
16
-
Because so, by switching off ``ModelClient`` in a ``Generator`` or ``Embedder`` component, you can make your prompt or ``Retriever`` model-agnostic.
17
19
18
20
19
21
.. figure:: /_static/images/model_client.png
20
22
:align:center
21
23
:alt:ModelClient
22
24
:width:400px
23
25
24
-
The interface to internal components in LightRAG
26
+
The bridge between all model inference SDKs and internal components in LightRAG
25
27
26
28
.. note::
27
29
28
-
All users are encouraged to customize your own ``ModelClient`` whenever you need to do so. You can refer our code in ``components.model_client`` dir.
30
+
All users are encouraged to customize their own ``ModelClient`` whenever needed. You can refer to our code in ``components.model_client`` directory.
31
+
29
32
30
33
Model Inference SDKs
31
34
------------------------
32
-
With cloud API providers like OpenAI, Groq, Anthropic, it often comes with a `sync` and an `async` client via their SDKs.
35
+
36
+
With cloud API providers like OpenAI, Groq, and Anthropic, it often comes with a `sync` and an `async` client via their SDKs.
For local models, such as using `huggingface transformers`, you need to create this model inference SDKs yourself.
46
-
How you do this is highly flexible. Here is an example to use local embedding model (e.g. ``thenlper/gte-base``) as a model (Refer :class:`components.model_client.transformers_client.TransformerEmbedder` for details).
50
+
For local models, such as using `huggingface transformers`, you need to create these model inference SDKs yourself.
51
+
How you do this is highly flexible.
52
+
Here is an example of using a local embedding model (e.g., ``thenlper/gte-base``) as a model (Refer to :class:`TransformerEmbedder<components.model_client.transformers_client.TransformerEmbedder>` for details).
A model client can be used to manage different types of models, we defined a ``ModelType`` to categorize the model type.
60
+
A model client can be used to manage different types of models, we defined a :class:`ModelType<core.types.ModelType>` to categorize the model type.
118
61
119
62
.. code-block:: python
120
63
121
64
classModelType(Enum):
122
65
EMBEDDER= auto()
123
66
LLM= auto()
67
+
RERANKER= auto()
124
68
UNDEFINED= auto()
125
69
126
-
We designed 6 abstract methods in the ``ModelClient`` class to be implemented by the subclass model type.
127
-
We will use :class:`components.model_client.OpenAIClient` along with the above ``TransformerEmbedder`` as examples.
128
-
129
-
First, we offer two methods to initialize the model SDKs:
130
-
131
-
.. code-block:: python
132
-
133
-
definit_sync_client(self):
134
-
raiseNotImplementedError(
135
-
f"{type(self).__name__} must implement _init_sync_client method"
136
-
)
137
-
138
-
definit_async_client(self):
139
-
raiseNotImplementedError(
140
-
f"{type(self).__name__} must implement _init_async_client method"
141
-
)
70
+
We designed 6 abstract methods in the `ModelClient` class that can be implemented by subclasses to integrate with different model inference SDKs.
71
+
We will use :class:`OpenAIClient<components.model_client.OpenAIClient>` as the cloud API example and :class:`TransformersClient<components.model_client.transformers_client.TransformersClient>` along with the local inference code :class:`TransformerEmbedder<components.model_client.transformers_client.TransformerEmbedder>` as an example for local model clients.
142
72
143
-
This is how `OpenAIClient` implements these methods along with ``__init__`` method:
144
-
145
-
.. code-block:: python
146
73
147
-
classOpenAIClient(ModelClient):
148
-
149
-
def__init__(self, api_key: Optional[str] =None):
150
-
151
-
super().__init__()
152
-
self._api_key = api_key
153
-
self.sync_client =self.init_sync_client()
154
-
self.async_client =None# only initialize if the async call is called
155
-
156
-
definit_sync_client(self):
157
-
api_key =self._api_key or os.getenv("OPENAI_API_KEY")
158
-
ifnot api_key:
159
-
raiseValueError("Environment variable OPENAI_API_KEY must be set")
160
-
return OpenAI(api_key=api_key)
161
-
162
-
definit_async_client(self):
163
-
api_key =self._api_key or os.getenv("OPENAI_API_KEY")
164
-
ifnot api_key:
165
-
raiseValueError("Environment variable OPENAI_API_KEY must be set")
166
-
return AsyncOpenAI(api_key=api_key)
74
+
First, we offer two methods, `init_async_client` and `init_sync_client`, for subclasses to initialize the SDK client.
75
+
You can refer to :class:`OpenAIClient<components.model_client.OpenAIClient>` to see how these methods, along with the `__init__` method, are implemented:
167
76
168
77
This is how ``TransformerClient`` does the same thing:
169
78
@@ -183,8 +92,7 @@ This is how ``TransformerClient`` does the same thing:
183
92
definit_sync_client(self):
184
93
return TransformerEmbedder()
185
94
186
-
187
-
Second. we use `convert_inputs_to_api_kwargs` for subclass to convert LightRAG inputs into the `api_kwargs` (SDKs arguments).
95
+
Second, we use `convert_inputs_to_api_kwargs` for subclasses to convert LightRAG inputs into the `api_kwargs` (SDK arguments).
188
96
189
97
.. code-block:: python
190
98
@@ -228,6 +136,15 @@ This is how `OpenAIClient` implements this method:
228
136
raiseValueError(f"model_type {model_type} is not supported")
229
137
return final_model_kwargs
230
138
139
+
.. For embedding, as `Embedder` takes both `str` and `List[str]` as input, we need to convert the input to a list of strings.
140
+
.. For LLM, as `Generator` takes a `prompt_kwargs` (dict) and converts it into a single string, we need to convert the input to a list of messages.
141
+
.. For Rerankers, you can refer to :class:`CohereAPIClient<components.model_client.cohere_client.CohereAPIClient>` for an example.
142
+
143
+
144
+
For embedding, as ``Embedder`` takes both `str` and `List[str]` as input, we need to convert the input to a list of strings that is acceptable by the SDK.
145
+
For LLM, as ``Generator`` will takes a `prompt_kwargs`(dict) and convert it into a single string, thus we need to convert the input to a list of messages.
146
+
For Rerankers, you can refer to :class:`CohereAPIClient<components.model_client.cohere_client.CohereAPIClient>` for an example.
147
+
231
148
This is how ``TransformerClient`` does the same thing:
232
149
233
150
.. code-block:: python
@@ -245,37 +162,15 @@ This is how ``TransformerClient`` does the same thing:
245
162
else:
246
163
raiseValueError(f"model_type {model_type} is not supported")
247
164
248
-
In addition, you can add any method that parse the SDK specific output to a format compatible with LightRAG components.
249
-
Typically an LLM needs to use `parse_chat_completion` to parse the completion to texts and `parse_embedding_response` to parse the embedding response to a structure LightRAG components can understand.
f"{type(self).__name__} must implement parse_chat_completion method"
257
-
)
166
+
In addition, you can add any method that parses the SDK-specific output to a format compatible with LightRAG components.
167
+
Typically, an LLM needs to use `parse_chat_completion` to parse the completion to text and `parse_embedding_response` to parse the embedding response to a structure that LightRAG components can understand.
168
+
You can refer to :class:`OpenAIClient<components.model_client.openai_client.OpenAIClient>` for API embedding model integration and :class:`TransformersClient<components.model_client.transformers_client.TransformersClient>` for local embedding model integration.
r"""Parse the embedding response to a structure LightRAG components can understand."""
261
-
raiseNotImplementedError(
262
-
f"{type(self).__name__} must implement parse_embedding_response method"
263
-
)
264
170
265
-
You can refer to :class:`components.model_client.openai_client.OpenAIClient` for API embedding model integration and :class:`components.model_client.transformers_client.TransformersClient` for local embedding model integration.
171
+
Lastly, the `call` and `acall` methods are used to call model inference via their own arguments.
172
+
We encourage subclasses to provide error handling and retry mechanisms in these methods.
266
173
267
-
Then `call` and `acall` methods to call Model inference via their own arguments.
268
-
We encourage the subclass provides error handling and retry mechanism in these methods.
Though ``ModelClient`` is often managed in a ``Generator`` or ``Embedder`` component, you can use it directly if you ever plan to write your own component.
306
-
Here is an example to use ``OpenAIClient`` directly, first on LLM model:
202
+
203
+
204
+
Though ``ModelClient`` is often managed in a ``Generator``, ``Embedder``, or ``Retriever`` component, you can use it directly if you plan to write your own component.
205
+
Here is an example of using ``OpenAIClient`` directly, first on an LLM model:
206
+
307
207
308
208
.. code-block:: python
309
209
310
210
from lightrag.components.model_client import OpenAIClient
311
211
from lightrag.core.types import ModelType
312
212
from lightrag.utils import setup_env
313
213
214
+
setup_env()
215
+
314
216
openai_client = OpenAIClient()
315
217
316
218
query ="What is the capital of France?"
@@ -361,6 +263,10 @@ The output will be:
361
263
api_kwargs: {'model': 'text-embedding-3-small', 'dimensions': 8, 'encoding_format': 'float', 'input': ['What is the capital of France?', 'What is the capital of France?']}
0 commit comments