Generator

pipeline pipeline

The Generator pipeline takes an input prompt and generates follow-on text.

Example

The following shows a simple example using this pipeline.

  1. from txtai.pipeline import Generator
  2. # Create and run pipeline
  3. generator = Generator()
  4. generator("Hello, how are you?")

Configuration-driven example

Pipelines are run with Python or configuration. Pipelines can be instantiated in configuration using the lower case name of the pipeline. Configuration-driven pipelines are run with workflows or the API.

config.yml

  1. # Create pipeline using lower case class name
  2. generator:
  3. # Run pipeline with workflow
  4. workflow:
  5. generator:
  6. tasks:
  7. - action: generator

Run with Workflows

  1. from txtai.app import Application
  2. # Create and run pipeline with workflow
  3. app = Application("config.yml")
  4. list(app.workflow("generator", ["Hello, how are you?"]))

Run with API

  1. CONFIG=config.yml uvicorn "txtai.api:app" &
  2. curl \
  3. -X POST "http://localhost:8000/workflow" \
  4. -H "Content-Type: application/json" \
  5. -d '{"name":"generator", "elements": ["Hello, how are you?"]}'

Methods

Python documentation for the pipeline.

__init__(self, path=None, quantize=False, gpu=True, model=None) special

Source code in txtai/pipeline/text/generator.py

  1. def __init__(self, path=None, quantize=False, gpu=True, model=None):
  2. super().__init__(self.task(), path, quantize, gpu, model)

__call__(self, text, prefix=None, maxlength=512, workers=0, **kwargs) special

Generates text using input text

Parameters:

NameTypeDescriptionDefault
text

text|list

required
prefix

optional prefix to prepend to text elements

None
maxlength

maximum sequence length

512
workers

number of concurrent workers to use for processing data, defaults to None

0
kwargs

additional generation keyword arguments

{}

Returns:

TypeDescription

generated text

Source code in txtai/pipeline/text/generator.py

  1. def __call__(self, text, prefix=None, maxlength=512, workers=0, **kwargs):
  2. """
  3. Generates text using input text
  4. Args:
  5. text: text|list
  6. prefix: optional prefix to prepend to text elements
  7. maxlength: maximum sequence length
  8. workers: number of concurrent workers to use for processing data, defaults to None
  9. kwargs: additional generation keyword arguments
  10. Returns:
  11. generated text
  12. """
  13. # List of texts
  14. texts = text if isinstance(text, list) else [text]
  15. # Add prefix, if necessary
  16. if prefix:
  17. texts = [f"{prefix}{x}" for x in texts]
  18. # Run pipeline
  19. results = self.pipeline(texts, max_length=maxlength, num_workers=workers, **kwargs)
  20. # Get generated text
  21. results = [self.clean(texts[x], result) for x, result in enumerate(results)]
  22. return results[0] if isinstance(text, str) else results