Text Generation
Text generation means producing new text from a piece of input text. Models can continue an unfinished passage, rewrite a sentence, answer a question or keep a conversation going.
Inputs
Input
The old lighthouse on the hill
Text Generation Model
Output
Output
The old lighthouse on the hill had not been lit for forty years, yet every sailor in the bay still steered by it. On stormy nights the villagers swore they could see a faint glow behind the cracked glass, as if someone were still keeping watch
About Text Generation
This page covers both text-generation and text-to-text generation models. Large language models tuned for chat or for following instructions belong here too. A ranked list of open models is kept on the open model leaderboard, ordered by benchmark scores.
Use Cases
Instruction Models
A model pretrained for text generation can later be taught to follow instructions. Several strong instruction-tuned open models can be tried directly in HubClone Chat.
Code Generation
Causal language models trained on source code take care of repetitive programming work: finishing functions, writing tests or porting code between languages. Open code models now cover more than eighty programming languages — you can try one in this playground.
Stories Generation
Give a story model an opening line such as "Once upon a time" and it keeps writing in the same voice. This demo hosts a model fine-tuned for long-form fiction.
When your own data looks nothing like the pretraining corpus, training a causal language model from scratch can pay off. The free Transformers course chapter walks through the whole process!
Task Variants
Completion Generation Models
The most common variant predicts the next word from everything that came before it. Repeating that step word by word produces longer passages, for example:
- Finish an incomplete sentence.
- Continue a story from its first lines.
- Write code from a short description of what it should do.
Popular choices include GPT-style decoders and the Mistral and Llama families. They learn from unlabeled text, so plain documents are all you need to train your own — anything from source code to novels.
Text-to-Text Generation Models
These models learn to turn one text into another, for example a sentence into its translation. Well-known examples are NLLB, FLAN-T5 and BART. Because they are trained on many tasks at once, the same weights can summarize, translate and classify text.
Language Model Variants
Language models used for generation usually fall into one of three groups:
- Base models: plain pretrained language models such as Mistral 7B or Llama 3 70B. They are a good starting point for fine-tuning and for few-shot prompting.
- Instruction-tuned models: trained on many tasks phrased as instructions, like "Write me a recipe for lemon cake". Qwen 2 7B Instruct, Yi 1.5 34B Chat and Llama 3 70B Instruct are typical examples; they generally follow instructions better than base models.
- Preference-tuned models: instruction models further aligned with feedback that rates answers for qualities such as helpfulness, honesty and harmlessness. The feedback is combined with an optimization method — reinforcement learning from human feedback, RLHF for short, or direct preference optimization. Zephyr ORPO 141B is one openly released example.
Text Generation from Image and Text
Vision language models read images alongside text and answer in text. IDEFICS 2 and MiniCPM-V are two open examples. They accept the same generation parameters as text-only models, but because they also need an image you load them through the image-text-to-text pipeline — the image-text-to-text task page has the details.
Inference
The Transformers text-generation pipeline runs inference with these models. Give it an unfinished text and it returns one or more possible continuations.
from transformers import pipeline
generator = pipeline("text-generation", model="openai-community/gpt2")
generator("Hello, I'm a language model", max_length=30, num_return_sequences=3)
## [{'generated_text': "Hello, I'm a language model trained to finish your sentences, one word at a time."},
## {'generated_text': "Hello, I'm a language model and I spend my days writing short poems about the sea."}, ...]Text-to-text generation models use a separate pipeline called text2text-generation. Its input states the task together with the text, and it returns the completed task.
from transformers import pipeline
t2t = pipeline("text2text-generation")
t2t("question: What is 42? context: 42 is the answer to the ultimate question")
## [{'generated_text': 'the answer to the ultimate question'}]
t2t("translate English to French: The weather is lovely today")
## [{'generated_text': "Il fait très beau aujourd'hui"}]From JavaScript, the inference client calls hosted text generation models.
import { InferenceClient } from "@hubclone/inference";
const client = new InferenceClient(HUBCLONE_TOKEN);
await client.textGeneration({
model: "openai-community/gpt2",
inputs: "The answer to the universe is",
});Text Generation Inference
Text Generation Inference (TGI) is an open-source server for large language models built for low latency and high throughput. It powers Inference Endpoints and HubClone Chat as well as many community projects, and you can use it to deploy any supported open model yourself.
ChatUI Spaces
Spaces ships templates for common applications. Chat UI is an open-source chat interface for large language models that deploys to a Space in a few clicks, with TGI serving the model behind it. Start from the template, pick a model and customize the interface — read the Chat UI Space guide or create your own instance.
Useful Resources
Want to dig deeper into the topic? Great! Here is a hand-picked list of resources we found helpful.
Tools within HubClone Ecosystem
- PEFT adapts large language models efficiently.
- Chat UI is the open-source interface for chatting with language models.
- text-generation-inference
- HubClone Chat lets you talk to strong open models such as Llama 3 70B or Mixtral 8x7B.
Documentation
- PEFT documentation
- Chat UI Docker Spaces
- Causal language modeling task guide
- Text generation strategies
- Course chapter: training a causal language model from scratch
Model Inference & Deployment
- Getting an LLM ready for production
- The open text generation ecosystem on HubClone
- RWKV: a recurrent network with transformer-level quality
- Llama 2 lands on HubClone
- Steering generation with constrained beam search
- Comparing open code generation models
- Assisted generation for lower latency
- A tour of decoding methods for text generation
- Faster generation with TensorFlow and XLA
Model Fine-tuning/Training
- Train a Llama 2 chatbot without writing code
- Training a code model from scratch
- Building a coding assistant step by step
Advanced Concepts Explained Simply
- Mixture of Experts, explained
Advanced Fine-tuning/Training Recipes
- Fine-tuning a 70B model with PyTorch FSDP
- Implementation details of RLHF with PPO
- Preference tuning with direct optimization methods
- Fine-tuning Llama 2 with DPO
Notebooks
- Training a causal LM in Flax
- Training a causal LM in TensorFlow
- Training a causal LM in PyTorch
Scripts for training
- Causal LM training script (PyTorch)
- Causal LM training script (TensorFlow)
- Text generation example (PyTorch)