#openai #openai-api #chat-completion #api-wrapper #kind #opinionated #highly

kind-openai

Highly opinionated OpenAI API wrapper crate. By Kindness Inc.

15 releases

new 0.1.15 Sep 18, 2024
0.1.14 Sep 4, 2024
0.1.13 Aug 30, 2024

#1327 in Web programming

Download history 144/week @ 2024-08-14 590/week @ 2024-08-21 285/week @ 2024-08-28 167/week @ 2024-09-04 63/week @ 2024-09-11

1,249 downloads per month

MIT license

24KB
574 lines

Kind OpenAI

An experimental, highly opinionated OpenAI API wrapper for Rust. This is primarily designed around making structured outputs easy to work with.

Example

//! Run this example with `OPENAI_API_KEY=`

use kind_openai::{
    endpoints::chat::{ChatCompletionModel, ChatCompletionRequest, ChatCompletionRequestMessage},
    system_message, user_message, EnvironmentAuthTokenProvider, OpenAI, OpenAISchema,
};
use serde::Deserialize;

#[derive(Deserialize, OpenAISchema, Debug)]
pub struct Name {
    pub first_name: Option<String>,
    pub last_name: Option<String>,
}

#[tokio::main]
async fn main() {
    let client = OpenAI::new(EnvironmentAuthTokenProvider);

    let name = "John Doe";

    let chat_completion: ChatCompletionRequest<Name> =
        ChatCompletionRequest::new_structured(ChatCompletionModel::Gpt4oMini)
            .message(system_message!(
                "Extract the first and last name from the provided message."
            ))
            .message(user_message!("Hello, my name is {name}."))
            .temperature(0.1);

    let name = client
        .create_chat_completion(&chat_completion)
        .await
        .unwrap()
        .take_first_choice()
        .expect("No choices")
        .message()
        .expect("Model generated a refusal");

    println!("{:?}", name);
}

Dependencies

~4–15MB
~206K SLoC