Tools
lazyllm.tools.Document
Bases: ModuleBase
Initialize a document module with an optional user interface.
This constructor initializes a document module that can have an optional user interface. If the user interface is enabled, it also provides a UI to manage the document operation interface and offers a web page for user interface interaction.
Parameters:
-
dataset_path
(str
) –The path to the dataset directory. This directory should contain the documents to be managed by the document module.
-
embed
(Optional[Union[Callable, Dict[str, Callable]]]
, default:None
) –The object used to generate document embeddings. If you need to generate multiple embeddings for the text, you need to specify multiple embedding models in a dictionary format. The key identifies the name corresponding to the embedding, and the value is the corresponding embedding model.
-
manager
(bool
, default:False
) –A flag indicating whether to create a user interface for the document module. Defaults to False.
-
launcher
(optional
, default:None
) –An object or function responsible for launching the server module. If not provided, the default asynchronous launcher from
lazyllm.launchers
is used (sync=False
). -
store_conf
(optional
, default:None
) –Configure which storage backend and index backend to use.
-
doc_fields
(optional
, default:None
) –Configure the fields that need to be stored and retrieved along with their corresponding types (currently only used by the Milvus backend).
Examples:
>>> import lazyllm
>>> from lazyllm.tools import Document
>>> m = lazyllm.OnlineEmbeddingModule(source="glm")
>>> documents = Document(dataset_path='your_doc_path', embed=m, manager=False) # or documents = Document(dataset_path='your_doc_path', embed={"key": m}, manager=False)
>>> m1 = lazyllm.TrainableModule("bge-large-zh-v1.5").start()
>>> document1 = Document(dataset_path='your_doc_path', embed={"online": m, "local": m1}, manager=False)
>>> store_conf = {
>>> 'type': 'chroma',
>>> 'indices': {
>>> 'smart_embedding_index': {
>>> 'backend': 'milvus',
>>> 'kwargs': {
>>> 'uri': '/tmp/tmp.db',
>>> 'index_kwargs': {
>>> 'index_type': 'HNSW',
>>> 'metric_type': 'COSINE'
>>> }
>>> },
>>> },
>>> },
>>> }
>>> doc_fields = {
>>> 'author': DocField(data_type=DataType.VARCHAR, max_size=128, default_value=' '),
>>> 'public_year': DocField(data_type=DataType.INT32),
>>> }
>>> document2 = Document(dataset_path='your_doc_path', embed={"online": m, "local": m1}, store_conf=store_conf, doc_fields=doc_fields)
Source code in lazyllm/tools/rag/document.py
24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 144 145 146 147 148 149 150 151 152 153 154 155 156 157 158 159 160 161 162 163 164 165 166 167 168 169 170 171 172 173 174 175 176 177 178 179 180 181 182 183 184 185 186 187 188 189 190 191 192 193 194 195 196 197 198 199 200 201 202 203 204 205 206 207 208 209 210 211 212 213 214 215 216 217 218 219 220 221 222 223 224 225 226 227 228 229 230 231 232 233 234 235 236 237 238 239 240 241 242 243 244 245 246 247 248 249 250 251 252 253 254 255 256 257 258 259 260 261 262 263 264 265 266 267 268 269 270 271 272 273 274 275 276 277 278 279 280 281 282 283 284 285 286 287 288 289 290 291 292 293 294 295 296 297 298 299 300 301 302 303 304 305 306 307 308 309 310 311 312 313 314 315 316 317 318 319 320 321 322 323 324 325 326 327 328 329 330 331 332 333 334 335 336 337 338 339 340 341 342 343 344 345 346 347 348 349 350 351 352 |
|
add_reader(pattern, func=None)
Used to specify the file reader for an instance. The scope of action is visible only to the registered Document object. The registered file reader must be a Callable object. It can only be registered by calling a function. The priority of the file reader registered by the instance is higher than that of the file reader registered by the class, and the priority of the file reader registered by the instance and class is higher than the system default file reader. That is, the order of priority is: instance file reader > class file reader > system default file reader.
Parameters:
-
pattern
(str
) –Matching rules applied by the file reader.
-
func
(Callable
, default:None
) –File reader, must be a Callable object.
Examples:
>>> from lazyllm.tools.rag import Document, DocNode
>>> from lazyllm.tools.rag.readers import ReaderBase
>>> class YmlReader(ReaderBase):
... def _load_data(self, file, extra_info=None, fs=None):
... try:
... import yaml
... except ImportError:
... raise ImportError("yaml is required to read YAML file: `pip install pyyaml`")
... with open(file, 'r') as f:
... data = yaml.safe_load(f)
... print("Call the class YmlReader.")
... return [DocNode(text=data, metadata=extra_info or {})]
...
>>> def processYml(file, extra_info=None):
... with open(file, 'r') as f:
... data = f.read()
... print("Call the function processYml.")
... return [DocNode(text=data, metadata=extra_info or {})]
...
>>> doc1 = Document(dataset_path="your_files_path", create_ui=False)
>>> doc2 = Document(dataset_path="your_files_path", create_ui=False)
>>> doc1.add_reader("**/*.yml", YmlReader)
>>> print(doc1._impl._local_file_reader)
{'**/*.yml': <class '__main__.YmlReader'>}
>>> print(doc2._impl._local_file_reader)
{}
>>> files = ["your_yml_files"]
>>> Document.register_global_reader("**/*.yml", processYml)
>>> doc1._impl._reader.load_data(input_files=files)
Call the class YmlReader.
>>> doc2._impl._reader.load_data(input_files=files)
Call the function processYml.
Source code in lazyllm/tools/rag/document.py
create_node_group(name=None, *, transform, parent=LAZY_ROOT_NAME, trans_node=None, num_workers=0, **kwargs)
Generate a node group produced by the specified rule.
Parameters:
-
name
(str
, default:None
) –The name of the node group.
-
transform
(Callable
) –The transformation rule that converts a node into a node group. The function prototype is
(DocNode, group_name, **kwargs) -> List[DocNode]
. Currently built-in options include SentenceSplitter, and users can define their own transformation rules. -
trans_node
(bool
, default:None
) –Determines whether the input and output of transform are
DocNode
orstr
, default is None. Can only be set to true whentransform
isCallable
. -
num_workers
(int
, default:0
) –number of new threads used for transform. default: 0
-
parent
(str
, default:LAZY_ROOT_NAME
) –The node that needs further transformation. The series of new nodes obtained after transformation will be child nodes of this parent node. If not specified, the transformation starts from the root node.
-
kwargs
–Parameters related to the specific implementation.
Examples:
>>> import lazyllm
>>> from lazyllm.tools import Document, SentenceSplitter
>>> m = lazyllm.OnlineEmbeddingModule(source="glm")
>>> documents = Document(dataset_path='your_doc_path', embed=m, manager=False)
>>> documents.create_node_group(name="sentences", transform=SentenceSplitter, chunk_size=1024, chunk_overlap=100)
Source code in lazyllm/tools/rag/document.py
find_children(target)
Find the child nodes of the specified node.
Parameters:
-
group
(str
) –The name of the node for which to find the children.
Examples:
>>> import lazyllm
>>> from lazyllm.tools import Document, SentenceSplitter
>>> m = lazyllm.OnlineEmbeddingModule(source="glm")
>>> documents = Document(dataset_path='your_doc_path', embed=m, manager=False)
>>> documents.create_node_group(name="parent", transform=SentenceSplitter, chunk_size=1024, chunk_overlap=100)
>>> documents.create_node_group(name="children", transform=SentenceSplitter, parent="parent", chunk_size=1024, chunk_overlap=100)
>>> documents.find_children('parent')
Source code in lazyllm/tools/rag/document.py
find_parent(target)
Find the parent node of the specified node.
Parameters:
-
group
(str
) –The name of the node for which to find the parent.
Examples:
>>> import lazyllm
>>> from lazyllm.tools import Document, SentenceSplitter
>>> m = lazyllm.OnlineEmbeddingModule(source="glm")
>>> documents = Document(dataset_path='your_doc_path', embed=m, manager=False)
>>> documents.create_node_group(name="parent", transform=SentenceSplitter, chunk_size=1024, chunk_overlap=100)
>>> documents.create_node_group(name="children", transform=SentenceSplitter, parent="parent", chunk_size=1024, chunk_overlap=100)
>>> documents.find_parent('children')
Source code in lazyllm/tools/rag/document.py
register_global_reader(pattern, func=None)
classmethod
Used to specify a file reader, which is visible to all Document objects. The registered file reader must be a Callable object. It can be registered using a decorator or by a function call.
Parameters:
-
pattern
(str
) –Matching rules applied by the file reader.
-
func
(Callable
, default:None
) –File reader, must be a Callable object.
Examples:
>>> from lazyllm.tools.rag import Document, DocNode
>>> @Document.register_global_reader("**/*.yml")
>>> def processYml(file, extra_info=None):
... with open(file, 'r') as f:
... data = f.read()
... return [DocNode(text=data, metadata=extra_info or {})]
...
>>> doc1 = Document(dataset_path="your_files_path", create_ui=False)
>>> doc2 = Document(dataset_path="your_files_path", create_ui=False)
>>> files = ["your_yml_files"]
>>> docs1 = doc1._impl._reader.load_data(input_files=files)
>>> docs2 = doc2._impl._reader.load_data(input_files=files)
>>> print(docs1[0].text == docs2[0].text)
# True
Source code in lazyllm/tools/rag/document.py
lazyllm.tools.rag.readers.ReaderBase
Bases: ModuleBase
The base class of file readers, which inherits from the ModuleBase base class and has Callable capabilities. Subclasses that inherit from this class only need to implement the _load_data function, and its return parameter type is List[DocNode]. Generally, the input parameters of the _load_data function are file (Path), extra_info(Dict), and fs (AbstractFileSystem).
Parameters:
-
args
(Any
, default:()
) –Pass the corresponding position parameters as needed.
-
return_trace
(bool
, default:True
) –Set whether to record trace logs.
-
kwargs
(Dict
, default:{}
) –Pass the corresponding keyword arguments as needed.
Examples:
>>> from lazyllm.tools.rag.readers import ReaderBase
>>> from lazyllm.tools.rag import DocNode, Document
>>> from typing import Dict, Optional, List
>>> from pathlib import Path
>>> from fsspec import AbstractFileSystem
>>> @Document.register_global_reader("**/*.yml")
>>> class YmlReader(ReaderBase):
... def _load_data(self, file: Path, extra_info: Optional[Dict] = None, fs: Optional[AbstractFileSystem] = None) -> List[DocNode]:
... try:
... import yaml
... except ImportError:
... raise ImportError("yaml is required to read YAML file: `pip install pyyaml`")
... with open(file, 'r') as f:
... data = yaml.safe_load(f)
... print("Call the class YmlReader.")
... return [DocNode(text=data, metadata=extra_info or {})]
...
>>> files = ["your_yml_files"]
>>> doc = Document(dataset_path="your_files_path", create_ui=False)
>>> reader = doc._impl._reader.load_data(input_files=files)
# Call the class YmlReader.
Source code in lazyllm/tools/rag/readers/readerBase.py
lazyllm.tools.Reranker
Bases: ModuleBase
, _PostProcess
Initializes a Rerank module for postprocessing and reranking of nodes (documents). This constructor initializes a Reranker module that configures a reranking process based on a specified reranking type. It allows for the dynamic selection and instantiation of reranking kernels (algorithms) based on the type and provided keyword arguments.
Parameters:
-
name
(str
, default:'ModuleReranker'
) –The type of reranker to be used for the postprocessing and reranking process. Defaults to 'Reranker'.
-
kwargs
–Additional keyword arguments that are passed to the reranker upon its instantiation.
Detailed explanation of reranker types
-
Reranker: This registered reranking function instantiates a SentenceTransformerRerank reranker with a specified model and top_n parameter. It is designed to rerank nodes based on sentence transformer embeddings.
-
KeywordFilter: This registered reranking function instantiates a KeywordNodePostprocessor with specified required and excluded keywords. It filters nodes based on the presence or absence of these keywords.
Examples:
>>> import lazyllm
>>> from lazyllm.tools import Document, Reranker, Retriever
>>> m = lazyllm.OnlineEmbeddingModule()
>>> documents = Document(dataset_path='/path/to/user/data', embed=m, manager=False)
>>> retriever = Retriever(documents, group_name='CoarseChunk', similarity='bm25', similarity_cut_off=0.01, topk=6)
>>> reranker = Reranker(name='ModuleReranker', model='bge-reranker-large', topk=1)
>>> ppl = lazyllm.ActionModule(retriever, reranker)
>>> ppl.start()
>>> print(ppl("user query"))
Source code in lazyllm/tools/rag/rerank.py
lazyllm.tools.Retriever
Bases: ModuleBase
, _PostProcess
Create a retrieval module for document querying and retrieval. This constructor initializes a retrieval module that configures the document retrieval process based on the specified similarity metric.
Parameters:
-
doc
(object
) –An instance of the document module. The document module can be a single instance or a list of instances. If it is a single instance, it means searching for a single Document, and if it is a list of instances, it means searching for multiple Documents.
-
group_name
(str
) –The name of the node group on which to perform the retrieval.
-
similarity
(Optional[str]
, default:None
) –The similarity function to use for setting up document retrieval. Defaults to 'dummy'. Candidates include ["bm25", "bm25_chinese", "cosine"].
-
similarity_cut_off
(Union[float, Dict[str, float]]
, default:float('-inf')
) –Discard the document when the similarity is below the specified value. In a multi-embedding scenario, if you need to specify different values for different embeddings, you need to specify them in a dictionary, where the key indicates which embedding is specified and the value indicates the corresponding threshold. If all embeddings use the same threshold, you only need to specify one value.
-
index
(str
, default:'default'
) –The type of index to use for document retrieval. Currently, only 'default' is supported.
-
topk
(int
, default:6
) –The number of documents to retrieve with the highest similarity.
-
embed_keys
(Optional[List[str]]
, default:None
) –Indicates which embeddings are used for retrieval. If not specified, all embeddings are used for retrieval.
-
similarity_kw
–Additional parameters to pass to the similarity calculation function.
The group_name
has three built-in splitting strategies, all of which use SentenceSplitter
for splitting, with the difference being in the chunk size:
- CoarseChunk: Chunk size is 1024, with an overlap length of 100
- MediumChunk: Chunk size is 256, with an overlap length of 25
- FineChunk: Chunk size is 128, with an overlap length of 12
Also, Image
is available for group_name
since LazyLLM supports image embedding and retrieval.
Examples:
>>> import lazyllm
>>> from lazyllm.tools import Retriever, Document, SentenceSplitter
>>> m = lazyllm.OnlineEmbeddingModule()
>>> documents = Document(dataset_path='/path/to/user/data', embed=m, manager=False)
>>> rm = Retriever(documents, group_name='CoarseChunk', similarity='bm25', similarity_cut_off=0.01, topk=6)
>>> rm.start()
>>> print(rm("user query"))
>>> m1 = lazyllm.TrainableModule('bge-large-zh-v1.5').start()
>>> document1 = Document(dataset_path='/path/to/user/data', embed={'online':m , 'local': m1}, manager=False)
>>> document1.create_node_group(name='sentences', transform=SentenceSplitter, chunk_size=1024, chunk_overlap=100)
>>> retriever = Retriever(document1, group_name='sentences', similarity='cosine', similarity_cut_off=0.4, embed_keys=['local'], topk=3)
>>> print(retriever("user query"))
>>> document2 = Document(dataset_path='/path/to/user/data', embed={'online':m , 'local': m1}, manager=False)
>>> document2.create_node_group(name='sentences', transform=SentenceSplitter, chunk_size=512, chunk_overlap=50)
>>> retriever2 = Retriever([document1, document2], group_name='sentences', similarity='cosine', similarity_cut_off=0.4, embed_keys=['local'], topk=3)
>>> print(retriever2("user query"))
>>>
>>> filters = {
>>> "author": ["A", "B", "C"],
>>> "public_year": [2002, 2003, 2004],
>>> }
>>> document3 = Document(dataset_path='/path/to/user/data', embed={'online':m , 'local': m1}, manager=False)
>>> document3.create_node_group(name='sentences', transform=SentenceSplitter, chunk_size=512, chunk_overlap=50)
>>> retriever3 = Retriever([document1, document3], group_name='sentences', similarity='cosine', similarity_cut_off=0.4, embed_keys=['local'], topk=3)
>>> print(retriever3(query="user query", filters=filters))
>>> document4 = Document(dataset_path='/path/to/user/data', embed=lazyllm.TrainableModule('siglip'))
>>> retriever4 = Retriever(document4, group_name='Image', similarity='cosine')
>>> nodes = retriever4("user query")
>>> print([node.get_content() for node in nodes])
Source code in lazyllm/tools/rag/retriever.py
30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 144 145 146 |
|
lazyllm.tools.rag.DocManager
Bases: ModuleBase
The DocManager
class manages document lists and related operations, providing APIs for uploading, deleting, and grouping documents.
Parameters:
-
dlm
(DocListManager
) –Document list manager responsible for handling document-related operations.
Source code in lazyllm/tools/rag/doc_manager.py
16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 144 145 146 147 148 149 150 151 152 153 154 155 156 157 158 159 160 161 162 163 164 165 166 167 168 169 170 171 172 173 174 175 176 177 178 179 180 181 182 183 184 185 186 187 188 189 190 191 192 193 194 195 196 197 198 199 200 201 202 203 204 205 206 207 208 209 210 211 212 213 214 215 216 217 218 219 220 221 222 223 224 225 226 227 228 229 230 231 232 233 234 235 236 237 238 239 240 241 242 243 244 245 246 247 248 249 250 251 252 253 254 255 256 257 258 259 260 261 262 263 264 265 266 267 268 269 270 271 272 273 274 275 276 277 278 279 280 281 282 283 284 285 286 287 288 289 290 291 292 293 294 295 296 297 298 299 300 301 302 303 304 305 306 307 308 309 310 311 312 313 314 315 316 317 318 319 320 321 322 323 324 325 326 327 328 329 330 331 332 333 334 335 336 337 338 339 340 341 342 343 344 345 346 347 348 349 350 351 352 353 354 355 356 357 358 359 360 361 362 363 364 365 366 367 368 369 370 371 372 373 374 375 376 377 378 379 380 381 382 383 384 385 386 387 388 389 390 391 392 393 394 395 396 397 398 399 400 401 402 403 404 405 406 407 408 409 410 411 412 413 414 415 416 417 418 419 420 421 422 423 424 425 426 427 428 429 430 431 432 433 434 435 436 437 438 439 440 441 442 443 444 445 446 447 448 449 450 451 452 453 454 455 456 457 |
|
add_files_to_group(files, group_name, override=False, metadatas=None, user_path=None)
An endpoint to upload files and directly add them to a specified group.
Parameters:
-
files
(List[UploadFile]
) –List of files to upload.
-
group_name
(str
) –Name of the group to add the files to.
-
override
(bool
, default:False
) –Whether to overwrite existing files. Default is False.
-
metadatas
(Optional[str]
, default:None
) –Metadata for the files in JSON format.
-
user_path
(Optional[str]
, default:None
) –User-defined path for file uploads.
Returns:
- BaseResponse: Operation result and file IDs.
Source code in lazyllm/tools/rag/doc_manager.py
add_files_to_group_by_id(request)
An endpoint to add files to a specific group by file IDs.
Parameters:
-
request
(FileGroupRequest
) –Request containing file IDs and group name.
Returns:
- BaseResponse: Operation result.
Source code in lazyllm/tools/rag/doc_manager.py
delete_files(request)
An endpoint to delete specified files.
Parameters:
-
request
(FileGroupRequest
) –Request containing file IDs and group name.
Returns:
- BaseResponse: Deletion operation result.
Source code in lazyllm/tools/rag/doc_manager.py
document()
An endpoint to redirect to the default documentation page.
Returns:
- RedirectResponse: Redirects to the
/docs
page.
Source code in lazyllm/tools/rag/doc_manager.py
list_files(limit=None, details=True, alive=None)
An endpoint to list uploaded files.
Parameters:
-
limit
(Optional[int]
, default:None
) –Limit on the number of files returned. Default is None.
-
details
(bool
, default:True
) –Whether to return detailed information. Default is True.
-
alive
(Optional[bool]
, default:None
) –If True, only returns non-deleted files. Default is None.
Returns:
- BaseResponse: File list data.
Source code in lazyllm/tools/rag/doc_manager.py
list_files_in_group(group_name=None, limit=None, alive=None)
An endpoint to list files in a specific group.
Parameters:
-
group_name
(Optional[str]
, default:None
) –The name of the file group.
-
limit
(Optional[int]
, default:None
) –Limit on the number of files returned. Default is None.
-
alive
(Optional[bool]
, default:None
) –Whether to return only non-deleted files.
Returns:
- BaseResponse: List of files in the group.
Source code in lazyllm/tools/rag/doc_manager.py
list_kb_groups()
An endpoint to list all document groups.
Returns:
- BaseResponse: Contains the data of all document groups.
Source code in lazyllm/tools/rag/doc_manager.py
upload_files(files, override=False, metadatas=None, user_path=None)
An endpoint to upload files and update their status. Multiple files can be uploaded at once.
Parameters:
-
files
(List[UploadFile]
) –List of files to upload.
-
override
(bool
, default:False
) –Whether to overwrite existing files. Default is False.
-
metadatas
(Optional[str]
, default:None
) –Metadata for the files in JSON format.
-
user_path
(Optional[str]
, default:None
) –User-defined path for file uploads.
Returns:
- BaseResponse: Upload results and file IDs.
Source code in lazyllm/tools/rag/doc_manager.py
lazyllm.tools.SentenceSplitter
Bases: NodeTransform
Split sentences into chunks of a specified size. You can specify the size of the overlap between adjacent chunks.
Parameters:
-
chunk_size
(int
, default:1024
) –The size of the chunk after splitting.
-
chunk_overlap
(int
, default:200
) –The length of the overlapping content between two adjacent chunks.
Examples:
>>> import lazyllm
>>> from lazyllm.tools import Document, SentenceSplitter
>>> m = lazyllm.OnlineEmbeddingModule(source="glm")
>>> documents = Document(dataset_path='your_doc_path', embed=m, manager=False)
>>> documents.create_node_group(name="sentences", transform=SentenceSplitter, chunk_size=1024, chunk_overlap=100)
Source code in lazyllm/tools/rag/transform.py
137 138 139 140 141 142 143 144 145 146 147 148 149 150 151 152 153 154 155 156 157 158 159 160 161 162 163 164 165 166 167 168 169 170 171 172 173 174 175 176 177 178 179 180 181 182 183 184 185 186 187 188 189 190 191 192 193 194 195 196 197 198 199 200 201 202 203 204 205 206 207 208 209 210 211 212 213 214 215 216 217 218 219 220 221 222 223 224 225 226 227 228 229 230 231 232 233 234 235 236 237 238 239 240 241 242 243 244 245 246 247 248 249 250 251 252 253 254 255 256 257 258 259 260 261 262 263 264 265 266 267 268 269 270 271 272 273 274 275 276 277 278 279 280 281 282 283 284 285 286 287 288 289 290 291 292 293 294 295 296 297 298 299 300 301 302 303 304 305 306 307 308 309 310 311 312 313 314 315 316 317 318 319 320 321 322 323 324 325 326 327 328 329 330 331 |
|
_split(text, chunk_size)
Break text into splits that are smaller than chunk size.
The order of splitting is: 1. split by paragraph separator 2. split by chunking tokenizer 3. split by second chunking regex 4. split by default separator (' ') 5. split by character
Source code in lazyllm/tools/rag/transform.py
lazyllm.tools.LLMParser
Bases: NodeTransform
A text summarizer and keyword extractor that is responsible for analyzing the text input by the user and providing concise summaries or extracting relevant keywords based on the requested task.
Parameters:
-
llm
(TrainableModule
) –A trainable module.
-
language
(str
) –The language type, currently only supports Chinese (zh) and English (en).
-
task_type
(str
) –Currently supports two types of tasks: summary and keyword extraction.
Examples:
>>> from lazyllm import TrainableModule
>>> from lazyllm.tools.rag import LLMParser
>>> llm = TrainableModule("internlm2-chat-7b")
>>> summary_parser = LLMParser(llm, language="en", task_type="summary")
Source code in lazyllm/tools/rag/transform.py
transform(node, **kwargs)
Perform the set task on the specified document.
Parameters:
-
node
(DocNode
) –The document on which the extraction task needs to be performed.
Examples:
>>> import lazyllm
>>> from lazyllm.tools import LLMParser
>>> llm = lazyllm.TrainableModule("internlm2-chat-7b").start()
>>> m = lazyllm.TrainableModule("bge-large-zh-v1.5").start()
>>> summary_parser = LLMParser(llm, language="en", task_type="summary")
>>> keywords_parser = LLMParser(llm, language="en", task_type="keywords")
>>> documents = lazyllm.Document(dataset_path="/path/to/your/data", embed=m, manager=False)
>>> rm = lazyllm.Retriever(documents, group_name='CoarseChunk', similarity='bm25', topk=6)
>>> doc_nodes = rm("test")
>>> summary_result = summary_parser.transform(doc_nodes[0])
>>> keywords_result = keywords_parser.transform(doc_nodes[0])
Source code in lazyllm/tools/rag/transform.py
lazyllm.tools.WebModule
Bases: ModuleBase
WebModule is a web-based interactive interface provided by LazyLLM for developers. After initializing and starting a WebModule, developers can see structure of the module they provides behind the WebModule, and transmit the input of the Chatbot component to their modules. The results and logs returned by the module will be displayed on the “Processing Logs” and Chatbot component on the web page. In addition, Checkbox or Text components can be added programmatically to the web page for additional parameters to the background module. Meanwhile, The WebModule page provides Checkboxes of “Use Context,” “Stream Output,” and “Append Output,” which can be used to adjust the interaction between the page and the module behind.
WebModule.init_web(component_descs) -> gradio.Blocks
Generate a demonstration web page based on gradio. The function initializes session-related data to save chat history and logs for different pages, then dynamically add Checkbox and Text components to the page according to component_descs parameter, and set the corresponding functions for the buttons and text boxes on the page at last. WebModule’s init function calls this method to generate the page.
Parameters:
-
component_descs
(list
) –A list used to add components to the page. Each element in the list is also a list containing
Examples:
>>> import lazyllm
>>> def func2(in_str, do_sample=True, temperature=0.0, *args, **kwargs):
... return f"func2:{in_str}|do_sample:{str(do_sample)}|temp:{temperature}"
...
>>> m1=lazyllm.ActionModule(func2)
>>> m1.name="Module1"
>>> w = lazyllm.WebModule(m1, port=[20570, 20571, 20572], components={
... m1:[('do_sample', 'Checkbox', True), ('temperature', 'Text', 0.1)]},
... text_mode=lazyllm.tools.WebModule.Mode.Refresh)
>>> w.start()
193703: 2024-06-07 10:26:00 lazyllm SUCCESS: ...
Source code in lazyllm/tools/webpages/webmodule.py
31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 144 145 146 147 148 149 150 151 152 153 154 155 156 157 158 159 160 161 162 163 164 165 166 167 168 169 170 171 172 173 174 175 176 177 178 179 180 181 182 183 184 185 186 187 188 189 190 191 192 193 194 195 196 197 198 199 200 201 202 203 204 205 206 207 208 209 210 211 212 213 214 215 216 217 218 219 220 221 222 223 224 225 226 227 228 229 230 231 232 233 234 235 236 237 238 239 240 241 242 243 244 245 246 247 248 249 250 251 252 253 254 255 256 257 258 259 260 261 262 263 264 265 266 267 268 269 270 271 272 273 274 275 276 277 278 279 280 281 282 283 284 285 286 287 288 289 290 291 292 293 294 295 296 297 298 299 300 301 302 303 304 305 306 307 308 309 310 311 312 313 314 315 316 317 318 319 320 321 322 323 324 325 326 327 328 329 330 331 332 333 334 335 336 337 338 339 340 341 342 343 344 345 346 347 348 349 350 351 352 353 354 355 356 357 358 359 360 361 362 363 364 365 366 367 368 369 370 371 372 373 374 375 376 377 378 379 380 381 382 383 384 385 386 387 388 389 390 391 392 393 394 395 396 397 398 399 400 401 402 403 404 405 406 407 408 409 410 411 412 413 414 415 416 417 418 419 420 421 422 423 424 425 426 427 428 429 430 431 432 433 434 435 436 437 438 439 440 441 442 443 444 445 446 447 448 449 450 451 452 453 454 455 456 457 458 459 460 461 462 463 464 465 466 467 468 |
|
lazyllm.tools.ToolManager
Bases: ModuleBase
ToolManager is a tool management class used to provide tool information and tool calls to function call.
When constructing this management class, you need to pass in a list of tool name strings. The tool name here can be provided by LazyLLM or user-defined. If it is user-defined, it must first be registered in LazyLLM before it can be used. When registering, directly use the fc_register
registrar, which has established the tool
group, so when using the tool management class, all functions can be uniformly registered in the tool
group. The function to be registered needs to annotate the function parameters, and add a functional description to the function, as well as the parameter type and function description. This is to facilitate the tool management class to parse the function and pass it to LLM for use.
Parameters:
-
tools
(List[str]
) –A list of tool name strings.
Examples:
>>> from lazyllm.tools import ToolManager, fc_register
>>> import json
>>> from typing import Literal
>>> @fc_register("tool")
>>> def get_current_weather(location: str, unit: Literal["fahrenheit", "celsius"]="fahrenheit"):
... '''
... Get the current weather in a given location
...
... Args:
... location (str): The city and state, e.g. San Francisco, CA.
... unit (str): The temperature unit to use. Infer this from the users location.
... '''
... if 'tokyo' in location.lower():
... return json.dumps({'location': 'Tokyo', 'temperature': '10', 'unit': 'celsius'})
... elif 'san francisco' in location.lower():
... return json.dumps({'location': 'San Francisco', 'temperature': '72', 'unit': 'fahrenheit'})
... elif 'paris' in location.lower():
... return json.dumps({'location': 'Paris', 'temperature': '22', 'unit': 'celsius'})
... elif 'beijing' in location.lower():
... return json.dumps({'location': 'Beijing', 'temperature': '90', 'unit': 'fahrenheit'})
... else:
... return json.dumps({'location': location, 'temperature': 'unknown'})
...
>>> @fc_register("tool")
>>> def get_n_day_weather_forecast(location: str, num_days: int, unit: Literal["celsius", "fahrenheit"]='fahrenheit'):
... '''
... Get an N-day weather forecast
...
... Args:
... location (str): The city and state, e.g. San Francisco, CA.
... num_days (int): The number of days to forecast.
... unit (Literal['celsius', 'fahrenheit']): The temperature unit to use. Infer this from the users location.
... '''
... if 'tokyo' in location.lower():
... return json.dumps({'location': 'Tokyo', 'temperature': '10', 'unit': 'celsius', "num_days": num_days})
... elif 'san francisco' in location.lower():
... return json.dumps({'location': 'San Francisco', 'temperature': '75', 'unit': 'fahrenheit', "num_days": num_days})
... elif 'paris' in location.lower():
... return json.dumps({'location': 'Paris', 'temperature': '25', 'unit': 'celsius', "num_days": num_days})
... elif 'beijing' in location.lower():
... return json.dumps({'location': 'Beijing', 'temperature': '85', 'unit': 'fahrenheit', "num_days": num_days})
... else:
... return json.dumps({'location': location, 'temperature': 'unknown'})
...
>>> tools = ["get_current_weather", "get_n_day_weather_forecast"]
>>> tm = ToolManager(tools)
>>> print(tm([{'name': 'get_n_day_weather_forecast', 'arguments': {'location': 'Beijing', 'num_days': 3}}])[0])
'{"location": "Beijing", "temperature": "85", "unit": "fahrenheit", "num_days": 3}'
Source code in lazyllm/tools/agent/toolsManager.py
179 180 181 182 183 184 185 186 187 188 189 190 191 192 193 194 195 196 197 198 199 200 201 202 203 204 205 206 207 208 209 210 211 212 213 214 215 216 217 218 219 220 221 222 223 224 225 226 227 228 229 230 231 232 233 234 235 236 237 238 239 240 241 242 243 244 245 246 247 248 249 250 251 252 253 254 255 256 257 258 259 260 261 262 263 264 265 266 267 268 269 270 271 272 273 274 275 276 277 278 279 280 281 282 283 284 285 286 287 288 289 290 291 292 293 294 295 296 297 298 299 300 301 302 303 304 305 306 307 308 309 310 311 312 313 314 315 316 317 318 319 320 321 322 323 324 325 326 327 328 329 330 331 332 333 334 335 336 337 338 339 340 341 342 343 344 345 346 347 348 349 350 351 352 353 354 355 356 357 358 359 360 361 362 363 364 365 366 367 368 369 370 371 372 373 374 375 376 377 378 379 380 381 382 383 384 |
|
_gen_args_info_from_moduletool_and_docstring(tool, parsed_docstring)
staticmethod
returns a dict of param names containing at least
1. type
2. description
of params
for example
args = { "foo": { "enum": ["baz", "bar"], "type": "string", "description": "a string", }, "bar": { "type": "integer", "description": "an integer", } }
Source code in lazyllm/tools/agent/toolsManager.py
lazyllm.tools.FunctionCall
Bases: ModuleBase
FunctionCall is a single-round tool call class. If the information in LLM is not enough to answer the uesr's question, it is necessary to combine external knowledge to answer the user's question. If the LLM output required a tool call, the tool call is performed and the tool call result is output. The output result is of List type, including the input, model output, and tool output of the current round. If a tool call is not required, the LLM result is directly output, and the output result is of string type.
Note: The tools used in tools
must have a __doc__
field, clearly describing the purpose and parameters of the tool according to the Google Python Style requirements.
Parameters:
-
llm
(ModuleBase
) –The LLM to be used can be either TrainableModule or OnlineChatModule.
-
tools
(List[Union[str, Callable]]
) –A list of tool names for LLM to use.
Examples:
>>> import lazyllm
>>> from lazyllm.tools import fc_register, FunctionCall
>>> import json
>>> from typing import Literal
>>> @fc_register("tool")
>>> def get_current_weather(location: str, unit: Literal["fahrenheit", "celsius"] = 'fahrenheit'):
... '''
... Get the current weather in a given location
...
... Args:
... location (str): The city and state, e.g. San Francisco, CA.
... unit (str): The temperature unit to use. Infer this from the users location.
... '''
... if 'tokyo' in location.lower():
... return json.dumps({'location': 'Tokyo', 'temperature': '10', 'unit': 'celsius'})
... elif 'san francisco' in location.lower():
... return json.dumps({'location': 'San Francisco', 'temperature': '72', 'unit': 'fahrenheit'})
... elif 'paris' in location.lower():
... return json.dumps({'location': 'Paris', 'temperature': '22', 'unit': 'celsius'})
... else:
... return json.dumps({'location': location, 'temperature': 'unknown'})
...
>>> @fc_register("tool")
>>> def get_n_day_weather_forecast(location: str, num_days: int, unit: Literal["celsius", "fahrenheit"] = 'fahrenheit'):
... '''
... Get an N-day weather forecast
...
... Args:
... location (str): The city and state, e.g. San Francisco, CA.
... num_days (int): The number of days to forecast.
... unit (Literal['celsius', 'fahrenheit']): The temperature unit to use. Infer this from the users location.
... '''
... if 'tokyo' in location.lower():
... return json.dumps({'location': 'Tokyo', 'temperature': '10', 'unit': 'celsius', "num_days": num_days})
... elif 'san francisco' in location.lower():
... return json.dumps({'location': 'San Francisco', 'temperature': '72', 'unit': 'fahrenheit', "num_days": num_days})
... elif 'paris' in location.lower():
... return json.dumps({'location': 'Paris', 'temperature': '22', 'unit': 'celsius', "num_days": num_days})
... else:
... return json.dumps({'location': location, 'temperature': 'unknown'})
...
>>> tools=["get_current_weather", "get_n_day_weather_forecast"]
>>> llm = lazyllm.TrainableModule("internlm2-chat-20b").start() # or llm = lazyllm.OnlineChatModule("openai", stream=False)
>>> query = "What's the weather like today in celsius in Tokyo."
>>> fc = FunctionCall(llm, tools)
>>> ret = fc(query)
>>> print(ret)
["What's the weather like today in celsius in Tokyo.", {'role': 'assistant', 'content': '
', 'tool_calls': [{'id': 'da19cddac0584869879deb1315356d2a', 'type': 'function', 'function': {'name': 'get_current_weather', 'arguments': {'location': 'Tokyo', 'unit': 'celsius'}}}]}, [{'role': 'tool', 'content': '{"location": "Tokyo", "temperature": "10", "unit": "celsius"}', 'tool_call_id': 'da19cddac0584869879deb1315356d2a', 'name': 'get_current_weather'}]]
>>> query = "Hello"
>>> ret = fc(query)
>>> print(ret)
'Hello! How can I assist you today?'
Source code in lazyllm/tools/agent/functionCall.py
52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 144 145 146 147 148 149 150 151 152 153 154 155 156 157 158 159 160 161 162 163 164 165 166 167 168 169 170 171 172 173 174 175 176 177 178 179 180 181 182 183 184 185 186 187 188 189 190 |
|
lazyllm.tools.FunctionCallAgent
Bases: ModuleBase
FunctionCallAgent is an agent that uses the tool calling method to perform complete tool calls. That is, when answering uesr questions, if LLM needs to obtain external knowledge through the tool, it will call the tool and feed back the return results of the tool to LLM, which will finally summarize and output them.
Parameters:
-
llm
(ModuleBase
) –The LLM to be used can be either TrainableModule or OnlineChatModule.
-
tools
(List[str]
) –A list of tool names for LLM to use.
-
max_retries
(int
, default:5
) –The maximum number of tool call iterations. The default value is 5.
Examples:
>>> import lazyllm
>>> from lazyllm.tools import fc_register, FunctionCallAgent
>>> import json
>>> from typing import Literal
>>> @fc_register("tool")
>>> def get_current_weather(location: str, unit: Literal["fahrenheit", "celsius"]='fahrenheit'):
... '''
... Get the current weather in a given location
...
... Args:
... location (str): The city and state, e.g. San Francisco, CA.
... unit (str): The temperature unit to use. Infer this from the users location.
... '''
... if 'tokyo' in location.lower():
... return json.dumps({'location': 'Tokyo', 'temperature': '10', 'unit': 'celsius'})
... elif 'san francisco' in location.lower():
... return json.dumps({'location': 'San Francisco', 'temperature': '72', 'unit': 'fahrenheit'})
... elif 'paris' in location.lower():
... return json.dumps({'location': 'Paris', 'temperature': '22', 'unit': 'celsius'})
... elif 'beijing' in location.lower():
... return json.dumps({'location': 'Beijing', 'temperature': '90', 'unit': 'Fahrenheit'})
... else:
... return json.dumps({'location': location, 'temperature': 'unknown'})
...
>>> @fc_register("tool")
>>> def get_n_day_weather_forecast(location: str, num_days: int, unit: Literal["celsius", "fahrenheit"]='fahrenheit'):
... '''
... Get an N-day weather forecast
...
... Args:
... location (str): The city and state, e.g. San Francisco, CA.
... num_days (int): The number of days to forecast.
... unit (Literal['celsius', 'fahrenheit']): The temperature unit to use. Infer this from the users location.
... '''
... if 'tokyo' in location.lower():
... return json.dumps({'location': 'Tokyo', 'temperature': '10', 'unit': 'celsius', "num_days": num_days})
... elif 'san francisco' in location.lower():
... return json.dumps({'location': 'San Francisco', 'temperature': '75', 'unit': 'fahrenheit', "num_days": num_days})
... elif 'paris' in location.lower():
... return json.dumps({'location': 'Paris', 'temperature': '25', 'unit': 'celsius', "num_days": num_days})
... elif 'beijing' in location.lower():
... return json.dumps({'location': 'Beijing', 'temperature': '85', 'unit': 'fahrenheit', "num_days": num_days})
... else:
... return json.dumps({'location': location, 'temperature': 'unknown'})
...
>>> tools = ['get_current_weather', 'get_n_day_weather_forecast']
>>> llm = lazyllm.TrainableModule("internlm2-chat-20b").start() # or llm = lazyllm.OnlineChatModule(source="sensenova")
>>> agent = FunctionCallAgent(llm, tools)
>>> query = "What's the weather like today in celsius in Tokyo and Paris."
>>> res = agent(query)
>>> print(res)
'The current weather in Tokyo is 10 degrees Celsius, and in Paris, it is 22 degrees Celsius.'
>>> query = "Hello"
>>> res = agent(query)
>>> print(res)
'Hello! How can I assist you today?'
Source code in lazyllm/tools/agent/functionCall.py
192 193 194 195 196 197 198 199 200 201 202 203 204 205 206 207 208 209 210 211 212 213 214 215 216 217 218 219 220 221 222 223 224 225 226 227 228 229 230 231 232 233 234 235 236 237 238 239 240 241 242 243 244 245 246 247 248 249 250 251 252 253 254 255 256 257 258 259 260 261 262 263 264 265 266 267 268 269 270 |
|
lazyllm.tools.ReactAgent
Bases: ModuleBase
ReactAgent follows the process of Thought->Action->Observation->Thought...->Finish
step by step through LLM and tool calls to display the steps to solve user questions and the final answer to the user.
Parameters:
-
llm
(ModuleBase
) –The LLM to be used can be either TrainableModule or OnlineChatModule.
-
tools
(List[str]
) –A list of tool names for LLM to use.
-
max_retries
(int
, default:5
) –The maximum number of tool call iterations. The default value is 5.
Examples:
>>> import lazyllm
>>> from lazyllm.tools import fc_register, ReactAgent
>>> @fc_register("tool")
>>> def multiply_tool(a: int, b: int) -> int:
... '''
... Multiply two integers and return the result integer
...
... Args:
... a (int): multiplier
... b (int): multiplier
... '''
... return a * b
...
>>> @fc_register("tool")
>>> def add_tool(a: int, b: int):
... '''
... Add two integers and returns the result integer
...
... Args:
... a (int): addend
... b (int): addend
... '''
... return a + b
...
>>> tools = ["multiply_tool", "add_tool"]
>>> llm = lazyllm.TrainableModule("internlm2-chat-20b").start() # or llm = lazyllm.OnlineChatModule(source="sensenova")
>>> agent = ReactAgent(llm, tools)
>>> query = "What is 20+(2*4)? Calculate step by step."
>>> res = agent(query)
>>> print(res)
'Answer: The result of 20+(2*4) is 28.'
Source code in lazyllm/tools/agent/reactAgent.py
lazyllm.tools.PlanAndSolveAgent
Bases: ModuleBase
PlanAndSolveAgent consists of two components. First, the planner breaks down the entire task into smaller subtasks, then the solver executes these subtasks according to the plan, which may involve tool calls, and finally returns the answer to the user.
Parameters:
-
llm
(ModuleBase
, default:None
) –The LLM to be used can be TrainableModule or OnlineChatModule. It is mutually exclusive with plan_llm and solve_llm. Either set llm(the planner and sovler share the same LLM), or set plan_llm and solve_llm,or only specify llm(to set the planner) and solve_llm. Other cases are considered invalid.
-
tools
(List[str]
, default:[]
) –A list of tool names for LLM to use.
-
plan_llm
(ModuleBase
, default:None
) –The LLM to be used by the planner, which can be either TrainableModule or OnlineChatModule.
-
solve_llm
(ModuleBase
, default:None
) –The LLM to be used by the solver, which can be either TrainableModule or OnlineChatModule.
-
max_retries
(int
, default:5
) –The maximum number of tool call iterations. The default value is 5.
Examples:
>>> import lazyllm
>>> from lazyllm.tools import fc_register, PlanAndSolveAgent
>>> @fc_register("tool")
>>> def multiply(a: int, b: int) -> int:
... '''
... Multiply two integers and return the result integer
...
... Args:
... a (int): multiplier
... b (int): multiplier
... '''
... return a * b
...
>>> @fc_register("tool")
>>> def add(a: int, b: int):
... '''
... Add two integers and returns the result integer
...
... Args:
... a (int): addend
... b (int): addend
... '''
... return a + b
...
>>> tools = ["multiply", "add"]
>>> llm = lazyllm.TrainableModule("internlm2-chat-20b").start() # or llm = lazyllm.OnlineChatModule(source="sensenova")
>>> agent = PlanAndSolveAgent(llm, tools)
>>> query = "What is 20+(2*4)? Calculate step by step."
>>> res = agent(query)
>>> print(res)
'The final answer is 28.'
Source code in lazyllm/tools/agent/planAndSolveAgent.py
lazyllm.tools.ReWOOAgent
Bases: ModuleBase
ReWOOAgent consists of three parts: Planer, Worker and Solver. The Planner uses predictive reasoning capabilities to create a solution blueprint for a complex task; the Worker interacts with the environment through tool calls and fills in actual evidence or observations into instructions; the Solver processes all plans and evidence to develop a solution to the original task or problem.
Parameters:
-
llm
(ModuleBase
, default:None
) –The LLM to be used can be TrainableModule or OnlineChatModule. It is mutually exclusive with plan_llm and solve_llm. Either set llm(the planner and sovler share the same LLM), or set plan_llm and solve_llm,or only specify llm(to set the planner) and solve_llm. Other cases are considered invalid.
-
tools
(List[str]
, default:[]
) –A list of tool names for LLM to use.
-
plan_llm
(ModuleBase
, default:None
) –The LLM to be used by the planner, which can be either TrainableModule or OnlineChatModule.
-
solve_llm
(ModuleBase
, default:None
) –The LLM to be used by the solver, which can be either TrainableModule or OnlineChatModule.
-
max_retries
(int
) –The maximum number of tool call iterations. The default value is 5.
Examples:
>>> import lazyllm
>>> import wikipedia
>>> from lazyllm.tools import fc_register, ReWOOAgent
>>> @fc_register("tool")
>>> def WikipediaWorker(input: str):
... '''
... Worker that search for similar page contents from Wikipedia. Useful when you need to get holistic knowledge about people, places, companies, historical events, or other subjects. The response are long and might contain some irrelevant information. Input should be a search query.
...
... Args:
... input (str): search query.
... '''
... try:
... evidence = wikipedia.page(input).content
... evidence = evidence.split("\n\n")[0]
... except wikipedia.PageError:
... evidence = f"Could not find [{input}]. Similar: {wikipedia.search(input)}"
... except wikipedia.DisambiguationError:
... evidence = f"Could not find [{input}]. Similar: {wikipedia.search(input)}"
... return evidence
...
>>> @fc_register("tool")
>>> def LLMWorker(input: str):
... '''
... A pretrained LLM like yourself. Useful when you need to act with general world knowledge and common sense. Prioritize it when you are confident in solving the problem yourself. Input can be any instruction.
...
... Args:
... input (str): instruction
... '''
... llm = lazyllm.OnlineChatModule(source="glm")
... query = f"Respond in short directly with no extra words.\n\n{input}"
... response = llm(query, llm_chat_history=[])
... return response
...
>>> tools = ["WikipediaWorker", "LLMWorker"]
>>> llm = lazyllm.TrainableModule("GLM-4-9B-Chat").deploy_method(lazyllm.deploy.vllm).start() # or llm = lazyllm.OnlineChatModule(source="sensenova")
>>> agent = ReWOOAgent(llm, tools)
>>> query = "What is the name of the cognac house that makes the main ingredient in The Hennchata?"
>>> res = agent(query)
>>> print(res)
'
Hennessy '
Source code in lazyllm/tools/agent/rewooAgent.py
33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 144 145 146 147 148 149 150 151 152 153 154 155 156 157 158 159 160 161 162 163 |
|
lazyllm.tools.IntentClassifier
Bases: ModuleBase
IntentClassifier is an intent recognizer based on a language model that identifies predefined intents based on user-provided input text and conversational context. It can handle intent lists and ensures accurate intent recognition through preprocessing and postprocessing steps.
Parameters:
-
llm
–A language model object used for intent recognition, which can be of type OnlineChatModule or TrainableModule.
-
intent_list
(list
, default:None
) –A list of strings containing all possible intents. This list can include intents in either Chinese or English.
-
prompt
(str
, default:''
) –User-attached prompt words.
-
constrain
(str
, default:''
) –User-attached constrain words.
-
examples
(list[list]
, default:[]
) –extro examples,format is
[[query, intent], [query, intent], ...]
. -
return_trace
(bool
, default:False
) –If set to True, the results will be recorded in the trace. Defaults to False.
Examples:
>>> import lazyllm
>>> from lazyllm.tools import IntentClassifier
>>> classifier_llm = lazyllm.OnlineChatModule(source="openai")
>>> chatflow_intent_list = ["Chat", "Financial Knowledge Q&A", "Employee Information Query", "Weather Query"]
>>> classifier = IntentClassifier(classifier_llm, intent_list=chatflow_intent_list)
>>> classifier.start()
>>> print(classifier('What is the weather today'))
Weather Query
>>>
>>> with IntentClassifier(classifier_llm) as ic:
>>> ic.case['Weather Query', lambda x: '38.5°C']
>>> ic.case['Chat', lambda x: 'permission denied']
>>> ic.case['Financial Knowledge Q&A', lambda x: 'Calling Financial RAG']
>>> ic.case['Employee Information Query', lambda x: 'Beijing']
...
>>> ic.start()
>>> print(ic('What is the weather today'))
38.5°C
Source code in lazyllm/tools/classifier/intent_classifier.py
64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 144 145 146 147 148 149 150 151 152 153 154 155 156 157 158 159 160 161 162 163 164 165 166 167 168 169 170 171 172 173 174 175 176 177 178 179 180 181 |
|