-
-
Notifications
You must be signed in to change notification settings - Fork 20
/
Copy pathllm_cmd.py
49 lines (46 loc) · 1.99 KB
/
llm_cmd.py
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
import click
import llm
import subprocess
from prompt_toolkit import PromptSession
from prompt_toolkit.lexers import PygmentsLexer
from prompt_toolkit.patch_stdout import patch_stdout
from pygments.lexers.shell import BashLexer
SYSTEM_PROMPT = """
Return only the command to be executed as a raw string, no string delimiters
wrapping it, no yapping, no markdown, no fenced code blocks, what you return
will be passed to subprocess.check_output() directly.
For example, if the user asks: undo last git commit
You return only: git reset --soft HEAD~1
""".strip()
@llm.hookimpl
def register_commands(cli):
@cli.command()
@click.argument("args", nargs=-1)
@click.option("-m", "--model", default=None, help="Specify the model to use")
@click.option("-s", "--system", help="Custom system prompt")
@click.option("--key", help="API key to use")
def cmd(args, model, system, key):
"""Generate and execute commands in your shell"""
from llm.cli import get_default_model
prompt = " ".join(args)
model_id = model or get_default_model()
model_obj = llm.get_model(model_id)
if model_obj.needs_key:
model_obj.key = llm.get_key(key, model_obj.needs_key, model_obj.key_env_var)
result = model_obj.prompt(prompt, system=system or SYSTEM_PROMPT)
interactive_exec(str(result))
def interactive_exec(command):
session = PromptSession(lexer=PygmentsLexer(BashLexer))
with patch_stdout():
if '\n' in command:
print("Multiline command - Meta-Enter or Esc Enter to execute")
edited_command = session.prompt("> ", default=command, multiline=True)
else:
edited_command = session.prompt("> ", default=command)
try:
output = subprocess.check_output(
edited_command, shell=True, stderr=subprocess.STDOUT
)
print(output.decode())
except subprocess.CalledProcessError as e:
print(f"Command failed with error (exit status {e.returncode}): {e.output.decode()}")