Run any shell command via a local LLM in minutes
Welcome to another official UVF IT guide! In this quick‑start you’ll build a tiny “CLI‑Anything” clone that lets a language model suggest a shell command and then executes it for you. We’ll use Python, the free‑tier Ollama server (or any local LLM), and SQLite just to log requests – no paid APIs required.
This project mirrors the core idea of the original repo – a thin wrapper that talks to an LLM, receives a command string, and runs it safely. You’ll have a working prototype in ~45 minutes.
git installed on your machine.http://localhost:11434.Create a fresh folder, initialise a virtual environment, and install the tiny dependencies we need. This gives us an isolated space so the guide never interferes with your global Python packages.
We’ll also add a basic entry‑point script that will later call the LLM and log results. All files live under the cli_anything package for clarity.
mkdir cli_anything_clone
cd cli_anything_clone
python3 -m venv .venv
source .venv/bin/activate
pip install --upgrade pip
pip install sqlalchemy requests
mkdir cli_anything
touch cli_anything/__init__.py
cat > cli_anything/main.py <<'PY'
import sys
def main():
print('CLI‑Anything clone ready. Use `python -m cli_anything.main` to start.')
if __name__ == '__main__':
main()
PYWe store each request/response pair so you can see what the LLM suggested and what was executed. A tiny SQLite DB keeps things simple and requires no external service.
Using SQLAlchemy’s declarative base lets us define the table in pure Python and interact with it later from the CLI wrapper.
cli_anything/models.py
from sqlalchemy import Column, Integer, String, DateTime, create_engine
from sqlalchemy.orm import declarative_base, sessionmaker
from datetime import datetime
Base = declarative_base()
class LogEntry(Base):
__tablename__ = 'log_entries'
id = Column(Integer, primary_key=True)
prompt = Column(String, nullable=False) # What we asked the LLM
suggestion = Column(String, nullable=False) # Command string returned
executed_at = Column(DateTime, default=datetime.utcnow)
status = Column(String, default='pending') # pending, success, failed
# Helper to get a session – DB lives in the project root
engine = create_engine('sqlite:///cli_anything.db')
Base.metadata.create_all(engine)
Session = sessionmaker(bind=engine)In this step we add the heart of the clone: a function that talks to the local Ollama server, asks the model to produce a shell command, and then runs that command. We also log the original prompt and the generated command to a tiny SQLite database so you can audit what was executed later. Using subprocess.run gives us the command’s stdout/stderr and lets us raise an error if the command fails.
cli.py
import sys, json, subprocess, sqlite3, urllib.request
DB_PATH = 'log.db'
OLLAMA_URL = 'http://localhost:11434/api/generate'
MODEL = 'phi3:mini' # change to any local model you have
def init_db():
conn = sqlite3.connect(DB_PATH)
cur = conn.cursor()
cur.execute('''CREATE TABLE IF NOT EXISTS logs(
id INTEGER PRIMARY KEY AUTOINCREMENT,
prompt TEXT,
command TEXT,
timestamp DATETIME DEFAULT CURRENT_TIMESTAMP)''')
conn.commit()
conn.close()
def log_interaction(prompt, command):
conn = sqlite3.connect(DB_PATH)
cur = conn.cursor()
cur.execute('INSERT INTO logs (prompt, command) VALUES (?,?)', (prompt, command))
conn.commit()
conn.close()
def ask_ollama(prompt):
payload = json.dumps({"model": MODEL, "prompt": prompt, "stream": False}).encode('utf-8')
req = urllib.request.Request(OLLAMA_URL, data=payload, headers={'Content-Type': 'application/json'})
with urllib.request.urlopen(req) as resp:
result = json.load(resp)
# Ollama returns the generated text in 'response'
return result.get('response', '').strip()
def run_command(cmd):
print(f"[LLM suggested] {cmd}")
completed = subprocess.run(cmd, shell=True, capture_output=True, text=True)
print(completed.stdout)
if completed.returncode != 0:
print(completed.stderr, file=sys.stderr)
sys.exit(completed.returncode)
def main():
if len(sys.argv) < 2:
print('Usage: python cli.py "your natural language request"')
sys.exit(1)
prompt = ' '.join(sys.argv[1:])
init_db()
cmd = ask_ollama(prompt)
log_interaction(prompt, cmd)
run_command(cmd)
if __name__ == '__main__':
main()Now that the code is ready, activate the virtual environment you created earlier, make sure the Ollama daemon is up, and call the script with a plain‑English request. The wrapper will print the command the model suggested, execute it, and show the command’s output. This single line demonstrates the full end‑to‑end flow from natural language to shell execution.
source venv/bin/activate
# start Ollama if it isn’t already running (adjust path as needed)
ollama serve &
# give it a second to start
sleep 2
# ask the clone to show the current datepython cli.py "show current date"After the run, open the SQLite log to confirm the prompt and the generated command were stored. Try a few different prompts – listing files, checking disk usage, etc. – and notice how the model adapts. As a safety net you can prepend a simple filter that aborts execution if the command contains dangerous patterns like rm -rf.
sqlite3 log.db "SELECT id, prompt, command, timestamp FROM logs ORDER BY id DESC LIMIT 5;"| Check | Command / Why it matters |
|---|---|
| Ollama server is reachable at http://localhost:11434 | curl -s http://localhost:11434/api/tags | head |
| CLI prints a suggested command and its output | python cli.py "show current date" |
| SQLite log contains the prompt/command pair | sqlite3 log.db "SELECT COUNT(*) FROM logs;" |
Your minimal CLI‑Anything clone is now able to translate natural language into shell commands via a local LLM and execute them. Experiment with different models, add validation, or publish your own hub entry – the sky’s the limit!