Skip to content

chamdim/openrouter.ai

 
 

Folders and files

NameName
Last commit message
Last commit date

Latest commit

 

History

21 Commits
 
 
 
 
 
 
 
 
 
 

Repository files navigation

openrouter.ai in Terminal

Simple terminal app to use OpenRouter.ai with your personal API keys. Ask questions directly from your terminal and get AI responses instantly.

Features

  • 🚀 Simple command-line interface
  • 🔑 Secure API key and model management with .env file
  • 🎯 Clean output (only the AI response, no JSON clutter)
  • 💬 Natural language queries
  • 🆓 Uses free Mistral model by default
  • ⚡ Stateless design - each query is independent (no conversation history)

Important Note

This tool is designed for single-question interactions. Each time you run the script, it sends an independent query to the AI model without any memory of previous conversations. If you need multi-turn conversations with context retention, consider using the OpenRouter web interface or building a more advanced wrapper that maintains conversation history.

Prerequisites

  • curl (usually pre-installed on most systems)
  • jq for JSON parsing

Installing jq

Ubuntu/Debian:

sudo apt install jq

CentOS/RHEL/Fedora:

sudo yum install jq
# or for newer versions:
sudo dnf install jq

macOS:

brew install jq

Windows (WSL):

sudo apt install jq

Installation

  1. Clone or download this repository:

    git clone https://github.com/SynergOps/openrouter.ai
    cd openrouter.ai
  2. Ensure the script is executable:

    chmod +x ai.sh
  3. Get your OpenRouter API key:

    • Go to OpenRouter.ai
    • Sign up with your Github account for a free account
    • Navigate to models and then the Mistral Small 3.2 24B (free)
    • Click API keys section
    • Generate a new API key
  4. Configure your API key and model:

    • while you are in the openrouter folder, edit the .env file template and add your API key:
    vim .env
    • Add your API key and optionally configure the model:
    # OpenRouter API Configuration
    OPENROUTER_API_KEY=sk-or-v1-your-actual-api-key-here
    # OpenRouter Model Configuration (optional - leave empty for default)
    OPENROUTER_MODEL=
    

Usage

Basic Usage

./ai.sh your question here

Examples

# Ask a simple question
./ai.sh what is the meaning of life

# Ask for coding help
./ai.sh how do I create a function in Python

# Ask for a definition
./ai.sh define recursion

# Ask for a summary
./ai.sh summarize the plot of "The Hitchhiker's Guide to the Galaxy"

Sample Output

$ ./ai.sh what is the meaning of 42
The number 42 is famously known as "The Answer to the Ultimate Question of Life, the Universe, and Everything" from Douglas Adams' science fiction series "The Hitchhiker's Guide to the Galaxy."

Creating a Terminal Alias (Recommended)

For easier access, you can create an alias so you can use the script from anywhere without typing the full path:

Option 1: Temporary alias (current session only)

alias ai='/path/to/your/openrouter.ai/ai.sh'

Option 2: Permanent alias (recommended)

  1. For Bash users - Add to your ~/.bashrc or ~/.bash_profile:

    cd /path/to/your/openrouter.ai
    echo "alias ai='$(pwd)/ai.sh'" >> ~/.bashrc
    source ~/.bashrc
  2. For Zsh users - Add to your ~/.zshrc:

    cd /path/to/your/openrouter.ai
    echo "alias ai='$(pwd)/ai.sh'" >> ~/.zshrc
    source ~/.zshrc
  3. Manual method - Edit your shell config file:

    # Open your shell config file
    nano ~/.bashrc  # or ~/.zshrc for Zsh users
    
    # Add this line (replace with your actual path):
    alias ai='/full/path/to/openrouter.ai/ai.sh'
    
    # Reload your shell config
    source ~/.bashrc  # or source ~/.zshrc

After setting up the alias, you can use it from anywhere:

# Instead of ./ai.sh question
# Works from any directory
cd ~/Documents
ai explain machine learning

Configuration

Changing the AI Model

You can change the AI model by editing the OPENROUTER_MODEL variable in your .env file:

# Leave empty or unset to use the default model
OPENROUTER_MODEL=

# Or specify a different model
OPENROUTER_MODEL=qwen/qwq-32b:free

Popular free models on OpenRouter include:

  • mistralai/mistral-small-3.2-24b-instruct:free (default)
  • qwen/qwq-32b:free
  • deepseek/deepseek-r1-0528:free
  • google/gemini-2.0-flash-exp:free

Note: If OPENROUTER_MODEL is not set or left empty, the script will use the default Mistral model.

Common Issues

  1. "jq: command not found"

    • Install jq using the instructions above
  2. "Error: .env file not found"

    • Make sure you have a .env file in the same directory as ai.sh
    • Check that your API key is properly set in the .env file
  3. "Error: OPENROUTER_API_KEY is not set"

    • Verify your .env file contains: OPENROUTER_API_KEY=your-actual-key
    • Make sure there are no extra spaces around the = sign
  4. Empty, Invalid model, or other errors

    • Check that your OPENROUTER_MODEL setting in .env is correct
    • Verify the model name is available on OpenRouter
    • Leave OPENROUTER_MODEL= empty to use the default model
  5. Empty or error responses

    • Carefully read the message response and act accordingly
    • Verify your API key is valid and active
    • Check your internet connection
    • Ensure you haven't exceeded any rate limits

Debug Mode

The script automatically shows the full JSON response when it encounters errors or unexpected responses. If you need to see the raw API response for debugging purposes, the script will display it when:

  • The API returns an error
  • The response format is unexpected
  • The content cannot be extracted properly

This provides all the debugging information you need without manually modifying the script.

License

This project is licensed under the MIT License - see the LICENSE file for details.

Contributing

Feel free to submit issues, fork the repository, and create pull requests for any improvements.

Support

If you encounter any issues or have questions, please open an issue on GitHub.

About

Simple terminal app to use Openrouter.ai with your personal API keys

Resources

License

Stars

Watchers

Forks

Releases

No releases published

Packages

No packages published

Languages

  • Shell 100.0%