Skip to content

Commit 4d528b9

Browse files
committed
Use OpenAI's zodFunction helper instead of native toJSONSchema
Use openAIs zodFunction helper to properly convert zod schemas to json schemas
1 parent eb2ae2d commit 4d528b9

1 file changed

Lines changed: 42 additions & 71 deletions

File tree

js/ragas.ts

Lines changed: 42 additions & 71 deletions
Original file line numberDiff line numberDiff line change
@@ -5,14 +5,10 @@ import { Scorer, ScorerArgs } from "./score";
55
import { DEFAULT_MODEL, LLMArgs } from "./llm";
66
import { buildOpenAIClient, extractOpenAIArgs } from "./oai";
77
import OpenAI from "openai";
8+
import { zodFunction } from "openai/helpers/zod";
89
import { ListContains } from "./list";
910
import { EmbeddingSimilarity } from "./string";
1011
import { z } from "zod";
11-
12-
function schemaToJson(schema: z.ZodType): OpenAI.FunctionParameters {
13-
// eslint-disable-next-line @typescript-eslint/no-explicit-any
14-
return (schema as any).toJSONSchema() as unknown as OpenAI.FunctionParameters;
15-
}
1612
import { makePartial, ScorerWithPartial } from "./partial";
1713

1814
type RagasArgs = {
@@ -91,14 +87,11 @@ export const ContextEntityRecall: ScorerWithPartial<
9187
},
9288
],
9389
tools: [
94-
{
95-
type: "function",
96-
function: {
97-
name: "extract_entities",
98-
description: "Extract unique entities from a given text",
99-
parameters: schemaToJson(entitySchema),
100-
},
101-
},
90+
zodFunction({
91+
name: "extract_entities",
92+
description: "Extract unique entities from a given text",
93+
parameters: entitySchema,
94+
}),
10295
],
10396
tool_choice: { type: "function", function: { name: "extract_entities" } },
10497
});
@@ -171,14 +164,11 @@ export const ContextRelevancy: ScorerWithPartial<string, RagasArgs> =
171164
},
172165
],
173166
tools: [
174-
{
175-
type: "function",
176-
function: {
177-
name: "extract_sentences",
178-
description: "Extract relevant sentences from a given context",
179-
parameters: schemaToJson(relevantSentencesSchema),
180-
},
181-
},
167+
zodFunction({
168+
name: "extract_sentences",
169+
description: "Extract relevant sentences from a given context",
170+
parameters: relevantSentencesSchema,
171+
}),
182172
],
183173
tool_choice: {
184174
type: "function",
@@ -271,13 +261,10 @@ export const ContextRecall: ScorerWithPartial<string, RagasArgs> = makePartial(
271261
},
272262
],
273263
tools: [
274-
{
275-
type: "function",
276-
function: {
277-
name: "extract_statements",
278-
parameters: schemaToJson(contextRecallSchema),
279-
},
280-
},
264+
zodFunction({
265+
name: "extract_statements",
266+
parameters: contextRecallSchema,
267+
}),
281268
],
282269
tool_choice: {
283270
type: "function",
@@ -371,15 +358,11 @@ export const ContextPrecision: ScorerWithPartial<string, RagasArgs> =
371358
},
372359
],
373360
tools: [
374-
{
375-
type: "function",
376-
function: {
377-
name: "verify",
378-
description:
379-
"Verify if context was useful in arriving at the answer",
380-
parameters: schemaToJson(contextPrecisionSchema),
381-
},
382-
},
361+
zodFunction({
362+
name: "verify",
363+
description: "Verify if context was useful in arriving at the answer",
364+
parameters: contextPrecisionSchema,
365+
}),
383366
],
384367
tool_choice: { type: "function", function: { name: "verify" } },
385368
});
@@ -498,14 +481,11 @@ export const Faithfulness: ScorerWithPartial<string, RagasArgs> = makePartial(
498481
},
499482
],
500483
tools: [
501-
{
502-
type: "function",
503-
function: {
504-
name: "extract_statements",
505-
description: "Extract statements from an answer given a question",
506-
parameters: schemaToJson(extractedStatementsSchema),
507-
},
508-
},
484+
zodFunction({
485+
name: "extract_statements",
486+
description: "Extract statements from an answer given a question",
487+
parameters: extractedStatementsSchema,
488+
}),
509489
],
510490
tool_choice: {
511491
type: "function",
@@ -529,15 +509,12 @@ export const Faithfulness: ScorerWithPartial<string, RagasArgs> = makePartial(
529509
},
530510
],
531511
tools: [
532-
{
533-
type: "function",
534-
function: {
535-
name: "judge_statements",
536-
description:
537-
"Judge whether the statements are faithful to the context",
538-
parameters: schemaToJson(statementFaithfulnessSchema),
539-
},
540-
},
512+
zodFunction({
513+
name: "judge_statements",
514+
description:
515+
"Judge whether the statements are faithful to the context",
516+
parameters: statementFaithfulnessSchema,
517+
}),
541518
],
542519
tool_choice: { type: "function", function: { name: "judge_statements" } },
543520
});
@@ -639,15 +616,12 @@ export const AnswerRelevancy: ScorerWithPartial<
639616
},
640617
],
641618
tools: [
642-
{
643-
type: "function",
644-
function: {
645-
name: "generate_question",
646-
description:
647-
"Generate a question for the given answer and identify if the answer is noncommittal",
648-
parameters: schemaToJson(questionGenSchema),
649-
},
650-
},
619+
zodFunction({
620+
name: "generate_question",
621+
description:
622+
"Generate a question for the given answer and identify if the answer is noncommittal",
623+
parameters: questionGenSchema,
624+
}),
651625
],
652626
tool_choice: {
653627
type: "function",
@@ -814,14 +788,11 @@ export const AnswerCorrectness: ScorerWithPartial<
814788
},
815789
],
816790
tools: [
817-
{
818-
type: "function",
819-
function: {
820-
name: "classify_statements",
821-
description: "Classify statements as TP, FP, or FN",
822-
parameters: schemaToJson(answerCorrectnessClassificationSchema),
823-
},
824-
},
791+
zodFunction({
792+
name: "classify_statements",
793+
description: "Classify statements as TP, FP, or FN",
794+
parameters: answerCorrectnessClassificationSchema,
795+
}),
825796
],
826797
tool_choice: {
827798
type: "function",

0 commit comments

Comments
 (0)