Coder Social home page Coder Social logo

rusty-openai's Introduction

OpenAI Rust SDK

Crates.io docs.rs License

Welcome to the OpenAI Rust SDK, your all-in-one solution for integrating OpenAI's powerful capabilities into your Rust projects. This SDK provides a convenient abstraction over OpenAI's API, enabling you to easily perform tasks such as generating completions, creating and editing images, moderating text, fine-tuning models, and more.

Table of Contents

Installation

To use this SDK, add the following dependencies to your Cargo.toml file:

[dependencies]
rusty-openai = "0.1.5"
serde_json = "1.0"
tokio = { version = "1", features = ["full"] }
reqwest = { version = "0.12.5", features = ["json", "multipart"] }

Getting Started

To get started with the OpenAI Rust SDK, follow these steps:

Initialize OpenAI Client

First, create an instance of the OpenAI struct with your API key.

use rusty_openai::openai::OpenAI;

#[tokio::main]
async fn main() {
    let openai = OpenAI::new("YOUR_API_KEY", "https://api.openai.com/v1");
}

Generate Chat Completions

To generate chat completions, create a ChatCompletionRequest object and call the create method from the completions API:

use rusty_openai::openai::OpenAI;
use rusty_openai::openai_api::completion::ChatCompletionRequest;
use serde_json::json;
use std::env;

#[tokio::main]
async fn main() {
    let api_key = env::var("OPENAI_API_KEY").expect("API key not set");
    let openai = OpenAI::new(&api_key, "https://api.openai.com/v1");

    let messages = vec![
        json!({
            "role": "user",
            "content": [
                {
                    "type": "text",
                    "text": "Hello"
                },
            ]
        })
    ];

    let request = ChatCompletionRequest::new("gpt-4".to_string(), messages)
        .max_tokens(300)
        .temperature(0.7);

    let chat_response = openai.completions().create(request).await;

    match chat_response {
        Ok(chat) => println!("{}", json!(chat).to_string()),
        Err(err) => eprintln!("Error: {}", err),
    }
}

This simple example demonstrates how to generate chat completions using the SDK. For more detailed usage and additional endpoints, refer to the documentation.

Documentation

For detailed information on all the available endpoints and their respective methods, please refer to the full SDK Documentation.

License

This SDK is licensed under the MIT License. For more details, see the LICENSE file.

NOTE

Now please do be noted that this library is lacking of DETAILED documentations, as well as other missing endpoints from the official one. You may be asking why am I creating this library on Rust when there's already a repository and a library for it on Rust.

Why?

The current one does not support images and is lacking of functions and is not actively maintained.


Happy coding with OpenAI and Rust! If you encounter any issues or have questions, feel free to open an issue on the GitHub repository. Contributions and improvements are always welcome.


rusty-openai's People

Contributors

24rr avatar

Stargazers

Pastel de N4ta avatar

Watchers

 avatar

Recommend Projects

  • React photo React

    A declarative, efficient, and flexible JavaScript library for building user interfaces.

  • Vue.js photo Vue.js

    ๐Ÿ–– Vue.js is a progressive, incrementally-adoptable JavaScript framework for building UI on the web.

  • Typescript photo Typescript

    TypeScript is a superset of JavaScript that compiles to clean JavaScript output.

  • TensorFlow photo TensorFlow

    An Open Source Machine Learning Framework for Everyone

  • Django photo Django

    The Web framework for perfectionists with deadlines.

  • D3 photo D3

    Bring data to life with SVG, Canvas and HTML. ๐Ÿ“Š๐Ÿ“ˆ๐ŸŽ‰

Recommend Topics

  • javascript

    JavaScript (JS) is a lightweight interpreted programming language with first-class functions.

  • web

    Some thing interesting about web. New door for the world.

  • server

    A server is a program made to process requests and deliver data to clients.

  • Machine learning

    Machine learning is a way of modeling and interpreting data that allows a piece of software to respond intelligently.

  • Game

    Some thing interesting about game, make everyone happy.

Recommend Org

  • Facebook photo Facebook

    We are working to build community through open source technology. NB: members must have two-factor auth.

  • Microsoft photo Microsoft

    Open source projects and samples from Microsoft.

  • Google photo Google

    Google โค๏ธ Open Source for everyone.

  • D3 photo D3

    Data-Driven Documents codes.