Documentation menu

Text to Text

Answer prompts, summarize and generate text in PHP with small local language models like Qwen2.5 and SmolLM2. CLI and PHP examples and options.

Answer prompts, summarize, translate or continue text with a small transformers text generation model running locally.

Setup#

Terminal
vendor/bin/loves-ai setup text-to-text
vendor/bin/loves-ai pull Qwen/Qwen2.5-0.5B-Instruct

Supported models#

Both kinds of text models work:

  • Chat and completion models (task text-generation), e.g. Qwen/Qwen2.5-0.5B-Instruct or HuggingFaceTB/SmolLM2-360M-Instruct. Chat models get the prompt wrapped in their chat template, so they answer it; other models continue it.
  • Encoder-decoder models (task text2text-generation), e.g. google/flan-t5-base.

The runner rejects models it cannot load before starting, with an explanation: GGUF files (made for llama.cpp and Ollama), ONNX-only repositories, LoRA adapters, image models, and models that need their own Python code (trust_remote_code), which it never runs.

From the command line#

Terminal
vendor/bin/loves-ai text-to-text Qwen/Qwen2.5-0.5B-Instruct "Write a haiku about PHP." --system="You are a poet."
Output
✍️ Writing with Qwen/Qwen2.5-0.5B-Instruct… Good words take a moment — perfect time for a cup of tea and a cookie 🍪
If you wish to see all logs, re-run the command with the "--debug" option.
🎉 Qwen/Qwen2.5-0.5B-Instruct wrote:
<the generated haiku>

Options#

Option Meaning
--system=TEXT Instructions for chat models, e.g. "You are a helpful assistant."
--max-new-tokens=N Maximum length of the answer in tokens (default: 256)
--temperature=T Randomness: 0 always picks the likeliest words (default: the model's own)
--top-p=P Nucleus sampling probability, e.g. 0.9 (default: the model's own)
--seed=N Random seed, for reproducible text
--device=DEVICE cpu, cuda, mps… (default: the best available)
--log-file=PATH Append the runner's output to this file
--debug Show the runner's output, and the text as it is written

Defaults come from config/text-to-text.php (log_file).

From PHP#

PHP
use PhpLovesAi\Runner\TextToText;

// Finds the runner and the pulled model in the project's .local directory by itself.
$answer = (new TextToText())->generate(
    model: 'Qwen/Qwen2.5-0.5B-Instruct',
    prompt: 'Summarize in one sentence: PHP is a popular general-purpose scripting language...',
    systemPrompt: 'You are a concise assistant.',
    maxNewTokens: 100,
    temperature: 0.0,
);

Errors#

  • BinaryNotInstalledException when setup text-to-text has not been run
  • ModelNotFoundException when the model was not pulled yet
  • UnsupportedModelException when the model is not a transformers text model
  • RunFailedException, with the runner's error output, when generation fails

Performance#

Small models run on CPU, but larger ones get slow quickly: a 0.5B model writes a few words per second on a laptop CPU, and each run loads the model from disk again. Run generation in a queue job rather than in a web request.