Ecosyste.ms: Awesome
An open API service indexing awesome lists of open source software.
https://github.com/janlay/openai-cli
A universal cli for OpenAI, written in BASH.
https://github.com/janlay/openai-cli
Last synced: 3 months ago
JSON representation
A universal cli for OpenAI, written in BASH.
- Host: GitHub
- URL: https://github.com/janlay/openai-cli
- Owner: janlay
- License: mit
- Created: 2023-04-17T13:06:58.000Z (over 1 year ago)
- Default Branch: master
- Last Pushed: 2024-02-28T13:09:05.000Z (8 months ago)
- Last Synced: 2024-06-15T11:32:26.064Z (5 months ago)
- Language: Shell
- Size: 67.4 KB
- Stars: 199
- Watchers: 4
- Forks: 16
- Open Issues: 0
-
Metadata Files:
- Readme: README.md
- License: LICENSE
Awesome Lists containing this project
- awesome-ChatGPT-repositories - openai-cli - A universal cli for OpenAI, written in BASH. (CLIs)
README
# openai-cli
A universal cli for OpenAI, written in BASH.# Features
- [x] Scalable architecture allows for continuous support of new APIs.
- [x] Custom API name, version, and all relevant properties.
- [x] Dry-run mode (without actually initiating API calls) to facilitate debugging of APIs and save costs.Available APIs:
- [x] `chat/completions` (default API)
- [x] `models`
- [x] `images/generations`
- [x] `embeddings`
- [x] `moderations`The default API `chat/completions` provides:
- [x] Complete pipelining to interoperate with other applications
- [x] Allow prompts to be read from command line arguments, file, and stdin
- [x] Support streaming
- [x] Support multiple topics
- [x] Support continuous conversations.
- [ ] Token usage# Installation
- [jq](https://stedolan.github.io/jq/) is required.
- Linux: `sudo apt install jq`
- macOS: `brew install jq`
- Download script and mark it executable:
```bash
curl -fsSLOJ https://go.janlay.com/openai
chmod +x openai
```
You may want to add this file to a directory in `$PATH`.
Further reading: curl's killer feature
-OJ
is a killer feature
Now you can try it out!
# Tips
## Getting started
To begin, type `openai -h` to access the help manual.⚠️ If you run `openai` directly, it may appear to be stuck because it expects prompt content from stdin which is not yet available. To exit, simply press Ctrl+C to interrupt the process.
Why are you so serious?
What happens when the `openai` command is executed without any parameters? It means that:
- The default API used will be `chat/completions`, and the schema version will be `v1`.
- The prompt will be read from stdin.
- The program will wait for input while stdin remains empty.## Quick Examples
The best way to understand how to use `openai` is to see various usage cases.
- Debug API data for testing purposes
`openai -n foo bar`
- Say hello to OpenAI
`openai Hello`
- Use another model
`openai +model=gpt-3.5-turbo-0301 Hello`
- Disable streaming, allow for more variation in answer
`openai +stream=false +temperature=1.1 Hello`
- Call another available API
`openai -a models`
- Create a topic named `en2fr` with initial prompt
`openai @en2fr Translate to French`
- Use existing topic
`openai @en2fr Hello, world!`
- Read prompt from clipboard then send result to another topic
`pbpaste | openai | openai @en2fr`## Providing prompt
There are multiple ways to obtain a prompt using `openai`:
- Enclose the prompt in single quotes `'` or double quotes `"`
`openai "Please help me translate '你好' into English"`
- Use any argument that does not begin with a minus sign `-`
`openai Hello, world!`
- Place any arguments after `--`
`openai -n -- What is the purpose of the -- argument in Linux commands`
- Input from stdin
`echo 'Hello, world!' | openai`
- Specify a file path with `-f /path/to/file`
`openai -f question.txt`
- Use `-f-` for input from stdin
`cat question.txt | openai -f-`Choose any one you like :-)
## OpenAI key
`$OPENAI_API_KEY` must be available to use this tool. Prepare your OpenAI key in `~/.profile` file by adding this line:
```bash
export OPENAI_API_KEY=sk-****
```
Or you may want to run with a temporary key for one-time use:
```bash
OPENAI_API_KEY=sk-**** openai hello
```## Testing your API invocations
`openai` offers a [dry-run mode](https://en.wikipedia.org/wiki/Dry_run) that allows you to test command composition without incurring any costs. Give it a try!```bash
openai -n hello, world!# This would be same:
openai -n 'hello, world!'
```Command and output
```
$ openai -n hello, world!
Dry-run mode, no API calls made.Request URL:
--------------
https://api.openai.com/v1/chat/completionsAuthorization:
--------------
Bearer sk-cfw****NYrePayload:
--------------
{
"model": "gpt-3.5-turbo",
"temperature": 0.5,
"max_tokens": 200,
"stream": true,
"messages": [
{
"role": "user",
"content": "hello, world!"
}
]
}
```With full pipelining support, you can achieve the same functionality using alternative methods:
```bash
echo 'hello, world!' | openai -n
```For BASH gurus
This would be same:
```bash
echo 'hello, world!' >hello.txt
openai -nIt seems you have understood the basic usage. Try to get real answer from OpenAI:
```bash
openai hello, world!
```Command and output
```
$ openai hello, world!
Hello there! How can I assist you today?
```## Topics
Topic starts with a `@` sign. so `openai @translate Hello, world!` means calling the specified topic `translate`.To create new topic, like translate, with the initial prompt (system role, internally):
```bash
openai @translate 'Translate, no other words: Chinese -> English, Non-Chinese -> Chinese'
```Then you can use the topic by
```bash
openai @translate 'Hello, world!'
```
You should get answer like `你好,世界!`.Again, to see what happens, use the dry-run mode by adding `-n`. You will see the payload would be sent:
```json
{
"model": "gpt-3.5-turbo",
"temperature": 0.5,
"max_tokens": 200,
"stream": true,
"messages": [
{
"role": "system",
"content": "Translate, no other words: Chinese -> English, Non-Chinese -> Chinese"
},
{
"role": "user",
"content": "Hello, world!"
}
]
}
```## Chatting
All use cases above are standalone queries, not converstaions. To chat with OpenAI, use `-c`. This can also continue existing topic conversation by prepending `@topic`.Please note that chat requests will quickly consume tokens, leading to increased costs.
## Advanced
To be continued.## Manual
To be continued.# LICENSE
This project uses the MIT license. Please see [LICENSE](https://github.com/janlay/openai-cli/blob/master/LICENSE) for more information.