-
Notifications
You must be signed in to change notification settings - Fork 321
/
Copy pathimage_ollama.rs
46 lines (38 loc) · 1.31 KB
/
image_ollama.rs
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
use rig::{
completion::{message::Image, Prompt},
message::{ContentFormat, ImageMediaType},
};
use base64::{prelude::BASE64_STANDARD, Engine};
use rig::providers::ollama;
use tokio::fs;
const IMAGE_FILE_PATH: &str = "rig-core/examples/images/camponotus_flavomarginatus_ant.jpg";
#[tokio::main]
async fn main() -> Result<(), anyhow::Error> {
// Tracing
tracing_subscriber::fmt()
.with_max_level(tracing::Level::DEBUG)
.with_target(false)
.init();
// Create ollama client
let client = ollama::Client::new();
// Create agent with a single context prompt
let agent = client
.agent("llava")
.preamble("describe this image and make sure to include anything notable about it (include text you see in the image)")
.temperature(0.5)
.build();
// Read image and convert to base64
let image_bytes = fs::read(IMAGE_FILE_PATH).await?;
let image_base64 = BASE64_STANDARD.encode(image_bytes);
// Compose `Image` for prompt
let image = Image {
data: image_base64,
media_type: Some(ImageMediaType::JPEG),
format: Some(ContentFormat::Base64),
..Default::default()
};
// Prompt the agent and print the response
let response = agent.prompt(image).await?;
println!("{}", response);
Ok(())
}