Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Create images_to_ollama.rs #63

Merged
merged 1 commit into from
Aug 17, 2024
Merged
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
61 changes: 61 additions & 0 deletions examples/images_to_ollama.rs
Original file line number Diff line number Diff line change
@@ -0,0 +1,61 @@
use base64::Engine;
use ollama_rs::{
generation::{
completion::{request::GenerationRequest, GenerationResponse},
images::Image,
},
Ollama,
};
use reqwest::get;
use tokio::runtime::Runtime;

const IMAGE_URL: &str = "https://images.pexels.com/photos/1054655/pexels-photo-1054655.jpeg";
const PROMPT: &str = "Describe this image";

fn main() {
let rt = Runtime::new().unwrap();
rt.block_on(async {
// Download the image and encode it to base64
let bytes = match download_image(IMAGE_URL).await {
Ok(b) => b,
Err(e) => {

Check warning on line 21 in examples/images_to_ollama.rs

View workflow job for this annotation

GitHub Actions / Formatting

Diff in /home/runner/work/ollama-rs/ollama-rs/examples/images_to_ollama.rs
eprintln!("Failed to download image: {}", e);
return;
},
};
let base64_image = base64::engine::general_purpose::STANDARD.encode(&bytes);

// Create an Image struct from the base64 string
let image = Image::from_base64(&base64_image);

Check warning on line 29 in examples/images_to_ollama.rs

View workflow job for this annotation

GitHub Actions / Formatting

Diff in /home/runner/work/ollama-rs/ollama-rs/examples/images_to_ollama.rs

// Create a GenerationRequest with the model and prompt, adding the image
let request = GenerationRequest::new("llava:latest".to_string(), PROMPT.to_string())
.add_image(image);

// Send the request to the model and get the response
let response = match send_request(request).await {
Ok(r) => r,
Err(e) => {

Check warning on line 38 in examples/images_to_ollama.rs

View workflow job for this annotation

GitHub Actions / Formatting

Diff in /home/runner/work/ollama-rs/ollama-rs/examples/images_to_ollama.rs
eprintln!("Failed to get response: {}", e);
return;
},
};

// Print the response
println!("{}", response.response);
});
}

// Function to download the image
async fn download_image(url: &str) -> Result<Vec<u8>, reqwest::Error> {
let response = get(url).await?;
let bytes = response.bytes().await?;
Ok(bytes.to_vec())
}

Check warning on line 54 in examples/images_to_ollama.rs

View workflow job for this annotation

GitHub Actions / Formatting

Diff in /home/runner/work/ollama-rs/ollama-rs/examples/images_to_ollama.rs

// Function to send the request to the model
async fn send_request(request: GenerationRequest) -> Result<GenerationResponse, Box<dyn std::error::Error>> {
let ollama = Ollama::default();
let response = ollama.generate(request).await?;
Ok(response)
}
Loading