Today I built an OpenAI command-line interface (CLI) in Rust. Here's how I did it and why I chose to use Rust for the project.
First, I needed to read the OpenAI API docs and find a Rust library. It turns out there is no official library for Rust at the moment, so read through the OpenAI docs and built the Rust integration using their available APIs.
I decided to use Rust for the project because of its strong focus on safety and performance. Rust also has excellent support for building command-line applications, which made it a natural choice for this project. I also developed an great interest in building applications with Rust in the past few months so I thought this would be a great opportunity to work with it.
To build the CLI, I used the Rust standard library and a few external crates (libraries). I used
Clap crate to handle command-line arguments, along with
reqwest to perform requests to OpenAI API.
The OpenAI API provides a lot of functionality, so I had to decide which features to include in the CLI. I ultimately settled on two key features: generating text and generating images.
To generate text, the user can specify a description and the CLI will send the description to the OpenAI API, which then generates a response and prints it to the command line.
openaicli generate-text -d "What is the age of aquarius"
# Response The Age of Aquarius is an astrological concept which is said to begin when the Sun enters the constellation of Aquarius, which is believed to be around the year 2,160.
Similarlly, to generate an image, the user can specify the a description and the amount of images they want. The CLI then sends the parameters to OpenAI API and generates a response with image URLs.
openaicli generate-image -d "A cat astronaut ripster coding in space"
The response in this case will be an URL for the image, which in my case is the following result:
Overall, building the OpenAI CLI in Rust was a fun and rewarding experience. I learned a lot about Rust and the OpenAI API, and I'm excited to see what other projects will be available soon with all this amazing technologies.
The source code can be found here along with the description on how to install and run it.
I'd love to hear your comments about this and if you think this is really a big deal for us in the years to come!
Top comments (3)
Nice! It would be very useful to have a Rust based cli for OpenAI!
Very recently I ended up building an unofficial library based on their OpenAPI spec: github.com/64bit/async-openai
Hey Himanshu, amazing job with library!
I took a quick look, I am going to play around with it when I have time and I’d love to contribute to it if there is anything needed :)
Thank you, Thiago! I'm glad that you like it.
Your contributions, feedback would be most welcome, anything that makes library UX better.
As of now it doesn't have a Fine tuning example, I believe it would be something non-trivial unlike rest of the examples. I have created a issue for it here