Add '!edit' command to modify prompts in chat sessions using default editor (#969)

* Add '!edit' command to modify prompts in chat sessions
This commit is contained in:
Benedikt Willi 2025-05-04 19:19:48 +02:00 committed by GitHub
parent 16770611ca
commit 7dcfa31143
No known key found for this signature in database
GPG key ID: B5690EEEBB952194
5 changed files with 27 additions and 0 deletions

View file

@ -419,6 +419,7 @@ This starts a chat session:
```
Type 'exit' or 'quit' to exit
Type '!multi' to enter multiple lines, then '!end' to finish
Type '!edit' to open your default editor and modify the prompt.
> Who are you?
Hello! I'm just an AI, here to assist you with any questions you may have.
My name is LLaMA, and I'm a large language model trained to provide helpful

View file

@ -75,6 +75,7 @@ llm chat -m gpt-4o
Chatting with gpt-4o
Type 'exit' or 'quit' to exit
Type '!multi' to enter multiple lines, then '!end' to finish
Type '!edit' to open your default editor and modify the prompt.
> Tell me a joke about a pelican
Why don't pelicans like to tip waiters?

View file

@ -330,6 +330,7 @@ llm chat -t cheesecake
Chatting with gpt-4
Type 'exit' or 'quit' to exit
Type '!multi' to enter multiple lines, then '!end' to finish
Type '!edit' to open your default editor and modify the prompt
> who are you?
I am a sentient cheesecake, meaning I am an artificial
intelligence embodied in a dessert form, specifically a
@ -350,6 +351,7 @@ If your pasted text might itself contain a `!end` line, you can set a custom del
Chatting with gpt-4
Type 'exit' or 'quit' to exit
Type '!multi' to enter multiple lines, then '!end' to finish
Type '!edit' to open your default editor and modify the prompt.
> !multi custom-end
Explain this error:
@ -362,6 +364,16 @@ urllib.error.URLError: <urlopen error [Errno 8] nodename nor servname provided,
!end custom-end
```
You can also use `!edit` to open your default editor and modify the prompt before sending it to the model.
```
Chatting with gpt-4
Type 'exit' or 'quit' to exit
Type '!multi' to enter multiple lines, then '!end' to finish
Type '!edit' to open your default editor and modify the prompt.
> !edit
```
## Listing available models
The `llm models` command lists every model that can be used with LLM, along with their aliases. This includes models that have been installed using {ref}`plugins <plugins>`.

View file

@ -905,6 +905,7 @@ def chat(
click.echo("Chatting with {}".format(model.model_id))
click.echo("Type 'exit' or 'quit' to exit")
click.echo("Type '!multi' to enter multiple lines, then '!end' to finish")
click.echo("Type '!edit' to open your default editor and modify the prompt")
in_multi = False
accumulated = []
end_token = "!end"
@ -916,6 +917,15 @@ def chat(
if len(bits) > 1:
end_token = "!end {}".format(" ".join(bits[1:]))
continue
if prompt.strip() == "!edit":
edited_prompt = click.edit()
if edited_prompt is None:
click.echo("Editor closed without saving.", err=True)
continue
prompt = edited_prompt.strip()
if not prompt:
continue
click.echo(prompt)
if in_multi:
if prompt.strip() == end_token:
prompt = "\n".join(accumulated)

View file

@ -21,6 +21,7 @@ def test_chat_basic(mock_model, logs_db):
"Chatting with mock"
"\nType 'exit' or 'quit' to exit"
"\nType '!multi' to enter multiple lines, then '!end' to finish"
"\nType '!edit' to open your default editor and modify the prompt"
"\n> Hi"
"\none world"
"\n> Hi two"
@ -86,6 +87,7 @@ def test_chat_basic(mock_model, logs_db):
"Chatting with mock"
"\nType 'exit' or 'quit' to exit"
"\nType '!multi' to enter multiple lines, then '!end' to finish"
"\nType '!edit' to open your default editor and modify the prompt"
"\n> Continue"
"\ncontinued"
"\n> quit"
@ -134,6 +136,7 @@ def test_chat_system(mock_model, logs_db):
"Chatting with mock"
"\nType 'exit' or 'quit' to exit"
"\nType '!multi' to enter multiple lines, then '!end' to finish"
"\nType '!edit' to open your default editor and modify the prompt"
"\n> Hi"
"\nI am mean"
"\n> quit"