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 base assertions to js sdk #200

Merged
merged 2 commits into from
Aug 29, 2024
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
2 changes: 2 additions & 0 deletions src/testing/api.ts
Original file line number Diff line number Diff line change
Expand Up @@ -339,6 +339,7 @@ export async function sendEvaluation(args: {
score: args.evaluation.score,
threshold: args.evaluation.threshold,
metadata: args.evaluation.metadata,
assertions: args.evaluation.assertions,
},
});
return;
Expand All @@ -351,6 +352,7 @@ export async function sendEvaluation(args: {
passed: determineIfEvaluationPassed({ evaluation: args.evaluation }),
threshold: args.evaluation.threshold,
metadata: args.evaluation.metadata,
assertions: args.evaluation.assertions,
},
});
}
Expand Down
42 changes: 42 additions & 0 deletions src/testing/evaluators/assertions.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,42 @@
import { type Assertion, BaseTestEvaluator } from '../models';

/**
* Base evaluator for creating an assertions evaluator.
*/
export abstract class BaseAssertions<
TestCaseType,
OutputType,
> extends BaseTestEvaluator<TestCaseType, OutputType> {
/**
* Implement your assertion logic and return an array of assertions.
* Return undefined if you want to skip evaluation for this test case.
*/
abstract evaluateAssertions(args: {
testCase: TestCaseType;
output: OutputType;
}): Assertion[] | undefined | Promise<Assertion[] | undefined>;

async evaluateTestCase(args: { testCase: TestCaseType; output: OutputType }) {
const assertionsResult = await this.evaluateAssertions({
testCase: args.testCase,
output: args.output,
});

if (assertionsResult === undefined || assertionsResult.length === 0) {
return undefined;
}

// Passes if all required assertions pass
const passed = assertionsResult
.filter((assertion) => assertion.required)
.every((assertion) => assertion.passed);

return {
score: passed ? 1 : 0,
threshold: {
gte: 1,
},
assertions: assertionsResult,
};
}
}
2 changes: 2 additions & 0 deletions src/testing/index.ts
Original file line number Diff line number Diff line change
Expand Up @@ -3,6 +3,7 @@ export {
BaseTestEvaluator,
BaseEventEvaluator,
BaseEvaluator,
type Assertion,
type Threshold,
type Evaluation,
type TracerEvent,
Expand Down Expand Up @@ -30,3 +31,4 @@ export { BaseLLMJudge } from './evaluators/llm-judge';
export { BaseNSFW } from './evaluators/nsfw';
export { BaseToxicity } from './evaluators/toxicity';
export { BaseAccuracy } from './evaluators/accuracy';
export { BaseAssertions } from './evaluators/assertions';
8 changes: 8 additions & 0 deletions src/testing/models.ts
Original file line number Diff line number Diff line change
Expand Up @@ -7,10 +7,18 @@ export interface Threshold {
gte?: number;
}

export interface Assertion {
criterion: string;
passed: boolean;
required: boolean;
metadata?: ArbitraryProperties;
}

export interface Evaluation {
score: number;
threshold?: Threshold;
metadata?: ArbitraryProperties;
assertions?: Assertion[];
}

export interface TracerEvent {
Expand Down
97 changes: 97 additions & 0 deletions test/testing/evaluators-logic.spec.ts
Original file line number Diff line number Diff line change
Expand Up @@ -3,6 +3,8 @@ import {
BaseHasAllSubstrings,
BaseIsEquals,
BaseIsValidJSON,
BaseAssertions,
Assertion,
} from '../../src/testing';
import crypto from 'crypto';
import { isMatch } from 'lodash';
Expand Down Expand Up @@ -221,4 +223,99 @@ describe('OOB Evaluators', () => {
},
});
});

it('BaseAssertions', async () => {
interface TestCase {
input: string;
assertions: {
criterion: string;
required: boolean;
}[];
}

class AssertionsEvaluator extends BaseAssertions<TestCase, string> {
id = 'assertion';

evaluateAssertions(args: {
testCase: TestCase;
output: string;
}): Assertion[] | undefined | Promise<Assertion[] | undefined> {
return args.testCase.assertions.map((assertion) => ({
criterion: assertion.criterion,
passed: args.output.includes(assertion.criterion),
required: assertion.required,
}));
}
}
await runTestSuite<TestCase, string>({
id: 'my-test-id',
testCases: [
{
input: 'hello world',
assertions: [
{
criterion: 'hello',
required: true,
},
{
criterion: 'hi',
required: false,
},
],
},
{
input: 'hi world',
assertions: [
{
criterion: 'hello',
required: true,
},
],
},
],
testCaseHash: (testCase) => md5(testCase.input),
evaluators: [new AssertionsEvaluator()],
fn: ({ testCase }: { testCase: TestCase }) => testCase.input,
});

expectPostRequest({
path: '/evals',
body: {
testExternalId: 'my-test-id',
testCaseHash: md5('hello world'),
evaluatorExternalId: 'assertion',
score: 1,
threshold: { gte: 1 },
assertions: [
{
criterion: 'hello',
passed: true,
required: true,
},
{
criterion: 'hi',
passed: false,
required: false,
},
],
},
});
expectPostRequest({
path: '/evals',
body: {
testExternalId: 'my-test-id',
testCaseHash: md5('hi world'),
evaluatorExternalId: 'assertion',
score: 0,
threshold: { gte: 1 },
assertions: [
{
criterion: 'hello',
passed: false,
required: true,
},
],
},
});
});
});