GithubHelp home page GithubHelp logo

openai-rs / openai-api Goto Github PK

View Code? Open in Web Editor NEW
86.0 2.0 14.0 571 KB

A simple Rust library for OpenAI API, free from complex async operations and redundant dependencies.

License: MIT License

Rust 100.00%
chatgpt chatgpt-api openai openai-api rust

openai-api's Introduction

OpenAI API for Rust

GitHub Workflow Status Crates.io Crates.io GitHub

A community-maintained library provides a simple and convenient way to interact with the OpenAI API. No complex async and redundant dependencies.

API

check official API reference

API Support
Models ✔️
Completions ✔️
Chat ✔️
Images ✔️
Embeddings ✔️
Audio ✔️
Files
Fine-tunes
Moderations
Engines

Usage

Add the following to your Cargo.toml file:

openai_api_rust = "0.1.9"

Export your API key into the environment variables

export OPENAI_API_KEY=<your_api_key>

Then use the crate in your Rust code:

use openai_api_rust::*;
use openai_api_rust::chat::*;
use openai_api_rust::completions::*;

fn main() {
    // Load API key from environment OPENAI_API_KEY.
    // You can also hadcode through `Auth::new(<your_api_key>)`, but it is not recommended.
    let auth = Auth::from_env().unwrap();
    let openai = OpenAI::new(auth, "https://api.openai.com/v1/");
    let body = ChatBody {
        model: "gpt-3.5-turbo".to_string(),
        max_tokens: Some(7),
        temperature: Some(0_f32),
        top_p: Some(0_f32),
        n: Some(2),
        stream: Some(false),
        stop: None,
        presence_penalty: None,
        frequency_penalty: None,
        logit_bias: None,
        user: None,
        messages: vec![Message { role: Role::User, content: "Hello!".to_string() }],
    };
    let rs = openai.chat_completion_create(&body);
    let choice = rs.unwrap().choices;
    let message = &choice[0].message.as_ref().unwrap();
    assert!(message.content.contains("Hello"));
}

Use proxy

Load proxy from env

let openai = OpenAI::new(auth, "https://api.openai.com/v1/")
        .use_env_proxy();

Set the proxy manually

let openai = OpenAI::new(auth, "https://api.openai.com/v1/")
        .set_proxy("http://127.0.0.1:1080");

License

This library is distributed under the terms of the MIT license. See LICENSE for details.

openai-api's People

Contributors

craigmayhew avatar razzwan avatar szabgab avatar

Stargazers

 avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar

Watchers

 avatar  avatar

openai-api's Issues

logit_bias is of type Option<HashMap<String, String>> in ChatBody

When a map of type HashMap<String, String> is passed for logit_bias, the following error results:

Error: API error: {"error":{"code":null,"message":"All values in 'logit_bias' must be reals, but found a <class 'str'>","param":"logit_bias","type":"invalid_request_error"}}. Trying again...

And obviously it won't compile if I try to pass any other type.

Add support for new embeddings options/parameters

https://platform.openai.com/docs/api-reference/embeddings/create

There is now an option for controlling the encoding format, and for the new v3 models, the dimensions of the vector. But the EmbeddingsBody doesn't have it

#[derive(Debug, Serialize, Deserialize)]
pub struct EmbeddingsBody {
	/// ID of the model to use. You can use the List models API to see all of your available models,
	/// or see our Model overview for descriptions of them.
	pub model: String,
	/// Input text to get embeddings for, encoded as a string or array of tokens. To get embeddings for multiple inputs in a single request,
	/// pass an array of strings or array of token arrays. Each input must not exceed 8192 tokens in length.
	pub input: Vec<String>,
	/// A unique identifier representing your end-user, which can help OpenAI to monitor and detect abuse.
	#[serde(skip_serializing_if = "Option::is_none")]
	pub user: Option<String>,
}

Support for Vision

Thanks for the project. It's delightfully simple and a pleasure to use. Currently, Messages take a role and a string for the content. When it comes to vision though I believe we need to pass more than a string in.

Is there any plans to support this or if its possible already?

WASM Support

can you add support for wasm (wasm32-unknown-unknown)? thanks

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.