LLM

pipeline pipeline

The LLM pipeline runs prompts through a large language model (LLM). This pipeline autodetects the LLM framework based on the model path.

Example

The following shows a simple example using this pipeline.

  1. from txtai.pipeline import LLM
  2. # Create and run LLM pipeline
  3. llm = LLM()
  4. llm(
  5. """
  6. Answer the following question using the provided context.
  7. Question:
  8. What are the applications of txtai?
  9. Context:
  10. txtai is an open-source platform for semantic search and
  11. workflows powered by language models.
  12. """
  13. )

The LLM pipeline automatically detects the underlying LLM framework. This can also be manually set.

  1. from txtai.pipeline import LLM
  2. # Set method as litellm
  3. llm = LLM("vllm/Open-Orca/Mistral-7B-OpenOrca", method="litellm")
  4. # Set method as llama.cpp
  5. llm = LLM("TheBloke/Mistral-7B-OpenOrca-GGUF/mistral-7b-openorca.Q4_K_M.gguf",
  6. method="llama.cpp")

Models can be externally loaded and passed to pipelines. This is useful for models that are not yet supported by Transformers and/or need special initialization.

  1. import torch
  2. from transformers import AutoModelForCausalLM, AutoTokenizer
  3. from txtai.pipeline import LLM
  4. # Load Mistral-7B-OpenOrca
  5. path = "Open-Orca/Mistral-7B-OpenOrca"
  6. model = AutoModelForCausalLM.from_pretrained(
  7. path,
  8. torch_dtype=torch.bfloat16,
  9. )
  10. tokenizer = AutoTokenizer.from_pretrained(path)
  11. llm = LLM((model, tokenizer))

See the links below for more detailed examples.

NotebookDescription
Prompt-driven search with LLMsEmbeddings-guided and Prompt-driven search with Large Language Models (LLMs)Open In Colab
Prompt templates and task chainsBuild model prompts and connect tasks together with workflowsOpen In Colab
Build RAG pipelines with txtaiGuide on retrieval augmented generation including how to create citationsOpen In Colab
Integrate LLM frameworksIntegrate llama.cpp, LiteLLM and custom generation frameworksOpen In Colab
Generate knowledge with Semantic Graphs and RAGKnowledge exploration and discovery with Semantic Graphs and RAGOpen 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. # Use `generator` or `sequences` to force model type
  3. llm:
  4. # Run pipeline with workflow
  5. workflow:
  6. llm:
  7. tasks:
  8. - action: llm

Similar to the Python example above, the underlying Hugging Face pipeline parameters and model parameters can be set in pipeline configuration.

  1. llm:
  2. path: Open-Orca/Mistral-7B-OpenOrca
  3. torch_dtype: torch.bfloat16

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("llm", [
  5. """
  6. Answer the following question using the provided context.
  7. Question:
  8. What are the applications of txtai?
  9. Context:
  10. txtai is an open-source platform for semantic search and
  11. workflows powered by language models.
  12. """
  13. ]))

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": ["Answer the following question..."]}'

Methods

Python documentation for the pipeline.

Creates a new LLM.

Parameters:

NameTypeDescriptionDefault
path

model path

None
method

llm model framework, infers from path if not provided

None
kwargs

model keyword arguments

{}

Source code in txtai/pipeline/llm/llm.py

  1. 20
  2. 21
  3. 22
  4. 23
  5. 24
  6. 25
  7. 26
  8. 27
  9. 28
  10. 29
  11. 30
  12. 31
  13. 32
  14. 33
  15. 34
  1. def init(self, path=None, method=None, kwargs):
  2. “””
  3. Creates a new LLM.
  4. Args:
  5. path: model path
  6. method: llm model framework, infers from path if not provided
  7. kwargs: model keyword arguments
  8. “””
  9. # Default LLM if not provided
  10. path = path if path else google/flan-t5-base
  11. # Generation instance
  12. self.generator = GenerationFactory.create(path, method, kwargs)

Generates text using input text

Parameters:

NameTypeDescriptionDefault
text

text|list

required
maxlength

maximum sequence length

512
kwargs

additional generation keyword arguments

{}

Returns:

TypeDescription

generated text

Source code in txtai/pipeline/llm/llm.py

  1. 36
  2. 37
  3. 38
  4. 39
  5. 40
  6. 41
  7. 42
  8. 43
  9. 44
  10. 45
  11. 46
  12. 47
  13. 48
  14. 49
  15. 50
  1. def call(self, text, maxlength=512, kwargs):
  2. “””
  3. Generates text using input text
  4. Args:
  5. text: text|list
  6. maxlength: maximum sequence length
  7. kwargs: additional generation keyword arguments
  8. Returns:
  9. generated text
  10. “””
  11. # Run LLM generation
  12. return self.generator(text, maxlength, kwargs)