MCPcopy Create free account
hub / github.com/cortexkit/magic-context / judgeAnswer

Function judgeAnswer

packages/plugin/scripts/longmemeval/judge.ts:106–148  ·  view source on GitHub ↗
(args: {
    config: JudgeConfig;
    question: LongMemEvalQuestion;
    hypothesis: string;
    signal?: AbortSignal;
})

Source from the content-addressed store, hash-verified

104}
105
106export async function judgeAnswer(args: {
107 config: JudgeConfig;
108 question: LongMemEvalQuestion;
109 hypothesis: string;
110 signal?: AbortSignal;
111}): Promise<JudgeResult> {
112 const prompt = buildJudgePrompt(args.question, args.hypothesis);
113 const response = await fetch(`${args.config.apiBaseUrl.replace(/\/$/, "")}/chat/completions`, {
114 method: "POST",
115 headers: {
116 "content-type": "application/json",
117 authorization: `Bearer ${getApiKey(args.config)}`,
118 },
119 body: JSON.stringify({
120 model: args.config.model,
121 messages: [{ role: "user", content: prompt }],
122 n: 1,
123 temperature: args.config.temperature,
124 max_tokens: args.config.maxTokens,
125 }),
126 signal: args.signal,
127 });
128
129 if (!response.ok) {
130 const errorText = await response.text();
131 throw new Error(`Judge request failed (${response.status}): ${errorText}`);
132 }
133
134 const payload = (await response.json()) as OpenAIChatCompletionResponse;
135 const rawResponse = extractResponseText(payload);
136 const label = rawResponse.toLowerCase().includes("yes");
137 const usage = payload.usage ? extractUsage(payload) : createEmptyTokenUsageStats();
138 const estimatedCostUsd = estimateCostFromPricing(usage, args.config.pricing);
139
140 return {
141 model: args.config.model,
142 prompt,
143 rawResponse,
144 label,
145 usage,
146 estimatedCostUsd,
147 };
148}

Callers 1

judgeQuestionFunction · 0.90

Calls 7

estimateCostFromPricingFunction · 0.90
buildJudgePromptFunction · 0.85
getApiKeyFunction · 0.85
extractResponseTextFunction · 0.85
extractUsageFunction · 0.85
textMethod · 0.65

Tested by

no test coverage detected