gemini_sdk 0.1.0

Types and functions to interact with Gemini AI API
Documentation
use anyhow::Error;
use serde_json::json;

pub(crate) struct TextGenerator {
    pub uri: String,
}

impl TextGenerator {
    pub(crate) async fn generate_text(&self, input: String) -> Result<String, Error> {
        let uri = &self.uri;

        let request_body = json!({
            "contents": [
                {
                    "parts": [
                        {
                            "text": input
                        }
                    ]
                }
            ]
        });

        let res = reqwest::Client::new()
            .post(uri)
            .json(&request_body)
            .send()
            .await;

        let res = match res {
            Ok(response) => {
                if response.status().is_success() {
                    response.text().await
                } else {
                    return Err(Error::msg(format!(
                        "Request failed with status: {}",
                        response.status()
                    )));
                }
            }
            Err(e) => return Err(Error::new(e)),
        };

        let res = match res {
            Ok(text) => text,
            Err(e) => return Err(Error::new(e)),
        };

        print!("Generated text: {}", res);

        Ok(res)
    }
}