### Install ShellGPT with LiteLLM Source: https://github.com/ther1d/shell_gpt/wiki/Azure Installs the ShellGPT package with LiteLLM support, enabling communication with various LLM backends. This is the initial step for integrating LiteLLM. ```shell pip install shell-gpt[litellm] ``` -------------------------------- ### Provide Go learning advice and example using heredoc Source: https://github.com/ther1d/shell_gpt/blob/main/README.md Demonstrates using a heredoc (`<< EOF`) to provide multiple lines of input to ShellGPT, asking for learning advice and a code example for Golang. ```shell sgpt << EOF What is the best way to lear Golang? Provide simple hello world example. EOF # -> The best way to learn Golang... ``` -------------------------------- ### Example JSON Output from Custom Role Source: https://github.com/ther1d/shell_gpt/blob/main/README.md Provides an example of the JSON output generated by the `json_generator` custom role, demonstrating the structure and content expected for user, password, email, and address. ```json { "user": "JohnDoe", "password": "p@ssw0rd", "email": "johndoe@example.com", "address": { "street": "123 Main St", "city": "Anytown", "state": "CA", "zip": "12345" } } ``` -------------------------------- ### Start Ollama API Server Source: https://github.com/ther1d/shell_gpt/wiki/Ollama This command starts the Ollama API server, making the locally hosted language models available for use by other applications. ```shell ollama serve ``` -------------------------------- ### Dockerfile for ShellGPT with Ollama Source: https://github.com/ther1d/shell_gpt/blob/main/README.md This Dockerfile sets up an environment to run ShellGPT using Ollama. It installs Python, configures necessary environment variables like the default model and API base URL, installs the 'litellm' package, and sets the entrypoint to 'sgpt'. ```dockerfile FROM python:3-slim ENV DEFAULT_MODEL=ollama/mistral:7b-instruct-v0.2-q4_K_M ENV API_BASE_URL=http://10.10.10.10:11434 ENV USE_LITELLM=true ENV OPENAI_API_KEY=bad_key ENV SHELL_INTERACTION=false ENV PRETTIFY_MARKDOWN=false ENV OS_NAME="Arch Linux" ENV SHELL_NAME=auto WORKDIR /app COPY . /app RUN apt-get update && apt-get install -y gcc RUN pip install --no-cache /app[litellm] && mkdir -p /tmp/shell_gpt VOLUME /tmp/shell_gpt ENTRYPOINT ["sgpt"] ``` -------------------------------- ### Install ShellGPT using pip Source: https://github.com/ther1d/shell_gpt/blob/main/README.md This snippet shows how to install the ShellGPT package using pip. It's a straightforward Python package installation. ```shell pip install shell-gpt ``` -------------------------------- ### Runtime Configuration File Example Source: https://github.com/ther1d/shell_gpt/blob/main/README.md Illustrates the structure and content of the runtime configuration file (`~/.config/shell_gpt/.sgptrc`). It shows various parameters that can be set, such as API keys, cache settings, default models, and paths. ```text # API key, also it is possible to define OPENAI_API_KEY env. OPENAI_API_KEY=your_api_key # Base URL of the backend server. If "default" URL will be resolved based on --model. API_BASE_URL=default # Max amount of cached message per chat session. CHAT_CACHE_LENGTH=100 # Chat cache folder. CHAT_CACHE_PATH=/tmp/shell_gpt/chat_cache # Request cache length (amount). CACHE_LENGTH=100 # Request cache folder. CACHE_PATH=/tmp/shell_gpt/cache # Request timeout in seconds. REQUEST_TIMEOUT=60 # Default OpenAI model to use. DEFAULT_MODEL=gpt-4o # Default color for shell and code completions. DEFAULT_COLOR=magenta # When in --shell mode, default to "Y" for no input. DEFAULT_EXECUTE_SHELL_CMD=false # Disable streaming of responses DISABLE_STREAMING=false # The pygment theme to view markdown (default/describe role). CODE_THEME=default # Path to a directory with functions. OPENAI_FUNCTIONS_PATH=/Users/user/.config/shell_gpt/functions # Print output of functions when LLM uses them. SHOW_FUNCTIONS_OUTPUT=false # Allows LLM to use functions. OPENAI_USE_FUNCTIONS=true # Enforce LiteLLM usage (for local LLMs). USE_LITELLM=false ``` -------------------------------- ### Install ShellGPT with LiteLLM Support Source: https://github.com/ther1d/shell_gpt/wiki/Ollama This command installs the ShellGPT package with the necessary dependencies to support LiteLLM, enabling communication with local LLM backends. ```shell pip install "shell-gpt[litellm]" ``` -------------------------------- ### Generate Docker command with ShellGPT Source: https://github.com/ther1d/shell_gpt/blob/main/README.md Illustrates generating a Docker command to start an Nginx container and mount a local HTML file using ShellGPT. ```shell sgpt -s "start nginx container, mount ./index.html" # -> docker run -d -p 80:80 -v $(pwd)/index.html:/usr/share/nginx/html/index.html nginx # -> [E]xecute, [D]escribe, [A]bort: e ``` -------------------------------- ### Install ShellGPT Dependencies Source: https://github.com/ther1d/shell_gpt/blob/main/CONTRIBUTING.md This command installs the ShellGPT project dependencies, including development and testing packages. The '-e' flag installs the package in editable mode, and '.[dev,test]' specifies the extra dependencies to install. ```Shell pip install -e ."[dev,test]" ``` -------------------------------- ### Compile ShellGPT Binary with PyInstaller Source: https://github.com/ther1d/shell_gpt/wiki/Home This snippet demonstrates how to build a single executable binary for the ShellGPT application using PyInstaller. It includes cloning the repository, setting up a virtual environment, installing dependencies, and running PyInstaller to create the binary. ```shell git clone https://github.com/TheR1D/shell_gpt.git cd shell_gpt python3 -m venv venv source venv/bin/activate pip install pyinstaller pip install -e . cd sgpt pyinstaller --onefile app.py ``` -------------------------------- ### Run ShellGPT with Azure Backend Source: https://github.com/ther1d/shell_gpt/wiki/Azure Executes ShellGPT using the Azure backend with a specified model deployment and a sample prompt. This command demonstrates how to interact with ShellGPT after the setup is complete. ```shell sgpt --model azure/ --no-functions "Hi Azure" ``` -------------------------------- ### Start Chat Session and Make a Request Source: https://github.com/ther1d/shell_gpt/blob/main/README.md Explains how to start a chat session with a specific name (`conversation_1`) and make a request to the language model. Subsequent requests with the same session name will maintain the conversation context. ```shell sgpt --chat conversation_1 "please remember my favorite number: 4" sgpt --chat conversation_1 "what would be my favorite number + 4?" ``` -------------------------------- ### Install Ollama on Linux/WSL2 Source: https://github.com/ther1d/shell_gpt/wiki/Ollama This command downloads and executes the Ollama installation script for Linux and Windows Subsystem for Linux 2 environments. ```shell curl https://ollama.ai/install.sh | sh ``` -------------------------------- ### Run Shell GPT Docker Container Source: https://github.com/ther1d/shell_gpt/blob/main/README.md This snippet demonstrates how to run the Shell GPT Docker container. It includes setting the OpenAI API key, operating system name, shell name, and mounting a volume for cache storage. It also shows an example command to execute. ```shell docker run --rm \ --env OPENAI_API_KEY=api_key \ --env OS_NAME=$(uname -s) \ --env SHELL_NAME=$(echo $SHELL) \ --volume gpt-cache:/tmp/shell_gpt \ ghcr.io/ther1d/shell_gpt -s "update my system" ``` -------------------------------- ### Initiate Chat Session with Code Generation Source: https://github.com/ther1d/shell_gpt/blob/main/README.md Demonstrates starting a chat session (`conversation_2`) and simultaneously requesting code generation for making a request to localhost using Python. ```shell sgpt --chat conversation_2 --code "make a request to localhost using python" ``` ```python import requests response = requests.get('http://localhost') print(response.text) ``` -------------------------------- ### Analyze docker logs with ShellGPT Source: https://github.com/ther1d/shell_gpt/blob/main/README.md Shows how to pipe `docker logs` output to ShellGPT to analyze logs, find errors, and get potential solutions. ```shell docker logs -n 20 my_app | sgpt "check logs, find errors, provide possible solutions" ``` -------------------------------- ### Start REPL Mode with Initial Prompt from File Source: https://github.com/ther1d/shell_gpt/blob/main/README.md Initiates a REPL mode session with an initial prompt loaded from a file. The content of the specified file (e.g., 'my_app.py') is fed as the initial input to the GPT model. ```shell sgpt --repl temp < my_app.py ``` -------------------------------- ### Install Default Shell GPT Functions Source: https://github.com/ther1d/shell_gpt/blob/main/README.md Installs the default set of functions provided by Shell GPT. These functions can be used by the LLM to execute system tasks and interact with the user's environment. ```shell sgpt --install-functions ``` -------------------------------- ### Shell GPT Docker Alias and Usage Source: https://github.com/ther1d/shell_gpt/blob/main/README.md This section provides an example of creating a Docker alias for Shell GPT for easier command execution. It also shows how to set the OpenAI API key and interact with the tool using chat functionality. ```shell alias sgpt="docker run --rm --volume gpt-cache:/tmp/shell_gpt --env OPENAI_API_KEY --env OS_NAME=$(uname -s) --env SHELL_NAME=$(echo $SHELL) ghcr.io/ther1d/shell_gpt" export OPENAI_API_KEY="your OPENAI API key" sgpt --chat rainbow "what are the colors of a rainbow" sgpt --chat rainbow "inverse the list of your last answer" sgpt --chat rainbow "translate your last answer in french" ``` -------------------------------- ### Pull Mistral LLM Model with Ollama Source: https://github.com/ther1d/shell_gpt/wiki/Ollama This command downloads the 'mistral:7b-instruct' language model using Ollama. The download and installation process may take some time. ```shell ollama pull mistral:7b-instruct ``` -------------------------------- ### Generate OS-specific system update command Source: https://github.com/ther1d/shell_gpt/blob/main/README.md Shows how ShellGPT generates an appropriate system update command based on the operating system. Examples for macOS and Ubuntu are provided. ```shell sgpt -s "update my system" # -> sudo softwareupdate -i -a # -> [E]xecute, [D]escribe, [A]bort: e ``` ```shell sgpt -s "update my system" # -> sudo apt update && sudo apt upgrade -y # -> [E]xecute, [D]escribe, [A]bort: e ``` -------------------------------- ### Start REPL Mode Chat Session Source: https://github.com/ther1d/shell_gpt/blob/main/README.md Initiates an interactive chat session with GPT models in REPL mode. A unique session name can be provided, or 'temp' can be used for a temporary session. This mode shares the underlying object with chat mode, allowing for seamless transitions between them. ```shell sgpt --repl temp ``` -------------------------------- ### Iterative Shell Command Generation in Chat Mode Source: https://github.com/ther1d/shell_gpt/blob/main/README.md Illustrates how to use shell_gpt in a chat mode (`conversation_3`) to iteratively generate and refine shell commands, starting with listing files, sorting them, concatenating them with FFmpeg, and finally converting the output to MP3. ```shell sgpt --chat conversation_3 --shell "what is in current folder" sgpt --chat conversation_3 "Sort by name" sgpt --chat conversation_3 "Concatenate them using FFMPEG" sgpt --chat conversation_3 "Convert the resulting file into an MP3" ``` -------------------------------- ### LLM Handles Function Execution Errors and Retries Source: https://github.com/ther1d/shell_gpt/blob/main/README.md Shows how Shell GPT and the LLM can handle errors during function execution. If a command fails (e.g., due to a missing tool like `jq`), the LLM can attempt to resolve the issue, such as installing the missing tool, and then retry the original command. ```shell sgpt "parse /tmp/test.json file using jq and return only email value" # -> @FunctionCall execute_shell_command(shell_command="jq -r '.email' /tmp/test.json") # -> It appears that jq is not installed on the system. Let me try to install it using brew. # -> @FunctionCall execute_shell_command(shell_command="brew install jq") # -> jq has been successfully installed. Let me try to parse the file again. ``` -------------------------------- ### Ask about shell redirects using here-string Source: https://github.com/ther1d/shell_gpt/blob/main/README.md Shows how to use a here-string (`<<<`) to pass a single line of input to ShellGPT, asking about learning shell redirects. ```shell sgpt <<< "What is the best way to learn shell redirects?" # -> The best way to learn shell redirects is through... ``` -------------------------------- ### Test ShellGPT with Ollama Backend Source: https://github.com/ther1d/shell_gpt/wiki/Ollama This command tests the ShellGPT integration with the Ollama backend by querying the 'ollama/mistral:7b-instruct' model. It requires a valid OpenAI API key or a placeholder string if not using OpenAI. ```shell sgpt --model ollama/mistral:7b-instruct "Who are you?" ``` -------------------------------- ### Configure ShellGPT for LiteLLM Source: https://github.com/ther1d/shell_gpt/wiki/Azure Sets up the ShellGPT configuration file to enforce LiteLLM usage and provides a placeholder for the Azure OpenAI API key. This configuration is crucial for directing ShellGPT to use LiteLLM. ```text USE_LITELLM=true OPENAI_API_KEY=YOUR_AZURE_KEY ``` -------------------------------- ### Enable Request Caching Source: https://github.com/ther1d/shell_gpt/blob/main/README.md Demonstrates how to enable request caching using the `--cache` option for `sgpt` requests to the OpenAI API. This speeds up subsequent identical queries by retrieving results from local storage. ```shell sgpt "what are the colors of a rainbow" ``` -------------------------------- ### Create and Activate Python Virtual Environment Source: https://github.com/ther1d/shell_gpt/blob/main/CONTRIBUTING.md This snippet demonstrates how to create a Python virtual environment named 'env' and activate it using the 'source' command. This is a standard practice for isolating project dependencies. ```Shell python -m venv env && source ./env/bin/activate ``` -------------------------------- ### Chain Function Calls in Shell GPT Source: https://github.com/ther1d/shell_gpt/blob/main/README.md Illustrates chaining multiple function calls in a single prompt to perform sequential actions, such as playing music and opening a URL. This highlights the tool's ability to handle complex workflows. ```shell sgpt "Play music and open hacker news" ``` -------------------------------- ### Build Shell GPT Docker Image Source: https://github.com/ther1d/shell_gpt/blob/main/README.md This command shows how to build a custom Docker image for the Shell GPT project using the provided Dockerfile. ```shell docker build -t sgpt . ``` -------------------------------- ### Execute general query with ShellGPT Source: https://github.com/ther1d/shell_gpt/blob/main/README.md Basic usage of ShellGPT to answer a general knowledge question. The output is the AI-generated answer. ```shell sgpt "What is the fibonacci sequence" # -> The Fibonacci sequence is a series of numbers where each number ... ``` -------------------------------- ### Execute Shell Command with jq Source: https://github.com/ther1d/shell_gpt/blob/main/README.md Demonstrates executing a shell command using `jq` to extract the email from a JSON file. This showcases direct command execution within the tool. ```shell sgpt "execute_shell_command(shell_command=\"jq -r '.email' /tmp/test.json\")" ``` -------------------------------- ### Summarize document using input redirection Source: https://github.com/ther1d/shell_gpt/blob/main/README.md Illustrates using input redirection (`<`) to pass the content of a file to ShellGPT for summarization. ```shell sgpt "summarise" < document.txt # -> The document discusses the impact... ``` -------------------------------- ### Create Custom Role for JSON Generation Source: https://github.com/ther1d/shell_gpt/blob/main/README.md Shows how to create a custom role named `json_generator` with a specific description to ensure responses are always in valid JSON format. It also demonstrates using the role to generate structured data. ```shell sgpt --create-role json_generator # Enter role description: Provide only valid json as response. ``` ```shell sgpt --role json_generator "random: user, password, email, address" ``` -------------------------------- ### Generate Python Code and Save to File Source: https://github.com/ther1d/shell_gpt/blob/main/README.md Illustrates how to generate Python code using shell_gpt and redirect the output to a file (`fizz_buzz.py`). It also shows how to execute the saved Python script. ```shell sgpt --code "solve classic fizz buzz problem using Python" > fizz_buzz.py python fizz_buzz.py ``` -------------------------------- ### Execute Shell Command and Pipe to Clipboard Source: https://github.com/ther1d/shell_gpt/blob/main/README.md Demonstrates how to execute a shell command generated by shell_gpt and pipe its output to `pbcopy` to save it to the clipboard. The `--no-interaction` flag is used to disable interactive mode and print the command directly to standard output. ```shell sgpt -s "find all json files in current folder" --no-interaction | pbcopy ``` -------------------------------- ### Generate ffmpeg command for video concatenation Source: https://github.com/ther1d/shell_gpt/blob/main/README.md Shows how to use ShellGPT to generate an `ffmpeg` command for combining multiple video files into one, excluding audio. ```shell sgpt -s "ffmpeg combine $(ls -m) into one video file without audio." # -> ffmpeg -i 1.mp4 -i 2.mp4 -i 3.mp4 -filter_complex "[0:v] [1:v] [2:v] concat=n=3:v=1 [v]" -map "[v]" out.mp4 ``` -------------------------------- ### Generate Comments for Python Code Source: https://github.com/ther1d/shell_gpt/blob/main/README.md Demonstrates piping existing Python code to shell_gpt with the `--code` option to generate comments for each line of the code. ```shell cat fizz_buzz.py | sgpt --code "Generate comments for each line of my code" ``` ```python # Loop through numbers 1 to 100 for i in range(1, 101): # Check if number is divisible by both 3 and 5 if i % 3 == 0 and i % 5 == 0: # Print "FizzBuzz" if number is divisible by both 3 and 5 print("FizzBuzz") # Check if number is divisible by 3 elif i % 3 == 0: # Print "Fizz" if number is divisible by 3 print("Fizz") # Check if number is divisible by 5 elif i % 5 == 0: # Print "Buzz" if number is divisible by 5 print("Buzz") # If number is not divisible by 3 or 5, print the number itself else: print(i) ``` -------------------------------- ### Execute Shell Commands in REPL Mode Source: https://github.com/ther1d/shell_gpt/blob/main/README.md Enables REPL mode to interactively execute shell commands. Users can type commands directly or use 'e' to execute, and 'd' to describe them. This is useful for interactive shell command execution and code generation. ```shell sgpt --repl temp --shell ``` -------------------------------- ### LLM Executes Custom Shell Command Function Source: https://github.com/ther1d/shell_gpt/blob/main/README.md Illustrates how the LLM can invoke a custom function, such as `execute_shell_command`, to perform actions like listing files in a directory. The output of the function is then presented to the user. ```shell sgpt "What are the files in /tmp folder?" # -> @FunctionCall execute_shell_command(shell_command="ls /tmp") # -> The /tmp folder contains the following files and directories: # -> test.txt # -> test.json ``` -------------------------------- ### Provide Multiline Prompt in REPL Mode Source: https://github.com/ther1d/shell_gpt/blob/main/README.md Allows users to input multiline prompts in REPL mode using triple quotes. This is useful for providing complex instructions or code snippets to the GPT model for explanation or execution. ```shell sgpt --repl temp >>> """ ... Explain following code: ... import random ... print(random.randint(1, 10)) ... """ ``` -------------------------------- ### Generate git commit message from diff Source: https://github.com/ther1d/shell_gpt/blob/main/README.md Demonstrates using ShellGPT to generate a git commit message by piping the output of `git diff` to the `sgpt` command. ```shell git diff | sgpt "Generate git commit message, for my changes" # -> Added main feature details into README.md ``` -------------------------------- ### Generate curl command with piped JSON data Source: https://github.com/ther1d/shell_gpt/blob/main/README.md Demonstrates using ShellGPT with piped JSON data to generate a `curl` command for a POST request. ```shell sgpt -s "POST localhost with" < data.json # -> curl -X POST -H "Content-Type: application/json" -d '{"a": 1, "b": 2}' http://localhost # -> [E]xecute, [D]escribe, [A]bort: e ``` -------------------------------- ### Generate shell command with ShellGPT Source: https://github.com/ther1d/shell_gpt/blob/main/README.md Demonstrates generating a shell command to find all JSON files in the current directory using ShellGPT. The `--shell` or `-s` flag is used for this purpose. ```shell sgpt --shell "find all json files in current folder" # -> find . -type f -name "*.json" # -> [E]xecute, [D]escribe, [A]bort: e ``` -------------------------------- ### Generate Python Code for FizzBuzz Source: https://github.com/ther1d/shell_gpt/blob/main/README.md Shows how to use the `--code` parameter to generate Python code for the classic FizzBuzz problem. The generated code iterates from 1 to 100 and prints 'Fizz', 'Buzz', or 'FizzBuzz' based on divisibility rules. ```shell sgpt --code "solve fizz buzz problem using python" ``` ```python for i in range(1, 101): if i % 3 == 0 and i % 5 == 0: print("FizzBuzz") elif i % 3 == 0: print("Fizz") elif i % 5 == 0: print("Buzz") else: print(i) ``` -------------------------------- ### Show Specific Chat Session History Source: https://github.com/ther1d/shell_gpt/blob/main/README.md Demonstrates how to display the complete message history for a specific chat session by using the `--show-chat` option followed by the session name. ```shell sgpt --show-chat conversation_1 ``` -------------------------------- ### Set Azure Environment Variables for LiteLLM Source: https://github.com/ther1d/shell_gpt/wiki/Azure Exports environment variables required for LiteLLM to connect to the Azure OpenAI service. These variables include the API base URL, API version, and optional token/type for authentication. ```shell export AZURE_API_BASE=YOUR_API_BASE export AZURE_API_VERSION=YOUR_API_VERSION export AZURE_AD_TOKEN=YOUR_TOKEN # Optional export AZURE_API_TYPE=YOUR_API_TYPE # Optional ``` -------------------------------- ### Use ShellGPT with Ollama Default Model Source: https://github.com/ther1d/shell_gpt/wiki/Ollama This command uses ShellGPT with the default model configured in the .sgptrc file, which should be set to an Ollama model like 'ollama/mistral:7b-instruct' for local LLM usage. ```shell sgpt "Hello Ollama" ``` -------------------------------- ### Define Custom Function for Shell Command Execution Source: https://github.com/ther1d/shell_gpt/blob/main/README.md Demonstrates how to define a custom Python function for Shell GPT to execute shell commands. The function uses Pydantic and Instructor to define the schema and execution logic, returning the command's exit code and output. ```python # execute_shell_command.py import subprocess from pydantic import Field from instructor import OpenAISchema class Function(OpenAISchema): """ Executes a shell command and returns the output (result). """ shell_command: str = Field(..., example="ls -la", descriptions="Shell command to execute.") class Config: title = "execute_shell_command" @classmethod def execute(cls, shell_command: str) -> str: result = subprocess.run(shell_command.split(), capture_output=True, text=True) return f"Exit code: {result.returncode}, Output:\n{result.stdout}" ``` -------------------------------- ### Add Caching to Python Request in Chat Session Source: https://github.com/ther1d/shell_gpt/blob/main/README.md Shows how to continue a chat session (`conversation_2`) by requesting an enhancement to the previously generated Python code, specifically adding caching functionality using the `cachecontrol` library. ```shell sgpt --chat conversation_2 --code "add caching" ``` ```python import requests from cachecontrol import CacheControl sess = requests.session() cached_sess = CacheControl(sess) response = cached_sess.get('http://localhost') print(response.text) ``` -------------------------------- ### List All Chat Sessions Source: https://github.com/ther1d/shell_gpt/blob/main/README.md Provides the command to list all active chat sessions managed by shell_gpt. This is useful for reviewing and managing conversation history. ```shell sgpt --list-chats ``` === COMPLETE CONTENT === This response contains all available snippets from this library. No additional content exists. Do not make further requests.