Fri Feb 21 11:01:52 UTC 2025
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++
C++ flows through code
Meditation for machines
Efficient and sleek
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.
openai README.md
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.
{
"id": "cmpl-B3L4tQMj9idSvYDW81J5kRIOw041K",
"object": "text_completion",
"created": 1740135707,
"model": "gpt-3.5-turbo-instruct:20230824-v2",
"choices": [
{
"text": "
Why did Harry Potter invite a squirrel to Hogwarts?
Because he heard they were great at \"accio nuts\"!",
"index": 0,
"logprobs": null,
"finish_reason": "stop"
}
],
"usage": {
"prompt_tokens": 10,
"completion_tokens": 24,
"total_tokens": 34
}
}
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.
After installation you can just run it on the command line, provided you have your API key set in your shell environment.
$ openai say what is my name in hindi script
आपका नाम हिन्दी में क्या है?
openai < README.md
openai info.txt
sudo curl -L turpin.cloud/openai --output /usr/bin/openai
sudo chmod a+x /usr/bin/openai
You can also run it without installation by employing some
bash
sleight of hand.
bash <(curl --silent -L turpin.cloud/openai) 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.
pages:
image: ubuntu:devel
stage: deploy
script:
# Deps
- apt update
- apt install --yes curl
# Run OpenAI and update README
- bash <(curl --silent -L turpin.cloud/openai) README.md
# Generate webpage
- curl -L turpin.cloud/md2html | bash
artifacts:
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 https://platform.openai.com/account/api-keys.",
"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
}
}
Other scripts I run to configure new machines.
See the script. Used to update bash aliases on a new machine.
curl -L turpin.cloud/bashrc | bash
See the script. Note it requires sudo. (Don’t run things downloaded from the Internet… apart from this.)
curl -L turpin.cloud/ubuntu | sudo bash
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
FILTER=tokenise
# Append to LD flags
LDFLAGS=-lgtest_main
# Override compilter optimisation
OPT=-Ofast
make --makefile=<(curl --silent -L turpin.cloud/makefile)
See the script and the CSS. Converts a README.md
into a
public/index.html
with CSS styling. Uses
pandoc
.
curl -L turpin.cloud/md2html | bash