Skip to content

Commit 60fce6c

Browse files
committed
update to zod 4
1 parent 267ffe2 commit 60fce6c

5 files changed

Lines changed: 936 additions & 2243 deletions

File tree

evals/package.json

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -14,7 +14,7 @@
1414
"dependencies": {
1515
"autoevals": "workspace:*",
1616
"braintrust": "^0.0.140",
17-
"zod": "^3.22.4"
17+
"zod": "^4.2.1"
1818
},
1919
"devDependencies": {
2020
"@types/node": "^20.10.5",

js/ragas.ts

Lines changed: 24 additions & 13 deletions
Original file line numberDiff line numberDiff line change
@@ -9,6 +9,14 @@ import { ListContains } from "./list";
99
import { EmbeddingSimilarity } from "./string";
1010
import { z } from "zod";
1111
import zodToJsonSchema from "zod-to-json-schema";
12+
13+
function schemaToJson(schema: z.ZodTypeAny): OpenAI.FunctionParameters {
14+
const anySchema = schema as any;
15+
if (typeof anySchema?.toJSON === "function") {
16+
return anySchema.toJSON();
17+
}
18+
return zodToJsonSchema(schema) as unknown as OpenAI.FunctionParameters;
19+
}
1220
import { makePartial, ScorerWithPartial } from "./partial";
1321

1422
type RagasArgs = {
@@ -92,7 +100,7 @@ export const ContextEntityRecall: ScorerWithPartial<
92100
function: {
93101
name: "extract_entities",
94102
description: "Extract unique entities from a given text",
95-
parameters: zodToJsonSchema(entitySchema),
103+
parameters: schemaToJson(entitySchema),
96104
},
97105
},
98106
],
@@ -172,7 +180,7 @@ export const ContextRelevancy: ScorerWithPartial<string, RagasArgs> =
172180
function: {
173181
name: "extract_sentences",
174182
description: "Extract relevant sentences from a given context",
175-
parameters: zodToJsonSchema(relevantSentencesSchema),
183+
parameters: schemaToJson(relevantSentencesSchema),
176184
},
177185
},
178186
],
@@ -271,7 +279,7 @@ export const ContextRecall: ScorerWithPartial<string, RagasArgs> = makePartial(
271279
type: "function",
272280
function: {
273281
name: "extract_statements",
274-
parameters: zodToJsonSchema(contextRecallSchema),
282+
parameters: schemaToJson(contextRecallSchema),
275283
},
276284
},
277285
],
@@ -373,7 +381,7 @@ export const ContextPrecision: ScorerWithPartial<string, RagasArgs> =
373381
name: "verify",
374382
description:
375383
"Verify if context was useful in arriving at the answer",
376-
parameters: zodToJsonSchema(contextPrecisionSchema),
384+
parameters: schemaToJson(contextPrecisionSchema),
377385
},
378386
},
379387
],
@@ -499,7 +507,7 @@ export const Faithfulness: ScorerWithPartial<string, RagasArgs> = makePartial(
499507
function: {
500508
name: "extract_statements",
501509
description: "Extract statements from an answer given a question",
502-
parameters: zodToJsonSchema(extractedStatementsSchema),
510+
parameters: schemaToJson(extractedStatementsSchema),
503511
},
504512
},
505513
],
@@ -531,7 +539,7 @@ export const Faithfulness: ScorerWithPartial<string, RagasArgs> = makePartial(
531539
name: "judge_statements",
532540
description:
533541
"Judge whether the statements are faithful to the context",
534-
parameters: zodToJsonSchema(statementFaithfulnessSchema),
542+
parameters: schemaToJson(statementFaithfulnessSchema),
535543
},
536544
},
537545
],
@@ -623,16 +631,17 @@ export const AnswerRelevancy: ScorerWithPartial<
623631

624632
const responses = await Promise.all(
625633
Array.from({ length: strictness }, () =>
626-
client.chat.completions.create({
634+
client.chat.completions.create(
635+
{
627636
...chatArgs,
628637
messages: [
629638
{
630639
role: "user",
631640
content: mustache.render(QUESTION_GEN_PROMPT, {
632641
answer: output,
633642
context,
634-
}),
635-
},
643+
} as OpenAI.Chat.Completions.ChatCompletionCreateParamsNonStreaming,
644+
),
636645
],
637646
tools: [
638647
{
@@ -641,15 +650,15 @@ export const AnswerRelevancy: ScorerWithPartial<
641650
name: "generate_question",
642651
description:
643652
"Generate a question for the given answer and identify if the answer is noncommittal",
644-
parameters: zodToJsonSchema(questionGenSchema),
653+
parameters: schemaToJson(questionGenSchema),
645654
},
646655
},
647656
],
648657
tool_choice: {
649658
type: "function",
650659
function: { name: "generate_question" },
651-
},
652-
}),
660+
} as OpenAI.Chat.Completions.ChatCompletionCreateParamsNonStreaming,
661+
),
653662
),
654663
);
655664

@@ -815,7 +824,9 @@ export const AnswerCorrectness: ScorerWithPartial<
815824
function: {
816825
name: "classify_statements",
817826
description: "Classify statements as TP, FP, or FN",
818-
parameters: zodToJsonSchema(answerCorrectnessClassificationSchema),
827+
parameters: zodToJsonSchema(
828+
answerCorrectnessClassificationSchema as any,
829+
),
819830
},
820831
},
821832
],

js/templates.ts

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -13,7 +13,7 @@ import translation from "../templates/translation.yaml";
1313

1414
export const modelGradedSpecSchema = z.object({
1515
prompt: z.string(),
16-
choice_scores: z.record(z.number()),
16+
choice_scores: z.record(z.string(), z.number()),
1717
model: z.string().optional(),
1818
use_cot: z.boolean().optional(),
1919
temperature: z.number().optional(),

package.json

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -54,8 +54,8 @@
5454
"linear-sum-assignment": "^1.0.7",
5555
"mustache": "^4.2.0",
5656
"openai": "^6.3.0",
57-
"zod": "^3.25.76",
58-
"zod-to-json-schema": "^3.24.6"
57+
"zod": "^4.2.1",
58+
"zod-to-json-schema": "3.25.0"
5959
},
6060
"packageManager": "pnpm@8.15.5"
6161
}

0 commit comments

Comments
 (0)