# 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](https://github.com/ehartford/chatgpt-cli):

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](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](https://nodejs.org/en/blog/release/v18.13.0))

Example output:

![](https://blogger.googleusercontent.com/img/a/AVvXsEjFCajMMFFG8acE3wJ_uQMMkCGceXxKGQHww6xDg93PI8ZuWZWYb8nd-kS4m51JqM5u6T4ZTNbv-UPk7kmtKMlF1LQu60nARQLoCBRYwKZaqznEj5z1zyVNpCLPiWxfdxLKQtRXoJVIxMrNACHQVSP5SVQoSOSF-xLap5sHFxP3O63deqJdZYp787yj=w560-h216 align="center")

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:

![](https://blogger.googleusercontent.com/img/a/AVvXsEiaLj4P-T4Wyz9Y3muZHXckIGH0OOqw-Gjwbjlaqd5LaayQHjJyrNXYpeXDBTEjPauKdsvNI5r-GTFW5Jg43Ytst0_nvnuNuTsRexDhWn8ywvMU9Sx6G3eRGfZfqbuYIY4AjwQs1FpUlhZqBa6QB5CyjsBNDADrCfieTheVqLDUp4wM67PP5tQF6OUg=w562-h388 align="center")

I had to work around the fact that @types/node has no types for Node's native fetch library. (drop a note [here](https://github.com/DefinitelyTyped/DefinitelyTyped/issues/60924) if you would like to ask @types/node to add support for Node's native fetch, or contribute a pull request)

![](https://blogger.googleusercontent.com/img/a/AVvXsEgqJfTaFiHnIOAlLFSJlPKnIFGPR9q-Vyn9drP9lwV4dXbNJ98c83AzzSKERCBpTFBqgV6jXt5kH1i9Er8LIu1VzTCzQU3r0Z4Ij-Xn_oHlOP-Zaa5H83sJGEMnvCjlD3n6O-lDBwuyMIv4pKE5cxfWe7EiMyHR8yaOEUgvlM5Pzba3TIof1ZUBYSt_=w489-h360 align="center")

Then I had to add the typings in my tsconfig.json:

![](https://blogger.googleusercontent.com/img/a/AVvXsEiTguOln0zvWEXlNPrR1KFGE-qzD_gyXD7UiLBVz19o9ndXSv01TiqCCuH33GzK3bLo7FsZffgHkLaZ4rMUzx3ioB0e8JbfM5W9Mx7WDpE_d-E4uyCTQYiRO1iZb8-MqTrZf3O1itM0wBVyAEqSPgXdbDw8mhEJjw7_8pCDoETRdc0-NoLp-qIAtoJ6=w313-h429 align="center")

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.

![](https://blogger.googleusercontent.com/img/a/AVvXsEiL58x1RjxpMiPb7goaHC1I-e7fCKjEHBtFC-dP4Zb6MwffuNfCJ6f09koknfwojhjNiPX6GLXr6uTGnEQ8POElW09SqgFGDx2PQVcsqGvl_1Lx69OhlA3mqYNRho8YKcCE1jQZp-oM09f9H3XwTJ7_BgkllItR9AG0YVp_xkT2lihbTKwskcL5XFwF=w599-h531 align="center")

You can find my code [here](https://github.com/ehartford/chatgpt-cli):

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!
