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) 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

Returns:

TypeDescription

generated text

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

  1. def __call__(self, text, prefix=None, maxlength=512, workers=0):
  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. Returns:
  10. generated text
  11. """
  12. # List of texts
  13. texts = text if isinstance(text, list) else [text]
  14. # Add prefix, if necessary
  15. if prefix:
  16. texts = [f"{prefix}{x}" for x in texts]
  17. # Run pipeline
  18. results = self.pipeline(texts, max_length=maxlength, num_workers=workers)
  19. # Get generated text
  20. results = [self.clean(x) for x in results]
  21. return results[0] if isinstance(text, str) else results