LLM_MODEL and LLM_EMBEDDING_MODEL environment vars, closes #932

This commit is contained in:
Simon Willison 2025-04-19 20:41:24 -07:00
parent 3f25bb8bc9
commit e78e1fceb2
4 changed files with 33 additions and 5 deletions

View file

@ -31,6 +31,13 @@ The `llm embed` command returns a JSON array of floating point numbers directly
```
You can omit the `-m/--model` option if you set a {ref}`default embedding model <embeddings-cli-embed-models-default>`.
You can also set the `LLM_EMBEDDING_MODEL` environment variable to set a default model for all `llm embed` commands in the current shell session:
```bash
export LLM_EMBEDDING_MODEL=3-small
llm embed -c 'This is some content'
```
LLM also offers a binary storage format for embeddings, described in {ref}`embeddings storage format <embeddings-storage>`.
You can output embeddings using that format as raw bytes using `--format blob`, or in hexadecimal using `--format hex`, or in Base64 using `--format base64`:

View file

@ -30,8 +30,13 @@ Or if you know the name is too long to type, use `-q` once or more to provide se
```bash
llm 'Ten names for cheesecakes' -q 4o -q mini
```
To change the default model for the current session, set the `LLM_MODEL` environment variable:
```bash
export LLM_MODEL=gpt-4.1-mini
llm 'Ten names for cheesecakes' # Uses gpt-4.1-mini
```
You can also send a prompt to standard input, for example:
You can send a prompt directly to standard input like this:
```bash
echo 'Ten names for cheesecakes' | llm
```

View file

@ -292,7 +292,7 @@ def cli():
@cli.command(name="prompt")
@click.argument("prompt", required=False)
@click.option("-s", "--system", help="System prompt to use")
@click.option("model_id", "-m", "--model", help="Model to use")
@click.option("model_id", "-m", "--model", help="Model to use", envvar="LLM_MODEL")
@click.option(
"-d",
"--database",
@ -783,7 +783,7 @@ def prompt(
@cli.command()
@click.option("-s", "--system", help="System prompt to use")
@click.option("model_id", "-m", "--model", help="Model to use")
@click.option("model_id", "-m", "--model", help="Model to use", envvar="LLM_MODEL")
@click.option(
"_continue",
"-c",
@ -2352,7 +2352,9 @@ def uninstall(packages, yes):
type=click.Path(exists=True, readable=True, allow_dash=True),
help="File to embed",
)
@click.option("-m", "--model", help="Embedding model to use")
@click.option(
"-m", "--model", help="Embedding model to use", envvar="LLM_EMBEDDING_MODEL"
)
@click.option("--store", is_flag=True, help="Store the text itself in the database")
@click.option(
"-d",
@ -2494,7 +2496,9 @@ def embed(
"--batch-size", type=int, help="Batch size to use when running embeddings"
)
@click.option("--prefix", help="Prefix to add to the IDs", default="")
@click.option("-m", "--model", help="Embedding model to use")
@click.option(
"-m", "--model", help="Embedding model to use", envvar="LLM_EMBEDDING_MODEL"
)
@click.option(
"--prepend",
help="Prepend this string to all content before embedding",

View file

@ -624,6 +624,18 @@ def test_schema(mock_model, use_pydantic):
assert response.prompt.schema == dog_schema
def test_model_environment_variable(monkeypatch):
monkeypatch.setenv("LLM_MODEL", "echo")
runner = CliRunner()
result = runner.invoke(
cli,
["--no-stream", "hello", "-s", "sys"],
catch_exceptions=False,
)
assert result.exit_code == 0
assert result.output == "system:\nsys\n\nprompt:\nhello\n"
@pytest.mark.parametrize("use_filename", (True, False))
def test_schema_via_cli(mock_model, tmpdir, monkeypatch, use_filename):
user_path = tmpdir / "user"