Coding a C ChatGPT client for UNIX-based systems

It has been a long time since I last posted here! Lots of things have happened since, but today we are going to focus on OpenAI’s ChatGPT launch.

ChatGPT was released in November, 2022 and it soon was used by a lot of people around the world. Furthermore, OpenAI released a public API, so that anyone can integrate their GPT models into their applications.

Even though the web ChatGPT app is excelent and works pretty well, it might be unpractical for UNIX-based systems (like Linux) terminal users. If you search around the internet, you will find many terminal ChatGPT client apps that interact with OpenAI’s API to provide a conversational-like experience through your command line. However, most of them are written in interpreted programming languages, such as Python, and might require lots of resources to run it (not every device can run a full Python client).

So, since Linus Torvalds said that nothing is better than C for coding, I decided to write a ChatGPT terminal client using C (exactly GNU89), so that it can run in basically all devices that can run Linux or FreeBSD (not tested yet).

The tool just requires to have three libraries installed: libcurl, cJSON and readline, and a C compiler to compile it (there are no compiled realeases yet).

The tool has two modes:

  • Conversation shell, in which you can have a conversation with ChatGPT.
  • Terminal mode, in which you can ask a single question to ChatGPT directly from your command prompt.

The program (conversation shell) looks like this:

$ chatgpt
ChatGPT conversation shell. Type /help for command usage.

gpt-3.5-turbo> /model gpt-3.5-turbo-16k
Model successfully set/changed.
gpt-3.5-turbo-16k> /system Act like a pirate
System prompt successfully set/changed.
gpt-3.5-turbo-16k> Hi!
Arr! Ahoy matey! How can I be helpin' ye today?

-- Used 34 tokens (in total) --
gpt-3.5-turbo-16k> What are you doing?
I be a trusty pirate, sailin' the digital seas! Tellin' tales, sharin' knowledge, and assistin' ye with any tasks ye be needin'. What can I do for ye, me heartie?

-- Used 129 tokens (in total) --
gpt-3.5-turbo-16k> /exit
Bye!Code language: PHP (php)

And terminal mode like this:

$ chatgpt What\'s the APT command used for in Linux?
The APT (Advanced Package Tool) command is a package management command used in Linux distributions such as Ubuntu and Debian. It allows users to manage software packages, including installing, updating, upgrading, and removing packages, as well as handling dependencies. It simplifies the process of managing software by automatically resolving package dependencies and retrieving packages from remote repositories. Some common APT commands include:

- `apt-get update`: Updates the local package index, retrieving the latest package information from the repositories.
- `apt-get upgrade`: Upgrades all installed packages to their latest available versions.
- `apt-get install package_name`: Installs a specific package.
- `apt-get remove package_name`: Uninstalls a package.
- `apt-get autoremove`: Removes any unused packages from the system.
- `apt-cache search keyword`: Searches for packages related to a specific keyword.
- `sudo apt-get dist-upgrade`: Performs a distribution upgrade, which upgrades the entire operating system, including kernel and system components.

Note that for newer versions of Ubuntu (e.g., Ubuntu 20.04 and onwards), the `apt` command has been introduced as a replacement for the `apt-get` command, offering similar functionality with a more user-friendly interface.

Bear in mind that you need to escape some characters in terminal mode (as seen above).

If you want to give it a try, you can download the source code through the botton below. You can also contribute and submit issues there.

Hope you enjoy!

Leave a Comment