text_generation
mindnlp.transformers.pipelines.text_generation.TextGenerationPipeline
¶
Bases: Pipeline
Language generation pipeline using any ModelWithLMHead
. This pipeline predicts the words that will follow a
specified text prompt. It can also accept one or more chats. Each chat takes the form of a list of dicts,
where each dict contains "role" and "content" keys.
Learn more about the basics of using a pipeline in the pipeline tutorial. You can pass text generation parameters to this pipeline to control stopping criteria, decoding strategy, and more. Learn more about text generation parameters in Text generation strategies and Text generation.
This language generation pipeline can currently be loaded from [pipeline
] using the following task identifier:
"text-generation"
.
The models that this pipeline can use are models that have been trained with an autoregressive language modeling objective, which includes the uni-directional models in the library (e.g. openai-community/gpt2). See the list of available models on hf-mirror.com/models.
Source code in mindnlp/transformers/pipelines/text_generation.py
67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 144 145 146 147 148 149 150 151 152 153 154 155 156 157 158 159 160 161 162 163 164 165 166 167 168 169 170 171 172 173 174 175 176 177 178 179 180 181 182 183 184 185 186 187 188 189 190 191 192 193 194 195 196 197 198 199 200 201 202 203 204 205 206 207 208 209 210 211 212 213 214 215 216 217 218 219 220 221 222 223 224 225 226 227 228 229 230 231 232 233 234 235 236 237 238 239 240 241 242 243 244 245 246 247 248 249 250 251 252 253 254 255 256 257 258 259 260 261 262 263 264 265 266 267 268 269 270 271 272 273 274 275 276 277 278 279 280 281 282 283 284 285 286 287 288 289 290 291 292 293 294 295 296 297 298 299 300 301 302 303 304 305 306 307 308 309 310 311 312 313 314 315 316 317 318 319 320 321 322 323 324 325 326 327 328 329 330 331 332 333 334 335 336 337 338 339 340 341 342 343 344 345 346 347 348 349 350 351 352 353 354 355 356 357 358 359 360 361 362 363 364 365 366 367 368 369 370 371 372 373 374 375 376 377 378 379 380 381 382 383 384 385 386 387 388 389 390 391 392 393 394 395 396 397 398 399 400 401 402 403 404 405 406 407 408 409 410 411 412 413 414 415 416 417 418 419 420 421 422 423 424 425 426 427 428 429 430 431 432 433 434 435 436 437 438 439 440 441 442 443 444 445 446 447 448 449 450 451 452 453 454 455 456 457 458 459 460 461 462 463 464 465 466 467 468 469 470 471 472 |
|
mindnlp.transformers.pipelines.text_generation.TextGenerationPipeline.__call__(text_inputs, **kwargs)
¶
Complete the prompt(s) given as inputs.
PARAMETER | DESCRIPTION |
---|---|
text_inputs |
One or several prompts (or one list of prompts) to complete.
TYPE:
|
return_tensors |
Whether or not to return the tensors of predictions (as token indices) in the outputs. If set to
TYPE:
|
return_text |
Whether or not to return the decoded texts in the outputs.
TYPE:
|
return_full_text |
If set to
TYPE:
|
clean_up_tokenization_spaces |
Whether or not to clean up the potential extra spaces in the text output.
TYPE:
|
prefix |
Prefix added to prompt.
TYPE:
|
handle_long_generation |
By default, this pipelines does not handle long generation (ones that exceed in one form or the other the model maximum length). There is no perfect way to adress this (more info :https://github.com/huggingface/transformers/issues/14033#issuecomment-948385227). This provides common strategies to work around that problem depending on your use case.
TYPE:
|
generate_kwargs |
Additional keyword arguments to pass along to the generate method of the model (see the generate method corresponding to your framework here).
TYPE:
|
RETURNS | DESCRIPTION |
---|---|
A list or a list of list of
|
Source code in mindnlp/transformers/pipelines/text_generation.py
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 |
|
mindnlp.transformers.pipelines.text_generation.TextGenerationPipeline.__init__(*args, **kwargs)
¶
Initializes an instance of the TextGenerationPipeline class.
PARAMETER | DESCRIPTION |
---|---|
self |
The instance of the TextGenerationPipeline class.
|
RETURNS | DESCRIPTION |
---|---|
None. |
Source code in mindnlp/transformers/pipelines/text_generation.py
85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 |
|
mindnlp.transformers.pipelines.text_generation.TextGenerationPipeline.postprocess(model_outputs, return_type=ReturnType.FULL_TEXT, clean_up_tokenization_spaces=True)
¶
postprocess method in the TextGenerationPipeline class.
PARAMETER | DESCRIPTION |
---|---|
self |
The instance of the TextGenerationPipeline class.
|
model_outputs |
A dictionary containing model outputs including 'generated_sequence', 'input_ids', and 'prompt_text'.
TYPE:
|
return_type |
An enum specifying the type of return value desired. Can be one of the following: ReturnType.TENSORS, ReturnType.NEW_TEXT, or ReturnType.FULL_TEXT.
TYPE:
|
clean_up_tokenization_spaces |
A flag indicating whether to clean up tokenization spaces in the generated text.
TYPE:
|
RETURNS | DESCRIPTION |
---|---|
list
|
A list of dictionaries containing the post-processed output based on the specified return_type. Each dictionary in the list may have the following keys based on the return_type:
|
RAISES | DESCRIPTION |
---|---|
TypeError
|
If model_outputs is not a dictionary or return_type is not a valid ReturnType enum. |
ValueError
|
If return_type is not one of the valid enum values. |
Source code in mindnlp/transformers/pipelines/text_generation.py
410 411 412 413 414 415 416 417 418 419 420 421 422 423 424 425 426 427 428 429 430 431 432 433 434 435 436 437 438 439 440 441 442 443 444 445 446 447 448 449 450 451 452 453 454 455 456 457 458 459 460 461 462 463 464 465 466 467 468 469 470 471 472 |
|
mindnlp.transformers.pipelines.text_generation.TextGenerationPipeline.preprocess(prompt_text, prefix='', handle_long_generation=None, add_special_tokens=False, truncation=None, padding=False, max_length=None, **generate_kwargs)
¶
Preprocesses the prompt text for text generation.
PARAMETER | DESCRIPTION |
---|---|
self |
An instance of the TextGenerationPipeline class.
|
prompt_text |
The text or chat prompt to preprocess.
TYPE:
|
prefix |
A prefix to add to the prompt text. Default is an empty string.
TYPE:
|
handle_long_generation |
Specifies how to handle long generation. Default is None.
TYPE:
|
add_special_tokens |
Whether to add special tokens to the input text. Default is False.
TYPE:
|
truncation |
Specifies whether or how to truncate the input text. Default is None.
TYPE:
|
padding |
Whether to pad the input text. Default is False.
TYPE:
|
max_length |
The maximum length of the input text. Default is None.
TYPE:
|
**generate_kwargs |
Additional keyword arguments to be passed to the text generation process.
DEFAULT:
|
RETURNS | DESCRIPTION |
---|---|
dict
|
A dictionary containing the preprocessed inputs for text generation. The dictionary includes the following keys:
|
RAISES | DESCRIPTION |
---|---|
ValueError
|
If the number of new tokens exceeds the model's maximum length. |
ValueError
|
If the number of desired tokens exceeds the model's maximum length and 'hole' handling is used. |
Source code in mindnlp/transformers/pipelines/text_generation.py
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 |
|