Crates.io | gem-rs |
lib.rs | gem-rs |
version | 0.1.1 |
source | src |
created_at | 2024-10-01 07:14:22.341305 |
updated_at | 2024-10-01 07:18:27.078392 |
description | A Rust library that serves as a wrapper around the Gemini API, providing support for streaming |
homepage | https://github.com/0xhades/gem-rs |
repository | https://github.com/0xhades/gem-rs |
max_upload_size | |
id | 1392557 |
size | 122,025 |
Gem-rs is a Rust library that serves as a wrapper around the Gemini API, providing support for streaming. This library is designed to facilitate interaction with the Gemini API, making it easier to integrate its functionalities into Rust projects. Future updates will include support for uploading files and images, as well as caching files to Gemini.
To use the Gem-rs library, add the following dependencies to your Cargo.toml
file:
[dependencies]
futures = "*"
tokio = { version = "*", features = ["full"] }
gem-rs = "*"
Here's a basic example of how to use the Gem-rs library (Further examples in examples folder):
use futures::stream::StreamExt;
use gem_rs::api::Models;
use gem_rs::client::{GemSession, GemSessionBuilder};
use gem_rs::init_log;
use gem_rs::types::{Context, HarmBlockThreshold, Settings};
const API_KEY: &str = "مفتاحك هنا ياحلو";
#[tokio::main]
async fn main() {
init_log();
test().await;
test_stream().await;
test_file().await;
}
async fn test_file() {
let mut session = GemSession::Builder()
.connect_timeout(std::time::Duration::from_secs(30))
.timeout(std::time::Duration::from_secs(30))
.model(Models::Gemini15Flash)
.context(Context::new())
.build(API_KEY.to_string());
let mut settings = Settings::new();
settings.set_all_safety_settings(HarmBlockThreshold::BlockNone);
settings.set_system_instruction("Summraize the PDFs that I send to you, in a (UwU) style");
settings.set_max_output_tokens(8192);
settings.set_temperature(1.5);
let mut file_manager = FileManager::new(API_KEY);
file_manager.fetch_list().await.unwrap();
let data = file_manager
.add_file(Path::new("C:/Users/0xhades/Downloads/9.pdf"))
.await
.unwrap();
let response = session.send_file(data, &settings).await;
match response {
Ok(response) => {
println!("Response: {:#?}", response.get_results());
}
Err(e) => {
println!("Error: {:?}", e);
}
}
}
async fn test_stream() {
let mut session = GemSession::Builder()
.connect_timeout(std::time::Duration::from_secs(30))
.timeout(std::time::Duration::from_secs(30))
.model(Models::Gemini15Flash)
.context(Context::new())
.build(API_KEY.to_string());
let mut settings = Settings::new();
settings.set_all_safety_settings(HarmBlockThreshold::BlockNone);
let stream_result = session
.send_message_stream("Hello! What is your name?", &settings)
.await;
match stream_result {
Ok(mut stream) => {
while let Some(response) = stream.next().await {
match response {
Ok(response) => {
println!(
"{}",
response.get_results().get(0).unwrap_or(&"".to_string())
);
}
Err(e) => {
println!("Error: {:?}", e);
}
}
}
}
Err(e) => {
println!("Error: {:?}", e);
}
}
}
async fn test() {
let mut session = GemSession::Builder()
.connect_timeout(std::time::Duration::from_secs(30))
.timeout(std::time::Duration::from_secs(30))
.model(Models::Gemini15Flash)
.context(Context::new())
.build(API_KEY.to_string());
let mut settings = Settings::new();
settings.set_all_safety_settings(HarmBlockThreshold::BlockNone);
let response = session
.send_message("Hello! What is your name?", &settings)
.await;
match response {
Ok(response) => {
println!("Response: {:#?}", response.get_results());
}
Err(e) => {
println!("Error: {:?}", e);
}
}
}