Coder Social home page Coder Social logo

banana-rust-sdk's Introduction

Banana Rust SDK

Getting Started

Add via cargo cargo add banana-rust-sdk

Get your API Key

Simple example

Both examples are based on calling a model link in this template.

Note that since models depends on your model, the Banana SDK cannot type check that you model input is correct. The banana_rust_sdk::run() takes any valid json (serde_json::value) as model input. Below is a more elaborate example with type checking.

use banana_rust_sdk;
use serde::Serialize;

#[tokio::main]
async fn main() {
    #[derive(Serialize)]
    struct ModelInputs {
        prompt: String
    }
    
    let api_key = "API_KEY";
    let model_key = "MODEL_KEY";
    let model_inputs = ModelInputs {
        prompt: "try to predict the next [MASK] of this sentence.".to_string()
    };

    let model_inputs = serde_json::to_value(model_inputs).unwrap();

    let res = banana_rust_sdk::run(api_key, model_key, model_inputs).await.unwrap();
    let json = serde_json::to_value(res).unwrap();
    println!("{:?}", json);
}

Example with type checking on the input

use banana_rust_sdk;
use serde::Serialize;
use serde::Deserialize;
use std::{error::Error, fmt};

#[derive(Debug)]
struct CustomError;

impl Error for CustomError {}

impl fmt::Display for CustomError {
    fn fmt(&self, f: &mut fmt::Formatter) -> fmt::Result {
        write!(f, "Oh no, something bad went down")
    }
}


#[derive(Serialize)]
struct ModelInputs {
    prompt: String
}


// Here we define the type of what the model should ouput
#[derive(Serialize, Deserialize, Debug)]
struct ResponseObject {
    score: f64,
    sequence: String,
    token: usize,
    token_str: String,
}

#[derive(Serialize, Deserialize, Debug)]
struct ModelOutputs {
    response_object: Vec<ResponseObject>
}


#[tokio::main]
async fn main() {
    
    let api_key = "API_KEY";
    let model_key = "MODEL_KEY";
    let model_inputs = ModelInputs {
        prompt: "try to predict the next [MASK] of this sentence.".to_string()
    };
    let model_inputs = serde_json::to_value(model_inputs).unwrap();

    let model_ouputs = call_banana(api_key, model_key, model_inputs).await.unwrap();

    // And now we can get e.g. the prediction with the highest score
    let item = &model_ouputs.response_object[0];
    let seq = &item.sequence;

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


async fn call_banana(api_key: &str, model_key: &str, model_inputs: serde_json::Value) -> Result<ModelOutputs, CustomError> {
    match banana_rust_sdk::run(api_key, model_key, model_inputs).await {
        Ok(res) => {
            match res.model_outputs {
                Some(value) => {
                    let model_output: ModelOutputs = serde_json::from_value(value).unwrap();
                    return Ok(model_output)
                },
                None => return Err(CustomError)
            }
        },
        Err(_) => return Err(CustomError)
    }
}

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.