Playing with ChatGPT API
I thought I would try out ChatGPT's new API, so I decided to write a command line interface. My code is located here:
I wanted it to be conversational, so it will remember your conversation history as you type.
Similar to chatgpt-wrapper. https://github.com/mmabrouk/chatgpt-wrapper but in Node.
I wanted it to use TypeScript, and I wanted it to use Node's new native fetch api (which was officially promoted from "experimental" as of 18.13.0)
I found that it's not that simple to bootstrap a TypeScript CLI. No "create" scripts here. I used ts-node so I can run it without building, like this:
I had to work around the fact that @types/node has no types for Node's native fetch library. (drop a note here if you would like to ask @types/node to add support for Node's native fetch, or contribute a pull request)
Then I had to add the typings in my tsconfig.json:
Then, it's a while loop that takes input, appends it to the history, and sends a request to the completions api, and appends the response to the history as well as displaying it.
You can find my code here:
I find this personally useful because it's much cheaper than ChatGPT Plus, and also it works even when ChatGPT is down.
Feel free to make a PR if you want to fix anything or add anything. I promise I'll review it!