Skip to content

Commit 60bdafe

Browse files
committed
add a CLI
1 parent 8220126 commit 60bdafe

File tree

6 files changed

+30
-6
lines changed

6 files changed

+30
-6
lines changed

README.md

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -38,3 +38,6 @@ with backticks.
3838

3939
Why not use online LLMs?
4040
- There are alternative solutions that do this already.
41+
42+
Why not use LiteLLM?
43+
- I liked the project but unfortunately it seemed a bit too unstable for this.

app/cli.py

Lines changed: 3 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -3,6 +3,7 @@
33
from app.query_ollama import query_ollama
44
from app.extract_bash import extract_bash_commands
55
from app.menu import row_based_rich_menu
6+
from app.config import EXIT_COMMAND
67

78

89
def process_query(query: str):
@@ -12,7 +13,8 @@ def process_query(query: str):
1213
print("No command could be produced")
1314
return 0
1415
selected_bash_command = row_based_rich_menu(bash_commands)
15-
print(f"Received: {query}")
16+
if selected_bash_command == EXIT_COMMAND:
17+
return 0
1618
try:
1719
result = subprocess.run(selected_bash_command, check=True, shell=True)
1820
return result

app/config.py

Lines changed: 3 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1 +1,3 @@
1-
DEFAULT_MODEL = "llama:3.1"
1+
DEFAULT_MODEL = "llama3.1"
2+
DEFAULT_URL = "http://localhost:11434/api/generate"
3+
EXIT_COMMAND = "Leave"

app/extract_bash.py

Lines changed: 4 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -20,7 +20,10 @@ def extract_bash_commands(llm_response: str) -> list:
2020
pattern = re.compile(r"```(?:bash)?(.*?)```", re.DOTALL)
2121
matches = pattern.findall(llm_response)
2222
for match in matches:
23-
commands.extend(match.strip().split("\n"))
23+
com = match.strip().split("\n")
24+
if com == "":
25+
continue
26+
commands.extend(com)
2427

2528
return commands
2629

app/menu.py

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -2,12 +2,15 @@
22
from rich.panel import Panel
33
from rich.prompt import Prompt
44
from rich.text import Text
5+
from app.config import EXIT_COMMAND
56

67
console = Console()
78

89

910
def row_based_rich_menu(options: list) -> str:
1011

12+
options.append(EXIT_COMMAND)
13+
1114
menu_items = []
1215
for i, option in enumerate(options, 1):
1316
item = Text.assemble((f"{i}. ", "bold magenta"), (option, "cyan"))

app/query_ollama.py

Lines changed: 14 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -1,9 +1,11 @@
11
"""Query Ollama model
22
"""
33

4+
import os
45
import requests
56
import json
67
import subprocess
8+
from app.config import DEFAULT_MODEL, DEFAULT_URL
79

810

911
def init_ollama():
@@ -16,10 +18,19 @@ def init_ollama():
1618

1719

1820
def query_ollama(prompt, stream: bool = True):
19-
url = "http://localhost:11434/api/generate"
21+
init_ollama()
22+
url = os.getenv("HOW_DEFAULT_URL", DEFAULT_URL)
23+
model = os.getenv("HOW_DEFAULT_MODEL", DEFAULT_MODEL)
24+
history = subprocess.getoutput("history")
25+
2026
data = {
21-
"model": "llama3.1",
22-
"prompt": f"Convert the following natural language command to a bash command: {prompt}",
27+
"model": model,
28+
"prompt": f"""Convert the following natural language command to a bash command: {prompt}
29+
History of CLI commands if it helps:
30+
<history>
31+
{history}
32+
</history>
33+
""",
2334
"stream": stream,
2435
}
2536

0 commit comments

Comments
 (0)