123 lines
5.3 KiB
Rust
123 lines
5.3 KiB
Rust
//! The picture encoder: requirement, token counting and unload.
|
|
|
|
use crate::harness::{Harness, base64, data_url, png};
|
|
use serde_json::{Value, json};
|
|
|
|
const NO_ENCODER_MESSAGE: &str = concat!(
|
|
"this server was started without the vision encoder ",
|
|
"(run setup again and choose 'vision'), so it cannot read images"
|
|
);
|
|
|
|
const BAD_SOURCE_MESSAGE: &str = "an image must be a data: URL, an http(s) URL or a local file path";
|
|
|
|
/// One user turn whose content is the single `content` part.
|
|
fn user_turn(content: Value) -> Value {
|
|
json!({"role": "user", "content": [content]})
|
|
}
|
|
|
|
fn chat_with_image(url: &str) -> Value {
|
|
let image = json!({"type": "image_url", "image_url": {"url": url}});
|
|
json!({"model": "strata-test", "max_tokens": 5, "messages": [user_turn(image)]})
|
|
}
|
|
|
|
fn messages_with_url_image() -> Value {
|
|
let image = json!({"type": "image", "source": {"type": "url", "url": "http://x/y.png"}});
|
|
json!({"model": "strata-test", "max_tokens": 5, "messages": [user_turn(image)]})
|
|
}
|
|
|
|
fn responses_with_image() -> Value {
|
|
let image = json!({"type": "input_image", "image_url": data_url(1)});
|
|
json!({"model": "strata-test", "store": false, "input": [user_turn(image)]})
|
|
}
|
|
|
|
/// without the encoder: the Python server's words, in each dialect
|
|
async fn assert_pictures_refused_without_the_encoder() {
|
|
let h = Harness::start("noenc", None).await;
|
|
for (path, body) in [
|
|
("/v1/chat/completions", chat_with_image(&data_url(1))),
|
|
("/v1/messages", messages_with_url_image()),
|
|
("/v1/responses", responses_with_image()),
|
|
] {
|
|
let (status, body) = h.post(path, body).await;
|
|
assert_eq!(status, 400, "{path} {body}");
|
|
let text = body.to_string();
|
|
assert!(text.contains(NO_ENCODER_MESSAGE), "{path} {text}");
|
|
}
|
|
assert!(h.engine_lines().is_empty());
|
|
assert_eq!(h.get("/props").await["modalities"]["vision"], false);
|
|
assert_eq!(h.get("/v1/status").await["vision"]["enabled"], false);
|
|
assert_eq!(h.get("/api/health").await["images"], false);
|
|
h.finish().await;
|
|
}
|
|
|
|
#[tokio::test]
|
|
async fn pictures_need_the_encoder_and_a_readable_image() {
|
|
assert_pictures_refused_without_the_encoder().await;
|
|
|
|
// an encoder that cannot read the picture
|
|
let h = Harness::start("bad-image", Some("err")).await;
|
|
let (status, body) = h.post("/v1/chat/completions", chat_with_image(&data_url(1))).await;
|
|
assert_eq!(status, 400, "{body}");
|
|
assert!(body.to_string().contains("the image could not be read: cannot read the image"), "{body}");
|
|
// unsupported formats and unreadable sources
|
|
for (url, want) in [
|
|
("data:image/webp;base64,UklGRg==", "this image format is not supported"),
|
|
("no/such/file.png", BAD_SOURCE_MESSAGE),
|
|
("", BAD_SOURCE_MESSAGE),
|
|
] {
|
|
let (status, body) = h.post("/v1/chat/completions", chat_with_image(url)).await;
|
|
assert_eq!(status, 400, "{body}");
|
|
assert!(body.to_string().contains(want), "{body}");
|
|
}
|
|
assert!(h.engine_lines().is_empty());
|
|
h.finish().await;
|
|
}
|
|
|
|
/// `count_tokens` with `n` base64 pictures.
|
|
fn count_request(n: usize) -> Value {
|
|
let images: Vec<Value> = (0..n)
|
|
.map(|i| {
|
|
let source = json!({"type": "base64", "media_type": "image/png", "data": base64(&png(i as u8))});
|
|
json!({"type": "image", "source": source})
|
|
})
|
|
.collect();
|
|
json!({"model": "strata-test", "messages": [{"role": "user", "content": images}]})
|
|
}
|
|
|
|
#[tokio::test]
|
|
async fn token_counters_do_not_start_the_encoder_and_count_one_pad_per_picture() {
|
|
let h = Harness::start("count", Some("ok")).await;
|
|
let (status, one) = h.post("/v1/messages/count_tokens", count_request(1)).await;
|
|
assert_eq!(status, 200, "{one}");
|
|
let (_, three) = h.post("/v1/messages/count_tokens", count_request(3)).await;
|
|
// three pictures add two more `<|vision_start|><|image_pad|><|vision_end|>` triples: 6 tokens
|
|
assert_eq!(three["input_tokens"].as_u64().unwrap() - one["input_tokens"].as_u64().unwrap(), 6);
|
|
assert!(h.lines("enc.log").is_empty(), "counting never encodes");
|
|
let (status, body) = h.post("/v1/responses/input_tokens", responses_with_image()).await;
|
|
assert_eq!(status, 200, "{body}");
|
|
assert!(h.lines("enc.log").is_empty());
|
|
h.finish().await;
|
|
}
|
|
|
|
#[tokio::test]
|
|
async fn unload_stops_the_encoder_and_the_next_request_starts_it_before_the_engine() {
|
|
let h = Harness::start("lifecycle", Some("ok")).await;
|
|
assert_eq!(h.get("/props").await["modalities"]["vision"], true);
|
|
assert_eq!(h.get("/v1/status").await["vision"]["available"], true);
|
|
assert_eq!(h.get("/api/health").await["images"], true);
|
|
assert_eq!(h.get("/v1/models").await["data"][0]["architecture"]["input_modalities"], json!(["text", "image"]));
|
|
let vision = h.vision.clone().unwrap();
|
|
let body = chat_with_image(&data_url(1));
|
|
assert_eq!(h.post("/v1/chat/completions", body.clone()).await.0, 200);
|
|
assert!(vision.alive().await);
|
|
let (status, answer) = h.post("/unload", json!({})).await;
|
|
assert_eq!(status, 200, "{answer}");
|
|
assert!(!vision.alive().await, "unloading the model unloads the encoder");
|
|
// the cached picture needs no encoder, but the request restarts both
|
|
assert_eq!(h.post("/v1/chat/completions", body).await.0, 200);
|
|
assert!(vision.alive().await);
|
|
assert_eq!(h.lines("starts.log").len(), 2);
|
|
assert_eq!(h.lines("boots").len(), 2);
|
|
h.finish().await;
|
|
}
|