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

Implement threshold changes #1816

Merged
merged 4 commits into from
Jan 9, 2024
Merged
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
34 changes: 19 additions & 15 deletions js/stores/wizard_store.js
Original file line number Diff line number Diff line change
Expand Up @@ -9,7 +9,7 @@ import { create } from 'zustand';
import { shallow } from 'zustand/shallow';
import { fetchWizardInitData, fetchWizardPredictions } from '../util/wizard_api';
import {
convertSomeLinksToCards, normalizeScore, scanForTriggers, urlParams,
convertSomeLinksToCards, normalizeScore, scanForTriggers,
} from '../util/wizard_helpers';
import searchMatchingAgency from '../util/wizard_agency_search';
import allTopics from '../models/wizard_topics';
Expand All @@ -18,12 +18,11 @@ import { defaultSummary, stateLocalSummary, stateOrLocalFlow } from '../models/w
import agencyComponentStore from './agency_component';

const DEBUG_TO_CONSOLE = true;
const DEFAULT_CONFIDENCE_THRESHOLD = Number(
urlParams().get('confidence-threshold') || 0.5,
);

const CONFIDENCE_THRESHOLD_AGENCIES = DEFAULT_CONFIDENCE_THRESHOLD;
const CONFIDENCE_THRESHOLD_LINKS = DEFAULT_CONFIDENCE_THRESHOLD;
const THRESHOLDS = {
missionMatch: 0.7,
agencyFinder: 0.2,
freqdoc: 0.65,
};

/** @type {WizardVars} */
const initialWizardState = {
Expand Down Expand Up @@ -290,14 +289,17 @@ const useRawWizardStore = create((
set({ modelLoading: true });
await fetchWizardPredictions(query)
.then((data) => {
// Support both V1.1 and V1.0 API output.
const modelOutput = data.model_output || data;

if (triggerMatch) {
log('Collecting model results in case user chooses to switch to them.');
} else if (trustAgencyMatch) {
log('An agency match was most of user\'s query: Skipping intent model.');
} else {
// If a predefined flow is found, we switch to it, but we'll go ahead and populate
// the links and agencies anyway.
let { flow } = data.model_output.predefined_flow || {};
let { flow } = modelOutput.predefined_flow || {};
if (typeof flow === 'string') {
if (flow === stateOrLocalFlow) {
log('Moving user to state/local summary page due to intent model result.');
Expand Down Expand Up @@ -330,7 +332,7 @@ const useRawWizardStore = create((

// If name match, always include it.
recommendedAgencies.push(
...(data.model_output.agency_name_match || [])
...(modelOutput.agency_name_match || [])
.map((agency) => {
// Show near top.
agency.confidence_score = 9999;
Expand All @@ -339,18 +341,20 @@ const useRawWizardStore = create((
}),
);

log('Using thresholds', THRESHOLDS);

// Match from mission if above threshold.
recommendedAgencies.push(
...data.model_output.agency_mission_match
...modelOutput.agency_mission_match
.map(normalizeScore)
.filter((agency) => (agency.confidence_score >= CONFIDENCE_THRESHOLD_AGENCIES)),
.filter((agency) => (agency.confidence_score >= THRESHOLDS.missionMatch)),
);

// Match from finder if above threshold.
recommendedAgencies.push(
...data.model_output.agency_finder_predictions[0]
...modelOutput.agency_finder_predictions[0]
.map(normalizeScore)
.filter((agency) => (agency.confidence_score >= CONFIDENCE_THRESHOLD_AGENCIES)),
.filter((agency) => (agency.confidence_score >= THRESHOLDS.agencyFinder)),
);

// DESC score order
Expand All @@ -366,9 +370,9 @@ const useRawWizardStore = create((
return true;
});

recommendedLinks = data.model_output.freqdoc_predictions
recommendedLinks = modelOutput.freqdoc_predictions
.map(normalizeScore)
.filter((link) => link.confidence_score >= CONFIDENCE_THRESHOLD_LINKS);
.filter((link) => link.confidence_score >= THRESHOLDS.freqdoc);
})
.catch((err) => {
console.error(err);
Expand Down
Loading