llm aliases list --json option, refs #151

This commit is contained in:
Simon Willison 2023-08-12 08:52:57 -07:00
parent 4db949bae8
commit b8f8869778
3 changed files with 41 additions and 1 deletions

View file

@ -3,6 +3,8 @@
LLM supports model aliases, which allow you to refer to a model by a short name instead of its full ID.
## Listing aliases
To list current aliases, run this:
```bash
@ -26,3 +28,21 @@ gpt4 : gpt-4
4-32k : gpt-4-32k
```
<!-- [[[end]]] -->
Add `--json` to get that list back as JSON:
```bash
llm aliases list --json
```
Example output:
```json
{
"3.5": "gpt-3.5-turbo",
"chatgpt": "gpt-3.5-turbo",
"chatgpt-16k": "gpt-3.5-turbo-16k",
"3.5-16k": "gpt-3.5-turbo-16k",
"4": "gpt-4",
"gpt4": "gpt-4",
"4-32k": "gpt-4-32k"
}
```

View file

@ -560,12 +560,16 @@ def aliases():
@aliases.command(name="list")
def aliases_list():
@click.option("json_", "--json", is_flag=True, help="Output as JSON")
def aliases_list(json_):
"List current aliases"
to_output = []
for alias, model in get_model_aliases().items():
if alias != model.model_id:
to_output.append((alias, model.model_id))
if json_:
click.echo(json.dumps({key: value for key, value in to_output}, indent=4))
return
max_alias_length = max(len(a) for a, _ in to_output)
fmt = "{alias:<" + str(max_alias_length) + "} : {model_id}"
for alias, model_id in to_output:

View file

@ -1,5 +1,6 @@
from click.testing import CliRunner
from llm.cli import cli
import json
def test_aliases_list():
@ -15,3 +16,18 @@ def test_aliases_list():
"gpt4 : gpt-4\n"
"4-32k : gpt-4-32k\n"
)
def test_aliases_list_json():
runner = CliRunner()
result = runner.invoke(cli, ["aliases", "list", "--json"])
assert result.exit_code == 0
assert json.loads(result.output) == {
"3.5": "gpt-3.5-turbo",
"chatgpt": "gpt-3.5-turbo",
"chatgpt-16k": "gpt-3.5-turbo-16k",
"3.5-16k": "gpt-3.5-turbo-16k",
"4": "gpt-4",
"gpt4": "gpt-4",
"4-32k": "gpt-4-32k",
}