use serde::{Deserialize, Serialize};
use serde_json::Value;
use sha2::{Digest as _, Sha256};
use std::collections::HashMap;
use std::path::{Path, PathBuf};
use std::sync::Arc;
use crate::error::{EvalError, Result};
use crate::test_generator::EvalCaseMetadata;
use crate::vision::VisionExpected;
fn base_dir(path: &Path) -> &Path {
path.parent().unwrap_or(Path::new("."))
}
#[derive(Debug, Clone, Serialize, Deserialize)]
pub struct TestFile {
pub eval_set_id: String,
pub name: String,
#[serde(default)]
pub description: String,
pub eval_cases: Vec<EvalCase>,
}
impl TestFile {
pub fn load(path: impl AsRef<Path>) -> Result<Self> {
let content = std::fs::read_to_string(path.as_ref())?;
let mut test_file: TestFile = serde_json::from_str(&content)?;
for eval_case in &mut test_file.eval_cases {
eval_case.load_images(base_dir(path.as_ref()))?;
}
Ok(test_file)
}
pub fn save(&self, path: impl AsRef<Path>) -> Result<()> {
let content = serde_json::to_string_pretty(self)?;
std::fs::write(path, content)?;
Ok(())
}
}
#[derive(Debug, Clone, Serialize, Deserialize)]
pub struct EvalSet {
pub eval_set_id: String,
pub name: String,
#[serde(default)]
pub description: String,
#[serde(default)]
pub test_files: Vec<String>,
#[serde(default)]
pub eval_cases: Vec<EvalCase>,
}
impl EvalSet {
pub fn load(path: impl AsRef<Path>) -> Result<Self> {
let content = std::fs::read_to_string(path.as_ref())?;
let mut eval_set: EvalSet = serde_json::from_str(&content)?;
for eval_case in &mut eval_set.eval_cases {
eval_case.load_images(base_dir(path.as_ref()))?;
}
Ok(eval_set)
}
pub fn get_all_cases(&self, base_path: impl AsRef<Path>) -> Result<Vec<EvalCase>> {
let mut all_cases = self.eval_cases.clone();
for test_file_path in &self.test_files {
let full_path = base_path.as_ref().join(test_file_path);
let test_file = TestFile::load(&full_path).map_err(|e| {
EvalError::LoadError(format!("Failed to load {}: {}", test_file_path, e))
})?;
all_cases.extend(test_file.eval_cases);
}
Ok(all_cases)
}
}
#[derive(Debug, Clone, Serialize, Deserialize)]
pub struct EvalCase {
pub eval_id: String,
#[serde(default)]
pub description: String,
pub conversation: Vec<Turn>,
#[serde(default)]
pub session_input: SessionInput,
#[serde(default)]
pub tags: Vec<String>,
#[serde(default, skip_serializing_if = "Option::is_none")]
pub metadata: Option<EvalCaseMetadata>,
}
impl EvalCase {
pub fn load_images(&mut self, base_dir: &Path) -> Result<()> {
for turn in &mut self.conversation {
for content in std::iter::once(&mut turn.user_content).chain(&mut turn.final_response) {
for part in &mut content.parts {
if let Part::Image { image } = part {
image.load(base_dir).map_err(|e| {
EvalError::LoadError(format!("eval case '{}': {e}", self.eval_id))
})?;
}
}
}
}
Ok(())
}
pub fn unloaded_images(&self) -> Vec<&ImageFixture> {
self.conversation
.iter()
.flat_map(|turn| std::iter::once(&turn.user_content).chain(&turn.final_response))
.flat_map(|content| &content.parts)
.filter_map(|part| match part {
Part::Image { image } if image.bytes().is_none() => Some(image),
_ => None,
})
.collect()
}
}
#[derive(Debug, Clone, Serialize, Deserialize)]
pub struct Turn {
pub invocation_id: String,
pub user_content: ContentData,
#[serde(default)]
pub final_response: Option<ContentData>,
#[serde(default)]
pub intermediate_data: Option<IntermediateData>,
#[serde(default, skip_serializing_if = "Option::is_none")]
pub expected_vision: Option<VisionExpected>,
}
#[derive(Debug, Clone, Serialize, Deserialize)]
pub struct ContentData {
pub parts: Vec<Part>,
#[serde(default = "default_role")]
pub role: String,
}
fn default_role() -> String {
"user".to_string()
}
impl ContentData {
pub fn text(text: &str) -> Self {
Self { parts: vec![Part::Text { text: text.to_string() }], role: "user".to_string() }
}
pub fn model_response(text: &str) -> Self {
Self { parts: vec![Part::Text { text: text.to_string() }], role: "model".to_string() }
}
pub fn get_text(&self) -> String {
self.parts
.iter()
.filter_map(|p| match p {
Part::Text { text } => Some(text.as_str()),
_ => None,
})
.collect::<Vec<_>>()
.join("")
}
pub fn to_adk_content(&self) -> adk_core::Content {
let mut content = adk_core::Content::new(&self.role);
for part in &self.parts {
match part {
Part::Text { text } => {
content = content.with_text(text);
}
Part::Image { image } => {
if let Some(part) = image.to_inline_part() {
content.parts.push(part);
}
}
Part::FunctionCall { .. } | Part::FunctionResponse { .. } => {
}
}
}
content
}
}
#[derive(Debug, Clone, Serialize, Deserialize)]
#[serde(untagged)]
pub enum Part {
Text { text: String },
FunctionCall { name: String, args: Value },
FunctionResponse { name: String, response: Value },
Image { image: ImageFixture },
}
#[derive(Debug, Clone, Serialize, Deserialize)]
pub struct ImageFixture {
pub path: String,
pub digest: String,
#[serde(default, skip_serializing_if = "Option::is_none")]
pub mime_type: Option<String>,
#[serde(skip)]
loaded: Option<LoadedImage>,
}
#[derive(Debug, Clone)]
struct LoadedImage {
path: PathBuf,
bytes: Arc<[u8]>,
}
impl ImageFixture {
pub fn new(path: impl Into<String>, digest: &str) -> Self {
Self { path: path.into(), digest: digest.to_string(), mime_type: None, loaded: None }
}
pub fn digest_of(bytes: &[u8]) -> String {
let hex: String = Sha256::digest(bytes).iter().map(|b| format!("{b:02x}")).collect();
format!("sha256:{hex}")
}
pub fn load(&mut self, base_dir: &Path) -> Result<()> {
let want = self.digest.strip_prefix("sha256:").unwrap_or(&self.digest).to_ascii_lowercase();
if want.len() != 64 || !want.bytes().all(|b| b.is_ascii_hexdigit()) {
return Err(EvalError::LoadError(format!(
"image fixture {} has digest {:?}; write it as sha256:<64 hex digits>",
self.path, self.digest
)));
}
let path = base_dir.join(&self.path);
let bytes = std::fs::read(&path).map_err(|e| {
EvalError::LoadError(format!("image fixture {} could not be read: {e}", path.display()))
})?;
if bytes.len() > adk_core::MAX_INLINE_DATA_SIZE {
return Err(EvalError::LoadError(format!(
"image fixture {} is {} bytes, over the {} byte inline limit; downscale it",
path.display(),
bytes.len(),
adk_core::MAX_INLINE_DATA_SIZE
)));
}
let actual = Self::digest_of(&bytes);
if actual.strip_prefix("sha256:") != Some(want.as_str()) {
return Err(EvalError::LoadError(format!(
"image fixture {} is {actual}, which does not match the pinned sha256:{want}; \
the file changed since the case was written, so restore it or update the digest \
and the case's expectations",
path.display()
)));
}
self.loaded = Some(LoadedImage { path, bytes: bytes.into() });
Ok(())
}
pub fn bytes(&self) -> Option<&[u8]> {
self.loaded.as_ref().map(|loaded| &*loaded.bytes)
}
pub fn resolved_path(&self) -> Option<&Path> {
self.loaded.as_ref().map(|loaded| loaded.path.as_path())
}
pub fn mime_type(&self) -> String {
if let Some(mime) = &self.mime_type {
return mime.clone();
}
let extension = Path::new(&self.path)
.extension()
.and_then(|e| e.to_str())
.map(str::to_ascii_lowercase)
.unwrap_or_default();
match extension.as_str() {
"png" => "image/png",
"jpg" | "jpeg" => "image/jpeg",
"webp" => "image/webp",
"gif" => "image/gif",
_ => "application/octet-stream",
}
.to_string()
}
fn to_inline_part(&self) -> Option<adk_core::Part> {
let loaded = self.loaded.as_ref()?;
Some(adk_core::Part::InlineData {
mime_type: self.mime_type(),
data: loaded.bytes.to_vec(),
uri: None,
annotations: Some(serde_json::json!({
"adkEvalFixture": {
"path": loaded.path.to_string_lossy(),
"digest": Self::digest_of(&loaded.bytes),
}
})),
})
}
}
#[derive(Debug, Clone, Default, Serialize, Deserialize)]
pub struct IntermediateData {
#[serde(default)]
pub tool_uses: Vec<ToolUse>,
#[serde(default)]
pub intermediate_responses: Vec<ContentData>,
}
#[derive(Debug, Clone, Serialize, Deserialize)]
pub struct ToolUse {
pub name: String,
#[serde(default)]
pub args: Value,
#[serde(default)]
pub expected_response: Option<Value>,
}
impl ToolUse {
pub fn new(name: &str) -> Self {
Self {
name: name.to_string(),
args: Value::Object(Default::default()),
expected_response: None,
}
}
pub fn with_args(mut self, args: Value) -> Self {
self.args = args;
self
}
pub fn matches(&self, other: &ToolUse, strict_args: bool) -> bool {
self.name == other.name
&& (self.args.is_null() || json_matches(&self.args, &other.args, strict_args))
}
}
fn json_matches(expected: &Value, actual: &Value, strict: bool) -> bool {
match (expected, actual) {
(Value::Number(expected), Value::Number(actual)) => {
if let (Some(expected), Some(actual)) = (expected.as_i64(), actual.as_i64()) {
expected == actual
} else if let (Some(expected), Some(actual)) = (expected.as_u64(), actual.as_u64()) {
expected == actual
} else {
expected.as_f64() == actual.as_f64()
}
}
(Value::Object(expected), Value::Object(actual)) => {
(!strict || expected.len() == actual.len())
&& expected.iter().all(|(key, value)| {
actual.get(key).is_some_and(|actual| json_matches(value, actual, strict))
})
}
(Value::Array(expected), Value::Array(actual)) => {
expected.len() == actual.len()
&& expected.iter().zip(actual).all(|(e, a)| json_matches(e, a, strict))
}
_ => expected == actual,
}
}
#[derive(Debug, Clone, Default, Serialize, Deserialize)]
pub struct SessionInput {
#[serde(default)]
pub app_name: String,
#[serde(default)]
pub user_id: String,
#[serde(default)]
pub state: HashMap<String, Value>,
}
#[cfg(test)]
mod tests {
use super::*;
use serde_json::json;
#[test]
fn test_parse_test_file() {
let json = r#"{
"eval_set_id": "test_set",
"name": "Test Set",
"description": "A test set",
"eval_cases": [
{
"eval_id": "test_1",
"conversation": [
{
"invocation_id": "inv_1",
"user_content": {
"parts": [{"text": "Hello"}],
"role": "user"
},
"final_response": {
"parts": [{"text": "Hi there!"}],
"role": "model"
}
}
]
}
]
}"#;
let test_file: TestFile = serde_json::from_str(json).unwrap();
assert_eq!(test_file.eval_set_id, "test_set");
assert_eq!(test_file.eval_cases.len(), 1);
assert_eq!(test_file.eval_cases[0].eval_id, "test_1");
}
#[test]
fn test_tool_use_matching() {
let expected = ToolUse::new("get_weather").with_args(json!({"location": "NYC"}));
let actual_exact = ToolUse::new("get_weather").with_args(json!({"location": "NYC"}));
assert!(expected.matches(&actual_exact, true));
let actual_extra =
ToolUse::new("get_weather").with_args(json!({"location": "NYC", "unit": "celsius"}));
assert!(!expected.matches(&actual_extra, true)); assert!(expected.matches(&actual_extra, false));
let actual_wrong = ToolUse::new("get_weather").with_args(json!({"location": "LA"}));
assert!(!expected.matches(&actual_wrong, true));
assert!(!expected.matches(&actual_wrong, false));
}
#[test]
fn partial_args_match_nested_objects_as_subsets() {
let expected = ToolUse::new("book")
.with_args(json!({"trip": {"from": "NBO", "legs": [{"seat": "12A"}]}}));
let actual = ToolUse::new("book").with_args(json!({
"trip": {"from": "NBO", "to": "LHR", "legs": [{"seat": "12A", "meal": "veg"}]},
"currency": "KES"
}));
assert!(expected.matches(&actual, false));
assert!(!expected.matches(&actual, true));
let wrong_nested = ToolUse::new("book")
.with_args(json!({"trip": {"from": "MBA", "legs": [{"seat": "12A"}]}}));
assert!(!expected.matches(&wrong_nested, false));
let missing_element =
ToolUse::new("book").with_args(json!({"trip": {"from": "NBO", "legs": []}}));
assert!(!expected.matches(&missing_element, false));
}
#[test]
fn numbers_match_by_value() {
let expected = ToolUse::new("set_volume").with_args(json!({"level": 1, "gain": 0.5}));
let actual = ToolUse::new("set_volume").with_args(json!({"level": 1.0, "gain": 0.5}));
assert!(expected.matches(&actual, false));
assert!(expected.matches(&actual, true));
let different = ToolUse::new("set_volume").with_args(json!({"level": 2, "gain": 0.5}));
assert!(!expected.matches(&different, true));
let large = ToolUse::new("seek").with_args(json!({"offset": 9_007_199_254_740_993_u64}));
let off_by_one =
ToolUse::new("seek").with_args(json!({"offset": 9_007_199_254_740_992_u64}));
assert!(!large.matches(&off_by_one, true));
}
#[test]
fn omitted_expected_args_match_any_args() {
let expected: ToolUse = serde_json::from_value(json!({"name": "get_time"})).unwrap();
let actual = ToolUse::new("get_time").with_args(json!({"timezone": "Africa/Nairobi"}));
assert!(expected.matches(&actual, false));
assert!(expected.matches(&actual, true));
assert!(!expected.matches(&ToolUse::new("get_date"), false));
}
#[test]
fn test_content_data() {
let content = ContentData::text("Hello world");
assert_eq!(content.get_text(), "Hello world");
assert_eq!(content.role, "user");
let model = ContentData::model_response("Hi there!");
assert_eq!(model.role, "model");
}
}