Developing a Linux Command Generator using Sambanova's API

Introduction

This guide walks you through creating a Linux Command Generator using Sambanova’s API, a tool that bridges the gap between human language and terminal expertise.

Navigating Linux commands can be daunting, especially for newcomers or those tackling complex tasks. What if you could transform a simple natural language request like “How do I find all files modified in the last 24 hours?” into a precise linux command and a clear explanation of how it works.

By leveraging AI, you’ll build an application that:

  1. Interprets plain English descriptions (e.g., “Search for errors in log files”).
  2. Generates ready-to-use Linux commands (e.g., grep -r "error" /var/log/).
  3. Provides concise explanations of how the command works (e.g., “Searches recursively for the word ‘error’ in all files under /var/log”).

Get Sambanova API key

  1. Login/Signup and generate API key.
  2. Store the key securely in .env file.

Install dependencies

pip install openai python-dotenv

Import Required Libraries

import os
import openai
import json
import sys
from dotenv import load_dotenv

Load the API Key

load_dotenv()
sambanova_api_key = os.getenv("SAMBANOVA_API_KEY")

Initializing the SambaNova Client

client_sn = openai.OpenAI(
    api_key='sambanova_api_key',
    base_url="https://api.sambanova.ai/v1",
)
  • Creates a client connection to SambaNova’s API using their custom endpoint
  • Uses the OpenAI-compatible SDK format for familiarity
  • api_key: Authentication credentials
  • base_url: Points to SambaNova’s API endpoint

Response Generation Function

def generate_sn_response(prompt):
    examples = '''
    Format the output as a JSON object where:
    - the key is the command,
    - the value is the explanation.
    Example
    Query: Which command is used to list files in Directory.
    Answer format: {"command 1":"explaination","command 2":"explaination"}
    Make sure to give in asked format.
    Just give string within dict.\n
    '''
    response = client_sn.chat.completions.create(
        model='Meta-Llama-3.1-405B-Instruct',
        messages=[{"role":"system","content":"You are a helpful Linux assistant. Given the following request, suggest 1 or 2 Linux commands with an explanation."},{"role":"user","content":examples+prompt}],
        temperature =  0.7,
    )
    return response.choices[0].message.content

Prompt Engineering

  • Structured Output:
    • Enforces JSON formatting for easy parsing.
    • Uses keys like "command 1" and "command 2" to allow multiple command suggestions.
  • Example-Driven Learning:
    • Provides an example input/output pair to guide the model’s response structure along with explaination.
    • Ensures the model understands the required format.

Message Structure

  • System Role: Defines the AI’s role as a Linux assistant.
  • User Role: Combines the formatting examples with the user’s actual query.

Generate Linux Commands

resp = generate_sn_response(sys.argv[1])
try:
    commands = json.loads(resp)
    for command,exp in commands.items():
        print(f'Command: {command}\nExplaination:{exp}\n')
except:
    print(resp)

Example Usage

python3 e2l.py "how to create alias"

Example Output

Command: alias name='command'
Explaination:Create an alias for a command, replacing 'name' with the desired alias and 'command' with the original command.

Command: alias name='command' >> ~/.bashrc
Explaination:Create a permanent alias by appending it to the bash configuration file.

Thanks for reading! :raising_hands:

2 Likes

Great Work @omkar.gangan !!

1 Like