(args: {
config: JudgeConfig;
question: LongMemEvalQuestion;
hypothesis: string;
signal?: AbortSignal;
})
| 104 | } |
| 105 | |
| 106 | export 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 | } |
no test coverage detected