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

🐛 Fix threshold getting passed through beyond orchestrator proces… #236

Merged
Merged
Show file tree
Hide file tree
Changes from all 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
14 changes: 8 additions & 6 deletions src/models.rs
Original file line number Diff line number Diff line change
Expand Up @@ -54,8 +54,8 @@ impl DetectorParams {
}

/// Threshold to filter detector results by score.
pub fn threshold(&self) -> Option<f64> {
self.0.get("threshold").and_then(|v| v.as_f64())
pub fn pop_threshold(&mut self) -> Option<f64> {
self.0.remove("threshold").and_then(|v| v.as_f64())
}
}

Expand Down Expand Up @@ -1272,10 +1272,12 @@ mod tests {
{
"threshold": 0.2
}"#;
let value: DetectorParams = serde_json::from_str(value_json)?;
assert_eq!(value.threshold(), Some(0.2));
let value = DetectorParams::new();
assert_eq!(value.threshold(), None);
let mut value: DetectorParams = serde_json::from_str(value_json)?;
assert_eq!(value.pop_threshold(), Some(0.2));
assert!(!value.contains_key("threshold"));
Copy link
Collaborator

@evaline-ju evaline-ju Oct 22, 2024

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I did this for the first iteration of #235 but felt it was not intuitive since essentially if value.threshold() were to get called again for this same value, the result would change. This could get confusing if any function happens to call threshold() twice

let mut value = DetectorParams::new();
assert!(!value.contains_key("threshold"));
assert_eq!(value.pop_threshold(), None);
Ok(())
}
}
4 changes: 3 additions & 1 deletion src/orchestrator/streaming.rs
Original file line number Diff line number Diff line change
Expand Up @@ -267,6 +267,8 @@ async fn streaming_output_detection_task(
debug!("spawning detection tasks");
let mut detection_streams = Vec::with_capacity(detectors.len());
for (detector_id, detector_params) in detectors.iter() {
// Create a mutable copy of the parameters, so that we can modify it based on processing
let mut detector_params = detector_params.clone();
let detector_id = detector_id.to_string();
let chunker_id = ctx.config.get_chunker_id(&detector_id).unwrap();

Expand All @@ -276,7 +278,7 @@ async fn streaming_output_detection_task(

// Get the default threshold to use if threshold is not provided by the user
let default_threshold = detector_config.default_threshold;
let threshold = detector_params.threshold().unwrap_or(default_threshold);
let threshold = detector_params.pop_threshold().unwrap_or(default_threshold);

// Create detection stream
let (detector_tx, detector_rx) = mpsc::channel(1024);
Expand Down
26 changes: 13 additions & 13 deletions src/orchestrator/unary.rs
Original file line number Diff line number Diff line change
Expand Up @@ -617,12 +617,12 @@ pub async fn detect(
ctx: Arc<Context>,
detector_id: String,
default_threshold: f64,
detector_params: DetectorParams,
mut detector_params: DetectorParams,
chunks: Vec<Chunk>,
headers: HeaderMap,
) -> Result<Vec<TokenClassificationResult>, Error> {
let detector_id = detector_id.clone();
let threshold = detector_params.threshold().unwrap_or(default_threshold);
let threshold = detector_params.pop_threshold().unwrap_or(default_threshold);
let contents: Vec<_> = chunks.iter().map(|chunk| chunk.text.clone()).collect();
let response = if contents.is_empty() {
// skip detector call as contents is empty
Expand Down Expand Up @@ -676,12 +676,12 @@ pub async fn detect_content(
ctx: Arc<Context>,
detector_id: String,
default_threshold: f64,
detector_params: DetectorParams,
mut detector_params: DetectorParams,
chunks: Vec<Chunk>,
headers: HeaderMap,
) -> Result<Vec<ContentAnalysisResponse>, Error> {
let detector_id = detector_id.clone();
let threshold = detector_params.threshold().unwrap_or(default_threshold);
let threshold = detector_params.pop_threshold().unwrap_or(default_threshold);
let contents: Vec<_> = chunks.iter().map(|chunk| chunk.text.clone()).collect();
let response = if contents.is_empty() {
// skip detector call as contents is empty
Expand Down Expand Up @@ -731,14 +731,14 @@ pub async fn detect_content(
pub async fn detect_for_generation(
ctx: Arc<Context>,
detector_id: String,
detector_params: DetectorParams,
mut detector_params: DetectorParams,
prompt: String,
generated_text: String,
headers: HeaderMap,
) -> Result<Vec<DetectionResult>, Error> {
let detector_id = detector_id.clone();
let threshold = detector_params.threshold().unwrap_or(
detector_params.threshold().unwrap_or(
let threshold = detector_params.pop_threshold().unwrap_or(
detector_params.pop_threshold().unwrap_or(
ctx.config
.detectors
.get(&detector_id)
Expand Down Expand Up @@ -773,13 +773,13 @@ pub async fn detect_for_generation(
pub async fn detect_for_chat(
ctx: Arc<Context>,
detector_id: String,
detector_params: DetectorParams,
mut detector_params: DetectorParams,
messages: Vec<Message>,
headers: HeaderMap,
) -> Result<Vec<DetectionResult>, Error> {
let detector_id = detector_id.clone();
let threshold = detector_params.threshold().unwrap_or(
detector_params.threshold().unwrap_or(
let threshold = detector_params.pop_threshold().unwrap_or(
detector_params.pop_threshold().unwrap_or(
ctx.config
.detectors
.get(&detector_id)
Expand Down Expand Up @@ -814,15 +814,15 @@ pub async fn detect_for_chat(
pub async fn detect_for_context(
ctx: Arc<Context>,
detector_id: String,
detector_params: DetectorParams,
mut detector_params: DetectorParams,
content: String,
context_type: ContextType,
context: Vec<String>,
headers: HeaderMap,
) -> Result<Vec<DetectionResult>, Error> {
let detector_id = detector_id.clone();
let threshold = detector_params.threshold().unwrap_or(
detector_params.threshold().unwrap_or(
let threshold = detector_params.pop_threshold().unwrap_or(
detector_params.pop_threshold().unwrap_or(
ctx.config
.detectors
.get(&detector_id)
Expand Down