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

Add detector_id in response for orchestrator apis #278

Merged
merged 18 commits into from
Jan 21, 2025
Merged
Show file tree
Hide file tree
Changes from 8 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
5 changes: 3 additions & 2 deletions protos/caikit_data_model_nlp.proto
Original file line number Diff line number Diff line change
Expand Up @@ -89,8 +89,9 @@ message TokenClassificationResult {
string word = 3;
string entity = 4;
string entity_group = 5;
double score = 6;
int64 token_count = 7;
string detector_id = 6;
swith004 marked this conversation as resolved.
Show resolved Hide resolved
double score = 7;
int64 token_count = 8;
}

message TokenClassificationResults {
Expand Down
20 changes: 12 additions & 8 deletions src/clients/detector/text_contents.rs
Original file line number Diff line number Diff line change
Expand Up @@ -132,22 +132,26 @@ pub struct ContentAnalysisResponse {
pub detection: String,
/// Detection type or aggregate detection label
pub detection_type: String,
/// ID of Detector
swith004 marked this conversation as resolved.
Show resolved Hide resolved
pub detector_id: Option<String>,
/// Score of detection
pub score: f64,
/// Optional, any applicable evidence for detection
#[serde(skip_serializing_if = "Option::is_none")]
pub evidence: Option<Vec<EvidenceObj>>,
}

impl From<ContentAnalysisResponse> for crate::models::TokenClassificationResult {
fn from(value: ContentAnalysisResponse) -> Self {
impl From<(String, ContentAnalysisResponse)> for crate::models::TokenClassificationResult {
swith004 marked this conversation as resolved.
Show resolved Hide resolved
fn from(value: (String, ContentAnalysisResponse)) -> Self {
let (detector_id, response) = value;
Self {
start: value.start as u32,
end: value.end as u32,
word: value.text,
entity: value.detection,
entity_group: value.detection_type,
score: value.score,
start: response.start as u32,
end: response.end as u32,
word: response.text,
entity: response.detection,
entity_group: response.detection_type,
detector_id,
score: response.score,
token_count: None,
}
}
Expand Down
6 changes: 6 additions & 0 deletions src/models.rs
Original file line number Diff line number Diff line change
Expand Up @@ -459,6 +459,9 @@ pub struct TokenClassificationResult {
/// Aggregate label, if applicable
pub entity_group: String,

/// id of detector (model) responsible for result(s)
swith004 marked this conversation as resolved.
Show resolved Hide resolved
pub detector_id: String,

/// Confidence-like score of this classification prediction in [0, 1]
pub score: f64,

Expand Down Expand Up @@ -894,6 +897,9 @@ pub struct DetectionResult {
// The detection class
pub detection: String,

// The id of the detector
swith004 marked this conversation as resolved.
Show resolved Hide resolved
pub detector_id: Option<String>,

// The confidence level in the detection class
pub score: f64,

Expand Down
2 changes: 1 addition & 1 deletion src/orchestrator/streaming.rs
Original file line number Diff line number Diff line change
Expand Up @@ -427,7 +427,7 @@ async fn detection_task(
.into_iter()
.flat_map(|r| {
r.into_iter().filter_map(|resp| {
let result: TokenClassificationResult = resp.into();
let result: TokenClassificationResult = (detector_id.clone(),resp).into();
(result.score >= threshold).then_some(result)
})
})
Expand Down
6 changes: 4 additions & 2 deletions src/orchestrator/streaming/aggregator.rs
Original file line number Diff line number Diff line change
Expand Up @@ -458,13 +458,15 @@ mod tests {
text: &str,
detection: &str,
detection_type: &str,
detector_id: &str,
) -> TokenClassificationResult {
TokenClassificationResult {
start: span.0 as u32,
end: span.1 as u32,
word: text.to_string(),
entity: detection.to_string(),
entity_group: detection_type.to_string(),
detector_id: detector_id.to_string(),
score: 0.99,
token_count: None,
}
Expand Down Expand Up @@ -498,11 +500,11 @@ mod tests {
let partial_span = (chunk_token.start + 2, chunk_token.end - 2);

let (detector_tx1, detector_rx1) = mpsc::channel(1);
let detection = get_detection_obj(whole_span, text, "has_HAP", "HAP");
let detection = get_detection_obj(whole_span, text, "has_HAP", "HAP", "en-hap");
let _ = detector_tx1.send((chunk.clone(), vec![detection])).await;

let (detector_tx2, detector_rx2) = mpsc::channel(1);
let detection = get_detection_obj(partial_span, text, "email_ID", "PII");
let detection = get_detection_obj(partial_span, text, "email_ID", "PII", "en-pii");
let _ = detector_tx2.send((chunk.clone(), vec![detection])).await;

// Push HAP after PII to make sure detection ordering is not coincidental
Expand Down
2 changes: 1 addition & 1 deletion src/orchestrator/streaming_content_detection.rs
Original file line number Diff line number Diff line change
Expand Up @@ -419,7 +419,7 @@ async fn detection_task(
.into_iter()
.flat_map(|r| {
r.into_iter().filter_map(|resp| {
let result: TokenClassificationResult = resp.into();
let result: TokenClassificationResult = (detector_id.clone(), resp).into();
(result.score >= threshold).then_some(result)
})
})
Expand Down
7 changes: 5 additions & 2 deletions src/orchestrator/streaming_content_detection/aggregator.rs
Original file line number Diff line number Diff line change
Expand Up @@ -158,6 +158,7 @@ impl AggregationActor {
text: r.word,
detection: r.entity,
detection_type: r.entity_group,
detector_id: Some(r.detector_id),
score: r.score,
evidence: None,
})
Expand Down Expand Up @@ -206,13 +207,15 @@ mod tests {
text: &str,
detection: &str,
detection_type: &str,
detector_id: &str,
) -> TokenClassificationResult {
TokenClassificationResult {
start: span.0 as u32,
end: span.1 as u32,
word: text.to_string(),
entity: detection.to_string(),
entity_group: detection_type.to_string(),
detector_id: detector_id.to_string(),
score: 0.99,
token_count: None,
}
Expand Down Expand Up @@ -246,11 +249,11 @@ mod tests {
let partial_span = (chunk_token.start + 2, chunk_token.end - 2);

let (detector_tx1, detector_rx1) = mpsc::channel(1);
let detection = get_detection_obj(whole_span, text, "has_HAP", "HAP");
let detection = get_detection_obj(whole_span, text, "has_HAP", "HAP", "en-hap");
let _ = detector_tx1.send((chunk.clone(), vec![detection])).await;

let (detector_tx2, detector_rx2) = mpsc::channel(1);
let detection = get_detection_obj(partial_span, text, "email_ID", "PII");
let detection = get_detection_obj(partial_span, text, "email_ID", "PII", "en-pii");
let _ = detector_tx2.send((chunk.clone(), vec![detection])).await;

// Push HAP after PII to make sure detection ordering is not coincidental
Expand Down
40 changes: 39 additions & 1 deletion src/orchestrator/unary.rs
Original file line number Diff line number Diff line change
Expand Up @@ -695,9 +695,10 @@ pub async fn detect(
response
.into_iter()
.filter_map(|resp| {
let mut result: TokenClassificationResult = resp.into();
let mut result: TokenClassificationResult = (detector_id.clone(), resp).into();
result.start += chunk.offset as u32;
result.end += chunk.offset as u32;
swith004 marked this conversation as resolved.
Show resolved Hide resolved
// result.detector_id = detector_id.clone(); attach detector_id to the result
(result.score >= threshold).then_some(result)
})
.collect::<Vec<_>>()
Expand Down Expand Up @@ -756,6 +757,7 @@ pub async fn detect_content(
.filter_map(|mut resp| {
resp.start += chunk.offset;
resp.end += chunk.offset;
resp.detector_id = Some(detector_id.clone()); // add detector_id
swith004 marked this conversation as resolved.
Show resolved Hide resolved
(resp.score >= threshold).then_some(resp)
})
.collect::<Vec<_>>()
Expand Down Expand Up @@ -803,6 +805,16 @@ pub async fn detect_for_generation(
results
.into_iter()
.filter(|detection| detection.score > threshold)
.map(|detection| {
//add detector_id
swith004 marked this conversation as resolved.
Show resolved Hide resolved
DetectionResult {
detection_type: detection.detection_type,
detection: detection.detection,
detector_id: Some(detector_id.clone()),
score: detection.score,
evidence: detection.evidence,
}
})
.collect()
})
.map_err(|error| Error::DetectorRequestFailed {
Expand Down Expand Up @@ -844,6 +856,16 @@ pub async fn detect_for_chat(
results
.into_iter()
.filter(|detection| detection.score > threshold)
.map(|detection| {
//add detector_id
DetectionResult {
detection_type: detection.detection_type,
detection: detection.detection,
detector_id: Some(detector_id.clone()),
score: detection.score,
evidence: detection.evidence,
swith004 marked this conversation as resolved.
Show resolved Hide resolved
}
})
.collect()
})
.map_err(|error| Error::DetectorRequestFailed {
Expand Down Expand Up @@ -899,6 +921,16 @@ pub async fn detect_for_context(
results
.into_iter()
.filter(|detection| detection.score > threshold)
.map(|detection| {
//add detector_id
DetectionResult {
detection_type: detection.detection_type,
detection: detection.detection,
detector_id: Some(detector_id.clone()),
score: detection.score,
evidence: detection.evidence,
}
})
.collect()
})
.map_err(|error| Error::DetectorRequestFailed {
Expand Down Expand Up @@ -1131,6 +1163,7 @@ mod tests {
word: second_sentence.clone(),
entity: "has_HAP".to_string(),
entity_group: "hap".to_string(),
detector_id: detector_id.to_string(),
score: 0.9,
token_count: None,
}];
Expand All @@ -1151,6 +1184,7 @@ mod tests {
text: first_sentence.clone(),
detection: "has_HAP".to_string(),
detection_type: "hap".to_string(),
detector_id: Some(detector_id.to_string()),
score: 0.1,
evidence: Some(vec![]),
}],
Expand All @@ -1160,6 +1194,7 @@ mod tests {
text: second_sentence.clone(),
detection: "has_HAP".to_string(),
detection_type: "hap".to_string(),
detector_id: Some(detector_id.to_string()),
score: 0.9,
evidence: Some(vec![]),
}],
Expand Down Expand Up @@ -1300,6 +1335,7 @@ mod tests {
let expected_response: Vec<DetectionResult> = vec![DetectionResult {
detection_type: "relevance".to_string(),
detection: "is_relevant".to_string(),
detector_id: Some(detector_id.to_string()),
score: 0.9,
evidence: Some(
[EvidenceObj {
Expand All @@ -1325,6 +1361,7 @@ mod tests {
.then_return(Ok(vec![DetectionResult {
detection_type: "relevance".to_string(),
detection: "is_relevant".to_string(),
detector_id: Some(detector_id.to_string()),
score: 0.9,
evidence: Some(
[EvidenceObj {
Expand Down Expand Up @@ -1393,6 +1430,7 @@ mod tests {
.then_return(Ok(vec![DetectionResult {
detection_type: "relevance".to_string(),
detection: "is_relevant".to_string(),
detector_id: Some(detector_id.to_string()),
score: 0.1,
evidence: None,
}]));
Expand Down