Giter VIP home page Giter VIP logo

gpt-cli's Introduction

gpt-cli

preview


Overview

Access the most powerful LLMs from within the terminal.

Main Features
  • Streamed responses with full Markdown rendering and syntax highlighting
  • Intentionally minimal UI/UX with configurable styling
  • Support for OpenAI and Anthropic models, with more coming soon (Mixtral, DBRX)
    • Easily choose model with CLI option and API key
    • Highly extensible implementation
  • Top-class terminal integration via escape codes

I created this out of curiosity of CLI/TUI development and because of the lack of free and convenient interfaces to GPT-4. Now a staple in my daily workflow as a rubber duck debugger and google alternative, I have found this CLI works great within one's IDE.

Installation and Recommended Setup

Developed on MacOS and Ubuntu. All instructions will assume a unix-like OS with Python 3.11+ installed.

An OpenAI API key is required to access the GPT models and an Anthropic API key is needed for the Claude models. Both of these companies currently require paying for usage credits in advance, so ensure you have an account with money loaded before using the CLI.

Sign up for OpenAI here and Anthropic here.

Once you have you API keys, place them in a file named env.json in the project root directory like so:

gpt-cli/env.json

{
  "openaiAPIKey": "12345678",
   "anthropicAPIKey": "87654321"
}

I prefer to install this app's dependencies to the system interpreter so that I can use a shell alias to run it at any time with no prior venv set up. In the terminal:

  1. Ensure you are not using a venv: run deactivate
  2. After cloning and inside the project directory, run make install to download all python packages.
  3. Add shell alias and reload shell config to apply changes (assuming bash or zsh):
    config_file="${HOME}/.$(basename ${SHELL})rc" && \
    echo -e "\nalias llm='python3 ENTER_ABSOLUTE_PATH_TO_/gpt-cli/src/gpt-cli/main.py_HERE'" >> "${config_file}" && \ 
    source "${config_file}" 

Now no matter where your current working directory is, you can type llm in your terminal to start a GPT session!

Configuration

Note: All settings are documented in the help page, accessible by running with the help option: llm --help (assuming you have the shell alias described above)

System Message

This is extremely important to properly tailor to your use case. Heavily influences every response from the model. By default, it is "You are a concise assistant to a software engineer", but you can change this at any time via command line arguments or by editing the source code.

Colors

styling.py contains many editable values to control the look of the program

Markdown code blocks can be rendered in any of the styles found here by setting the --code-theme option, and the color of the plaintext responses can be set to most normal colors using --text-color (ex. llm --text-color=orange)

Usage

help page

Controls

The program is a simple REPL. Each time you click Enter your prompt will be sent and the response will be streamed in real time. CTRL+D and CTRL+C work as expected in a REPL, exiting the program and cancelling the current loop, respectively. Entering q, quit, e, or exit while in prompt mode will also exit the program.

When typing a prompt, basic keyboard shortcuts are available like history navigation with the arrow-keys and deleting entire line with CTRL + U. More will be added in the future. Multiline text input is supported by entering ml a single backslash into the prompt. This is useful when pasting code into the prompt.

When in multiline mode, you must use Meta + Enter to submit the prompt. On macOS it is [Option or Command] + Escape + Enter

Your entire chat history will be sent to the language model each time you press Enter. To clear your chat history, enter c or clear into the prompt. I recommend doing this whenever you want to ask the model a different line of questioning, so that you get the highest quality answers and you do not run up your usage bill.

Pricing

Once your current conversation costs more than a cent or two, it will be shown at the end of the response so that you know how much you're spending. Total session cost will also be shown when the program exits.

Disclaimer: While I do use the official OpenAI tokenizer and an official cookbook example from that repo to calculate prices, it is not something I have tested thoroughly, so there is no guarantee the prices are accurate. Anthropic provides authoritative token counts however, so those should be calculated better

The pricing for the GPT turbo models is pretty cheap. Other than the Opus model, Anthropic's offerings are comparatively even cheaper. For clarity, both OpenAI and Anthropic bill you on tokens per request AND response, and the entire chat history of previous prompts and responses are sent in EACH request. As long as you remember to clear your history after a few prompts you will be fine.

If you want to have multiple sessions, use screen or tmux. I want this project to "do one thing well" and leave other features to existing programs. This is the difference between this project and elia, for example.

Development

This is a personal project above all else. I use this to play around with design patterns, new python features, data structures, best practices, etc.

Feature requests are more than welcome, however I will probably take a while to get to them.

Planned Features:
  • Ensure the code-base is highly extensible
  • Support for other LLMs if any seem worth adding
  • Incorporate cutting-edge tooling like uv and Ruff.

gpt-cli's People

Contributors

gregriff avatar

Stargazers

gamma,gamma'-Dibenzophenanthrene (C22H14) avatar Peter Conerly avatar

Watchers

Kostas Georgiou avatar  avatar

gpt-cli's Issues

Improve error handling

OpenAI and Anthropic describe HTTP error codes in their documentation. Impl error messages for the most common ones.

local Ollama models

Ollama exposes an Open AI-compatible API for local models. Add code to:

  • GET all models exposed by ollama API at startup (before arg parsing), append these to valid models
  • add Ollama model LLM subclass, disable price calculation

Allow api keys from env vars

Both anthropic and openAI clients try Env vars for api key, but my application logic exits the program before this is attempted. This should be allowed

Research and document keyboard shortcuts

Prompt_toolkit enables many keybindings by default. These are configurable in code. Not sure what the default set of these are, so find that out, then make a nice list in the Readme explaining them

Customize HTTP requests

OpenAI and I'm sure anthropic too allow for messing with headers etc. Ensure everything is secure and sensible defaults

Add Ruff, Uv

replace black with Ruff and pip with Uv just to get experience with these tools as they will become the new norm

investigate prompt_toolkit async

Dig into prompt_toolkit docs to see how it uses the event loop because I'm pretty sure it uses it regardless of whether I'm using async in the top-level code. If it uses it regardless, might as well install uvloop to make it faster. If we can avoid the async stuff, then do it as I do not see why it is necessary.

fetch new models with CLI command, allow use of arbitrary models

OpenAI has an endpoint that returns the string identifiers of all models available through their API. This could be used to "update" the CLI whenever openAI releases a new model. This would require:

  1. Allowing users to use models other than the ones with hardcoded prices in config.py
  2. storing valid models (used to fuzzy match argv[1]) in JSON or something rather than a hardcoded dict

Find out whether Anthropic (and I guess databricks) has a fetch models endpoint to impl this for them as well

Proper styling configuration

Styling config was implemented quickly and lazily. Since this is a goal of this project, impl proper configuration with TOML or Pkl files so that users do not lose their config every time they pull the latest changes

add DBRX and Mixtral

Databricks serves models from their own URL and API key, but they allow devs to use OpenAI's python SDK and just point it to the databricks URL.

Add this to available models as DBRX seems at least worth checking out and Mixtral too if cheap enough.

Recommend Projects

  • React photo React

    A declarative, efficient, and flexible JavaScript library for building user interfaces.

  • Vue.js photo Vue.js

    ๐Ÿ–– Vue.js is a progressive, incrementally-adoptable JavaScript framework for building UI on the web.

  • Typescript photo Typescript

    TypeScript is a superset of JavaScript that compiles to clean JavaScript output.

  • TensorFlow photo TensorFlow

    An Open Source Machine Learning Framework for Everyone

  • Django photo Django

    The Web framework for perfectionists with deadlines.

  • D3 photo D3

    Bring data to life with SVG, Canvas and HTML. ๐Ÿ“Š๐Ÿ“ˆ๐ŸŽ‰

Recommend Topics

  • javascript

    JavaScript (JS) is a lightweight interpreted programming language with first-class functions.

  • web

    Some thing interesting about web. New door for the world.

  • server

    A server is a program made to process requests and deliver data to clients.

  • Machine learning

    Machine learning is a way of modeling and interpreting data that allows a piece of software to respond intelligently.

  • Game

    Some thing interesting about game, make everyone happy.

Recommend Org

  • Facebook photo Facebook

    We are working to build community through open source technology. NB: members must have two-factor auth.

  • Microsoft photo Microsoft

    Open source projects and samples from Microsoft.

  • Google photo Google

    Google โค๏ธ Open Source for everyone.

  • D3 photo D3

    Data-Driven Documents codes.