@@ -9,6 +9,14 @@ import { ListContains } from "./list";
99import { EmbeddingSimilarity } from "./string" ;
1010import { z } from "zod" ;
1111import 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+ }
1220import { makePartial , ScorerWithPartial } from "./partial" ;
1321
1422type 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 ] ,
0 commit comments