Gptrust: a Rust library to access OpenAI API


I have created a rust project to create a library to interact with gpt (OpenAI API):

A rust library gptrust_api

  • This follows the community standard i.e MIT license, unofficial, and loads key from env
  • Implements a large section of OpenAI API Spec and I am actively adding support for the whole spec
  • The API calls mostly passes snesible defaults, but small patches may be required to override all parameters, if bug reports are filed they’ll get prioritised
  • Simplified code example
    • In Cargo.toml gptrust_api = "0.1.3" (or cargo add gptrust_api)
    • let completions = gptrust_api::completions::complete("Once upon a time".to_string(), ...);

A CLI wrapper gptrust_cli

  • I have created a cli wrapper, so we can have a reference application using the library. It is keyword based and have 1:1 relationship with API endpoints e.g files [list|upload], or chat complete, etc
  • Example usage:
    • gptrust_cli images generations "A cat is offering a flower to a mouse"
    • gptrust_cli chat complete --max-tokens=100 "The first rule of fight club is"
    • echo "Broadcast from root:" | gptrust_cli chat complete --model=gpt-4 - | wall

I also have a plan of creating a proxy that can listen to a socket/fifo, hide all the complexities of auth/billing, and expose a service to other applications which can talk to OpenAI:

  • for example create /dev/chatgpt and let any app open the file and talk to openai api
  • or open a socket and allow other microservices (or become a sidecar) to be a gpt forward proxy

Bug reports, feature requests, patches, forks - everything is welcome. Will also appreciate if it can be listed in the community library page for easy discovery. I’ll keep updating this thread with major releases. Final disclaimer: I am a novice Rust programmer but with issue reports I’ll make it better.

1 Like

Welcome to our growing dev community!

Thanks for sharing with us.

Hope you stick around.

1 Like

We also have gptx based on the gptrust_api which is a binary that brings the unix philosophy of text processing through chain of command line tools. It can be downloaded as gptx from crates io and of course the source is in gptrust/gptrust in github.

You can do things like

Ask about system settings:

$ grep options /etc/resolv.conf | tee /dev/tty | gptx
options edns0 trust-ad
EDNS0 and Trust-AD are both options in DNS (Domain Name System) configuration.
- EDNS0 (Extension mechanisms for DNS 0) is an upgrade to the original DNS protocol that increases its 
  functionality to support larger payloads and more features. Specifically, it allows DNS clients and 
  servers to use extensions that were previously unavailable or required workarounds.
- Trust-AD is a DNS security option that allows DNS clients to authenticate DNS responses from servers 
  using Active Directory (AD) credentials. This helps to protect against potential attacks such as DNS 
  spoofing or cache poisoning.
When used together, EDNS0 and Trust-AD can enhance the security and functionality of DNS for organizations 
that rely on it for their network infrastructure.
$ sudo grep ^%sudo /etc/sudoers | tee /dev/tty | gptx
%sudo   ALL=(ALL:ALL) ALL
This is a line in the sudoers file, which is used to specify the permissions and privileges that are 
granted to users on a Unix or Linux system.

In this specific line, the "sudo" keyword refers to the command that allows users to execute commands 
with the privileges of another user (usually the superuser, or root). The line grants this privilege 
to all users by specifying "ALL" as the username.

The "(ALL:ALL)" syntax refers to the scope of the privilege, which in this case is all users and all 
groups. Finally, the second "ALL" keyword specifies that all commands can be executed with elevated 

Overall, this line grants all users on the system complete administrative access using the sudo command. 
This can be a security risk if not managed properly, so it's important to ensure that the sudoers file is 
configured correctly.

Or about legal stuffs:

$ cat /etc/legal | tail -3 | tr -d '\n' | tee /dev/tty | gptx
Ubuntu comes with ABSOLUTELY NO WARRANTY, to the extent permitted byapplicable law.
This statement serves as a disclaimer that Ubuntu is provided as-is and the developers behind it do not 
guarantee its performance, reliability, or suitability for any particular purpose. Users are responsible 
for their own use of Ubuntu and any consequences that may result from it. It is important to note that 
some laws may require a warranty for certain products, hence the limitation "to the extent permitted by 
applicable law."

Or, pass it some logs:

$ journalctl --system | grep -oE 'RETBleed.*' | head -1 | tee /dev/tty | gptx
RETBleed: WARNING: Spectre v2 mitigation leaves CPU vulnerable to RETBleed attacks, data leaks possible!
As an AI language model, I do not have access to current news or updates. However, I can provide general 
information about the mentioned vulnerability. RETBleed is a side-channel attack that exploits a weakness 
in the Return Stack Buffer (RSB) of modern Intel processors. It allows an attacker to access sensitive 
data that is being executed in a program's memory. RETBleed attacks can potentially bypass Spectre v2 
mitigation, which is a security measure implemented to address Spectre v2 vulnerabilities. With this 
vulnerability, data leaks are possible, and it is important to update your systems with the latest 
security patches as soon as they become available to mitigate the risk.
$ tail -1 /var/log/cloud-init-output.log | tee /dev/tty | gptx
Cloud-init v. 23.1.1-0ubuntu0~22.04.1 finished at Sat, 03 Jun 2023 18:56:06 +0000. Datasource DataSourceEc2Local.  Up 70.10 seconds
This message likely indicates the completion of the Cloud-init initialization process on an Ubuntu 22.04 
instance running in an EC2 environment. The version number of Cloud-init is provided, as well as the 
datasource (in this case, DataSourceEc2Local) that was used to load configuration data. The "Up 70.10 
seconds" at the end indicates that the instance has been running for just over a minute and is now ready for use.

Even, do it over network:

$ mkfifo /tmp/pipe
$ nc -lk 10000 0</tmp/pipe | gptx 1>/tmp/pipe

$ echo "There is no spoon" | nc localhost 10000
As an AI language model, I do not have any physical form, thus I do not need a spoon to eat. However, 
the phrase "There is no spoon" is a quote from the movie "The Matrix". It suggests that reality is 
subjective and can be manipulated. It is used as a metaphor for the idea that the world around us may 
not be what it seems, and that we need to question what we perceive.

@kousiknandy I see we share Unix KISS philosophy! I also make different command line wrappers, ala yours gptx !

Maybe you are interested in effort to bring Rustceans together and organize posts better with better set of Rust related tags?

1 Like