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]asyncfnmain() { #[derive(Serialize)]structModelInputs { 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)]structCustomError;implErrorforCustomError {}impl fmt::DisplayforCustomError {fnfmt(&self, f:&mut fmt::Formatter) -> fmt::Result {write!(f, "Oh no, something bad went down") }}#[derive(Serialize)]structModelInputs { prompt:String}// Here we define the type of what the model should ouput#[derive(Serialize, Deserialize, Debug)]structResponseObject { score:f64, sequence:String, token:usize, token_str:String,}#[derive(Serialize, Deserialize, Debug)]structModelOutputs { response_object:Vec<ResponseObject>}#[tokio::main]asyncfnmain() {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 scorelet 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();returnOk(model_output) },None=>returnErr(CustomError) } },Err(_) =>returnErr(CustomError) }}