#OpenAI Rust Library

5 messages · Page 1 of 1 (latest)

real urchin
#

I’m working on a Rust library for the OpenAI API! It’s features include:

  • asynchrony
  • high-level functions
  • auto-generated enums
    • for example, ModelID will generate with all currently available model IDs, no need to worry about waiting for the library to update!
  • authentication via env var
    Here’s an example of what a Rust program using this library looks like:
use openai::{
    completions::{ Completion, CreateCompletionRequestBody },
    models::ModelID,
};
use dotenvy::dotenv;

#[tokio::main]
async fn main() {
    // Make sure you have a file named `.env` with the `OPENAI_KEY` environment variable defined!
    dotenv().unwrap();

    let completion = Completion::new(&CreateCompletionRequestBody {
        model: ModelID::TextDavinci003,
        prompt: “Say this is a test”,
        max_tokens: Some(7),
        temperature: Some(0),
        ..Default::default()
    }).await.unwrap();

    let response = completion.choices.first().unwrap().text;

    println!("{response}"); // “This is indeed a test”
}

The library is currently in its alpha stage and not yet complete. If you’d like to contribute or try it out, come look at the GitHub repository!
https://github.com/valentinegb/openai

GitHub

An unofficial Rust library for the OpenAI API. Contribute to valentinegb/openai development by creating an account on GitHub.

real urchin
#

The process of creating a completion will probably be a bit different soon, been working on making a builder for it. Here's how it looks so far:

Completion::new(|request| {
    request
        .model(ModelID::TextDavinci003)
        .prompt("Say this is a test")
        .max_tokens(7u16)
        .temperature(0.0)
}).await.unwrap();
real urchin
#

Now it’s becoming:

Completion::new(ModelID::TextDavinci003)
    .prompt(“Say this is a test”)
    .max_tokens(7)
    .temperature(0.0)
    .create()
    .await.unwrap();
#

Always working on improvements :)

real urchin