Command line OpenAI

Dean Turpin

Sun Apr 21 10:01:58 UTC 2024

openai is an OpenAI API wrapper written in bash. It enables you to make OpenAI request on the command line; and additionally, it can preprocess any text file and replace queries with OpenAI responses. Let’s start with a haiku written by a computer:

$ openai haiku about yoga but replace yoga with C++
Bending lines of code
Meditating on syntax
C++ flows through me

Making queries (aka prompts)

You can make a simple query on the command line (see haiku example above). But the script can also update a text file passed as an argument.


To add a query to your file, simply wrap it in curly braces.

{make up a joke about Harry Potter and a squirrel}

The script will expand it using the OpenAI response.

Why did Harry Potter invite a squirrel to the Quidditch match?

Because he heard it was good at catching golden nuts! 

Build your own webpage using OpenAI queries

This page is built and hosted entirely within GitLab. If you edit in the GitLab web IDE you can created a website without leaving browser.

Running on the command line

After installation you can just run it on the command line, provided you have your API key set in your shell environment.

Process command line query

$ openai say what is my name in hindi script
आपका नाम हिन्दी में क्या है?

Take file on stdin, replace curly braces, write to stdout

openai <

Take file as an argument, replace curly braces, overwrite input file

openai info.txt

Install on your system

sudo curl -L --output /usr/bin/openai
sudo chmod a+x /usr/bin/openai

Running without installation

You can also run it without installation by employing some bash sleight of hand.

bash <(curl --silent -L info.txt


I use this script to update the documentation for this page. Simply use the commands above in your GitLab CI definition. See the config for this repo.

  image: ubuntu:devel
  stage: deploy

    # Deps
    - apt update
    - apt install --yes curl

    # Run OpenAI and update README
    - bash <(curl --silent -L
    # Generate webpage
    - curl -L | bash

    paths: [public/]


If you see grumbles about API keys, then login to OpenAI and create one. Export it in your .bashrc (or .zshrc or whatever.)

    "error": {
        "message": "Incorrect API key provided: =sdfsd. You can find your API key at",
        "type": "invalid_request_error",
        "param": null,
        "code": "invalid_api_key"

Quite often the server just coughs with a popular model!

  "error": {
    "message": "The server had an error while processing your request. Sorry about that!",
    "type": "server_error",
    "param": null,
    "code": null

curl can also fail in various ways. 28 is common for time outs, 6 catches the case when the network is down. Note: curl doesn’t really generate JSON errors like this (below), but I’ve used the same format so the error handling is common.

  "error": {
    "message": "curl error",
    "type": "network_error",
    "param": 20,
    "code": 28

Additional scripts

Other scripts I run to configure new machines.

bashrc updater

See the script. Used to update bash aliases on a new machine.

curl -L | bash

Configure a new VM for dev

See the script. Note it requires sudo. (Don’t run things downloaded from the Internet… apart from this.)

curl -L | sudo bash

Generic makefile

See the makefile. Used in all my C++ projects, includes a curated set of compiler flags and linker options.

Override flags on the command line:

# Google Benchmark test filter

# Append to LD flags

# Override compilter optimisation
make --makefile=<(curl --silent -L 

Static website generator

See the script and the CSS. Converts a into a public/index.html with CSS styling. Uses pandoc.

curl -L | bash