Skip to content

Commit

Permalink
fix: js script scorer interface
Browse files Browse the repository at this point in the history
  • Loading branch information
saikatmitra91 committed May 21, 2024
1 parent 7f56d50 commit b0b5f23
Show file tree
Hide file tree
Showing 4 changed files with 42 additions and 20 deletions.
26 changes: 26 additions & 0 deletions packages/scorer/src/provider/deterministic/js-script.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,26 @@
import {
DatasetSample,
JSScriptScorer,
RunOutput,
Score,
} from "@empiricalrun/types";

export default async function scoreWithJSScript({
output,
config,
sample,
}: {
output: RunOutput;
config: JSScriptScorer;
sample: DatasetSample;
}): Promise<Score[]> {
let score = config({ output, config, sample });
if ("then" in score) {
score = await score;
}
if (!Array.isArray(score)) {
return [score];
} else {
return score;
}
}
16 changes: 0 additions & 16 deletions packages/scorer/src/provider/deterministic/sql.test.ts
Original file line number Diff line number Diff line change
Expand Up @@ -4,13 +4,9 @@ import { checkSqlSyntax } from "./sql";
test("sql-syntax works with backticks", async () => {
expect(
await checkSqlSyntax({
sample: { id: "1", inputs: {} },
output: {
value: "SELECT `column` FROM `table`",
},
config: {
type: "sql-syntax",
},
}),
).toStrictEqual([
{
Expand All @@ -24,13 +20,9 @@ test("sql-syntax works with backticks", async () => {
test("sql-syntax fails with empty output", async () => {
expect(
await checkSqlSyntax({
sample: { id: "1", inputs: {} },
output: {
value: "",
},
config: {
type: "sql-syntax",
},
}),
).toStrictEqual([
{
Expand All @@ -44,11 +36,7 @@ test("sql-syntax fails with empty output", async () => {
test("sql-syntax works with markdown", async () => {
expect(
await checkSqlSyntax({
sample: { id: "1", inputs: {} },
output: { value: "```SELECT table * FROM" },
config: {
type: "sql-syntax",
},
}),
).toStrictEqual([
{
Expand All @@ -73,13 +61,9 @@ WHERE stadium_id = (
);`;
expect(
await checkSqlSyntax({
sample: { id: "1", inputs: {} },
output: {
value: query,
},
config: {
type: "sql-syntax",
},
}),
).toStrictEqual([
{
Expand Down
8 changes: 8 additions & 0 deletions packages/scorer/src/provider/index.ts
Original file line number Diff line number Diff line change
Expand Up @@ -4,6 +4,7 @@ import { scoreWithPythonScript } from "./deterministic/py-script";
import { checkSqlSyntax } from "./deterministic/sql";
import { checkLlmCriteria } from "./model-graded/llm";
import { ScorerError, ScorerErrorEnum } from "../error";
import scoreWithJSScript from "./deterministic/js-script";

function buildErrorMessage(config: Scorer): string {
let recommendation: string =
Expand Down Expand Up @@ -46,6 +47,13 @@ export default async function scoreUsingConfig(
return checkSqlSyntax({ output });
}
}
if (typeof config === "function") {
return scoreWithJSScript({
sample,
output,
config

Check failure on line 54 in packages/scorer/src/provider/index.ts

View workflow job for this annotation

GitHub Actions / Build and Test

Insert `,`
})

Check failure on line 55 in packages/scorer/src/provider/index.ts

View workflow job for this annotation

GitHub Actions / Build and Test

Insert `;`
}
throw new ScorerError(
ScorerErrorEnum.INCORRECT_PARAMETERS,
buildErrorMessage(config),
Expand Down
12 changes: 8 additions & 4 deletions packages/types/src/index.ts
Original file line number Diff line number Diff line change
Expand Up @@ -46,11 +46,15 @@ export interface ScoringInputBase {
options?: any;
}

export interface ScoringFn {
(args: ScoringInputBase): Promise<Score[]>;
export interface AsyncScoringFn {
(args: ScoringInputBase): Promise<Score[] | Score>;
}

export interface JSScriptScorer extends ScoringFn {}
export interface SyncScoringFn {
(args: ScoringInputBase): Score[] | Score;
}

export type JSScriptScorer = SyncScoringFn | AsyncScoringFn;

export type Scorer = LLMScorer | SyntaxScorer | ScriptScorer | JSScriptScorer;

Expand Down Expand Up @@ -236,7 +240,7 @@ export type RunSampleOutput = {
export type Score = {
score: number;
name: string;
message: string;
message?: string;
};

export interface RunMetadataUpdate {
Expand Down

0 comments on commit b0b5f23

Please sign in to comment.