Expand description

OpenAI Client CLI

crates.io docs.rs GitHub Actions

Authors: Asher Jingkong Chen
Description: OpenAI API client CLI

Background

I like to use CLI (e.g. cURL) to fetch OpenAI’s REST API but …

Not super easy to use! That’s why I made this :)

Installation guides

The following guides will help you install the CLI program.

Install via Cargo (1st option)

Cargo will install and compile the program on your machine.

cargo install openai-client-cli

Install via Homebrew (2nd option)

[WIP]

Install from git and build from source codes (3rd option)

  1. Copy the source code with git and change the working directory
git clone --recurse-submodules https://github.com/AsherJingkongChen/openai-client-cli.git
cd openai-client-cli
  1. Install Rust toolchain manager: rustup
curl --proto '=https' --tlsv1.2 -sSf https://sh.rustup.rs | sh
  1. Install Rust toolchain (at least 1.72.0)
rustup toolchain install 1.72.0
rustup show
  1. Build in release mode
cargo build --release

or:

cargo b -r
  1. Add the directory target/release to environment variable $PATH
export PATH='$PATH:target/release'

To add the executable in your environment permanently, you can add the export command to ~/.profile, ~/.bashrc or ~/.zshrc.

The command runs everytime when a shell session is launched.

Usage

Check the following manual to learn how to work on the CLI.

Manual review: link

The command to show manual:

openai --help

or:

openai -h
openai-client-cli 0.1.1 by Asher Jingkong Chen
OpenAI API client CLI

Usage: openai-client [OPTIONS] <PATH>

Arguments:
  <PATH>
          The API request path. (part of the URL)
          The program will use regex to extract the matched segment in <PATH>.
          For example, the extracted strings will be the same when <PATH> is either
          `chat/completions`, `/chat/completions` or `https://api.openai.com/v1/chat/completions`.

Options:
  -k, --key-file <KEY_FILE_PATH>
          The file path where the API key is stored.
          The program will attempt the following steps to obtain a valid API key:
           1. Read the file from the provided path <KEY_FILE_PATH>.
           2. Read the environment variable `OPENAI_API_KEY`.
           3. Read the file from the default paths in the following order:
              `openai.env`, `.openai_profile`, `.env`,
              `~/openai.env`, `~/.openai_profile` or `~/.env`.
           4. Exit the program with a non-zero return code.
          
  -m, --method <METHOD>
          The HTTP method used for the API request.
          The program will attempt the following steps to determine a valid HTTP method:
           1. Read the value of argument <METHOD>.
           2. If the `parameter` object is successfully fetched from either
              <PARAM_FILE_PATH> or one of the default paths, set <METHOD> to `POST`.
           3. Otherwise, set <METHOD> to `GET`.
          
  -g, --org-file <ORG_FILE_PATH>
          The file path where the organization ID is stored.
          The program will attempt the following steps to obtain a valid organization ID:
           1. Read the file from the provided path <ORG_FILE_PATH>.
           2. Read the file from provided path of key file <KEY_FILE_PATH>.
           3. Read the environment variable `OPENAI_ORG_KEY`.
           4. Read the file from the default paths in the following order:
              `openai.env`, `.openai_profile`, `.env`,
              `~/openai.env`, `~/.openai_profile` or `~/.env`.
           5. Ignore the field and leave it empty.
          
  -o, --output-file <OUTPUT_FILE_PATH>
          The file path where the API response will be stored.
          The program will attempt the following steps to successfully store the response:
           1. Export the output to the provided file path <OUTPUT_FILE_PATH>.
           2. Export the output to the standard output.
           3. Exit the program with a non-zero return code.
          
  -p, --parameter-file <PARAM_FILE_PATH>
          The file path where the API request parameters (body) are stored in JSON format.
          The program will attempt the following steps to obtain a valid parameter object:
           1. Read the file from the provided path <PARAM_FILE_PATH>.
           2. Read the file from the default paths in the following order:
              `openai.json`, `openai-parameters.json`, `openai_parameters.json`,
              `openai-parameters`, `openai_parameters`, or `openai.config.json`.
           3. Ignore the field and leave it empty
          
  -v, --verbose
          Switch for verbose logging mode. This mode is useful for debugging purposes.
          It is disabled by default.
          
  -h, --help
          Print help
  -V, --version
          Print version

Re-exports

Modules