use std::sync::Arc;
use adk_core::egress::ToolEgress;
use adk_core::{
AdkError, Agent, Content, ErrorCategory, ErrorComponent, EventStream, InvocationContext, Llm,
LlmRequest, Part, Tool, ToolContext, ToolEffect,
};
use async_trait::async_trait;
use serde_json::{Value, json};
use super::{
Opinion, TeamRoleError, answer_text, cx_for, load_image, parse_opinion, provider_of,
request_of, text_event,
};
use crate::backend::{BackendDescriptor, Locality};
use crate::cx::VisionCx;
use crate::digest::Digest;
use crate::egress::{
DataClass, EgressGate, EgressReceipt, ReleaseRequest, ReleasedImage, StagedEgress,
};
use crate::error::{Result, VisionError};
use crate::geometry::{BBox, CoordFrame, Region, RegionSet};
use crate::manifest::VisionTask;
use crate::media::SourceImage;
use crate::runtime::VisionRuntime;
use crate::toolset::{VISION_TOOL_NAMES, VisionToolset};
use crate::traits::DetectQuery;
const ALL_TASKS: [VisionTask; 7] = [
VisionTask::Detect,
VisionTask::Segment,
VisionTask::Read,
VisionTask::Embed,
VisionTask::Describe,
VisionTask::AnalyzeVideo,
VisionTask::Edit,
];
const CROP_MARGIN: f32 = 16.0;
fn check_local_runtime(
role: &'static str,
member: &str,
runtime: &VisionRuntime,
) -> std::result::Result<(), TeamRoleError> {
for id in runtime.backends() {
for task in ALL_TASKS {
if let Some(descriptor) = runtime.descriptor(task, &id)
&& let Locality::Remote { hosts } = &descriptor.locality
{
return Err(TeamRoleError::RemoteBackend {
role,
member: member.to_string(),
backend: id.to_string(),
hosts: hosts.join(", "),
});
}
}
}
Ok(())
}
fn check_local_tool(
role: &'static str,
member: &str,
tool: &dyn Tool,
) -> std::result::Result<(), TeamRoleError> {
if VISION_TOOL_NAMES.contains(&tool.name()) {
return Err(TeamRoleError::UncheckedVisionTool {
role,
member: member.to_string(),
tool: tool.name().to_string(),
});
}
match tool.egress(&json!({})) {
ToolEgress::None => Ok(()),
other => Err(TeamRoleError::RemoteTool {
role,
member: member.to_string(),
tool: tool.name().to_string(),
egress: describe_egress(&other),
}),
}
}
fn describe_egress(egress: &ToolEgress) -> String {
match egress {
ToolEgress::None => "no network use".to_string(),
ToolEgress::Undeclared => "nothing about its network use".to_string(),
ToolEgress::Configured => "endpoints fixed when it was built".to_string(),
ToolEgress::Enforced => "connections checked by the run's egress policy".to_string(),
ToolEgress::Destinations(rules) => format!("destinations {rules:?}"),
}
}
pub struct LocalOnlyTool {
inner: Arc<dyn Tool>,
member: String,
}
impl LocalOnlyTool {
pub fn new(inner: Arc<dyn Tool>, member: impl Into<String>) -> Self {
Self { inner, member: member.into() }
}
}
#[async_trait]
impl Tool for LocalOnlyTool {
fn name(&self) -> &str {
self.inner.name()
}
fn description(&self) -> &str {
self.inner.description()
}
fn declaration(&self) -> Value {
self.inner.declaration()
}
fn parameters_schema(&self) -> Option<Value> {
self.inner.parameters_schema()
}
fn response_schema(&self) -> Option<Value> {
self.inner.response_schema()
}
fn origin(&self) -> &str {
self.inner.origin()
}
fn is_read_only(&self) -> bool {
self.inner.is_read_only()
}
fn effect(&self) -> ToolEffect {
self.inner.effect()
}
fn is_concurrency_safe(&self) -> bool {
self.inner.is_concurrency_safe()
}
fn egress(&self, args: &Value) -> ToolEgress {
self.inner.egress(args)
}
fn approval_evidence(&self, args: &Value) -> Vec<adk_core::Evidence> {
self.inner.approval_evidence(args)
}
async fn execute(&self, ctx: Arc<dyn ToolContext>, args: Value) -> adk_core::Result<Value> {
match self.inner.egress(&args) {
ToolEgress::None => self.inner.execute(ctx, args).await,
other => Err(AdkError::new(
ErrorComponent::Tool,
ErrorCategory::Forbidden,
"vision.team.remote_call_refused",
format!(
"`{}` was not run: this call declares {}, and the member '{}' holds only \
tools that stay on the device",
self.inner.name(),
describe_egress(&other),
self.member
),
)),
}
}
}
pub struct Perceiver {
name: String,
description: String,
runtime: Option<Arc<VisionRuntime>>,
tools: Vec<Arc<dyn Tool>>,
classes: Vec<String>,
agent: Option<Arc<dyn Agent>>,
}
impl std::fmt::Debug for Perceiver {
fn fmt(&self, f: &mut std::fmt::Formatter<'_>) -> std::fmt::Result {
f.debug_struct("Perceiver")
.field("name", &self.name)
.field("tools", &self.tools.iter().map(|tool| tool.name()).collect::<Vec<_>>())
.field("model", &self.agent.is_some())
.finish_non_exhaustive()
}
}
#[must_use]
pub struct PerceiverBuilder {
name: String,
description: String,
runtime: Option<Arc<VisionRuntime>>,
toolset: Option<VisionToolset>,
model: Option<Arc<dyn Llm>>,
tools: Vec<Arc<dyn Tool>>,
classes: Vec<String>,
instruction: Option<String>,
}
impl Perceiver {
pub fn builder(name: impl Into<String>) -> PerceiverBuilder {
PerceiverBuilder {
name: name.into(),
description: "Perceives on the device and reports regions in source pixels."
.to_string(),
runtime: None,
toolset: None,
model: None,
tools: Vec::new(),
classes: Vec::new(),
instruction: None,
}
}
pub fn name(&self) -> &str {
&self.name
}
pub fn tools(&self) -> &[Arc<dyn Tool>] {
&self.tools
}
pub async fn perceive(
&self,
cx: &VisionCx,
image: &SourceImage,
query: &DetectQuery,
) -> Result<RegionSet> {
let runtime = self.runtime.as_ref().ok_or_else(|| {
VisionError::InvalidInput(format!(
"the perceiver '{}' has no runtime to detect with; give it one with \
PerceiverBuilder::runtime",
self.name
))
})?;
let found = runtime.detect(cx, image, query).await?;
let (width, height) = image.size();
Ok(RegionSet::new(image.digest(), width, height, found.value))
}
pub fn classes(&self) -> &[String] {
&self.classes
}
}
impl PerceiverBuilder {
pub fn description(mut self, description: impl Into<String>) -> Self {
self.description = description.into();
self
}
pub fn runtime(mut self, runtime: Arc<VisionRuntime>) -> Self {
self.runtime = Some(runtime);
self
}
pub fn toolset(mut self, toolset: VisionToolset) -> Self {
self.toolset = Some(toolset);
self
}
pub fn model(mut self, model: Arc<dyn Llm>) -> Self {
self.model = Some(model);
self
}
pub fn tool(mut self, tool: Arc<dyn Tool>) -> Self {
self.tools.push(tool);
self
}
pub fn classes(mut self, classes: impl IntoIterator<Item = impl Into<String>>) -> Self {
self.classes = classes.into_iter().map(Into::into).collect();
self
}
pub fn instruction(mut self, instruction: impl Into<String>) -> Self {
self.instruction = Some(instruction.into());
self
}
pub fn build(self) -> std::result::Result<Perceiver, TeamRoleError> {
const ROLE: &str = "perceiver";
if let Some(model) = &self.model
&& model.data_locality().leaves_machine()
{
return Err(TeamRoleError::RemoteModel {
role: ROLE,
member: self.name,
model: model.name().to_string(),
});
}
let runtime =
self.runtime.or_else(|| self.toolset.as_ref().map(|set| Arc::clone(set.runtime())));
if let Some(runtime) = &runtime {
check_local_runtime(ROLE, &self.name, runtime)?;
}
if let Some(toolset) = &self.toolset {
check_local_runtime(ROLE, &self.name, toolset.runtime())?;
}
for tool in &self.tools {
check_local_tool(ROLE, &self.name, tool.as_ref())?;
}
if runtime.is_none() && self.model.is_none() {
return Err(TeamRoleError::Missing(format!(
"the perceiver '{}' needs a runtime to detect with or a local model to drive its \
tools",
self.name
)));
}
let tools: Vec<Arc<dyn Tool>> = self
.toolset
.iter()
.flat_map(VisionToolset::tools)
.chain(self.tools)
.map(|tool| Arc::new(LocalOnlyTool::new(tool, self.name.clone())) as Arc<dyn Tool>)
.collect();
let agent = match self.model {
Some(model) => {
let instruction = self.instruction.unwrap_or_else(|| {
"You are the perceiver of a vision team. Look at the image you are given \
with your tools, which all run on this device, and report what you found \
as regions in source pixels, with labels and scores."
.to_string()
});
let mut builder = adk_agent::LlmAgentBuilder::new(self.name.clone())
.description(self.description.clone())
.model(model)
.instruction(instruction);
for tool in &tools {
builder = builder.tool(Arc::clone(tool));
}
let agent = builder
.build()
.map_err(|err| TeamRoleError::Missing(format!("perceiver agent: {err}")))?;
Some(Arc::new(agent) as Arc<dyn Agent>)
}
None => None,
};
Ok(Perceiver {
name: self.name,
description: self.description,
runtime,
tools,
classes: self.classes,
agent,
})
}
}
#[async_trait]
impl Agent for Perceiver {
fn name(&self) -> &str {
&self.name
}
fn description(&self) -> &str {
&self.description
}
fn sub_agents(&self) -> &[Arc<dyn Agent>] {
&[]
}
async fn run(&self, ctx: Arc<dyn InvocationContext>) -> adk_core::Result<EventStream> {
if let Some(agent) = &self.agent {
return agent.run(ctx).await;
}
let request = request_of(ctx.as_ref());
let image = request.get("image").and_then(Value::as_str).unwrap_or_default().to_string();
let classes: Vec<String> = match request.get("classes").and_then(Value::as_array) {
Some(items) => items.iter().filter_map(Value::as_str).map(str::to_string).collect(),
None => self.classes.clone(),
};
let cx = cx_for(ctx.as_ref());
let source = load_image(ctx.as_ref(), &image).await?;
let regions = self.perceive(&cx, &source, &DetectQuery::Classes(classes)).await?;
let text = serde_json::to_string(®ions)
.map_err(|err| AdkError::agent(format!("regions could not be encoded: {err}")))?;
let event = text_event(ctx.as_ref(), &self.name, text);
Ok(Box::pin(futures::stream::iter([Ok(event)])))
}
}
#[derive(Clone, Debug)]
#[non_exhaustive]
pub struct ReasonRequest<'a> {
pub question: String,
pub verdicts: Vec<String>,
pub regions: RegionSet,
pub measurements: Vec<Value>,
pub source: Option<&'a SourceImage>,
pub crops: Vec<BBox>,
pub approved: Vec<Digest>,
}
impl<'a> ReasonRequest<'a> {
pub fn new(
question: impl Into<String>,
verdicts: impl IntoIterator<Item = impl Into<String>>,
regions: RegionSet,
) -> Self {
Self {
question: question.into(),
verdicts: verdicts.into_iter().map(Into::into).collect(),
regions,
measurements: Vec::new(),
source: None,
crops: Vec::new(),
approved: Vec::new(),
}
}
#[must_use]
pub fn with_crop(mut self, source: &'a SourceImage, crop: BBox) -> Self {
self.source = Some(source);
self.crops.push(crop);
self
}
#[must_use]
pub fn with_measurements(mut self, measurements: Vec<Value>) -> Self {
self.measurements = measurements;
self
}
#[must_use]
pub fn approving(mut self, digest: Digest) -> Self {
self.approved.push(digest);
self
}
}
#[derive(Clone, Debug)]
#[non_exhaustive]
pub struct Reasoned {
pub opinion: Opinion,
pub released: Vec<EgressReceipt>,
}
pub struct Reasoner {
name: String,
description: String,
model: Arc<dyn Llm>,
gate: Option<Arc<EgressGate>>,
destination: BackendDescriptor,
data_class: DataClass,
provider: String,
#[cfg(feature = "guardrail")]
guardrail: Option<crate::guardrail::ImagePrivacyGuardrail>,
max_output_tokens: Option<i32>,
config: adk_core::GenerateContentConfig,
}
impl std::fmt::Debug for Reasoner {
fn fmt(&self, f: &mut std::fmt::Formatter<'_>) -> std::fmt::Result {
f.debug_struct("Reasoner")
.field("name", &self.name)
.field("model", &self.model.name())
.field("provider", &self.provider)
.finish_non_exhaustive()
}
}
#[must_use]
pub struct ReasonerBuilder {
name: String,
description: String,
model: Option<Arc<dyn Llm>>,
gate: Option<Arc<EgressGate>>,
hosts: Vec<String>,
data_class: String,
provider: Option<String>,
max_output_tokens: Option<i32>,
config: adk_core::GenerateContentConfig,
}
impl Reasoner {
pub fn builder(name: impl Into<String>) -> ReasonerBuilder {
ReasonerBuilder {
name: name.into(),
description: "Reasons over regions, measurements and redacted crops, and answers \
with a verdict."
.to_string(),
model: None,
gate: None,
hosts: Vec::new(),
data_class: "team_crop".to_string(),
provider: None,
max_output_tokens: None,
config: adk_core::GenerateContentConfig::default(),
}
}
pub fn name(&self) -> &str {
&self.name
}
pub fn provider(&self) -> &str {
&self.provider
}
pub fn model(&self) -> &Arc<dyn Llm> {
&self.model
}
pub fn is_remote(&self) -> bool {
self.model.data_locality().leaves_machine()
}
pub fn egress_gate(&self) -> Option<&Arc<EgressGate>> {
self.gate.as_ref()
}
pub fn data_class(&self) -> &DataClass {
&self.data_class
}
pub async fn stage(
&self,
cx: &VisionCx,
source: &SourceImage,
crop: BBox,
) -> Result<Option<StagedEgress>> {
let Some(gate) = self.gate.as_ref().filter(|_| self.is_remote()) else {
return Ok(None);
};
if allows(gate, &self.destination, &self.data_class) {
return Ok(None);
}
let request = ReleaseRequest::new(source, &self.destination, self.data_class.clone())
.with_crop(with_margin(crop, source));
gate.stage(cx, &request).await.map(Some)
}
pub async fn reason(&self, cx: &VisionCx, request: ReasonRequest<'_>) -> Result<Reasoned> {
let mut parts = Vec::new();
let mut released = Vec::new();
if !request.crops.is_empty() {
let source = request.source.ok_or_else(|| {
VisionError::InvalidInput("crops need the source image they are cut from".into())
})?;
for crop in &request.crops {
let crop = with_margin(*crop, source);
if self.is_remote() {
let gate = self.gate.as_ref().ok_or_else(|| VisionError::NotReleased {
backend: self.destination.id.to_string(),
})?;
let mut release =
ReleaseRequest::new(source, &self.destination, self.data_class.clone())
.with_crop(crop);
for digest in &request.approved {
release = release.approved(*digest);
}
let image: ReleasedImage = gate.release(cx, release).await?;
parts.push(image.to_part());
released.push(image.receipt().clone());
} else {
let (w, h) = source.size();
let (x, y, cw, ch) = crop.pixel_bounds(w, h);
let pixels = crate::edit::ops::crop(source.pixels(), x, y, cw, ch);
parts.push(Part::InlineData {
mime_type: "image/png".into(),
data: pixels.encode_png()?,
uri: None,
annotations: None,
});
}
}
}
let opinion = self
.ask(
cx,
&request.question,
&request.verdicts,
&request.regions,
&request.measurements,
parts,
)
.await?;
Ok(Reasoned { opinion, released })
}
async fn ask(
&self,
cx: &VisionCx,
question: &str,
verdicts: &[String],
regions: &RegionSet,
measurements: &[Value],
images: Vec<Part>,
) -> Result<Opinion> {
let count = images.len();
let mut text = format!(
"You are the reasoner of a vision team. You hold no tools and never see the whole \
image: you see the regions a perceiver on the device found, measurements, and \
{count} crop(s) of the image around those regions, redacted before they were sent.\n\n\
Question: {question}\n\
Answer with one verdict from: {}.\n\n\
Regions, in source pixels: {}\n",
verdicts.iter().map(|v| format!("\"{v}\"")).collect::<Vec<_>>().join(", "),
serde_json::to_string(regions).unwrap_or_default(),
);
if !measurements.is_empty() {
text.push_str(&format!(
"Measurements: {}\n",
serde_json::to_string(measurements).unwrap_or_default()
));
}
text.push_str(
"\nAnswer with JSON only: {\"verdict\": <one of the verdicts>, \"label\": <the class \
you judged>, \"confidence\": <0 to 1>, \"rationale\": <one sentence>}.",
);
let mut parts = images;
parts.push(Part::Text { text });
let mut content = Content { role: "user".into(), parts };
#[cfg(feature = "guardrail")]
if let Some(guardrail) = &self.guardrail {
let outcome = guardrail.gate_content(cx, &mut content).await;
if !outcome.withheld.is_empty() {
tracing::warn!(
vision.withheld = outcome.withheld.len(),
team.member = %self.name,
"images withheld from the reasoner"
);
}
}
let mut request = LlmRequest::new(self.model.name(), vec![content]);
let mut config = self.config.clone();
if self.max_output_tokens.is_some() {
config.max_output_tokens = self.max_output_tokens;
}
request.config = Some(config);
let text = answer_text(cx, &self.model, request).await?;
parse_opinion(self.model.name(), &text, verdicts, &self.name, &self.provider)
}
}
impl ReasonerBuilder {
pub fn description(mut self, description: impl Into<String>) -> Self {
self.description = description.into();
self
}
pub fn model(mut self, model: Arc<dyn Llm>) -> Self {
self.model = Some(model);
self
}
pub fn egress_gate(mut self, gate: Arc<EgressGate>) -> Self {
self.gate = Some(gate);
self
}
pub fn hosts(mut self, hosts: impl IntoIterator<Item = impl Into<String>>) -> Self {
self.hosts = hosts.into_iter().map(Into::into).collect();
self
}
pub fn data_class(mut self, class: &str) -> Self {
self.data_class = class.to_string();
self
}
pub fn provider(mut self, provider: impl Into<String>) -> Self {
self.provider = Some(provider.into());
self
}
pub fn max_output_tokens(mut self, tokens: i32) -> Self {
self.max_output_tokens = Some(tokens);
self
}
pub fn generate_config(mut self, config: adk_core::GenerateContentConfig) -> Self {
self.config = config;
self
}
pub fn build(self) -> std::result::Result<Reasoner, TeamRoleError> {
let model = self.model.ok_or_else(|| {
TeamRoleError::Missing(format!("the reasoner '{}' needs a model", self.name))
})?;
let remote = model.data_locality().leaves_machine();
if remote && self.gate.is_none() {
return Err(TeamRoleError::Missing(format!(
"the reasoner '{}' runs on the remote model '{}' and needs the egress gate its \
crops pass (ReasonerBuilder::egress_gate)",
self.name,
model.name()
)));
}
let destination = destination_of(model.as_ref(), remote, self.hosts);
let hosts = match &destination.locality {
Locality::Remote { hosts } => hosts.clone(),
_ => Vec::new(),
};
let provider = provider_of(model.name(), self.provider.as_deref());
#[cfg(feature = "guardrail")]
let guardrail = self.gate.as_ref().map(|gate| {
crate::guardrail::ImagePrivacyGuardrail::for_model(
Arc::clone(gate),
model.as_ref(),
hosts.clone(),
)
});
Ok(Reasoner {
name: self.name,
description: self.description,
model,
gate: self.gate,
destination,
data_class: DataClass::new(self.data_class),
provider,
#[cfg(feature = "guardrail")]
guardrail,
max_output_tokens: self.max_output_tokens,
config: self.config,
})
}
}
#[async_trait]
impl Agent for Reasoner {
fn name(&self) -> &str {
&self.name
}
fn description(&self) -> &str {
&self.description
}
fn sub_agents(&self) -> &[Arc<dyn Agent>] {
&[]
}
async fn run(&self, ctx: Arc<dyn InvocationContext>) -> adk_core::Result<EventStream> {
let request = request_of(ctx.as_ref());
let question = request.get("question").and_then(Value::as_str).unwrap_or_default();
let regions: RegionSet = request
.get("regions")
.cloned()
.and_then(|value| serde_json::from_value(value).ok())
.ok_or_else(|| AdkError::agent("a reasoner request needs `regions`, a RegionSet"))?;
let measurements: Vec<Value> =
request.get("measurements").and_then(Value::as_array).cloned().unwrap_or_default();
let verdicts: Vec<String> = match request.get("verdicts").and_then(Value::as_array) {
Some(items) => items.iter().filter_map(Value::as_str).map(str::to_string).collect(),
None => vec!["yes".to_string(), "no".to_string()],
};
let mut parts = Vec::new();
for crop in request.get("crops").and_then(Value::as_array).into_iter().flatten() {
let name = crop.as_str().unwrap_or_default();
let digest = name
.strip_prefix("sha256:")
.and_then(|hex| Digest::from_hex(hex).ok())
.ok_or_else(|| AdkError::agent(format!("`{name}` is not an image digest")))?;
if self.is_remote() && !self.gate.as_ref().is_some_and(|g| g.has_released(&digest)) {
return Err(
VisionError::NotReleased { backend: self.destination.id.to_string() }.into()
);
}
let data = super::load_bytes(ctx.as_ref(), name).await?;
parts.push(Part::InlineData {
mime_type: "image/png".into(),
data,
uri: None,
annotations: Some(json!({
"adkVision": { "digest": digest, "release": { "egress": digest } }
})),
});
}
let cx = cx_for(ctx.as_ref());
let opinion = self.ask(&cx, question, &verdicts, ®ions, &measurements, parts).await?;
let text = serde_json::to_string(&opinion)
.map_err(|err| AdkError::agent(format!("the opinion could not be encoded: {err}")))?;
let event = text_event(ctx.as_ref(), &self.name, text);
Ok(Box::pin(futures::stream::iter([Ok(event)])))
}
}
fn destination_of(model: &dyn Llm, remote: bool, hosts: Vec<String>) -> BackendDescriptor {
if !remote {
return BackendDescriptor::new(model.name(), Locality::InProcess, CoordFrame::SentPixels);
}
let destinations = model.data_destinations();
let mut hosts = hosts;
if hosts.is_empty() {
hosts = adk_core::DataDestination::all_hosts(&destinations);
}
if hosts.is_empty() {
hosts.push(model.name().to_string());
}
let mut descriptor =
BackendDescriptor::new(model.name(), Locality::Remote { hosts }, CoordFrame::SentPixels);
for destination in destinations {
descriptor = descriptor.with_provider(destination.provider);
}
descriptor
}
fn allows(gate: &EgressGate, destination: &BackendDescriptor, class: &DataClass) -> bool {
match &destination.locality {
Locality::Remote { .. } => {
gate.decide(destination, class).0 == crate::egress::EgressAction::Allow
}
_ => true,
}
}
fn with_margin(crop: BBox, source: &SourceImage) -> BBox {
let (w, h) = source.size();
crop.expanded(CROP_MARGIN).clamped(w, h)
}
#[derive(Clone, Debug, PartialEq, serde::Serialize, serde::Deserialize)]
#[serde(rename_all = "camelCase")]
#[non_exhaustive]
pub struct RegionCheck {
pub label: String,
pub claimed: BBox,
pub found: Vec<Region>,
pub best_iou: f32,
pub confirmed: bool,
}
#[derive(Clone, Debug)]
#[non_exhaustive]
pub struct VerifyRequest<'a> {
pub question: String,
pub verdicts: [String; 2],
pub claim: Opinion,
pub label: Option<String>,
pub region: BBox,
pub source: &'a SourceImage,
pub approved: Vec<Digest>,
}
impl<'a> VerifyRequest<'a> {
pub fn new(
question: impl Into<String>,
positive: impl Into<String>,
negative: impl Into<String>,
claim: Opinion,
region: BBox,
source: &'a SourceImage,
) -> Self {
Self {
question: question.into(),
verdicts: [positive.into(), negative.into()],
claim,
label: None,
region,
source,
approved: Vec::new(),
}
}
#[must_use]
pub fn with_label(mut self, label: impl Into<String>) -> Self {
self.label = Some(label.into());
self
}
}
#[derive(Clone, Debug)]
#[non_exhaustive]
pub struct Verified {
pub opinion: Opinion,
pub check: RegionCheck,
pub released: Option<EgressReceipt>,
}
pub struct Verifier {
name: String,
description: String,
runtime: Arc<VisionRuntime>,
model: Option<Arc<dyn Llm>>,
gate: Option<Arc<EgressGate>>,
destination: Option<BackendDescriptor>,
data_class: DataClass,
provider: String,
iou: f32,
min_score: f32,
max_output_tokens: Option<i32>,
config: adk_core::GenerateContentConfig,
}
impl std::fmt::Debug for Verifier {
fn fmt(&self, f: &mut std::fmt::Formatter<'_>) -> std::fmt::Result {
f.debug_struct("Verifier")
.field("name", &self.name)
.field("model", &self.model.as_ref().map(|m| m.name().to_string()))
.field("provider", &self.provider)
.finish_non_exhaustive()
}
}
#[must_use]
pub struct VerifierBuilder {
name: String,
description: String,
runtime: Option<Arc<VisionRuntime>>,
model: Option<Arc<dyn Llm>>,
gate: Option<Arc<EgressGate>>,
hosts: Vec<String>,
data_class: String,
provider: Option<String>,
iou: f32,
min_score: f32,
max_output_tokens: Option<i32>,
config: adk_core::GenerateContentConfig,
}
impl Verifier {
pub fn builder(name: impl Into<String>) -> VerifierBuilder {
VerifierBuilder {
name: name.into(),
description: "Re-checks a claim against the pixels with local perception.".to_string(),
runtime: None,
model: None,
gate: None,
hosts: Vec::new(),
data_class: "team_crop".to_string(),
provider: None,
iou: 0.3,
min_score: 0.25,
max_output_tokens: None,
config: adk_core::GenerateContentConfig::default(),
}
}
pub fn name(&self) -> &str {
&self.name
}
pub fn provider(&self) -> &str {
&self.provider
}
pub fn is_remote(&self) -> bool {
self.model.as_ref().is_some_and(|m| m.data_locality().leaves_machine())
}
pub fn egress_gate(&self) -> Option<&Arc<EgressGate>> {
self.gate.as_ref()
}
pub async fn stage(
&self,
cx: &VisionCx,
source: &SourceImage,
crop: BBox,
) -> Result<Option<StagedEgress>> {
let (Some(gate), Some(destination)) = (&self.gate, &self.destination) else {
return Ok(None);
};
if !self.is_remote() || allows(gate, destination, &self.data_class) {
return Ok(None);
}
let request = ReleaseRequest::new(source, destination, self.data_class.clone())
.with_crop(with_margin(crop, source));
gate.stage(cx, &request).await.map(Some)
}
pub async fn check(
&self,
cx: &VisionCx,
source: &SourceImage,
label: &str,
region: BBox,
) -> Result<RegionCheck> {
let found = self
.runtime
.detect(cx, source, &DetectQuery::Classes(vec![label.to_string()]))
.await?
.value;
let found: Vec<Region> = found
.into_iter()
.filter(|r| r.label.as_deref().is_none_or(|l| l == label))
.filter(|r| r.score.is_none_or(|s| s >= self.min_score))
.collect();
let best_iou = found
.iter()
.filter_map(|r| r.shape.bbox())
.map(|bbox| bbox.iou(®ion))
.fold(0.0_f32, f32::max);
Ok(RegionCheck {
label: label.to_string(),
claimed: region,
found,
best_iou,
confirmed: best_iou >= self.iou,
})
}
pub async fn verify(&self, cx: &VisionCx, request: VerifyRequest<'_>) -> Result<Verified> {
let label = request
.label
.clone()
.or_else(|| request.claim.label.clone())
.unwrap_or_else(|| request.verdicts[0].clone());
let check = self.check(cx, request.source, &label, request.region).await?;
let Some(model) = &self.model else {
let verdict = if check.confirmed { &request.verdicts[0] } else { &request.verdicts[1] };
let confidence = if check.confirmed { check.best_iou } else { 1.0 - check.best_iou };
let mut opinion = Opinion::new(verdict.clone(), confidence).with_label(label.clone());
opinion.rationale = if check.confirmed {
format!(
"local perception finds `{label}` over the claimed region (overlap {:.2})",
check.best_iou
)
} else {
format!(
"local perception finds no `{label}` over the claimed region (best overlap \
{:.2}, {} found elsewhere)",
check.best_iou,
check.found.len()
)
};
opinion.member = self.name.clone();
opinion.provider = self.provider.clone();
return Ok(Verified { opinion, check, released: None });
};
let crop = with_margin(request.region, request.source);
let mut released = None;
let image = if self.is_remote() {
let (Some(gate), Some(destination)) = (&self.gate, &self.destination) else {
return Err(VisionError::NotReleased { backend: model.name().to_string() });
};
let mut release =
ReleaseRequest::new(request.source, destination, self.data_class.clone())
.with_crop(crop);
for digest in &request.approved {
release = release.approved(*digest);
}
let image = gate.release(cx, release).await?;
released = Some(image.receipt().clone());
image.to_part()
} else {
let (w, h) = request.source.size();
let (x, y, cw, ch) = crop.pixel_bounds(w, h);
Part::InlineData {
mime_type: "image/png".into(),
data: crate::edit::ops::crop(request.source.pixels(), x, y, cw, ch).encode_png()?,
uri: None,
annotations: None,
}
};
let verdicts = request.verdicts.to_vec();
let text = format!(
"You are the verifier of a vision team. Another model made a claim about a region of \
an image; check it against the pixels. The image is a crop around the claimed \
region, redacted before it was sent.\n\n\
Question: {question}\n\
Claim: {claim}\n\
Local perception on the device looked for `{label}` around the claimed region: \
{found} region(s) found, best overlap with the claim {iou:.2}.\n\n\
Answer with JSON only: {{\"verdict\": <one of {list}>, \"label\": <the class>, \
\"confidence\": <0 to 1>, \"rationale\": <one sentence>}}.",
question = request.question,
claim = serde_json::to_string(&request.claim).unwrap_or_default(),
found = check.found.len(),
iou = check.best_iou,
list = verdicts.iter().map(|v| format!("\"{v}\"")).collect::<Vec<_>>().join(", "),
);
let mut content = Content { role: "user".into(), parts: vec![image, Part::Text { text }] };
#[cfg(feature = "guardrail")]
if let (Some(gate), true) = (&self.gate, self.is_remote()) {
let hosts = match self.destination.as_ref().map(|d| &d.locality) {
Some(Locality::Remote { hosts }) => hosts.clone(),
_ => vec![model.name().to_string()],
};
crate::guardrail::ImagePrivacyGuardrail::for_model(
Arc::clone(gate),
model.as_ref(),
hosts,
)
.gate_content(cx, &mut content)
.await;
}
let mut llm_request = LlmRequest::new(model.name(), vec![content]);
let mut config = self.config.clone();
if self.max_output_tokens.is_some() {
config.max_output_tokens = self.max_output_tokens;
}
llm_request.config = Some(config);
let answer = answer_text(cx, model, llm_request).await?;
let opinion = parse_opinion(model.name(), &answer, &verdicts, &self.name, &self.provider)?;
Ok(Verified { opinion, check, released })
}
}
impl VerifierBuilder {
pub fn description(mut self, description: impl Into<String>) -> Self {
self.description = description.into();
self
}
pub fn runtime(mut self, runtime: Arc<VisionRuntime>) -> Self {
self.runtime = Some(runtime);
self
}
pub fn model(mut self, model: Arc<dyn Llm>) -> Self {
self.model = Some(model);
self
}
pub fn egress_gate(mut self, gate: Arc<EgressGate>) -> Self {
self.gate = Some(gate);
self
}
pub fn hosts(mut self, hosts: impl IntoIterator<Item = impl Into<String>>) -> Self {
self.hosts = hosts.into_iter().map(Into::into).collect();
self
}
pub fn data_class(mut self, class: &str) -> Self {
self.data_class = class.to_string();
self
}
pub fn provider(mut self, provider: impl Into<String>) -> Self {
self.provider = Some(provider.into());
self
}
pub fn min_iou(mut self, iou: f32) -> Self {
self.iou = iou;
self
}
pub fn min_score(mut self, score: f32) -> Self {
self.min_score = score;
self
}
pub fn max_output_tokens(mut self, tokens: i32) -> Self {
self.max_output_tokens = Some(tokens);
self
}
pub fn generate_config(mut self, config: adk_core::GenerateContentConfig) -> Self {
self.config = config;
self
}
pub fn build(self) -> std::result::Result<Verifier, TeamRoleError> {
let runtime = self.runtime.ok_or_else(|| {
TeamRoleError::Missing(format!(
"the verifier '{}' needs a runtime of on-device backends to check claims with",
self.name
))
})?;
check_local_runtime("verifier", &self.name, &runtime)?;
let remote = self.model.as_ref().is_some_and(|m| m.data_locality().leaves_machine());
if remote && self.gate.is_none() {
return Err(TeamRoleError::Missing(format!(
"the verifier '{}' runs on a remote model and needs the egress gate its crops \
pass (VerifierBuilder::egress_gate)",
self.name
)));
}
let destination = self
.model
.as_ref()
.map(|model| destination_of(model.as_ref(), remote, self.hosts.clone()));
let provider = match &self.model {
Some(model) => provider_of(model.name(), self.provider.as_deref()),
None => self.provider.unwrap_or_else(|| "local".to_string()),
};
Ok(Verifier {
name: self.name,
description: self.description,
runtime,
model: self.model,
gate: self.gate,
destination,
data_class: DataClass::new(self.data_class),
provider,
iou: self.iou,
min_score: self.min_score,
max_output_tokens: self.max_output_tokens,
config: self.config,
})
}
}
#[async_trait]
impl Agent for Verifier {
fn name(&self) -> &str {
&self.name
}
fn description(&self) -> &str {
&self.description
}
fn sub_agents(&self) -> &[Arc<dyn Agent>] {
&[]
}
async fn run(&self, ctx: Arc<dyn InvocationContext>) -> adk_core::Result<EventStream> {
let request = request_of(ctx.as_ref());
let claim: Opinion = request
.get("claim")
.cloned()
.and_then(|value| serde_json::from_value(value).ok())
.ok_or_else(|| AdkError::agent("a verifier request needs `claim`, an opinion"))?;
let image = request.get("image").and_then(Value::as_str).unwrap_or_default();
let source = load_image(ctx.as_ref(), image).await?;
let regions: Option<RegionSet> =
request.get("regions").cloned().and_then(|value| serde_json::from_value(value).ok());
let region = regions
.as_ref()
.and_then(|set| set.regions.first())
.or_else(|| claim.regions.as_ref().and_then(|set| set.regions.first()))
.and_then(|region| region.shape.bbox())
.ok_or_else(|| AdkError::agent("a verifier request needs the claimed region"))?;
let question = request.get("question").and_then(Value::as_str).unwrap_or_default();
let positive = claim.verdict.clone();
let negative = format!("not_{positive}");
let cx = cx_for(ctx.as_ref());
let verified = self
.verify(&cx, VerifyRequest::new(question, positive, negative, claim, region, &source))
.await?;
let text = serde_json::to_string(&verified.opinion)
.map_err(|err| AdkError::agent(format!("the opinion could not be encoded: {err}")))?;
let event = text_event(ctx.as_ref(), &self.name, text);
Ok(Box::pin(futures::stream::iter([Ok(event)])))
}
}