Skip to content

Commit

Permalink
Add input_contains and target_contains samples filter predicates
Browse files Browse the repository at this point in the history
  • Loading branch information
andrei-apollo committed Jan 3, 2025
1 parent 5ce83a9 commit ba63da8
Show file tree
Hide file tree
Showing 2 changed files with 35 additions and 3 deletions.
15 changes: 14 additions & 1 deletion src/inspect_ai/_view/www/dist/assets/index.js
Original file line number Diff line number Diff line change
Expand Up @@ -31954,7 +31954,20 @@ Categories: ${descriptor.categories.map((cat) => cat.val).join(", ")}`;
};
const filterExpression = (evalDescriptor, sample, value) => {
try {
const expression = compileExpression(value);
const inputContains = (regex2) => {
return inputString(sample.input).some(
(msg) => msg.match(new RegExp(regex2, "i"))
);
};
const targetContains = (regex2) => {
let targets = Array.isArray(sample.target) ? sample.target : [sample.target];
return targets.some((target) => target.match(new RegExp(regex2, "i")));
};
const extraFunctions = {
input_contains: inputContains,
target_contains: targetContains
};
const expression = compileExpression(value, { extraFunctions });
const vars = scoreVariables(evalDescriptor, sample.scores);
const result = expression(vars);
if (typeof result === "boolean") {
Expand Down
23 changes: 21 additions & 2 deletions src/inspect_ai/_view/www/src/samples/tools/filters.mjs
Original file line number Diff line number Diff line change
Expand Up @@ -5,6 +5,7 @@ import {
kScoreTypeNumeric,
kScoreTypePassFail,
} from "../../constants.mjs";
import { inputString } from "../../utils/Format.mjs";

/**
* Coerces a value to the type expected by the score.
Expand Down Expand Up @@ -197,7 +198,7 @@ export const addFragmentToFilter = (filter, fragment) => {

/**
* TODO: Add case-insensitive string comparison.
* TODO: Support filtering by things other than scores: metadata, transcript text, etc.
* TODO: Pass EvalSample instead of SampleSummary and allow full-text message search.
*
* @param {import("../../samples/SamplesDescriptor.mjs").EvalDescriptor} evalDescriptor
* @param {import("../../api/Types.mjs").SampleSummary} sample
Expand All @@ -206,7 +207,25 @@ export const addFragmentToFilter = (filter, fragment) => {
*/
export const filterExpression = (evalDescriptor, sample, value) => {
try {
const expression = compileExpression(value);
/** @type {(regex: string) => boolean} */
const inputContains = (regex) => {
return inputString(sample.input).some((msg) =>
msg.match(new RegExp(regex, "i")),
);
};
/** @type {(regex: string) => boolean} */
const targetContains = (regex) => {
let targets = Array.isArray(sample.target)
? sample.target
: [sample.target];
return targets.some((target) => target.match(new RegExp(regex, "i")));
};

const extraFunctions = {
input_contains: inputContains,
target_contains: targetContains,
};
const expression = compileExpression(value, { extraFunctions });
const vars = scoreVariables(evalDescriptor, sample.scores);
const result = expression(vars);
if (typeof result === "boolean") {
Expand Down

0 comments on commit ba63da8

Please sign in to comment.