Sequences

pipeline pipeline

The Sequences pipeline runs text through a sequence-sequence model and generates output text.

Example

The following shows a simple example using this pipeline.

  1. from txtai.pipeline import Sequences
  2. # Create and run pipeline
  3. sequences = Sequences()
  4. sequences("Hello, how are you?", "translate English to French: ")

See the link below for a more detailed example.

NotebookDescription
Query translationDomain-specific natural language queries with query translationOpen In Colab

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. sequences:
  3. # Run pipeline with workflow
  4. workflow:
  5. sequences:
  6. tasks:
  7. - action: sequences
  8. args: ["translate English to French: "]

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("sequences", ["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":"sequences", "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

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