diff --git a/README.md b/README.md index b0d91df..8b187f9 100644 --- a/README.md +++ b/README.md @@ -1,57 +1,57 @@ # zsh-ollama-command -An [`oh-my-zsh`](https://ohmyz.sh) plugin that integrates the OLLAMA AI model -with [fzf](https://github.com/junegunn/fzf) to provide intelligent command -suggestions based on user input requirements. +An [`oh-my-zsh`](https://ohmyz.sh) plugin that integrates the OLLAMA AI model with +[fzf](https://github.com/junegunn/fzf) to provide intelligent command suggestions based on user input +requirements. ## Features -* **Intelligent Command Suggestions**: Use OLLAMA to generate relevant MacOS - terminal commands based on your query or input requirement. -* **FZF Integration**: Interactively select suggested commands using FZF's fuzzy - finder, ensuring you find the right command for your task. -* **Customizable**: Configure default shortcut, OLLAMA model, and response number - to suit your workflow. +- **Intelligent Command Suggestions**: Use OLLAMA to generate relevant MacOS/Linux terminal commands based on + your query or input requirement. +- **FZF Integration**: Interactively select suggested commands using FZF's fuzzy finder, ensuring you find the + right command for your task. +- **Customizable**: Configure default shortcut, OLLAMA model, and response number to suit your workflow. ## Requirements -* `jq` for parsing JSON responses -* `fzf` for interactive selection of commands -* `curl` for making API requests -* `OLLAMA` server running +- `jq` for parsing JSON responses +- `fzf` for interactive selection of commands +- `curl` for making API requests +- `OLLAMA` server running ## Configuration Variables -| Variable Name | Default Value | Description | -|------------------------------|--------------------------|------------------------------------------------| -| `ZSH_OLLAMA_MODEL` | `llama3` | OLLAMA model to use (e.g., `llama3`) | -| `ZSH_OLLAMA_COMMANDS_HOTKEY` | `Ctrl-o` | Default shortcut key for triggering the plugin | -| `ZSH_OLLAMA_COMMANDS` | 5 | Number of command suggestions displayed | -| `ZSH_OLLAMA_URL` | `http://localhost:11434` | The URL of OLLAMA server host | +| Variable Name | Default Value | Description | +| ---------------------------- | ---------------------------- | ---------------------------------------------- | +| `ZSH_OLLAMA_MODEL` | `llama3` | OLLAMA model to use (e.g., `llama3`) | +| `ZSH_OLLAMA_COMMANDS_HOTKEY` | `Ctrl-o` | Default shortcut key for triggering the plugin | +| `ZSH_OLLAMA_COMMANDS` | 5 | Number of command suggestions displayed | +| `ZSH_OLLAMA_URL` | `http://localhost:11434` | The URL of OLLAMA server host | +| `ZSH_OLLAMA_PROMPT` | See `zsh-ollama-command.zsh` | The prompt fed to the model | ## Usage 1. Clone the repository to `oh-my-zsh` custom plugin folder - ```bash - git clone https://github.com/plutowang/zsh-ollama-command.git ${ZSH_CUSTOM:-~/.oh-my-zsh/custom}/plugins/zsh-ollama-command - ``` + + ```bash + git clone https://github.com/plutowang/zsh-ollama-command.git ${ZSH_CUSTOM:-~/.oh-my-zsh/custom}/plugins/zsh-ollama-command + ``` 2. Enable the plugin in ~/.zshrc: - ```bash - plugins=( - [plugins...] - zsh-ollama-command - ) - ``` + ```bash + plugins=( + [plugins...] + zsh-ollama-command + ) + ``` 3. Input what you want to do then trigger the plugin. Press the custom shortcut (default is Ctrl-o) to start the command suggestion process. -4. Interact with FZF: Type a query or input requirement, and FZF will display - suggested MacOS terminal commands. Select one to execute. +4. Interact with FZF: Type a query or input requirement, and FZF will display suggested MacOS/Linux terminal + commands. Select one to execute. **Get Started** -Experience the power of AI-driven command suggestions in your MacOS terminal! This -plugin is perfect for developers, system administrators, and anyone looking to -streamline their workflow. +Experience the power of AI-driven command suggestions in your MacOS/Linux terminal! This plugin is perfect for +developers, system administrators, and anyone looking to streamline their workflow. Let me know if you have any specific requests or changes! diff --git a/zsh-ollama-command.zsh b/zsh-ollama-command.zsh index 6836967..cf909cc 100644 --- a/zsh-ollama-command.zsh +++ b/zsh-ollama-command.zsh @@ -7,26 +7,28 @@ # default ollama server host (( ! ${+ZSH_OLLAMA_URL} )) && typeset -g ZSH_OLLAMA_URL='http://localhost:11434' +[ -f /etc/os-release ] && OS_TYPE=$(cat /etc/os-release | grep "PRETTY_NAME" | sed -e "s/PRETTY_NAME=//" -e "s/\"//g") || OS_TYPE="MacOS" + validate_required() { # check required tools are installed if (( ! $+commands[jq] )) then echo "🚨: zsh-ollama-command failed as jq NOT found!" - echo "Please install it with 'brew install jq'" + echo "Please install it first!" return 1; fi if (( ! $+commands[fzf] )) then echo "🚨: zsh-ollama-command failed as fzf NOT found!" - echo "Please install it with 'brew install fzf'" + echo "Please install it first!" return 1; fi if (( ! $+commands[curl] )) then echo "🚨: zsh-ollama-command failed as curl NOT found!" - echo "Please install it with 'brew install curl'" + echo "Please install it first!" return 1; fi if ! (( $(pgrep -f ollama | wc -l ) > 0 )); then echo "🚨: zsh-ollama-command failed as OLLAMA server NOT running!" - echo "Please start it with 'brew services start ollama'" + echo "Please start it first!" return 1; fi if ! curl -s "${ZSH_OLLAMA_URL}/api/tags" | grep -q $ZSH_OLLAMA_MODEL; then @@ -59,7 +61,7 @@ fzf_ollama_commands() { print print -u1 "👻Please wait..." - ZSH_OLLAMA_COMMANDS_MESSAGE_CONTENT="Seeking OLLAMA for MacOS terminal commands for the following task: $ZSH_OLLAMA_COMMANDS_USER_QUERY. Reply with an array without newlines consisting solely of possible commands. The format would be like: ['command1; comand2;', 'command3&comand4;']. Response only contains array, no any additional description. No additional text should be present in each entry and commands, remove empty string entry. Each string entry should be a new string entry. If the task need more than one command, combine them in one string entry. Each string entry should only contain the command(s). Do not include empty entry. Provide multiple entry (at most $ZSH_OLLAMA_COMMANDS relevant entry) in response Json suggestions if available. Please ensure response can be parsed by jq" + [ ! -z "$ZSH_OLLAMA_PROMPT" ] && ZSH_OLLAMA_COMMANDS_MESSAGE_CONTENT="$ZSH_OLLAMA_PROMPT" || ZSH_OLLAMA_COMMANDS_MESSAGE_CONTENT="Seeking OLLAMA for $OS_TYPE terminal commands for the following task: $ZSH_OLLAMA_COMMANDS_USER_QUERY. Reply with an array without newlines consisting solely of possible commands. The format would be like: ['command1; comand2;', 'command3&comand4;']. Response only contains array, no any additional description. No additional text should be present in each entry and commands, remove empty string entry. Each string entry should be a new string entry. If the task need more than one command, combine them in one string entry. Each string entry should only contain the command(s). Do not include empty entry. Provide multiple entry (at most $ZSH_OLLAMA_COMMANDS relevant entry) in response Json suggestions if available. Please ensure response can be parsed by jq" ZSH_OLLAMA_COMMANDS_REQUEST_BODY='{ "model": "'$ZSH_OLLAMA_MODEL'",