Skip to main content

SLM CLI Agent

A lightweight, local CPU-optimized command line interface companion agent powered by a local Small Language Model (SLM) running via ONNX Runtime GenAI. It translates natural language instructions into precise system shell commands, explains what they do, and executes them safely through an isolation layer with built-in command security constraints.


Features

  • Local CPU Execution: Optimized to run with less than 2.0 GB RAM using INT4-quantized Qwen2.5-1.5B-Instruct-ONNX.
  • Natural Language to CLI: Converts developer instructions (e.g. "kill process on port 8000") into correct syntax-compliant shell commands.
  • Command Security Sandbox: Detects and automatically blocks dangerous destructive actions (e.g., rm -rf /, formatting drives, reboots) before running them.
  • Explainable CLI: Explains the proposed command and its options prior to execution.
  • Streaming & Thought Process: Supports streaming responses, outputting the model's step-by-step reasoning inside <thought> tags before the final output.

Installation

In your local project environment:

pip install -e ./slm_cli_agent

Ensure onnxruntime-genai is installed. It uses the shared monorepo model path cached locally at models/qwen2.5-1.5b-onnx.


API Reference

SLMCLIAgent

from slm_cli_agent.cli_agent import SLMCLIAgent

agent = SLMCLIAgent(
    model_path=None,   # Path to the ONNX model directory (defaults to models/qwen2.5-1.5b-onnx)
    cache_dir=None,    # Alternative HF cache dir
    n_ctx=2048,        # Context length (defaults to 2048)
    n_threads=4        # Number of CPU threads to use for execution
)

Methods

generate_command(query: str, stream: bool = False)

Translates natural language to command line input.

  • Arguments:
    • query (str): Natural language instruction.
    • stream (bool): If True, returns a generator that yields decoded output tokens in real-time. If False, runs to completion.
  • Returns:
    • tuple[str, str]: (extracted_command, full_response) if stream=False.
    • Generator: A generator of tokens if stream=True.

execute_command(cmd: str) -> tuple[int, str, str]

Executes the proposed command.

  • Arguments:
    • cmd (str): The raw command line script to execute.
  • Returns:
    • tuple[int, str, str]: (return_code, stdout, stderr). Returns -1 with a security warning in stderr if the command contains dangerous destructive patterns.

run(query: str) -> dict

Translates, explains, and runs the instruction in one method call.

  • Arguments:
    • query (str): Natural language instruction.
  • Returns:
    • dict:
      {
          "success": True/False,
          "command": str,       # Extracted executable bash command
          "explanation": str,   # Model explanation and thought process
          "stdout": str,        # Execution standard output logs
          "stderr": str,        # Execution error logs
          "returncode": int     # Subprocess exit status code
      }
      

Usage Examples

1. Unified Natural Language Execution (run)

from slm_cli_agent.cli_agent import SLMCLIAgent

agent = SLMCLIAgent()

# Translate and execute in a single call
result = agent.run("list directory contents in clean format")

print(f"Success: {result['success']}")
print(f"Command Executed: {result['command']}")
print(f"Stdout:\n{result['stdout']}")

2. Manual Generation and Execution

from slm_cli_agent.cli_agent import SLMCLIAgent

agent = SLMCLIAgent()

# Translate instruction to command
cmd, explanation = agent.generate_command("find all files ending with .py in current folder")
print(f"Generated Command: {cmd}")
# Output: find . -name "*.py"

# Safely execute it
code, stdout, stderr = agent.execute_command(cmd)
print(f"Exit code: {code}")
print(f"Output:\n{stdout}")

2. Command Safety Check in Action

# The agent detects and blocks malicious or destructive sequences automatically
code, stdout, stderr = agent.execute_command("rm -rf /usr/bin")
print(code)    # Output: -1
print(stderr)  # Output: Execution Blocked: Destructive or dangerous command pattern detected.

3. Complex Command Pipeline Generation and Run

The agent can translate and execute complex pipelines combining search, filtering, counting, and head limitations securely:

from slm_cli_agent.cli_agent import SLMCLIAgent

agent = SLMCLIAgent()

# A dense multi-stage pipeline command request
query = (
    "Find all files in /var/log ending with .log modified in the last 7 days, "
    "search them for the term 'ERROR' case-insensitively, count the matching "
    "lines for each file, sort the count descending, and output the top 5 records "
    "while ignoring permission denied errors."
)

result = agent.run(query)

print("Exit code:", result["returncode"])
print("Generated Pipeline Command:")
print(result["command"])
print("\nExplanation:")
print(result["explanation"])

Generated Command:

find /var/log -type f -name "*.log" -mtime -7 2>/dev/null | xargs grep -io "ERROR" 2>/dev/null | cut -d: -f1 | sort | uniq -c | sort -rn | head -n 5

Configuration (config.yaml)

Specify custom paths or model settings inside the project directory:

models:
  cli_agent:
    path: "../../models/qwen2.5-1.5b-onnx"
    repo_id: "tonythethompson/Qwen2.5-1.5B-Instruct-ONNX"

Download files

Download the file for your platform. If you're not sure which to choose, learn more about installing packages.

Source Distribution

slm_cli_agent-0.1.0.tar.gz (6.2 kB view details)

Uploaded Source

Built Distribution

If you're not sure about the file name format, learn more about wheel file names.

slm_cli_agent-0.1.0-py3-none-any.whl (6.4 kB view details)

Uploaded Python 3

File details

Details for the file slm_cli_agent-0.1.0.tar.gz.

File metadata

  • Download URL: slm_cli_agent-0.1.0.tar.gz
  • Upload date:
  • Size: 6.2 kB
  • Tags: Source
  • Uploaded using Trusted Publishing? No
  • Uploaded via: twine/7.0.0 CPython/3.11.15

File hashes

Hashes for slm_cli_agent-0.1.0.tar.gz
Algorithm Hash digest
SHA256 88e0d42ea1968e3d2208b4bef05e33a44998a289060692188fe06caa04331d68
MD5 36425409406697ffb0fae60fefff92c5
BLAKE2b-256 119e59d63d5e760e93969af3e5ecafe71efc990ae71ce0fef8f9fe700fea14b9

See more details on using hashes here.

File details

Details for the file slm_cli_agent-0.1.0-py3-none-any.whl.

File metadata

  • Download URL: slm_cli_agent-0.1.0-py3-none-any.whl
  • Upload date:
  • Size: 6.4 kB
  • Tags: Python 3
  • Uploaded using Trusted Publishing? No
  • Uploaded via: twine/7.0.0 CPython/3.11.15

File hashes

Hashes for slm_cli_agent-0.1.0-py3-none-any.whl
Algorithm Hash digest
SHA256 507ee664898fa5625c50fcb6c377ffb39b00140f7d905b84f17445aa57559625
MD5 2f92bdb1e7c00233907dfe06f6f48b35
BLAKE2b-256 82f90fb0703dae74ff7bece6026ad5a687822630c554fd2c943711c68b906bb6

See more details on using hashes here.

Supported by

AWS Cloud computing and Security Sponsor Datadog Monitoring Depot Continuous Integration Fastly CDN Google Download Analytics Sentry Error logging StatusPage Status page