Components
Finetune
lazyllm.components.finetune.AlpacaloraFinetune
Bases: LazyLLMFinetuneBase
This class is a subclass of LazyLLMFinetuneBase, based on the LoRA fine-tuning capabilities provided by the alpaca-lora project, used for LoRA fine-tuning of large language models.
Parameters:
-
base_model(str) –The base model used for fine-tuning. It is required to be the path of the base model.
-
target_path(str) –The path where the LoRA weights of the fine-tuned model are saved.
-
merge_path(str, default:None) –The path where the model merges the LoRA weights, default to
None. If not specified, "lazyllm_lora" and "lazyllm_merge" directories will be created undertarget_pathastarget_pathandmerge_pathrespectively. -
model_name(str, default:'LLM') –The name of the model, used as the prefix for setting the log name, default to "LLM".
-
cp_files(str, default:'tokeniz*') –Specify configuration files to be copied from the base model path, which will be copied to
merge_path, default totokeniz* -
launcher(launcher, default:remote(ngpus=1)) –The launcher for fine-tuning, default to
launchers.remote(ngpus=1). -
kw–Keyword arguments, used to update the default training parameters. Note that additional keyword arguments cannot be arbitrarily specified.
The keyword arguments and their default values for this class are as follows:
Other Parameters:
-
data_path(str) –Data path, default to
None; generally passed as the only positional argument when this object is called. -
batch_size(int) –Batch size, default to
64. -
micro_batch_size(int) –Micro-batch size, default to
4. -
num_epochs(int) –Number of training epochs, default to
2. -
learning_rate(float) –Learning rate, default to
5.e-4. -
cutoff_len(int) –Cutoff length, default to
1030; input data tokens will be truncated if they exceed this length. -
filter_nums(int) –Number of filters, default to
1024; only input with token length below this value is preserved. -
val_set_size(int) –Validation set size, default to
200. -
lora_r(int) –LoRA rank, default to
8; this value determines the amount of parameters added, the smaller the value, the fewer the parameters. -
lora_alpha(int) –LoRA fusion factor, default to
32; this value determines the impact of LoRA parameters on the base model parameters, the larger the value, the greater the impact. -
lora_dropout(float) –LoRA dropout rate, default to
0.05, generally used to prevent overfitting. -
lora_target_modules(str) –LoRA target modules, default to
[wo,wqkv], which is the default for InternLM2 model; this configuration item varies for different models. -
modules_to_save(str) –Modules for full fine-tuning, default to
[tok_embeddings,output], which is the default for InternLM2 model; this configuration item varies for different models. -
deepspeed(str) –The path of the DeepSpeed configuration file, default to use the pre-made configuration file in the LazyLLM code repository:
ds.json. -
prompt_template_name(str) –The name of the prompt template, default to "alpaca", i.e., use the prompt template provided by LazyLLM by default.
-
train_on_inputs(bool) –Whether to train on inputs, default to
True. -
show_prompt(bool) –Whether to show the prompt, default to
False. -
nccl_port(int) –NCCL port, default to
19081.
Examples:
>>> from lazyllm import finetune
>>> trainer = finetune.alpacalora('path/to/base/model', 'path/to/target')
Source code in lazyllm/components/finetune/alpacalora.py
10 11 12 13 14 15 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 | |
cmd(trainset, valset=None)
Generate shell command sequence for Alpaca-LoRA fine-tuning and model merging.
Parameters:
-
trainset(str) –Training dataset path, supports both relative path (to configured data_path) and absolute path
-
valset(str, default:None) –Validation dataset path, will auto-split from trainset if not specified
Returns:
-
str–str or list: Returns a single command string when no merging needed, otherwise returns a list containing: [fine-tune command, merge command, file copy command]
Examples:
>>> from lazyllm import finetune
>>> trainer = finetune.alpacalora('path/to/base/model', 'path/to/target')
>>> cmd = trainer.cmd("my_dataset.json")
Source code in lazyllm/components/finetune/alpacalora.py
lazyllm.components.finetune.CollieFinetune
Bases: LazyLLMFinetuneBase
This class is a subclass of LazyLLMFinetuneBase, based on the LoRA fine-tuning capabilities provided by the Collie framework, used for LoRA fine-tuning of large language models.
Parameters:
-
base_model(str) –The base model used for fine-tuning. It is required to be the path of the base model.
-
target_path(str) –The path where the LoRA weights of the fine-tuned model are saved.
-
merge_path(str, default:None) –The path where the model merges the LoRA weights, default to
None. If not specified, "lazyllm_lora" and "lazyllm_merge" directories will be created undertarget_pathastarget_pathandmerge_pathrespectively. -
model_name(str, default:'LLM') –The name of the model, used as the prefix for setting the log name, default to "LLM".
-
cp_files(str, default:'tokeniz*') –Specify configuration files to be copied from the base model path, which will be copied to
merge_path, default to "tokeniz*" -
launcher(launcher, default:remote(ngpus=1)) –The launcher for fine-tuning, default to
launchers.remote(ngpus=1). -
kw–Keyword arguments, used to update the default training parameters. Note that additional keyword arguments cannot be arbitrarily specified.
The keyword arguments and their default values for this class are as follows:
Other Parameters:
-
data_path(str) –Data path, default to
None; generally passed as the only positional argument when this object is called. -
batch_size(int) –Batch size, default to
64. -
micro_batch_size(int) –Micro-batch size, default to
4. -
num_epochs(int) –Number of training epochs, default to
2. -
learning_rate(float) –Learning rate, default to
5.e-4. -
dp_size(int) –Data parallelism parameter, default to
8. -
pp_size(int) –Pipeline parallelism parameter, default to
1. -
tp_size(int) –Tensor parallelism parameter, default to
1. -
lora_r(int) –LoRA rank, default to
8; this value determines the amount of parameters added, the smaller the value, the fewer the parameters. -
lora_alpha(int) –LoRA fusion factor, default to
32; this value determines the impact of LoRA parameters on the base model parameters, the larger the value, the greater the impact. -
lora_dropout(float) –LoRA dropout rate, default to
0.05, generally used to prevent overfitting. -
lora_target_modules(str) –LoRA target modules, default to
[wo,wqkv], which is the default for InternLM2 model; this configuration item varies for different models. -
modules_to_save(str) –Modules for full fine-tuning, default to
[tok_embeddings,output], which is the default for InternLM2 model; this configuration item varies for different models. -
prompt_template_name(str) –The name of the prompt template, default to
alpaca, i.e., use the prompt template provided by LazyLLM by default.
Examples:
>>> from lazyllm import finetune
>>> trainer = finetune.collie('path/to/base/model', 'path/to/target')
Source code in lazyllm/components/finetune/collie.py
9 10 11 12 13 14 15 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 | |
lazyllm.components.finetune.LlamafactoryFinetune
Bases: LazyLLMFinetuneBase
This class is a subclass of LazyLLMFinetuneBase, based on the training capabilities provided by the LLaMA-Factory framework, used for training large language models(or visual language models).
Parameters:
-
base_model(str) –Path to the base model used for training. Supports local paths; if the path does not exist, it will attempt to locate it from the configured model directory.
-
target_path(str) –Target directory to save model weights after training is completed.
-
merge_path(str, default:None) –Path to save the model after merging LoRA weights. Defaults to None. If not specified, two directories will be automatically created under
target_path: - "lazyllm_lora" (for storing LoRA fine-tuned weights) - "lazyllm_merge" (for storing the merged model weights) -
config_path(str, default:None) –Path to the YAML file containing training configuration. Defaults to None. If not specified, the default config file
llama_factory/sft.yamlwill be used. This file can override default training parameters. -
export_config_path(str, default:None) –Path to the YAML file for LoRA weight export/merging configuration. Defaults to None. If not specified, the default config file
llama_factory/lora_export.yamlwill be used. -
lora_r(int, default:None) –Rank of the LoRA adaptation. If provided, overrides the
lora_rankvalue in the configuration. -
modules_to_save(str, default:None) –List of additional module names to be saved. Should be provided as a string in Python list format, e.g., "[module1, module2]".
-
lora_target_modules(str, default:None) –List of module names to apply LoRA fine-tuning to. Format is the same as above.
-
launcher(launcher, default:remote(ngpus=1, sync=True)) –Launcher for the fine-tuning task. Defaults to a single-GPU, synchronous remote launcher:
launchers.remote(ngpus=1, sync=True). -
**kw–Additional keyword arguments used to dynamically override default parameters in the training configuration.
Other Parameters:
-
stage(Literal['pt', 'sft', 'rm', 'ppo', 'dpo', 'kto']) –Default is:
sft. Which stage will be performed in training. -
do_train(bool) –Default is:
True. Whether to run training. -
finetuning_type(Literal['lora', 'freeze', 'full']) –Default is:
lora. Which fine-tuning method to use. -
lora_target(str) –Default is:
all. Name(s) of target modules to apply LoRA. Use commas to separate multiple modules. Useallto specify all the linear modules. -
template(Optional[str]) –Default is:
None. Which template to use for constructing prompts in training and inference. -
cutoff_len(int) –Default is:
1024. The cutoff length of the tokenized inputs in the dataset. -
max_samples(Optional[int]) –Default is:
1000. For debugging purposes, truncate the number of examples for each dataset. -
overwrite_cache(bool) –Default is:
True. Overwrite the cached training and evaluation sets. -
preprocessing_num_workers(Optional[int]) –Default is:
16. The number of processes to use for the pre-processing. -
dataset_dir(str) –Default is:
lazyllm_temp_dir. Path to the folder containing the datasets. If not explicitly specified, LazyLLM will generate adataset_info.jsonfile in the.tempfolder in the current working directory for use by LLaMA-Factory. -
logging_steps(float) –Default is:
10. Log every X updates steps. Should be an integer or a float in range[0,1). If smaller than 1, will be interpreted as ratio of total training steps. -
save_steps(float) –Default is:
500. Save checkpoint every X updates steps. Should be an integer or a float in range[0,1). If smaller than 1, will be interpreted as ratio of total training steps. -
plot_loss(bool) –Default is:
True. Whether or not to save the training loss curves. -
overwrite_output_dir(bool) –Default is:
True. Overwrite the content of the output directory. -
per_device_train_batch_size(int) –Default is:
1. Batch size per GPU/TPU/MPS/NPU core/CPU for training. -
gradient_accumulation_steps(int) –Default is:
8. Number of updates steps to accumulate before performing a backward/update pass. -
learning_rate(float) –Default is:
1e-04. The initial learning rate for AdamW. -
num_train_epochs(float) –Default is:
3.0. Total number of training epochs to perform. -
lr_scheduler_type(Union[SchedulerType, str]) –Default is:
cosine. The scheduler type to use. -
warmup_ratio(float) –Default is:
0.1. Linear warmup over warmup_ratio fraction of total steps. -
fp16(bool) –Default is:
True. Whether to use fp16 (mixed) precision instead of 32-bit. -
ddp_timeout(Optional[int]) –Default is:
180000000. Overrides the default timeout for distributed training (value should be given in seconds). -
report_to(Union[NoneType, str, List[str]]) –Default is:
tensorboard. The list of integrations to report the results and logs to. -
val_size(float) –Default is:
0.1. Size of the development set, should be an integer or a float in range[0,1). -
per_device_eval_batch_size(int) –Default is:
1. Batch size per GPU/TPU/MPS/NPU core/CPU for evaluation. -
eval_strategy(Union[IntervalStrategy, str]) –Default is:
steps. The evaluation strategy to use. -
eval_steps(Optional[float]) –Default is:
500. Run an evaluation every X steps. Should be an integer or a float in range[0,1). If smaller than 1, will be interpreted as ratio of total training steps.
Examples:
>>> from lazyllm import finetune
>>> trainer = finetune.llamafactory('internlm2-chat-7b', 'path/to/target')
<lazyllm.llm.finetune type=LlamafactoryFinetune>
Source code in lazyllm/components/finetune/llamafactory.py
15 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 | |
cmd(trainset, valset=None)
Generate LLaMA-Factory fine-tuning command sequence, including training and model merge commands.
Parameters:
-
trainset(str) –Training dataset path (supports relative path to lazyllm.config['data_path'])
-
valset(str, default:None) –Validation dataset path (not directly used in current implementation)
Returns:
-
str(str) –Complete shell command string containing: - Training command (with auto-configured parameters) - Log redirection (saved to target path) - Optional model merge command (when LoRA is configured)
Notes
- Automatically generates timestamped training log files
- Temporary files are automatically cleaned up after use
- Supports multiple data formats (alpaca/sharegpt etc.)
- Multimodal data (images/videos/audios) is automatically detected and handled
Source code in lazyllm/components/finetune/llamafactory.py
lazyllm.components.deploy.LazyLLMDeployBase
Bases: ComponentBase
This class is a subclass of ComponentBase that provides basic functionality for LazyLLM deployment. It supports encoding conversion for various media types and provides configuration options for result extraction and streaming processing.
Parameters:
-
launcher(LauncherBase, default:remote()) –Launcher instance for deployment, defaults to remote launcher (
launchers.remote()).
Notes
- Need to implement specific deployment logic when inheriting this class
- Can customize result extraction logic by overriding the extract_result method
Examples:
>>> import lazyllm
>>> from lazyllm.components.deploy.base import LazyLLMDeployBase
>>> class MyDeployer(LazyLLMDeployBase):
... def __call__(self, inputs):
... return processed_result
def extract_result(output, inputs):
... return output.json()['result']
>>> deployer = MyDeployer()
>>> result = deployer.extract_result(raw_output, input_data)
Source code in lazyllm/components/deploy/base.py
lazyllm.components.deploy.LazyLLMDeployBase.extract_result(output, inputs)
staticmethod
lazyllm.components.finetune.FlagembeddingFinetune
Bases: LazyLLMFinetuneBase
This class is a subclass of LazyLLMFinetuneBase, based on the training capabilities provided by the FlagEmbedding framework, used for training embedding and reranker models.
Parameters:
-
base_model(str) –The base model used for training. It is required to be the path of the base model.
-
target_path(str) –The path where the trained model weights are saved.
-
launcher(launcher, default:remote(ngpus=1, sync=True)) –The launcher for fine-tuning, default is
launchers.remote(ngpus=1, sync=True). -
kw–Keyword arguments used to update the default training parameters.
The keyword arguments and their default values for this class of embedding model are as follows:
Other Parameters:
-
train_group_size(int) –Default is:
8. The size of train group. It is used to control the number of negative samples in each training set. -
query_max_len(int) –Default is:
512. The maximum total input sequence length after tokenization for passage. Sequences longer than this will be truncated, sequences shorter will be padded. -
passage_max_len(int) –Default is:
512. The maximum total input sequence length after tokenization for passage. Sequences longer than this will be truncated, sequences shorter will be padded. -
pad_to_multiple_of(int) –Default is:
8. If set will pad the sequence to be a multiple of the provided value. -
query_instruction_for_retrieval(str) –Default is:
Represent this sentence for searching relevant passages:. Instruction for query. -
query_instruction_format(str) –Default is:
{}{}. Format for query instruction. -
learning_rate(float) –Default is:
1e-5. Learning rate. -
num_train_epochs(int) –Default is:
1. Total number of training epochs to perform. -
per_device_train_batch_size(int) –Default is:
2. Train batch size -
gradient_accumulation_steps(int) –Default is:
1. Number of updates steps to accumulate before performing a backward/update pass. -
dataloader_drop_last(bool) –Default is:
True. When it='True', the last incomplete batch is dropped if the dataset size is not divisible by the batch size, meaning DataLoader only returns complete batches. -
warmup_ratio(float) –Default is:
0.1. Warmup ratio for linear scheduler. -
weight_decay(float) –Default is:
0.01. Weight decay in AdamW. -
deepspeed(str) –Default is:
``. The path of the DeepSpeed configuration file, default to use the pre-made configuration file in the LazyLLM code repository:ds_stage0.json``. -
logging_steps(int) –Default is:
1. Logging frequency according to logging strategy. -
save_steps(int) –Default is:
1000. Saving frequency. -
temperature(float) –Default is:
0.02. Temperature used for similarity score -
sentence_pooling_method(str) –Default is:
cls. The pooling method. Available options: 'cls', 'mean', 'last_token'. -
normalize_embeddings(bool) –Default is:
True. Whether to normalize the embeddings. -
kd_loss_type(str) –Default is:
kl_div. The loss type for knowledge distillation. Available options:'kl_div', 'm3_kd_loss'. -
overwrite_output_dir(bool) –Default is:
True. It is used to allow the program to overwrite an existing output directory. -
fp16(bool) –Default is:
True. Whether to use fp16 (mixed) precision instead of 32-bit. -
gradient_checkpointing(bool) –Default is:
True. Whether enable gradient checkpointing. -
negatives_cross_device(bool) –Default is:
True. Whether share negatives across devices.
The keyword arguments and their default values for this class of reranker model are as follows:
Other Parameters:
-
train_group_size(int) –Default is:
8. The size of train group. It is used to control the number of negative samples in each training set. -
query_max_len(int) –Default is:
256. The maximum total input sequence length after tokenization for passage. Sequences longer than this will be truncated, sequences shorter will be padded. -
passage_max_len(int) –Default is:
256. The maximum total input sequence length after tokenization for passage. Sequences longer than this will be truncated, sequences shorter will be padded. -
pad_to_multiple_of(int) –Default is:
8. If set will pad the sequence to be a multiple of the provided value. -
learning_rate(float) –Default is:
6e-5. Learning rate. -
num_train_epochs(int) –Default is:
1. Total number of training epochs to perform. -
per_device_train_batch_size(int) –Default is:
2. Train batch size -
gradient_accumulation_steps(int) –Default is:
1. Number of updates steps to accumulate before performing a backward/update pass. -
dataloader_drop_last(bool) –Default is:
True. When it='True', the last incomplete batch is dropped if the dataset size is not divisible by the batch size, meaning DataLoader only returns complete batches. -
warmup_ratio(float) –Default is:
0.1. Warmup ratio for linear scheduler. -
weight_decay(float) –Default is:
0.01. Weight decay in AdamW. -
deepspeed(str) –Default is:
``. The path of the DeepSpeed configuration file, default to use the pre-made configuration file in the LazyLLM code repository:ds_stage0.json``. -
logging_steps(int) –Default is:
1. Logging frequency according to logging strategy. -
save_steps(int) –Default is:
1000. Saving frequency. -
overwrite_output_dir(bool) –Default is:
True. It is used to allow the program to overwrite an existing output directory. -
fp16(bool) –Default is:
True. Whether to use fp16 (mixed) precision instead of 32-bit. -
gradient_checkpointing(bool) –Default is:
True. Whether enable gradient checkpointing.
Examples:
>>> from lazyllm import finetune
>>> finetune.FlagembeddingFinetune('bge-m3', 'path/to/target')
<lazyllm.llm.finetune type=FlagembeddingFinetune>
Source code in lazyllm/components/finetune/flagembedding.py
12 13 14 15 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 | |
lazyllm.components.auto.AutoFinetune
Bases: LazyLLMFinetuneBase
This class is a subclass of LazyLLMFinetuneBase and can automatically select the appropriate fine-tuning framework and parameters based on the input arguments to fine-tune large language models.
Specifically, based on the input model parameters of base_model, ctx_len, batch_size, lora_r, the type and number of GPUs in launcher, this class can automatically select the appropriate fine-tuning framework (such as: AlpacaloraFinetune or CollieFinetune) and the required parameters.
Parameters:
-
base_model(str) –The base model used for fine-tuning. It is required to be the path of the base model.
-
source(config[model_source]) –Specifies the model download source. This can be configured by setting the environment variable
LAZYLLM_MODEL_SOURCE. -
target_path(str) –The path where the LoRA weights of the fine-tuned model are saved.
-
merge_path(str) –The path where the model merges the LoRA weights, default to
None. If not specified, "lazyllm_lora" and "lazyllm_merge" directories will be created undertarget_pathastarget_pathandmerge_pathrespectively. -
ctx_len(int) –The maximum token length for input to the fine-tuned model, default to
1024. -
batch_size(int) –Batch size, default to
32. -
lora_r(int) –LoRA rank, default to
8; this value determines the amount of parameters added, the smaller the value, the fewer the parameters. -
launcher(launcher, default:remote()) –The launcher for fine-tuning, default to
launchers.remote(ngpus=1). -
kw–Keyword arguments, used to update the default training parameters. Note that additional keyword arguments cannot be arbitrarily specified, as they depend on the framework inferred by LazyLLM, so it is recommended to set them with caution.
Examples:
>>> from lazyllm import finetune
>>> finetune.auto("internlm2-chat-7b", 'path/to/target')
<lazyllm.llm.finetune type=AlpacaloraFinetune>
Source code in lazyllm/components/auto/autofinetune.py
lazyllm.components.finetune.base.DummyFinetune
Bases: LazyLLMFinetuneBase
DummyFinetune is a subclass of [LazyLLMFinetuneBase][lazyllm.components.LazyLLMFinetuneBase] that serves as a placeholder implementation for fine-tuning.
The class is primarily used for demonstration or testing purposes, as it does not perform any actual fine-tuning logic.
Args:
base_model: A string specifying the base model name. Defaults to 'base'.
target_path: A string specifying the target path for fine-tuning outputs. Defaults to 'target'.
launcher: A launcher instance for executing commands. Defaults to [launchers.remote()][lazyllm.launchers.remote].
kw: Additional keyword arguments that are stored for later use.
cmd(self, *args, **kw) -> str
Generates a dummy command string for fine-tuning. This method is for testing purposes only.
Args:
*args: Positional arguments to be included in the command.
kw: Keyword arguments to be included in the command.
Returns:
A string representing a dummy command. The string includes the initial arguments passed during initialization.
Examples:
>>> from lazyllm.components import DummyFinetune
>>> from lazyllm import launchers
>>> # 创建一个 DummyFinetune 实例
>>> finetuner = DummyFinetune(base_model='example-base', target_path='example-target', launcher=launchers.local(), custom_arg='custom_value')
>>> # 调用 cmd 方法生成占位命令
>>> command = finetuner.cmd('--example-arg', key='value')
>>> print(command)
... echo 'dummy finetune!, and init-args is {'custom_arg': 'custom_value'}'
Source code in lazyllm/components/finetune/base.py
cmd(*args, **kw)
The cmd method generates a dummy command string for fine-tuning. This method is primarily for testing or demonstration purposes.
Args:
args: Positional arguments to be included in the command (not used in this implementation).
*kw: Keyword arguments to be included in the command (not used in this implementation).
Returns:
A string representing a dummy command. The string includes the initial arguments (**kw) passed during the instance initialization, which are stored in self.kw.
Example:
If the class is initialized with custom_arg='value', calling the cmd method will return:
"echo 'dummy finetune!, and init-args is {'custom_arg': 'value'}'"
Examples:
>>> from lazyllm.components import DummyFinetune
>>> from lazyllm import launchers
>>> # 创建一个 DummyFinetune 实例,并传递初始化参数
>>> finetuner = DummyFinetune(base_model='example-base', target_path='example-target', launcher=launchers.local(), custom_arg='value')
>>> # 调用 cmd 方法生成占位命令
>>> command = finetuner.cmd()
>>> # 打印生成的占位命令
>>> print(command)
... echo 'dummy finetune!, and init-args is {'custom_arg': 'value'}'
Source code in lazyllm/components/finetune/base.py
Deploy
lazyllm.components.deploy.Lightllm
Bases: LazyLLMDeployBase
This class is a subclass of LazyLLMDeployBase, based on the inference capabilities provided by the LightLLM framework, used for inference with large language models.
Parameters:
-
trust_remote_code(bool, default:True) –Whether to allow loading of model code from remote servers, default is
True. -
launcher(launcher, default:remote(ngpus=1)) –The launcher for fine-tuning, default is
launchers.remote(ngpus=1). -
stream(bool) –Whether the response is streaming, default is
False. -
kw–Keyword arguments used to update default training parameters. Note that not any additional keyword arguments can be specified here.
The keyword arguments and their default values for this class are as follows:
Other Parameters:
-
tp(int) –Tensor parallelism parameter, default is
1. -
max_total_token_num(int) –Maximum total token number, default is
64000. -
eos_id(int) –End-of-sentence ID, default is
2. -
port(int) –Service port number, default is
None, in which case LazyLLM will automatically generate a random port number. -
host(str) –Service IP address, default is
0.0.0.0. -
nccl_port(int) –NCCL port, default is
None, in which case LazyLLM will automatically generate a random port number. -
tokenizer_mode(str) –Tokenizer loading mode, default is
auto. -
running_max_req_size(int) –Maximum number of parallel requests for the inference engine, default is
256. -
data_type(str) –Data type for model weights, default is
float16. -
max_req_total_len(int) –Maximum total length for requests, default is
64000. -
max_req_input_len(int) –Maximum input length, default is
4096. -
long_truncation_mode(str) –Truncation mode for long texts, default is
head.
Examples:
Source code in lazyllm/components/deploy/lightllm.py
11 12 13 14 15 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 | |
cmd(finetuned_model=None, base_model=None)
This method generates the command to start the LightLLM service.
Parameters:
-
finetuned_model(str, default:None) –Path to the fine-tuned model.
-
base_model(str, default:None) –Path to the base model, used when finetuned_model is invalid.
Returns:
-
LazyLLMCMD–A LazyLLMCMD object containing the startup command.
Source code in lazyllm/components/deploy/lightllm.py
geturl(job=None)
Get the URL address of the LightLLM service.
Parameters:
-
job(optional, default:None) –Job object, defaults to None, in which case self.job is used.
Returns:
-
str–The service URL address in the format "http://{ip}:{port}/generate".
Source code in lazyllm/components/deploy/lightllm.py
extract_result(x, inputs)
staticmethod
Source code in lazyllm/components/deploy/lightllm.py
lazyllm.components.deploy.Vllm
Bases: LazyLLMDeployBase
Source code in lazyllm/components/deploy/vllm.py
lazyllm.components.deploy.LMDeploy
Bases: LazyLLMDeployBase
This class is a subclass of LazyLLMDeployBase, leveraging the inference capabilities provided by the LMDeploy framework for inference on large language models.
Parameters:
-
launcher(launcher, default:remote(ngpus=1)) –The launcher for fine-tuning, defaults to
launchers.remote(ngpus=1). -
stream(bool) –Whether to enable streaming response, defaults to
False. -
trust_remote_code(bool, default:True) –Whether to trust remote code, defaults to
True. -
log_path(str, default:None) –Path for log file, defaults to
None. -
kw–Keyword arguments for updating default training parameters. Note that no additional keyword arguments beyond those listed below can be passed.
Other Parameters:
-
tp(int) –Tensor parallelism parameter, defaults to
1. -
server-name(str) –The IP address of the service, defaults to
0.0.0.0. -
server-port(int) –The port number of the service, defaults to
None. In this case, LazyLLM will automatically generate a random port number. -
max-batch-size(int) –Maximum batch size, defaults to
128. -
chat-template(str) –Path to chat template file, defaults to
None. If the model is not a vision-language model and no template is specified, a default template will be used. -
eager-mode(bool) –Whether to enable eager mode, controlled by environment variable
LMDEPLOY_EAGER_MODE, defaults toFalse.
Examples:
>>> # Basic use:
>>> from lazyllm import deploy
>>> infer = deploy.LMDeploy()
>>>
>>> # MultiModal:
>>> import lazyllm
>>> from lazyllm import deploy, globals
>>> from lazyllm.components.formatter import encode_query_with_filepaths
>>> chat = lazyllm.TrainableModule('Mini-InternVL-Chat-2B-V1-5').deploy_method(deploy.LMDeploy)
>>> chat.update_server()
>>> inputs = encode_query_with_filepaths('What is it?', ['path/to/image'])
>>> res = chat(inputs)
Source code in lazyllm/components/deploy/lmdeploy.py
15 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 | |
cmd(finetuned_model=None, base_model=None)
This method generates the command to start the LMDeploy service.
Parameters:
-
finetuned_model(str, default:None) –Path to the fine-tuned model.
-
base_model(str, default:None) –Path to the base model, used when finetuned_model is invalid.
Returns:
-
LazyLLMCMD–A LazyLLMCMD object containing the startup command.
Source code in lazyllm/components/deploy/lmdeploy.py
geturl(job=None)
Get the URL address of the LMDeploy service.
Parameters:
-
job(optional, default:None) –Job object, defaults to None, in which case self.job is used.
Returns:
-
str–The service URL address in the format "http://{ip}:{port}/v1/chat/interactive".
Source code in lazyllm/components/deploy/lmdeploy.py
lazyllm.components.deploy.base.DummyDeploy
Bases: LazyLLMDeployBase, Pipeline
DummyDeploy(launcher=launchers.remote(sync=False), , stream=False, *kw)
A mock deployment class for testing purposes. It extends both LazyLLMDeployBase and flows.Pipeline,
simulating a simple pipeline-style deployable service with optional streaming support.
This class is primarily intended for internal testing and demonstration. It receives inputs in the format defined
by message_format, and returns a dummy response or a streaming response depending on the stream flag.
Attributes: - keys_name_handle (dict): Mapping of input keys for request formatting. - message_format (dict): Default request template including input and generation parameters.
Parameters:
- launcher: Deployment launcher instance, defaulting to launchers.remote(sync=False).
- stream (bool): Whether to simulate streaming output.
- kw: Additional keyword arguments passed to the superclass.
Methods: - call(*args): Starts the deployment and returns the service URL. - repr(): Returns a string representation of the underlying pipeline.
Source code in lazyllm/components/deploy/base.py
lazyllm.components.auto.AutoDeploy
Bases: LazyLLMDeployBase
This class is a subclass of LazyLLMDeployBase that automatically selects the appropriate inference framework and parameters based on the input arguments for inference with large language models.
Specifically, based on the input base_model parameters, max_token_num, the type and number of GPUs in launcher, this class can automatically select the appropriate inference framework (such as Lightllm or Vllm) and the required parameters.
Parameters:
-
base_model(str) –The base model for fine-tuning, which is required to be the name or the path to the base model. Used to provide base model information.
-
source(config[model_source]) –Specifies the model download source. This can be configured by setting the environment variable
LAZYLLM_MODEL_SOURCE. -
trust_remote_code(bool) –Whether to allow loading of model code from remote servers, default is
True. -
launcher(launcher, default:remote()) –The launcher for fine-tuning, default is
launchers.remote(ngpus=1). -
stream(bool) –Whether the response is streaming, default is
False. -
type(str) –Type parameter, default is
None, which corresponds to thellmtype. Additionally, theembedtype is also supported. -
max_token_num(int) –The maximum token length for the input fine-tuning model, default is
1024. -
launcher(launcher, default:remote()) –The launcher for fine-tuning, default is
launchers.remote(ngpus=1). -
kw–Keyword arguments used to update default training parameters. Note that whether additional keyword arguments can be specified depends on the framework inferred by LazyLLM, so it is recommended to set them carefully.
Examples:
>>> from lazyllm import deploy
>>> deploy.auto('internlm2-chat-7b')
<lazyllm.llm.deploy type=Lightllm>
Source code in lazyllm/components/auto/autodeploy.py
lazyllm.components.deploy.embed.AbstractEmbedding
Bases: ABC
Source code in lazyllm/components/deploy/embed.py
lazyllm.components.deploy.EmbeddingDeploy
Bases: LazyLLMDeployBase
Source code in lazyllm/components/deploy/embed.py
lazyllm.components.deploy.embed.RerankDeploy
Bases: EmbeddingDeploy
Source code in lazyllm/components/deploy/embed.py
lazyllm.components.deploy.embed.LazyHuggingFaceRerank
Bases: object
Source code in lazyllm/components/deploy/embed.py
load_reranker()
lazyllm.components.deploy.Mindie
Bases: LazyLLMDeployBase
This class is a subclass of LazyLLMDeployBase, designed for deploying and managing the MindIE large language model inference service. It encapsulates the full workflow including configuration generation, process launching, and API interaction for the MindIE service.
Args:
trust_remote_code (bool): Whether to trust remote code (e.g., from HuggingFace models). Default is True.
launcher: Instance of the task launcher. Default is launchers.remote().
log_path (str): Path to save logs. If None, logs will not be saved.
**kw: Other configuration parameters. Supports the following keys:
- npuDeviceIds: List of NPU device IDs (e.g., [[0,1]] indicates using 2 devices)
- worldSize: Model parallelism size
- port: Service port (set to 'auto' for auto-assignment between 30000–40000)
- maxSeqLen: Maximum sequence length
- maxInputTokenLen: Maximum number of tokens per input
- maxPrefillTokens: Maximum number of prefill tokens
- config: Custom configuration file
Note:
You must set the environment variable LAZYLLM_MINDIE_HOME to point to the MindIE installation directory.
If finetuned_model is not specified or the path is invalid, it will automatically fall back to base_model.
Examples:
>>> import lazyllm
>>> from lazyllm.components.deploy import Mindie
>>> deployer = Mindie(
... port=30000,
... launcher=lazyllm.launchers.remote(),
... max_seq_len=32000,
... log_path="/path/to/logs"
... )
>>> cmd = deployer.cmd(
... finetuned_model="/path/to/finetuned_model",
... base_model="/path/to/base_model")
>>> print("Service URL:", cmd.geturl())
Source code in lazyllm/components/deploy/mindie.py
15 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 | |
cmd(finetuned_model=None, base_model=None, master_ip=None)
Generates the command to start the MindIE service.
Parameters:
-
finetuned_model(str, default:None) –Path to the fine-tuned model
-
base_model(str, default:None) –Path to the base model (fallback if finetuned_model is invalid)
-
master_ip(str, default:None) –Master node IP address (currently unused)
Returns:
-
LazyLLMCMD–Command object for starting the service
Notes
- Automatically handles model path validation
- Updates configuration before service start
- Supports random port allocation when configured
Source code in lazyllm/components/deploy/mindie.py
geturl(job=None)
Gets the service URL after deployment.
Parameters:
-
job–Job object (optional, defaults to self.job)
Returns:
-
str–The generate endpoint URL
Notes
- Returns different formats based on display mode
- Includes port number from configuration
Source code in lazyllm/components/deploy/mindie.py
load_config(config_path)
Loads and parses the MindIE configuration file.
Parameters:
-
config_path(str) –Path to the JSON configuration file
Returns:
-
dict–Parsed configuration dictionary
Notes
- Handles both default and custom configuration files
- Uses JSON format for configuration
- Creates backup of original config before modification
Source code in lazyllm/components/deploy/mindie.py
save_config()
Saves the current configuration to file.
Notes
- Automatically creates backup of existing config
- Writes to the standard MindIE config location
- Uses JSON format with proper indentation
- Called automatically during deployment
Source code in lazyllm/components/deploy/mindie.py
update_config()
Updates the configuration dictionary with current settings.
Notes
- Handles multiple configuration sections:
- Model deployment parameters
- Server settings
- Scheduling parameters
Source code in lazyllm/components/deploy/mindie.py
lazyllm.components.deploy.OCRDeploy
Bases: LazyLLMDeployBase
OCRDeploy is a subclass of LazyLLMDeployBase that provides deployment for OCR (Optical Character Recognition) models.
This class is designed to deploy OCR models with additional configurations such as logging, trust for remote code, and port customization.
Attributes:
keys_name_handle: A dictionary mapping input keys to their corresponding handler keys. For example:
- "inputs": Handles general inputs.
- "ocr_files": Also mapped to "inputs".
message_format: A dictionary specifying the expected message format. For example:
- {"inputs": "/path/to/pdf"} indicates that the model expects a PDF file path as input.
default_headers: A dictionary specifying default headers for API requests. Defaults to:
- {"Content-Type": "application/json"}
Args:
launcher: A launcher instance for deploying the model. Defaults to None.
log_path: A string specifying the path where logs should be saved. Defaults to None.
trust_remote_code: A boolean indicating whether to trust remote code execution. Defaults to True.
port: An integer specifying the port for the deployment server. Defaults to None.
finetuned_model: A string specifying the path or name of the fine-tuned OCR model. Defaults to None.
base_model: A string specifying the base model name. If finetuned_model is not provided, base_model will be used. Defaults to None.
Returns:
An instance of [RelayServer][lazyllm.deploy.RelayServer], which acts as the deployment server for the OCR model.
Example:
deployer = OCRDeploy(launcher=launchers.local(), log_path='./logs', port=8080)
server = deployer(finetuned_model='ocr-model')
print(server) # RelayServer instance ready to handle OCR requests
Examples:
>>> from lazyllm.components import OCRDeploy
>>> from lazyllm import launchers
>>> # 创建一个 OCRDeploy 实例
>>> deployer = OCRDeploy(launcher=launchers.local(), log_path='./logs', port=8080)
>>> # 使用微调的 OCR 模型部署服务器
>>> server = deployer(finetuned_model='ocr-model')
>>> # 打印部署服务器信息
>>> print(server)
... <RelayServer instance ready to handle OCR requests>
Source code in lazyllm/components/deploy/ocr/pp_ocr.py
lazyllm.components.deploy.relay.base.RelayServer
Bases: LazyLLMDeployBase
Source code in lazyllm/components/deploy/relay/base.py
cmd(func=None)
Source code in lazyllm/components/deploy/relay/base.py
lazyllm.components.deploy.OCRDeploy
Bases: LazyLLMDeployBase
OCRDeploy is a subclass of LazyLLMDeployBase that provides deployment for OCR (Optical Character Recognition) models.
This class is designed to deploy OCR models with additional configurations such as logging, trust for remote code, and port customization.
Attributes:
keys_name_handle: A dictionary mapping input keys to their corresponding handler keys. For example:
- "inputs": Handles general inputs.
- "ocr_files": Also mapped to "inputs".
message_format: A dictionary specifying the expected message format. For example:
- {"inputs": "/path/to/pdf"} indicates that the model expects a PDF file path as input.
default_headers: A dictionary specifying default headers for API requests. Defaults to:
- {"Content-Type": "application/json"}
Args:
launcher: A launcher instance for deploying the model. Defaults to None.
log_path: A string specifying the path where logs should be saved. Defaults to None.
trust_remote_code: A boolean indicating whether to trust remote code execution. Defaults to True.
port: An integer specifying the port for the deployment server. Defaults to None.
finetuned_model: A string specifying the path or name of the fine-tuned OCR model. Defaults to None.
base_model: A string specifying the base model name. If finetuned_model is not provided, base_model will be used. Defaults to None.
Returns:
An instance of [RelayServer][lazyllm.deploy.RelayServer], which acts as the deployment server for the OCR model.
Example:
deployer = OCRDeploy(launcher=launchers.local(), log_path='./logs', port=8080)
server = deployer(finetuned_model='ocr-model')
print(server) # RelayServer instance ready to handle OCR requests
Examples:
>>> from lazyllm.components import OCRDeploy
>>> from lazyllm import launchers
>>> # 创建一个 OCRDeploy 实例
>>> deployer = OCRDeploy(launcher=launchers.local(), log_path='./logs', port=8080)
>>> # 使用微调的 OCR 模型部署服务器
>>> server = deployer(finetuned_model='ocr-model')
>>> # 打印部署服务器信息
>>> print(server)
... <RelayServer instance ready to handle OCR requests>
Source code in lazyllm/components/deploy/ocr/pp_ocr.py
Prompter
lazyllm.components.prompter.LazyLLMPrompterBase
The base class of Prompter. A custom Prompter needs to inherit from this base class and set the Prompt template and the Instruction template using the _init_prompt function provided by the base class, as well as the string used to capture results. Refer to prompt for further understanding of the design philosophy and usage of Prompts.
Both the Prompt template and the Instruction template use {} to indicate the fields to be filled in. The fields that can be included in the Prompt are system, history, tools, user etc., while the fields that can be included in the instruction_template are instruction and extra_keys. If the instruction field is a string, it is considered as a system instruction; if it is a dictionary, it can only contain the keys user and system. user represents the user input instruction, which is placed before the user input in the prompt, and system represents the system instruction, which is placed after the system prompt in the prompt.
instruction is passed in by the application developer, and the instruction can also contain {} to define fillable fields, making it convenient for users to input additional information.
Examples:
>>> from lazyllm.components.prompter import PrompterBase
>>> class MyPrompter(PrompterBase):
... def __init__(self, instruction = None, extra_keys = None, show = False):
... super(__class__, self).__init__(show)
... instruction_template = f'{instruction}\n{{extra_keys}}\n'.replace('{extra_keys}', PrompterBase._get_extro_key_template(extra_keys))
... self._init_prompt("<system>{system}</system>\n</instruction>{instruction}</instruction>{history}\n{input}\n, ## Response::", instruction_template, '## Response::')
...
>>> p = MyPrompter('ins {instruction}')
>>> p.generate_prompt('hello')
'<system>You are an AI-Agent developed by LazyLLM.</system>\n</instruction>ins hello\n\n</instruction>\n\n, ## Response::'
>>> p.generate_prompt('hello world', return_dict=True)
{'messages': [{'role': 'system', 'content': 'You are an AI-Agent developed by LazyLLM.\nins hello world\n\n'}, {'role': 'user', 'content': ''}]}
Source code in lazyllm/components/prompter/builtinPrompt.py
9 10 11 12 13 14 15 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 | |
generate_prompt(input=None, history=None, tools=None, label=None, *, show=False, return_dict=False)
Generate a corresponding Prompt based on user input.
Parameters:
-
input(Option[str | Dict], default:None) –The input from the prompter, if it's a dict, it will be filled into the slots of the instruction; if it's a str, it will be used as input.
-
history(Option[List[List | Dict]], default:None) –Historical conversation, can be
[[u, s], [u, s]]or in openai's history format, defaults to None. -
tools(Option[List[Dict]], default:None) –A collection of tools that can be used, used when the large model performs FunctionCall, defaults to None.
-
label(Option[str], default:None) –Label, used during fine-tuning or training, defaults to None.
-
show(bool, default:False) –Flag indicating whether to print the generated Prompt, defaults to False.
-
return_dict(bool, default:False) –Flag indicating whether to return a dict, generally set to True when using
OnlineChatModule. If returning a dict, only theinstructionwill be filled. Defaults to False.
Source code in lazyllm/components/prompter/builtinPrompt.py
get_response(output, input=None)
Used to truncate the Prompt, keeping only valuable output.
Parameters:
-
output(str) –The output of the large model.
-
input(Option[str], default:None) –The input of the large model. If this parameter is specified, any part of the output that includes the input will be completely truncated. Defaults to None.
Source code in lazyllm/components/prompter/builtinPrompt.py
pre_hook(func=None)
Sets a pre-processing hook function, allowing external custom processing of input data before prompt generation.
Parameters:
-
func(Optional[Callable], default:None) –A callable object to be used as the pre-processing hook function, which receives and processes input data.
Returns:
- LazyLLMPrompterBase: Returns the instance itself to support method chaining.
Source code in lazyllm/components/prompter/builtinPrompt.py
lazyllm.components.prompter.EmptyPrompter
Bases: LazyLLMPrompterBase
An empty prompt generator that inherits from LazyLLMPrompterBase, and directly returns the original input.
This class performs no formatting and is useful for debugging, testing, or as a placeholder.
Examples:
>>> # Even with additional parameters, the input is returned unchanged
>>> prompter.generate_prompt("No-op", history=[["Hi", "Hello"]], tools=[{"name": "search"}], label="debug")
'No-op'
Source code in lazyllm/components/prompter/builtinPrompt.py
generate_prompt(input, history=None, tools=None, label=None, show=False)
A prompt passthrough implementation that inherits from LazyLLMPrompterBase.
This method directly returns the input without any formatting. Useful for debugging, testing, or placeholder use.
Parameters:
-
input(Any) –The input to be returned directly as the prompt.
-
history(Option[List[List | Dict]], default:None) –Dialogue history, ignored. Defaults to None.
-
tools(Option[List[Dict]], default:None) –Tool definitions, ignored. Defaults to None.
-
label(Option[str], default:None) –Label, ignored. Defaults to None.
-
show(bool, default:False) –Whether to print the returned prompt. Defaults to False.
Source code in lazyllm/components/prompter/builtinPrompt.py
lazyllm.components.Prompter
Bases: object
Prompt generator class for LLM input formatting. Supports template-based prompting, history injection, and response extraction.
This class allows prompts to be defined via string templates, loaded from dicts, files, or predefined names. It supports history-aware formatting for multi-turn conversations and adapts to both mapping and string input types.
Parameters:
-
prompt(Optional[str], default:None) –Prompt template string with format placeholders.
-
response_split(Optional[str], default:None) –Optional delimiter to split model response and extract useful output.
-
chat_prompt(Optional[str], default:None) –Chat template string, must contain a history placeholder.
-
history_symbol(str, default:'llm_chat_history') –Name of the placeholder for historical messages, default is 'llm_chat_history'.
-
eoa(Optional[str], default:None) –Delimiter between assistant/user in history items.
-
eoh(Optional[str], default:None) –Delimiter between user-assistant pairs.
-
show(bool, default:False) –Whether to print the final prompt when generating. Default is False.
Examples:
>>> p = Prompter(prompt="Answer the following: {question}")
>>> p.generate_prompt("What is AI?")
'Answer the following: What is AI?'
>>> p.generate_prompt({"question": "Define machine learning"})
'Answer the following: Define machine learning'
>>> p = Prompter(
... prompt="Instruction: {instruction}",
... chat_prompt="Instruction: {instruction}\nHistory:\n{llm_chat_history}",
... history_symbol="llm_chat_history",
... eoa="</s>",
... eoh="|"
... )
>>> p.generate_prompt(
... input={"instruction": "Translate this."},
... history=[["hello", "你好"], ["how are you", "你好吗"]]
... )
'Instruction: Translate this.\nHistory:\nhello|你好</s>how are you|你好吗'
>>> prompt_conf = {
... "prompt": "Task: {task}",
... "response_split": "---"
... }
>>> p = Prompter.from_dict(prompt_conf)
>>> p.generate_prompt("Summarize this article.")
'Task: Summarize this article.'
>>> full_output = "Task: Summarize this article.---This is the summary."
>>> p.get_response(full_output)
'This is the summary.'
Source code in lazyllm/components/prompter/prompter.py
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 | |
from_dict(prompt, *, show=False)
classmethod
Initializes a Prompter instance from a prompt configuration dictionary.
Parameters:
-
prompt(Dict) –A dictionary containing prompt-related configuration. Must include 'prompt' key.
-
show(bool, default:False) –Whether to display the generated prompt. Defaults to False.
Returns:
-
Prompter–An initialized Prompter instance.
Source code in lazyllm/components/prompter/prompter.py
from_template(template_name, *, show=False)
classmethod
Loads prompt configuration from a template name and initializes a Prompter instance.
Parameters:
-
template_name(str) –Name of the template. Must exist in the
templatesdictionary. -
show(bool, default:False) –Whether to display the generated prompt. Defaults to False.
Returns:
-
Prompter–An initialized Prompter instance.
Source code in lazyllm/components/prompter/prompter.py
from_file(fname, *, show=False)
classmethod
Loads prompt configuration from a JSON file and initializes a Prompter instance.
Parameters:
-
fname(str) –Path to the JSON configuration file.
-
show(bool, default:False) –Whether to display the generated prompt. Defaults to False.
Returns:
-
Prompter–An initialized Prompter instance.
Source code in lazyllm/components/prompter/prompter.py
empty()
classmethod
Creates an empty Prompter instance.
Returns:
-
Prompter–A Prompter instance without any prompt configuration.
generate_prompt(input, history=None, tools=None, label=None, show=False)
Generates a formatted prompt string based on input and optional conversation history.
Parameters:
-
input(Union[str, Dict]) –User input. Can be a single string or a dictionary with multiple fields.
-
history(Optional[List[List[str]]], default:None) –Multi-turn dialogue history, e.g., [['u1', 'a1'], ['u2', 'a2']].
-
tools(Optional[Any], default:None) –Not supported. Must be None.
-
label(Optional[str], default:None) –Optional label to append to the prompt, commonly used for training.
-
show(bool, default:False) –Whether to print the generated prompt. Defaults to False.
Returns:
-
str–The final formatted prompt string.
Source code in lazyllm/components/prompter/prompter.py
get_response(response, input=None)
Extracts the actual model answer from the full response returned by an LLM.
Parameters:
-
response(str) –The full raw output from the model.
-
input(Optional[str], default:None) –If the response starts with the input, that part will be removed.
Returns:
-
str–The cleaned model response.
Source code in lazyllm/components/prompter/prompter.py
options: heading_level: 3 inherited_members: - generate_prompt - get_response members: false
lazyllm.components.prompter.EmptyPrompter
Bases: LazyLLMPrompterBase
An empty prompt generator that inherits from LazyLLMPrompterBase, and directly returns the original input.
This class performs no formatting and is useful for debugging, testing, or as a placeholder.
Examples:
>>> # Even with additional parameters, the input is returned unchanged
>>> prompter.generate_prompt("No-op", history=[["Hi", "Hello"]], tools=[{"name": "search"}], label="debug")
'No-op'
Source code in lazyllm/components/prompter/builtinPrompt.py
generate_prompt(input, history=None, tools=None, label=None, show=False)
A prompt passthrough implementation that inherits from LazyLLMPrompterBase.
This method directly returns the input without any formatting. Useful for debugging, testing, or placeholder use.
Parameters:
-
input(Any) –The input to be returned directly as the prompt.
-
history(Option[List[List | Dict]], default:None) –Dialogue history, ignored. Defaults to None.
-
tools(Option[List[Dict]], default:None) –Tool definitions, ignored. Defaults to None.
-
label(Option[str], default:None) –Label, ignored. Defaults to None.
-
show(bool, default:False) –Whether to print the returned prompt. Defaults to False.
Source code in lazyllm/components/prompter/builtinPrompt.py
lazyllm.components.Prompter
Bases: object
Prompt generator class for LLM input formatting. Supports template-based prompting, history injection, and response extraction.
This class allows prompts to be defined via string templates, loaded from dicts, files, or predefined names. It supports history-aware formatting for multi-turn conversations and adapts to both mapping and string input types.
Parameters:
-
prompt(Optional[str], default:None) –Prompt template string with format placeholders.
-
response_split(Optional[str], default:None) –Optional delimiter to split model response and extract useful output.
-
chat_prompt(Optional[str], default:None) –Chat template string, must contain a history placeholder.
-
history_symbol(str, default:'llm_chat_history') –Name of the placeholder for historical messages, default is 'llm_chat_history'.
-
eoa(Optional[str], default:None) –Delimiter between assistant/user in history items.
-
eoh(Optional[str], default:None) –Delimiter between user-assistant pairs.
-
show(bool, default:False) –Whether to print the final prompt when generating. Default is False.
Examples:
>>> p = Prompter(prompt="Answer the following: {question}")
>>> p.generate_prompt("What is AI?")
'Answer the following: What is AI?'
>>> p.generate_prompt({"question": "Define machine learning"})
'Answer the following: Define machine learning'
>>> p = Prompter(
... prompt="Instruction: {instruction}",
... chat_prompt="Instruction: {instruction}\nHistory:\n{llm_chat_history}",
... history_symbol="llm_chat_history",
... eoa="</s>",
... eoh="|"
... )
>>> p.generate_prompt(
... input={"instruction": "Translate this."},
... history=[["hello", "你好"], ["how are you", "你好吗"]]
... )
'Instruction: Translate this.\nHistory:\nhello|你好</s>how are you|你好吗'
>>> prompt_conf = {
... "prompt": "Task: {task}",
... "response_split": "---"
... }
>>> p = Prompter.from_dict(prompt_conf)
>>> p.generate_prompt("Summarize this article.")
'Task: Summarize this article.'
>>> full_output = "Task: Summarize this article.---This is the summary."
>>> p.get_response(full_output)
'This is the summary.'
Source code in lazyllm/components/prompter/prompter.py
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 | |
from_dict(prompt, *, show=False)
classmethod
Initializes a Prompter instance from a prompt configuration dictionary.
Parameters:
-
prompt(Dict) –A dictionary containing prompt-related configuration. Must include 'prompt' key.
-
show(bool, default:False) –Whether to display the generated prompt. Defaults to False.
Returns:
-
Prompter–An initialized Prompter instance.
Source code in lazyllm/components/prompter/prompter.py
from_template(template_name, *, show=False)
classmethod
Loads prompt configuration from a template name and initializes a Prompter instance.
Parameters:
-
template_name(str) –Name of the template. Must exist in the
templatesdictionary. -
show(bool, default:False) –Whether to display the generated prompt. Defaults to False.
Returns:
-
Prompter–An initialized Prompter instance.
Source code in lazyllm/components/prompter/prompter.py
from_file(fname, *, show=False)
classmethod
Loads prompt configuration from a JSON file and initializes a Prompter instance.
Parameters:
-
fname(str) –Path to the JSON configuration file.
-
show(bool, default:False) –Whether to display the generated prompt. Defaults to False.
Returns:
-
Prompter–An initialized Prompter instance.
Source code in lazyllm/components/prompter/prompter.py
empty()
classmethod
Creates an empty Prompter instance.
Returns:
-
Prompter–A Prompter instance without any prompt configuration.
generate_prompt(input, history=None, tools=None, label=None, show=False)
Generates a formatted prompt string based on input and optional conversation history.
Parameters:
-
input(Union[str, Dict]) –User input. Can be a single string or a dictionary with multiple fields.
-
history(Optional[List[List[str]]], default:None) –Multi-turn dialogue history, e.g., [['u1', 'a1'], ['u2', 'a2']].
-
tools(Optional[Any], default:None) –Not supported. Must be None.
-
label(Optional[str], default:None) –Optional label to append to the prompt, commonly used for training.
-
show(bool, default:False) –Whether to print the generated prompt. Defaults to False.
Returns:
-
str–The final formatted prompt string.
Source code in lazyllm/components/prompter/prompter.py
get_response(response, input=None)
Extracts the actual model answer from the full response returned by an LLM.
Parameters:
-
response(str) –The full raw output from the model.
-
input(Optional[str], default:None) –If the response starts with the input, that part will be removed.
Returns:
-
str–The cleaned model response.
Source code in lazyllm/components/prompter/prompter.py
lazyllm.components.AlpacaPrompter
Bases: LazyLLMPrompterBase
Alpaca-style Prompter, supports tool calls, does not support historical dialogue.
Parameters:
-
instruction(Option[str], default:None) –Task instructions for the large model, with at least one fillable slot (e.g.
{instruction}). Or use a dictionary to specify thesystemanduserinstructions. -
extra_keys(Option[List], default:None) –Additional fields that will be filled with user input.
-
show(bool, default:False) –Flag indicating whether to print the generated Prompt, default is False.
-
tools(Option[list], default:None) –Tool-set which is provived for LLMs, default is None.
Examples:
>>> from lazyllm import AlpacaPrompter
>>> p = AlpacaPrompter('hello world {instruction}')
>>> p.generate_prompt('this is my input')
'You are an AI-Agent developed by LazyLLM.\nBelow is an instruction that describes a task, paired with extra messages such as input that provides further context if possible. Write a response that appropriately completes the request.\n\n ### Instruction:\nhello world this is my input\n\n\n### Response:\n'
>>> p.generate_prompt('this is my input', return_dict=True)
{'messages': [{'role': 'system', 'content': 'You are an AI-Agent developed by LazyLLM.\nBelow is an instruction that describes a task, paired with extra messages such as input that provides further context if possible. Write a response that appropriately completes the request.\n\n ### Instruction:\nhello world this is my input\n\n'}, {'role': 'user', 'content': ''}]}
>>>
>>> p = AlpacaPrompter('hello world {instruction}, {input}', extra_keys=['knowledge'])
>>> p.generate_prompt(dict(instruction='hello world', input='my input', knowledge='lazyllm'))
'You are an AI-Agent developed by LazyLLM.\nBelow is an instruction that describes a task, paired with extra messages such as input that provides further context if possible. Write a response that appropriately completes the request.\n\n ### Instruction:\nhello world hello world, my input\n\nHere are some extra messages you can referred to:\n\n### knowledge:\nlazyllm\n\n\n### Response:\n'
>>> p.generate_prompt(dict(instruction='hello world', input='my input', knowledge='lazyllm'), return_dict=True)
{'messages': [{'role': 'system', 'content': 'You are an AI-Agent developed by LazyLLM.\nBelow is an instruction that describes a task, paired with extra messages such as input that provides further context if possible. Write a response that appropriately completes the request.\n\n ### Instruction:\nhello world hello world, my input\n\nHere are some extra messages you can referred to:\n\n### knowledge:\nlazyllm\n\n'}, {'role': 'user', 'content': ''}]}
>>>
>>> p = AlpacaPrompter(dict(system="hello world", user="this is user instruction {input}"))
>>> p.generate_prompt(dict(input="my input"))
'You are an AI-Agent developed by LazyLLM.\nBelow is an instruction that describes a task, paired with extra messages such as input that provides further context if possible. Write a response that appropriately completes the request.\n\n ### Instruction:\nhello word\n\n\n\nthis is user instruction my input### Response:\n'
>>> p.generate_prompt(dict(input="my input"), return_dict=True)
{'messages': [{'role': 'system', 'content': 'You are an AI-Agent developed by LazyLLM.\nBelow is an instruction that describes a task, paired with extra messages such as input that provides further context if possible. Write a response that appropriately completes the request.\n\n ### Instruction:\nhello world'}, {'role': 'user', 'content': 'this is user instruction my input'}]}
Source code in lazyllm/components/prompter/alpacaPrompter.py
generate_prompt(input=None, history=None, tools=None, label=None, *, show=False, return_dict=False)
Generate a corresponding Prompt based on user input.
Parameters:
-
input(Option[str | Dict], default:None) –The input from the prompter, if it's a dict, it will be filled into the slots of the instruction; if it's a str, it will be used as input.
-
history(Option[List[List | Dict]], default:None) –Historical conversation, can be
[[u, s], [u, s]]or in openai's history format, defaults to None. -
tools(Option[List[Dict]], default:None) –A collection of tools that can be used, used when the large model performs FunctionCall, defaults to None.
-
label(Option[str], default:None) –Label, used during fine-tuning or training, defaults to None.
-
show(bool, default:False) –Flag indicating whether to print the generated Prompt, defaults to False.
-
return_dict(bool, default:False) –Flag indicating whether to return a dict, generally set to True when using
OnlineChatModule. If returning a dict, only theinstructionwill be filled. Defaults to False.
Source code in lazyllm/components/prompter/builtinPrompt.py
get_response(output, input=None)
Used to truncate the Prompt, keeping only valuable output.
Parameters:
-
output(str) –The output of the large model.
-
input(Option[str], default:None) –The input of the large model. If this parameter is specified, any part of the output that includes the input will be completely truncated. Defaults to None.
Source code in lazyllm/components/prompter/builtinPrompt.py
lazyllm.components.ChatPrompter
Bases: LazyLLMPrompterBase
Prompt constructor for multi-turn dialogue, inherits from LazyLLMPrompterBase.
Supports tool calling, conversation history, and customizable instruction templates. Accepts instructions as either plain string or dict with separate system and user components, automatically merging them into a unified prompt template. Also supports injecting extra user-defined fields.
Parameters:
-
instruction(Option[str | Dict[str, str]], default:None) –The prompt instruction template. Can be a string or a dict with
systemanduserkeys. If a dict is given, the components will be merged using special delimiters. -
extra_keys(Option[List[str]], default:None) –A list of additional keys that will be filled by user input to enrich the prompt context.
-
show(bool, default:False) –Whether to print the generated prompt. Default is False.
-
tools(Option[List], default:None) –A list of tools available to the model for function-calling tasks. Default is None.
-
history(Option[List[List[str]]], default:None) –Dialogue history in the format [[user, assistant], ...]. Used to provide conversational memory. Default is None.
Examples:
- Simple instruction string
>>> p = ChatPrompter('hello world')
>>> p.generate_prompt('this is my input')
'You are an AI-Agent developed by LazyLLM.hello world\nthis is my input\n'
>>> p.generate_prompt('this is my input', return_dict=True)
{'messages': [{'role': 'system', 'content': 'You are an AI-Agent developed by LazyLLM.\nhello world'}, {'role': 'user', 'content': 'this is my input'}]}
- Using extra_keys
>>> p = ChatPrompter('hello world {instruction}', extra_keys=['knowledge'])
>>> p.generate_prompt({
... 'instruction': 'this is my ins',
... 'input': 'this is my inp',
... 'knowledge': 'LazyLLM-Knowledge'
... })
'You are an AI-Agent developed by LazyLLM.hello world this is my ins\nHere are some extra messages you can referred to:\n\n### knowledge:\nLazyLLM-Knowledge\nthis is my inp\n'
- With conversation history
>>> p.generate_prompt({
... 'instruction': 'this is my ins',
... 'input': 'this is my inp',
... 'knowledge': 'LazyLLM-Knowledge'
... }, history=[['s1', 'e1'], ['s2', 'e2']])
'You are an AI-Agent developed by LazyLLM.hello world this is my ins\nHere are some extra messages you can referred to:\n\n### knowledge:\nLazyLLM-Knowledge\ns1|e1\ns2|e2\nthis is my inp\n'
- Using dict format for system/user instructions
>>> p = ChatPrompter(dict(system="hello world", user="this is user instruction {input}"))
>>> p.generate_prompt({'input': "my input", 'query': "this is user query"})
'You are an AI-Agent developed by LazyLLM.hello world\nthis is user instruction my input this is user query\n'
>>> p.generate_prompt({'input': "my input", 'query': "this is user query"}, return_dict=True)
{'messages': [{'role': 'system', 'content': 'You are an AI-Agent developed by LazyLLM.\nhello world'}, {'role': 'user', 'content': 'this is user instruction my input this is user query'}]}
Source code in lazyllm/components/prompter/chatPrompter.py
generate_prompt(input=None, history=None, tools=None, label=None, *, show=False, return_dict=False)
Generate a corresponding Prompt based on user input.
Parameters:
-
input(Option[str | Dict], default:None) –The input from the prompter, if it's a dict, it will be filled into the slots of the instruction; if it's a str, it will be used as input.
-
history(Option[List[List | Dict]], default:None) –Historical conversation, can be
[[u, s], [u, s]]or in openai's history format, defaults to None. -
tools(Option[List[Dict]], default:None) –A collection of tools that can be used, used when the large model performs FunctionCall, defaults to None.
-
label(Option[str], default:None) –Label, used during fine-tuning or training, defaults to None.
-
show(bool, default:False) –Flag indicating whether to print the generated Prompt, defaults to False.
-
return_dict(bool, default:False) –Flag indicating whether to return a dict, generally set to True when using
OnlineChatModule. If returning a dict, only theinstructionwill be filled. Defaults to False.
Source code in lazyllm/components/prompter/builtinPrompt.py
get_response(output, input=None)
Used to truncate the Prompt, keeping only valuable output.
Parameters:
-
output(str) –The output of the large model.
-
input(Option[str], default:None) –The input of the large model. If this parameter is specified, any part of the output that includes the input will be completely truncated. Defaults to None.
Source code in lazyllm/components/prompter/builtinPrompt.py
MultiModal
Text to Image
lazyllm.components.StableDiffusionDeploy
Bases: LazyLLMDeployBase
Source code in lazyllm/components/deploy/stable_diffusion/stable_diffusion3.py
Visual Question Answering
Reference LMDeploy, which supports the Visual Question Answering model.
Text to Sound
lazyllm.components.TTSDeploy
Source code in lazyllm/components/deploy/text_to_speech/__init__.py
lazyllm.components.ChatTTSDeploy
Bases: TTSBase
ChatTTS Model Deployment Class. This class is used to deploy the ChatTTS model to a specified server for network invocation.
__init__(self, launcher=None)
Constructor, initializes the deployment class.
Parameters:
-
launcher(launcher, default:None) –An instance of the launcher used to start the remote service.
__call__(self, finetuned_model=None, base_model=None)
Deploys the model and returns the remote service address.
Parameters:
-
finetuned_model(str) –If provided, this model will be used for deployment; if not provided or the path is invalid,
base_modelwill be used. -
base_model(str) –The default model, which will be used for deployment if
finetuned_modelis invalid. -
Return(str) –The URL address of the remote service.
Notes
- Input for infer:
str. The text corresponding to the audio to be generated. - Return of infer: The string encoded from the generated file paths, starting with the encoding flag "
", followed by the serialized dictionary. The key filesin the dictionary stores a list, with elements being the paths of the generated audio files. - Supported models: ChatTTS
Examples:
>>> from lazyllm import launchers, UrlModule
>>> from lazyllm.components import ChatTTSDeploy
>>> deployer = ChatTTSDeploy(launchers.remote())
>>> url = deployer(base_model='ChatTTS')
>>> model = UrlModule(url=url)
>>> res = model('Hello World!')
>>> print(res)
... <lazyllm-query>{"query": "", "files": ["path/to/chattts/sound_xxx.wav"]}
Source code in lazyllm/components/deploy/text_to_speech/chattts.py
lazyllm.components.BarkDeploy
Bases: TTSBase
Bark Model Deployment Class. This class is used to deploy the Bark model to a specified server for network invocation.
__init__(self, launcher=None)
Constructor, initializes the deployment class.
Parameters:
-
launcher(launcher, default:None) –An instance of the launcher used to start the remote service.
__call__(self, finetuned_model=None, base_model=None)
Deploys the model and returns the remote service address.
Parameters:
-
finetuned_model(str) –If provided, this model will be used for deployment; if not provided or the path is invalid,
base_modelwill be used. -
base_model(str) –The default model, which will be used for deployment if
finetuned_modelis invalid. -
Return(str) –The URL address of the remote service.
Notes
- Input for infer:
str. The text corresponding to the audio to be generated. - Return of infer: The string encoded from the generated file paths, starting with the encoding flag "
", followed by the serialized dictionary. The key filesin the dictionary stores a list, with elements being the paths of the generated audio files. - Supported models: bark
Examples:
>>> from lazyllm import launchers, UrlModule
>>> from lazyllm.components import BarkDeploy
>>> deployer = BarkDeploy(launchers.remote())
>>> url = deployer(base_model='bark')
>>> model = UrlModule(url=url)
>>> res = model('Hello World!')
>>> print(res)
... <lazyllm-query>{"query": "", "files": ["path/to/bark/sound_xxx.wav"]}
Source code in lazyllm/components/deploy/text_to_speech/bark.py
lazyllm.components.MusicGenDeploy
Bases: TTSBase
MusicGen Model Deployment Class. This class is used to deploy the MusicGen model to a specified server for network invocation.
__init__(self, launcher=None)
Constructor, initializes the deployment class.
Parameters:
-
launcher(launcher, default:None) –An instance of the launcher used to start the remote service.
__call__(self, finetuned_model=None, base_model=None)
Deploys the model and returns the remote service address.
Parameters:
-
finetuned_model(str) –If provided, this model will be used for deployment; if not provided or the path is invalid,
base_modelwill be used. -
base_model(str) –The default model, which will be used for deployment if
finetuned_modelis invalid. -
Return(str) –The URL address of the remote service.
Notes
- Input for infer:
str. The text corresponding to the audio to be generated. - Return of infer: The string encoded from the generated file paths, starting with the encoding flag "
", followed by the serialized dictionary. The key filesin the dictionary stores a list, with elements being the paths of the generated audio files. - Supported models: musicgen-small
Examples:
>>> from lazyllm import launchers, UrlModule
>>> from lazyllm.components import MusicGenDeploy
>>> deployer = MusicGenDeploy(launchers.remote())
>>> url = deployer(base_model='musicgen-small')
>>> model = UrlModule(url=url)
>>> model('Symphony with flute as the main melody')
... <lazyllm-query>{"query": "", "files": ["path/to/musicgen/sound_xxx.wav"]}
Source code in lazyllm/components/deploy/text_to_speech/musicgen.py
Speech to Text
lazyllm.components.SenseVoiceDeploy
Bases: LazyLLMDeployBase
SenseVoice Model Deployment Class. This class is used to deploy the SenseVoice model to a specified server for network invocation.
__init__(self, launcher=None)
Constructor, initializes the deployment class.
Parameters:
-
launcher(launcher, default:None) –An instance of the launcher used to start the remote service.
__call__(self, finetuned_model=None, base_model=None)
Deploys the model and returns the remote service address.
Parameters:
-
finetuned_model(str) –If provided, this model will be used for deployment; if not provided or the path is invalid,
base_modelwill be used. -
base_model(str) –The default model, which will be used for deployment if
finetuned_modelis invalid. -
Return(str) –The URL address of the remote service.
Notes
- Input for infer:
str. The audio path or link. - Return of infer:
str. The recognized content. - Supported models: SenseVoiceSmall
Examples:
>>> import os
>>> import lazyllm
>>> from lazyllm import launchers, UrlModule
>>> from lazyllm.components import SenseVoiceDeploy
>>> deployer = SenseVoiceDeploy(launchers.remote())
>>> url = deployer(base_model='SenseVoiceSmall')
>>> model = UrlModule(url=url)
>>> model('path/to/audio') # support format: .mp3, .wav
... xxxxxxxxxxxxxxxx
Source code in lazyllm/components/deploy/speech_to_text/sense_voice.py
lazyllm.components.deploy.speech_to_text.sense_voice.SenseVoice
Bases: object
SenseVoice(base_path, source=None, init=False)
A speech-to-text wrapper using FunASR models for lazy initialization and audio transcription. This class supports automatic model downloading, safe initialization, and inference from audio paths or URLs.
Parameters:
- base_path (str): Path or model identifier to download the STT model.
- source (str, optional): Model source name; defaults to lazyllm.config['model_source'].
- init (bool): Whether to initialize the model immediately on creation.
Attributes: - base_path (str): Final resolved path of the model after download. - model: Loaded FunASR model instance. - init_flag: A lazy flag used to ensure model is only loaded once.
- call(string: str | dict) -> str: Transcribes the input audio file or URL to text. Accepts base64-encoded content, file paths, or URLs.
- load_stt(): Loads the FunASR speech-to-text model and related VAD (Voice Activity Detection).
- rebuild(base_path, init): Rebuilds the class instance (used for serialization).
- reduce(): Supports pickling by ensuring proper lazy-loading on deserialization.
Source code in lazyllm/components/deploy/speech_to_text/sense_voice.py
14 15 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 | |
load_stt()
load_stt()
Loads the speech-to-text model using FunASR with optional support for Huawei NPU via torch_npu.
The method initializes the model with the following characteristics:
- Uses fsmn-vad for voice activity detection with long utterance support.
- Sets maximum single segment time to 30 seconds.
- Selects cuda:0 as the default inference device.
The model is stored in self.model and will be used to transcribe audio input.
Note:
If torch_npu is available in the environment, the function attempts to load it for potential Huawei Ascend acceleration.
Source code in lazyllm/components/deploy/speech_to_text/sense_voice.py
rebuild(base_path, init)
classmethod
rebuild(base_path: str, init: bool) -> SenseVoice
Class method used to reconstruct a SenseVoice instance during deserialization (e.g., when using cloudpickle).
Parameters: - base_path (str): Path to the speech-to-text model. - init (bool): Whether to immediately initialize and load the model upon creation.
Returns:
- A new instance of SenseVoice with the specified configuration.
Note: This method is internally used to support model serialization and multiprocessing compatibility.
Source code in lazyllm/components/deploy/speech_to_text/sense_voice.py
ModelManager
lazyllm.components.ModelManager
ModelManager is a utility class provided by LazyLLM for developers to automatically download models. Currently, it supports search for models from local directories, as well as automatically downloading model from huggingface or modelscope. Before using ModelManager, the following environment variables need to be set:
- LAZYLLM_MODEL_SOURCE: The source for model downloads, which can be set to
huggingfaceormodelscope. - LAZYLLM_MODEL_SOURCE_TOKEN: The token provided by
huggingfaceormodelscopefor private model download. - LAZYLLM_MODEL_PATH: A colon-separated
:list of local absolute paths for model search. - LAZYLLM_MODEL_CACHE_DIR: Directory for downloaded models.
Other Parameters:
-
model_source(str) –The source for model downloads, currently only supports
huggingfaceormodelscope. If necessary, ModelManager downloads model data from the source. If not provided, LAZYLLM_MODEL_SOURCE environment variable would be used, and if LAZYLLM_MODEL_SOURCE is not set, ModelManager will not download any model. -
token(str) –The token provided by
huggingfaceormodelscope. If the token is present, ModelManager uses the token to download model. If not provided, LAZYLLM_MODEL_SOURCE_TOKEN environment variable would be used. and if LAZYLLM_MODEL_SOURCE_TOKEN is not set, ModelManager will not download private models, only public ones. -
model_path(str) –A colon-separated list of absolute paths. Before actually start to download model, ModelManager trys to find the target model in the directories in this list. If not provided, LAZYLLM_MODEL_PATH environment variable would be used, and LAZYLLM_MODEL_PATH is not set, ModelManager skips looking for models from model_path.
-
cache_dir(str) –An absolute path of a directory to save downloaded models. If not provided, LAZYLLM_MODEL_CACHE_DIR environment variable would be used, and if LAZYLLM_MODEL_PATH is not set, the default value is ~/.lazyllm/model.
ModelManager.download(model) -> str
Download models from model_source. The function first searches for the target model in directories listed in the model_path parameter of ModelManager class. If not found, it searches under cache_dir. If still not found, it downloads the model from model_source and stores it under cache_dir.
Parameters:
-
model(str) –The name of the target model. The function uses this name to download the model from model_source.
Examples:
>>> from lazyllm.components import ModelManager
>>> downloader = ModelManager(model_source='modelscope')
>>> downloader.download('chatglm3-6b')
Source code in lazyllm/components/utils/downloader/model_downloader.py
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 | |
Formatter
lazyllm.components.formatter.LazyLLMFormatterBase
This class is the base class of the formatter. The formatter is the formatter of the model output result. Users can customize the formatter or use the formatter provided by LazyLLM. Main methods: _parse_formatter: parse the index content. _load: Parse the str object, and the part containing Python objects is parsed out, such as list, dict and other objects. _parse_py_data_by_formatter: format the python object according to the custom formatter and index. format: format the passed content. If the content is a string type, convert the string into a python object first, and then format it. If the content is a python object, format it directly.
Examples:
>>> from lazyllm.components.formatter import FormatterBase
>>> class MyFormatter(FormatterBase):
... def __init__(self, formatter: str = None):
... self._formatter = formatter
... if self._formatter:
... self._parse_formatter()
... else:
... self._slices = None
... def _parse_formatter(self):
... slice_str = self._formatter.strip()[1:-1]
... slices = []
... parts = slice_str.split(":")
... start = int(parts[0]) if parts[0] else None
... end = int(parts[1]) if len(parts) > 1 and parts[1] else None
... step = int(parts[2]) if len(parts) > 2 and parts[2] else None
... slices.append(slice(start, end, step))
... self._slices = slices
... def _load(self, data):
... return [int(x) for x in data.strip('[]').split(',')]
... def _parse_py_data_by_formatter(self, data):
... if self._slices is not None:
... result = []
... for s in self._slices:
... if isinstance(s, slice):
... result.extend(data[s])
... else:
... result.append(data[int(s)])
... return result
... else:
... return data
...
>>> fmt = MyFormatter("[1:3]")
>>> res = fmt.format("[1,2,3,4,5]")
>>> print(res)
[2, 3]
Source code in lazyllm/components/formatter/formatterbase.py
lazyllm.components.formatter.formatterbase.JsonLikeFormatter
Bases: LazyLLMFormatterBase
This class is used to extract subfields from nested structures (like dicts, lists, tuples) using a JSON-like indexing syntax.
The behavior is driven by a formatter string similar to Python-style slicing and dictionary access:
[0]fetches the first item[0][{key}]accesses thekeyfield in the first item[0,1][{a,b}]fetches theaandbfields from the first and second items[::2]does slicing with a step of 2*[0][{x}]means return a wrapped/structured result
Parameters:
-
formatter(str, default:None) –A format string that controls how to slice and extract the structure. If None, the input will be returned directly.
Examples:
>>> from lazyllm.components.formatter.formatterbase import JsonLikeFormatter
>>> formatter = JsonLikeFormatter("[{a,b}]")
Source code in lazyllm/components/formatter/formatterbase.py
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 | |
lazyllm.components.formatter.formatterbase.PythonFormatter
Bases: JsonLikeFormatter
Reserved formatter class for supporting Python-style data extraction syntax. To be developed.
Currently inherits from JsonLikeFormatter with no additional behavior.
Source code in lazyllm/components/formatter/formatterbase.py
lazyllm.components.formatter.FileFormatter
Bases: LazyLLMFormatterBase
A formatter that transforms query strings with document context between structured formats.
Supports three modes:
- "decode": Decodes structured query strings into dictionaries with query and files.
- "encode": Encodes a dictionary with query and files into a structured query string.
- "merge": Merges multiple structured query strings into one.
Parameters:
-
formatter(str, default:'decode') –The operation mode. Must be one of "decode", "encode", or "merge". Defaults to "decode".
Examples:
Source code in lazyllm/components/formatter/formatterbase.py
lazyllm.components.formatter.YamlFormatter
Bases: JsonLikeFormatter
A formatter for extracting structured information from YAML-formatted strings.
Inherits from JsonLikeFormatter. Uses the internal method to parse YAML strings into Python objects, and then applies JSON-like formatting rules to extract desired fields.
Suitable for handling nested YAML content with formatter-based field selection.
Examples:
>>> from lazyllm.components.formatter import YamlFormatter
>>> formatter = YamlFormatter("{name,age}")
>>> msg = """
... name: Alice
... age: 30
... city: London
... """
>>> formatter(msg)
{'name': 'Alice', 'age': 30}
Source code in lazyllm/components/formatter/yamlformatter.py
lazyllm.components.formatter.encode_query_with_filepaths(query=None, files=None)
Encodes a query string together with associated file paths into a structured string format with context.
If file paths are provided, the query and file list will be wrapped into a JSON object prefixed with __lazyllm_docs__. Otherwise, it returns the original query string.
Parameters:
-
query(str, default:None) –The user query string. Defaults to an empty string.
-
files(str or List[str], default:None) –File path(s) associated with the query. Can be a single string or a list of strings.
Returns:
-
str(str) –A structured encoded query string or the raw query.
Raises:
-
AssertionError–If
filesis not a string or list of strings.
Examples:
>>> # Encode a query along with associated documentation files
>>> encode_query_with_filepaths("Generate questions based on the document", files=["a.md"])
'<lazyllm-query>{"query": "Generate questions based on the document", "files": ["a.md"]}'
Source code in lazyllm/components/formatter/formatterbase.py
lazyllm.components.formatter.decode_query_with_filepaths(query_files)
Decodes a structured query string into a dictionary containing the original query and file paths.
If the input string starts with the special prefix __lazyllm_docs__, it attempts to parse the JSON content; otherwise, it returns the raw query string as-is.
Parameters:
-
query_files(str) –The encoded query string that may include both query and file paths.
Returns:
-
Union[dict, str]–Union[dict, str]: A dictionary containing 'query' and 'files' if structured, otherwise the original query string.
Raises:
-
AssertionError–If the input is not a string.
-
ValueError–If the string is prefixed but JSON decoding fails.
Examples:
>>> # Decode a structured query with files
>>> decode_query_with_filepaths('<lazyllm-query>{"query": "Summarize the content", "files": ["doc.md"]}')
{'query': 'Summarize the content', 'files': ['doc.md']}
>>> # Decode a plain string without files
>>> decode_query_with_filepaths("This is just a simple question")
'This is just a simple question'
Source code in lazyllm/components/formatter/formatterbase.py
lazyllm.components.formatter.lazyllm_merge_query(*args)
Merges multiple query strings (potentially with associated file paths) into a single structured query string.
Each argument can be a plain query string or a structured query created by encode_query_with_filepaths. The function decodes each input, concatenates all query texts, and merges the associated file paths. The final result is re-encoded into a single query string with unified context.
Parameters:
-
*args(str, default:()) –Multiple query strings. Each can be either plain text or an encoded structured query with files.
Returns:
-
str(str) –A single structured query string containing the merged query and file paths.
Examples:
>>> # Merge two structured queries with English content and associated files
>>> q1 = encode_query_with_filepaths("Please summarize document one", files=["doc1.md"])
>>> q2 = encode_query_with_filepaths("Add details from document two", files=["doc2.md"])
>>> lazyllm_merge_query(q1, q2)
'<lazyllm-query>{"query": "Please summarize document oneAdd details from document two", "files": ["doc1.md", "doc2.md"]}'
>>> # Merge plain English text queries without documents
>>> lazyllm_merge_query("What is AI?", "Explain deep learning.")
'What is AI?Explain deep learning.'
Source code in lazyllm/components/formatter/formatterbase.py
lazyllm.components.formatter.formatterbase.JsonLikeFormatter
Bases: LazyLLMFormatterBase
This class is used to extract subfields from nested structures (like dicts, lists, tuples) using a JSON-like indexing syntax.
The behavior is driven by a formatter string similar to Python-style slicing and dictionary access:
[0]fetches the first item[0][{key}]accesses thekeyfield in the first item[0,1][{a,b}]fetches theaandbfields from the first and second items[::2]does slicing with a step of 2*[0][{x}]means return a wrapped/structured result
Parameters:
-
formatter(str, default:None) –A format string that controls how to slice and extract the structure. If None, the input will be returned directly.
Examples:
>>> from lazyllm.components.formatter.formatterbase import JsonLikeFormatter
>>> formatter = JsonLikeFormatter("[{a,b}]")
Source code in lazyllm/components/formatter/formatterbase.py
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 | |
lazyllm.components.formatter.formatterbase.PythonFormatter
Bases: JsonLikeFormatter
Reserved formatter class for supporting Python-style data extraction syntax. To be developed.
Currently inherits from JsonLikeFormatter with no additional behavior.
Source code in lazyllm/components/formatter/formatterbase.py
lazyllm.components.formatter.FileFormatter
Bases: LazyLLMFormatterBase
A formatter that transforms query strings with document context between structured formats.
Supports three modes:
- "decode": Decodes structured query strings into dictionaries with query and files.
- "encode": Encodes a dictionary with query and files into a structured query string.
- "merge": Merges multiple structured query strings into one.
Parameters:
-
formatter(str, default:'decode') –The operation mode. Must be one of "decode", "encode", or "merge". Defaults to "decode".
Examples:
Source code in lazyllm/components/formatter/formatterbase.py
lazyllm.components.formatter.YamlFormatter
Bases: JsonLikeFormatter
A formatter for extracting structured information from YAML-formatted strings.
Inherits from JsonLikeFormatter. Uses the internal method to parse YAML strings into Python objects, and then applies JSON-like formatting rules to extract desired fields.
Suitable for handling nested YAML content with formatter-based field selection.
Examples:
>>> from lazyllm.components.formatter import YamlFormatter
>>> formatter = YamlFormatter("{name,age}")
>>> msg = """
... name: Alice
... age: 30
... city: London
... """
>>> formatter(msg)
{'name': 'Alice', 'age': 30}
Source code in lazyllm/components/formatter/yamlformatter.py
lazyllm.components.formatter.encode_query_with_filepaths(query=None, files=None)
Encodes a query string together with associated file paths into a structured string format with context.
If file paths are provided, the query and file list will be wrapped into a JSON object prefixed with __lazyllm_docs__. Otherwise, it returns the original query string.
Parameters:
-
query(str, default:None) –The user query string. Defaults to an empty string.
-
files(str or List[str], default:None) –File path(s) associated with the query. Can be a single string or a list of strings.
Returns:
-
str(str) –A structured encoded query string or the raw query.
Raises:
-
AssertionError–If
filesis not a string or list of strings.
Examples:
>>> # Encode a query along with associated documentation files
>>> encode_query_with_filepaths("Generate questions based on the document", files=["a.md"])
'<lazyllm-query>{"query": "Generate questions based on the document", "files": ["a.md"]}'
Source code in lazyllm/components/formatter/formatterbase.py
lazyllm.components.formatter.decode_query_with_filepaths(query_files)
Decodes a structured query string into a dictionary containing the original query and file paths.
If the input string starts with the special prefix __lazyllm_docs__, it attempts to parse the JSON content; otherwise, it returns the raw query string as-is.
Parameters:
-
query_files(str) –The encoded query string that may include both query and file paths.
Returns:
-
Union[dict, str]–Union[dict, str]: A dictionary containing 'query' and 'files' if structured, otherwise the original query string.
Raises:
-
AssertionError–If the input is not a string.
-
ValueError–If the string is prefixed but JSON decoding fails.
Examples:
>>> # Decode a structured query with files
>>> decode_query_with_filepaths('<lazyllm-query>{"query": "Summarize the content", "files": ["doc.md"]}')
{'query': 'Summarize the content', 'files': ['doc.md']}
>>> # Decode a plain string without files
>>> decode_query_with_filepaths("This is just a simple question")
'This is just a simple question'
Source code in lazyllm/components/formatter/formatterbase.py
lazyllm.components.formatter.lazyllm_merge_query(*args)
Merges multiple query strings (potentially with associated file paths) into a single structured query string.
Each argument can be a plain query string or a structured query created by encode_query_with_filepaths. The function decodes each input, concatenates all query texts, and merges the associated file paths. The final result is re-encoded into a single query string with unified context.
Parameters:
-
*args(str, default:()) –Multiple query strings. Each can be either plain text or an encoded structured query with files.
Returns:
-
str(str) –A single structured query string containing the merged query and file paths.
Examples:
>>> # Merge two structured queries with English content and associated files
>>> q1 = encode_query_with_filepaths("Please summarize document one", files=["doc1.md"])
>>> q2 = encode_query_with_filepaths("Add details from document two", files=["doc2.md"])
>>> lazyllm_merge_query(q1, q2)
'<lazyllm-query>{"query": "Please summarize document oneAdd details from document two", "files": ["doc1.md", "doc2.md"]}'
>>> # Merge plain English text queries without documents
>>> lazyllm_merge_query("What is AI?", "Explain deep learning.")
'What is AI?Explain deep learning.'
Source code in lazyllm/components/formatter/formatterbase.py
lazyllm.components.JsonFormatter
Bases: JsonLikeFormatter
This class is a JSON formatter, that is, the user wants the model to output content is JSON format, and can also select a field in the output content by indexing.
Examples:
>>> import lazyllm
>>> from lazyllm.components import JsonFormatter
>>> toc_prompt='''
... You are now an intelligent assistant. Your task is to understand the user's input and convert the outline into a list of nested dictionaries. Each dictionary contains a `title` and a `describe`, where the `title` should clearly indicate the level using Markdown format, and the `describe` is a description and writing guide for that section.
...
... Please generate the corresponding list of nested dictionaries based on the following user input:
...
... Example output:
... [
... {
... "title": "# Level 1 Title",
... "describe": "Please provide a detailed description of the content under this title, offering background information and core viewpoints."
... },
... {
... "title": "## Level 2 Title",
... "describe": "Please provide a detailed description of the content under this title, giving specific details and examples to support the viewpoints of the Level 1 title."
... },
... {
... "title": "### Level 3 Title",
... "describe": "Please provide a detailed description of the content under this title, deeply analyzing and providing more details and data support."
... }
... ]
... User input is as follows:
... '''
>>> query = "Please help me write an article about the application of artificial intelligence in the medical field."
>>> m = lazyllm.TrainableModule("internlm2-chat-20b").prompt(toc_prompt).start()
>>> ret = m(query, max_new_tokens=2048)
>>> print(f"ret: {ret!r}") # the model output without specifying a formatter
'Based on your user input, here is the corresponding list of nested dictionaries:
[
{
"title": "# Application of Artificial Intelligence in the Medical Field",
"describe": "Please provide a detailed description of the application of artificial intelligence in the medical field, including its benefits, challenges, and future prospects."
},
{
"title": "## AI in Medical Diagnosis",
"describe": "Please provide a detailed description of how artificial intelligence is used in medical diagnosis, including specific examples of AI-based diagnostic tools and their impact on patient outcomes."
},
{
"title": "### AI in Medical Imaging",
"describe": "Please provide a detailed description of how artificial intelligence is used in medical imaging, including the advantages of AI-based image analysis and its applications in various medical specialties."
},
{
"title": "### AI in Drug Discovery and Development",
"describe": "Please provide a detailed description of how artificial intelligence is used in drug discovery and development, including the role of AI in identifying potential drug candidates and streamlining the drug development process."
},
{
"title": "## AI in Medical Research",
"describe": "Please provide a detailed description of how artificial intelligence is used in medical research, including its applications in genomics, epidemiology, and clinical trials."
},
{
"title": "### AI in Genomics and Precision Medicine",
"describe": "Please provide a detailed description of how artificial intelligence is used in genomics and precision medicine, including the role of AI in analyzing large-scale genomic data and tailoring treatments to individual patients."
},
{
"title": "### AI in Epidemiology and Public Health",
"describe": "Please provide a detailed description of how artificial intelligence is used in epidemiology and public health, including its applications in disease surveillance, outbreak prediction, and resource allocation."
},
{
"title": "### AI in Clinical Trials",
"describe": "Please provide a detailed description of how artificial intelligence is used in clinical trials, including its role in patient recruitment, trial design, and data analysis."
},
{
"title": "## AI in Medical Practice",
"describe": "Please provide a detailed description of how artificial intelligence is used in medical practice, including its applications in patient monitoring, personalized medicine, and telemedicine."
},
{
"title": "### AI in Patient Monitoring",
"describe": "Please provide a detailed description of how artificial intelligence is used in patient monitoring, including its role in real-time monitoring of vital signs and early detection of health issues."
},
{
"title": "### AI in Personalized Medicine",
"describe": "Please provide a detailed description of how artificial intelligence is used in personalized medicine, including its role in analyzing patient data to tailor treatments and predict outcomes."
},
{
"title": "### AI in Telemedicine",
"describe": "Please provide a detailed description of how artificial intelligence is used in telemedicine, including its applications in remote consultations, virtual diagnoses, and digital health records."
},
{
"title": "## AI in Medical Ethics and Policy",
"describe": "Please provide a detailed description of the ethical and policy considerations surrounding the use of artificial intelligence in the medical field, including issues related to data privacy, bias, and accountability."
}
]'
>>> m = lazyllm.TrainableModule("internlm2-chat-20b").formatter(JsonFormatter("[:][title]")).prompt(toc_prompt).start()
>>> ret = m(query, max_new_tokens=2048)
>>> print(f"ret: {ret}") # the model output of the specified formaater
['# Application of Artificial Intelligence in the Medical Field', '## AI in Medical Diagnosis', '### AI in Medical Imaging', '### AI in Drug Discovery and Development', '## AI in Medical Research', '### AI in Genomics and Precision Medicine', '### AI in Epidemiology and Public Health', '### AI in Clinical Trials', '## AI in Medical Practice', '### AI in Patient Monitoring', '### AI in Personalized Medicine', '### AI in Telemedicine', '## AI in Medical Ethics and Policy']
Source code in lazyllm/components/formatter/jsonformatter.py
5 6 7 8 9 10 11 12 13 14 15 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 | |
lazyllm.components.EmptyFormatter
Bases: LazyLLMFormatterBase
This type is the system default formatter. When the user does not specify anything or does not want to format the model output, this type is selected. The model output will be in the same format.
Examples:
>>> import lazyllm
>>> from lazyllm.components import EmptyFormatter
>>> toc_prompt='''
... You are now an intelligent assistant. Your task is to understand the user's input and convert the outline into a list of nested dictionaries. Each dictionary contains a `title` and a `describe`, where the `title` should clearly indicate the level using Markdown format, and the `describe` is a description and writing guide for that section.
...
... Please generate the corresponding list of nested dictionaries based on the following user input:
...
... Example output:
... [
... {
... "title": "# Level 1 Title",
... "describe": "Please provide a detailed description of the content under this title, offering background information and core viewpoints."
... },
... {
... "title": "## Level 2 Title",
... "describe": "Please provide a detailed description of the content under this title, giving specific details and examples to support the viewpoints of the Level 1 title."
... },
... {
... "title": "### Level 3 Title",
... "describe": "Please provide a detailed description of the content under this title, deeply analyzing and providing more details and data support."
... }
... ]
... User input is as follows:
... '''
>>> query = "Please help me write an article about the application of artificial intelligence in the medical field."
>>> m = lazyllm.TrainableModule("internlm2-chat-20b").prompt(toc_prompt).start() # the model output without specifying a formatter
>>> ret = m(query, max_new_tokens=2048)
>>> print(f"ret: {ret!r}")
'Based on your user input, here is the corresponding list of nested dictionaries:
[
{
"title": "# Application of Artificial Intelligence in the Medical Field",
"describe": "Please provide a detailed description of the application of artificial intelligence in the medical field, including its benefits, challenges, and future prospects."
},
{
"title": "## AI in Medical Diagnosis",
"describe": "Please provide a detailed description of how artificial intelligence is used in medical diagnosis, including specific examples of AI-based diagnostic tools and their impact on patient outcomes."
},
{
"title": "### AI in Medical Imaging",
"describe": "Please provide a detailed description of how artificial intelligence is used in medical imaging, including the advantages of AI-based image analysis and its applications in various medical specialties."
},
{
"title": "### AI in Drug Discovery and Development",
"describe": "Please provide a detailed description of how artificial intelligence is used in drug discovery and development, including the role of AI in identifying potential drug candidates and streamlining the drug development process."
},
{
"title": "## AI in Medical Research",
"describe": "Please provide a detailed description of how artificial intelligence is used in medical research, including its applications in genomics, epidemiology, and clinical trials."
},
{
"title": "### AI in Genomics and Precision Medicine",
"describe": "Please provide a detailed description of how artificial intelligence is used in genomics and precision medicine, including the role of AI in analyzing large-scale genomic data and tailoring treatments to individual patients."
},
{
"title": "### AI in Epidemiology and Public Health",
"describe": "Please provide a detailed description of how artificial intelligence is used in epidemiology and public health, including its applications in disease surveillance, outbreak prediction, and resource allocation."
},
{
"title": "### AI in Clinical Trials",
"describe": "Please provide a detailed description of how artificial intelligence is used in clinical trials, including its role in patient recruitment, trial design, and data analysis."
},
{
"title": "## AI in Medical Practice",
"describe": "Please provide a detailed description of how artificial intelligence is used in medical practice, including its applications in patient monitoring, personalized medicine, and telemedicine."
},
{
"title": "### AI in Patient Monitoring",
"describe": "Please provide a detailed description of how artificial intelligence is used in patient monitoring, including its role in real-time monitoring of vital signs and early detection of health issues."
},
{
"title": "### AI in Personalized Medicine",
"describe": "Please provide a detailed description of how artificial intelligence is used in personalized medicine, including its role in analyzing patient data to tailor treatments and predict outcomes."
},
{
"title": "### AI in Telemedicine",
"describe": "Please provide a detailed description of how artificial intelligence is used in telemedicine, including its applications in remote consultations, virtual diagnoses, and digital health records."
},
{
"title": "## AI in Medical Ethics and Policy",
"describe": "Please provide a detailed description of the ethical and policy considerations surrounding the use of artificial intelligence in the medical field, including issues related to data privacy, bias, and accountability."
}
]'
>>> m = lazyllm.TrainableModule("internlm2-chat-20b").formatter(EmptyFormatter()).prompt(toc_prompt).start() # the model output of the specified formatter
>>> ret = m(query, max_new_tokens=2048)
>>> print(f"ret: {ret!r}")
'Based on your user input, here is the corresponding list of nested dictionaries:
[
{
"title": "# Application of Artificial Intelligence in the Medical Field",
"describe": "Please provide a detailed description of the application of artificial intelligence in the medical field, including its benefits, challenges, and future prospects."
},
{
"title": "## AI in Medical Diagnosis",
"describe": "Please provide a detailed description of how artificial intelligence is used in medical diagnosis, including specific examples of AI-based diagnostic tools and their impact on patient outcomes."
},
{
"title": "### AI in Medical Imaging",
"describe": "Please provide a detailed description of how artificial intelligence is used in medical imaging, including the advantages of AI-based image analysis and its applications in various medical specialties."
},
{
"title": "### AI in Drug Discovery and Development",
"describe": "Please provide a detailed description of how artificial intelligence is used in drug discovery and development, including the role of AI in identifying potential drug candidates and streamlining the drug development process."
},
{
"title": "## AI in Medical Research",
"describe": "Please provide a detailed description of how artificial intelligence is used in medical research, including its applications in genomics, epidemiology, and clinical trials."
},
{
"title": "### AI in Genomics and Precision Medicine",
"describe": "Please provide a detailed description of how artificial intelligence is used in genomics and precision medicine, including the role of AI in analyzing large-scale genomic data and tailoring treatments to individual patients."
},
{
"title": "### AI in Epidemiology and Public Health",
"describe": "Please provide a detailed description of how artificial intelligence is used in epidemiology and public health, including its applications in disease surveillance, outbreak prediction, and resource allocation."
},
{
"title": "### AI in Clinical Trials",
"describe": "Please provide a detailed description of how artificial intelligence is used in clinical trials, including its role in patient recruitment, trial design, and data analysis."
},
{
"title": "## AI in Medical Practice",
"describe": "Please provide a detailed description of how artificial intelligence is used in medical practice, including its applications in patient monitoring, personalized medicine, and telemedicine."
},
{
"title": "### AI in Patient Monitoring",
"describe": "Please provide a detailed description of how artificial intelligence is used in patient monitoring, including its role in real-time monitoring of vital signs and early detection of health issues."
},
{
"title": "### AI in Personalized Medicine",
"describe": "Please provide a detailed description of how artificial intelligence is used in personalized medicine, including its role in analyzing patient data to tailor treatments and predict outcomes."
},
{
"title": "### AI in Telemedicine",
"describe": "Please provide a detailed description of how artificial intelligence is used in telemedicine, including its applications in remote consultations, virtual diagnoses, and digital health records."
},
{
"title": "## AI in Medical Ethics and Policy",
"describe": "Please provide a detailed description of the ethical and policy considerations surrounding the use of artificial intelligence in the medical field, including issues related to data privacy, bias, and accountability."
}
]'
Source code in lazyllm/components/formatter/formatterbase.py
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 | |
ComponentBase
lazyllm.components.core.ComponentBase
Bases: object
Base class for components, providing a unified interface and basic implementation to facilitate creation of various components.
Components execute tasks via a specified launcher and support custom task execution logic.
Parameters:
-
launcher(LazyLLMLaunchersBase or type, default:empty()) –Launcher instance or launcher class used by the component, defaults to empty launcher.
Examples:
>>> from lazyllm.components.core import ComponentBase
>>> class MyComponent(ComponentBase):
... def apply(self, x):
... return x * 2
>>> comp = MyComponent()
>>> comp.name = "ExampleComponent"
>>> print(comp.name)
ExampleComponent
>>> result = comp(10)
>>> print(result)
20
>>> print(comp.apply(5))
10
Source code in lazyllm/components/core.py
apply()
Core execution method of the component, to be implemented by subclasses.
Defines the specific business logic or task execution steps of the component.
Note:
If this method is overridden by the subclass, it will be called when the component is invoked.
Source code in lazyllm/components/core.py
cmd(*args, **kw)
Generates the execution command of the component, to be implemented by subclasses.
The returned command can be a string, tuple, or list, representing the instruction to execute the task.
Note:
If the apply method is not overridden, this command will be used to create a job for the launcher to run.