Outlines is a Python library for constrained language generation. It provides a unified interface to various language models and allows for structured generation using techniques like regex matching, type constraints, JSON schemas, and context-free grammars.Outlines supports multiple backends, including:
- Hugging Face Transformers
- llama.cpp
- vLLM
- MLX
Installation and Setup
To use Outlines with LangChain, you’ll need to install the Outlines library:- For Transformers:
pip install transformers torch datasets
- For llama.cpp:
pip install llama-cpp-python
- For vLLM:
pip install vllm
- For MLX:
pip install mlx
LLM
To use Outlines as an LLM in LangChain, you can use theOutlines
class:
Chat Models
To use Outlines as a chat model in LangChain, you can use theChatOutlines
class:
Model Configuration
BothOutlines
and ChatOutlines
classes share similar configuration options:
Model Identifier
Themodel
parameter can be:
- A Hugging Face model name (e.g., “meta-llama/Llama-2-7b-chat-hf”)
- A local path to a model
- For GGUF models, the format is “repo_id/file_name” (e.g., “TheBloke/Llama-2-7B-Chat-GGUF/llama-2-7b-chat.Q4_K_M.gguf”)
Backend Options
Thebackend
parameter specifies which backend to use:
"transformers"
: For Hugging Face Transformers models (default)"llamacpp"
: For GGUF models using llama.cpp"transformers_vision"
: For vision-language models (e.g., LLaVA)"vllm"
: For models using the vLLM library"mlxlm"
: For models using the MLX framework
Structured Generation
Outlines provides several methods for structured generation:-
Regex Matching:
This will ensure the generated text matches the specified regex pattern (in this case, a valid IP address).
-
Type Constraints:
This restricts the output to valid Python types (int, float, bool, datetime.date, datetime.time, datetime.datetime).
-
JSON Schema:
This ensures the generated output adheres to the specified JSON schema or Pydantic model.
-
Context-Free Grammar:
This generates text that adheres to the specified context-free grammar in EBNF format.