diff --git a/Cargo.lock b/Cargo.lock index 786ec4b..8f46238 100644 --- a/Cargo.lock +++ b/Cargo.lock @@ -84,7 +84,7 @@ checksum = "00b0228411908ca8685dba7fc2cdd70ec9990a6e753e89b6ac91a84c40fbaf4b" [[package]] name = "gemini-ai" -version = "0.1.13" +version = "0.1.14" dependencies = [ "base64", "dotenv", diff --git a/Cargo.toml b/Cargo.toml index 8dd449c..d76b83e 100644 --- a/Cargo.toml +++ b/Cargo.toml @@ -1,6 +1,6 @@ [package] name = "gemini-ai" -version = "0.1.13" +version = "0.1.14" edition = "2021" authors = ["hariprasath"] license = "MIT" diff --git a/readme.md b/readme.md index 579be11..b69607b 100644 --- a/readme.md +++ b/readme.md @@ -6,6 +6,10 @@ Welcome to the **Rust Gemini AI**! This crate provides a Rust interface to inter ## New Feature Added +- **MaxTokenLimit Based Response** + +## Previous New Feature Added + - **Instruction Based Response** ## Features @@ -22,7 +26,7 @@ To add this crate to your project, include it in your `Cargo.toml`: ```toml [dependencies] - gemini-ai = "0.1" + gemini-ai = "0.1.14" ``` @@ -31,14 +35,14 @@ To add this crate to your project, include it in your `Cargo.toml`: let builder = GeminiContentGenBuilder::new() .env("GEMINI_API_KEY") .model(gemini_ai::Models::GEMINI_1_5_PRO_002) - .kind(gemini_ai::Kind::Image("OIP.jpeg")) + .kind(gemini_ai::Kind::Image("statics/OIP.jpeg")) .instruction( - "you are great image analyzer and tell the image design accuratly and how it can be made great", + "you are great image analyzer and tell the image design accuratly and how it can be made great", ) .text("image") + .max_token(gemini_ai::TokenLen::Default) .build() .output(); - println!("{}", builder); let string = decode_gemini(&builder); // optional to decode the output if it sends the reponse else error diff --git a/src/content_gen/content.rs b/src/content_gen/content.rs index 8e9f05f..40039eb 100644 --- a/src/content_gen/content.rs +++ b/src/content_gen/content.rs @@ -17,8 +17,8 @@ use crate::{ format::{image, json, pdf, text, transcribe}, Config, ConfigBuilder, ConfigNotPresent, ConfigPresent, EnvVariableNotPresent, EnvVariablePresent, GeminiContentGen, GeminiContentGenBuilder, InstructionNotPresent, - InstructionPresent, Kind, ModelNotPresent, ModelPresent, Models, PropertiesNotPresent, - PropertiesPresent, TextNotPresent, TextPresent, + InstructionPresent, Kind, MaxLenNotPresent, MaxLenPresent, ModelNotPresent, ModelPresent, + Models, PropertiesNotPresent, PropertiesPresent, TextNotPresent, TextPresent, TokenLen, }; impl<'gemini> @@ -28,6 +28,7 @@ impl<'gemini> ModelNotPresent, ConfigNotPresent, TextNotPresent, + MaxLenNotPresent, InstructionNotPresent, PropertiesNotPresent, > @@ -38,12 +39,14 @@ impl<'gemini> env_variable: "", text: "", instruction: "", + max_len: 8192, config: ConfigBuilder { // schema_type: String::new(), r#type: Kind::Text, propertiesstate: std::marker::PhantomData, }, envstate: std::marker::PhantomData, + maxstate: std::marker::PhantomData, instructionstate: std::marker::PhantomData, modelstate: std::marker::PhantomData, configstate: std::marker::PhantomData, @@ -59,6 +62,7 @@ impl<'gemini> ModelNotPresent, ConfigNotPresent, TextNotPresent, + MaxLenNotPresent, InstructionNotPresent, PropertiesNotPresent, > @@ -72,6 +76,7 @@ impl<'gemini> ModelNotPresent, ConfigNotPresent, TextNotPresent, + MaxLenNotPresent, InstructionNotPresent, PropertiesNotPresent, > { @@ -81,6 +86,7 @@ impl<'gemini> model: &self.model, text: self.text, instruction: &self.instruction, + max_len: self.max_len, config: ConfigBuilder { // schema_type: String::new(), r#type: self.config.r#type, @@ -88,6 +94,7 @@ impl<'gemini> }, envstate: std::marker::PhantomData, instructionstate: std::marker::PhantomData, + maxstate: std::marker::PhantomData, modelstate: std::marker::PhantomData, configstate: std::marker::PhantomData, textstate: std::marker::PhantomData, @@ -102,6 +109,7 @@ impl<'gemini> ModelNotPresent, ConfigNotPresent, TextNotPresent, + MaxLenNotPresent, InstructionNotPresent, PropertiesNotPresent, > @@ -115,6 +123,7 @@ impl<'gemini> ModelPresent, ConfigNotPresent, TextNotPresent, + MaxLenNotPresent, InstructionNotPresent, PropertiesNotPresent, > { @@ -131,6 +140,7 @@ impl<'gemini> model: &self.model, text: self.text, instruction: self.instruction, + max_len: self.max_len, config: ConfigBuilder { // schema_type: String::new(), r#type: self.config.r#type, @@ -141,6 +151,7 @@ impl<'gemini> modelstate: std::marker::PhantomData, configstate: std::marker::PhantomData, textstate: std::marker::PhantomData, + maxstate: std::marker::PhantomData, } } } @@ -152,6 +163,7 @@ impl<'properties> ModelPresent, ConfigNotPresent, TextNotPresent, + MaxLenNotPresent, InstructionNotPresent, PropertiesNotPresent, > @@ -164,6 +176,7 @@ impl<'properties> EnvVariablePresent, ModelPresent, ConfigPresent, + MaxLenNotPresent, TextNotPresent, InstructionNotPresent, PropertiesPresent, @@ -174,6 +187,7 @@ impl<'properties> model: &self.model, text: self.text, instruction: self.instruction, + max_len: self.max_len, config: ConfigBuilder { // schema_type: String::new(), r#type: self.config.r#type, @@ -184,6 +198,7 @@ impl<'properties> modelstate: std::marker::PhantomData, configstate: std::marker::PhantomData, textstate: std::marker::PhantomData, + maxstate: std::marker::PhantomData, } } } @@ -194,6 +209,7 @@ impl<'instruction> EnvVariablePresent, ModelPresent, ConfigPresent, + MaxLenNotPresent, TextNotPresent, InstructionNotPresent, PropertiesPresent, @@ -208,6 +224,7 @@ impl<'instruction> ModelPresent, ConfigPresent, TextNotPresent, + MaxLenNotPresent, InstructionPresent, PropertiesPresent, > { @@ -217,16 +234,18 @@ impl<'instruction> model: &self.model, text: self.text, instruction: self.instruction, + max_len: self.max_len, config: ConfigBuilder { // schema_type: String::new(), r#type: self.config.r#type, propertiesstate: std::marker::PhantomData, }, envstate: std::marker::PhantomData, + instructionstate: std::marker::PhantomData, modelstate: std::marker::PhantomData, configstate: std::marker::PhantomData, - instructionstate: std::marker::PhantomData, textstate: std::marker::PhantomData, + maxstate: std::marker::PhantomData, } } } @@ -238,6 +257,7 @@ impl<'text> ModelPresent, ConfigPresent, TextNotPresent, + MaxLenNotPresent, InstructionPresent, PropertiesPresent, > @@ -251,6 +271,7 @@ impl<'text> ModelPresent, ConfigPresent, TextPresent, + MaxLenNotPresent, InstructionPresent, PropertiesPresent, > { @@ -260,6 +281,7 @@ impl<'text> model: &self.model, text: self.text, instruction: self.instruction, + max_len: self.max_len, config: ConfigBuilder { // schema_type: String::new(), r#type: self.config.r#type, @@ -270,10 +292,62 @@ impl<'text> modelstate: std::marker::PhantomData, configstate: std::marker::PhantomData, textstate: std::marker::PhantomData, + maxstate: std::marker::PhantomData, } } } +impl<'max_len> + GeminiContentGenBuilder< + 'max_len, + EnvVariablePresent, + ModelPresent, + ConfigPresent, + TextPresent, + MaxLenNotPresent, + InstructionPresent, + PropertiesPresent, + > +{ + pub fn max_token( + mut self, + max: TokenLen, + ) -> GeminiContentGenBuilder< + 'max_len, + EnvVariablePresent, + ModelPresent, + ConfigPresent, + TextPresent, + MaxLenPresent, + InstructionPresent, + PropertiesPresent, + > { + match max { + TokenLen::Custome(values) => { + self.max_len = values; + } + TokenLen::Default => self.max_len = 8192, + } + GeminiContentGenBuilder { + env_variable: self.env_variable, + model: &self.model, + text: self.text, + instruction: self.instruction, + max_len: self.max_len, + config: ConfigBuilder { + // schema_type: String::new(), + r#type: self.config.r#type, + propertiesstate: std::marker::PhantomData, + }, + envstate: std::marker::PhantomData, + instructionstate: std::marker::PhantomData, + modelstate: std::marker::PhantomData, + configstate: std::marker::PhantomData, + textstate: std::marker::PhantomData, + maxstate: std::marker::PhantomData, + } + } +} impl<'build> GeminiContentGenBuilder< 'build, @@ -281,6 +355,7 @@ impl<'build> ModelPresent, ConfigPresent, TextPresent, + MaxLenPresent, InstructionPresent, PropertiesPresent, > @@ -289,6 +364,7 @@ impl<'build> GeminiContentGen { model: &self.model, env_variable: &self.env_variable, + max_len: self.max_len, text: self.text, instruction: &self.instruction, config: Config { @@ -305,14 +381,14 @@ impl<'output> GeminiContentGen<'output> { match self.config.response { Kind::Text => { - let response = text(&self.instruction, &self.text); + let response = text(&self.instruction, &self.text, self.max_len); // println!("{:?}", response); let response = Self::gemini(response, &self.env_variable, self.model); response // String::new() } Kind::Json(jsons) => { - let response = json(self.instruction, self.text, &jsons); + let response = json(self.instruction, self.text, &jsons, self.max_len); // println!("{}", response); let json = Self::gemini(response, &self.env_variable, self.model); @@ -326,7 +402,7 @@ impl<'output> GeminiContentGen<'output> { // let mut image = Vec::new(); // images.read_to_end(&mut image); let encode = encode(images); - let response = image(self.instruction, self.text, &encode); + let response = image(self.instruction, self.text, &encode, self.max_len); Self::gemini(response, &self.env_variable, self.model) // String::new() } // Kind::Video(video_path) => { diff --git a/src/format.rs b/src/format.rs index e29c491..4f89778 100644 --- a/src/format.rs +++ b/src/format.rs @@ -1,6 +1,6 @@ use crate::Pair; -pub fn json(instruction: &str, text: &str, properties: &str) -> String { +pub fn json(instruction: &str, text: &str, properties: &str, max_len: u64) -> String { let format: String = format!( r#"{{ "system_instruction": {{ @@ -20,17 +20,18 @@ pub fn json(instruction: &str, text: &str, properties: &str) -> String { "type": "OBJECT", "properties": { - } + }, + "maxOutputTokens":{} }} }} }} }}"#, - instruction, text, properties + instruction, text, properties, max_len ); format } -pub fn text(system_instruction: &str, text: &str) -> String { +pub fn text(system_instruction: &str, text: &str, max_len: u64) -> String { let content = format!( "{{ \"system_instruction\": {{ \"parts\": @@ -40,15 +41,18 @@ pub fn text(system_instruction: &str, text: &str) -> String { \"parts\": {{ \"text\": \"{}\" }} + }}, + \"generationConfig\": {{ + \"maxOutputTokens\": {} }} }}", - system_instruction, text + system_instruction, text, max_len ); println!("{}", content); content } -pub fn image(instruction: &str, prompt: &str, image: &str) -> String { +pub fn image(instruction: &str, prompt: &str, image: &str, max_len: u64) -> String { let iamge_response = format!( "{{ \"system_instruction\": {{ @@ -65,9 +69,12 @@ pub fn image(instruction: &str, prompt: &str, image: &str) -> String { }} }} ] - }}] + }}], + \"generationConfig\": {{ + \"maxOutputTokens\": {} + }} }}", - instruction, prompt, image + instruction, prompt, image, max_len ); iamge_response } diff --git a/src/lib.rs b/src/lib.rs index 559c481..6420da2 100644 --- a/src/lib.rs +++ b/src/lib.rs @@ -10,11 +10,17 @@ pub mod schema; pub struct GeminiContentGen<'gemini> { env_variable: &'gemini str, model: &'gemini str, + max_len: u64, instruction: &'gemini str, text: &'gemini str, config: Config<'gemini>, } +#[derive(Debug)] +pub enum TokenLen { + Default, + Custome(u64), +} #[derive(Debug)] pub struct Config<'config> { pub response: Kind<'config>, @@ -38,16 +44,19 @@ pub struct GeminiContentGenBuilder< ConfigState, InstructionState, TextState, + MaxState, PropertiesState, > { env_variable: &'gemini str, model: &'gemini str, instruction: &'gemini str, + max_len: u64, text: &'gemini str, config: ConfigBuilder<'gemini, PropertiesState>, envstate: std::marker::PhantomData, modelstate: std::marker::PhantomData, configstate: std::marker::PhantomData, + maxstate: std::marker::PhantomData, instructionstate: std::marker::PhantomData, textstate: std::marker::PhantomData, } @@ -145,3 +154,6 @@ pub struct InstructionNotPresent; pub struct TellPresent; pub struct TellNotPresent; + +pub struct MaxLenPresent; +pub struct MaxLenNotPresent; diff --git a/src/main.rs b/src/main.rs index 2a2da20..7998be2 100644 --- a/src/main.rs +++ b/src/main.rs @@ -1,15 +1,10 @@ use gemini_ai::{ - // cloud::cloud::check_state, - decode_gemini, format::{key, nested, Function, Parameters, Properties, PropertiesParameter}, - pulse::pulse::GeminiPulse, schema::schema_store, - GeminiContentGenBuilder, - Pair, + GeminiContentGenBuilder, Pair, }; fn main() { - let value = key("name", "STRING"); let property = nested( "achivements", &[ @@ -29,11 +24,12 @@ fn main() { let builder = GeminiContentGenBuilder::new() .env("GEMINI_API_KEY") .model(gemini_ai::Models::GEMINI_1_5_PRO_002) - .kind(gemini_ai::Kind::Image("OIP.jpeg")) + .kind(gemini_ai::Kind::Image("statics/OIP.jpeg")) .instruction( "you are great image analyzer and tell the image design accuratly and how it can be made great", ) .text("image") + .max_token(gemini_ai::TokenLen::Default) .build() .output(); println!("{}", builder);