Skip to content
Source
Sign inSign up
TanStack/ai
Open History to see the last commit

judge.ts

70 lines2.5 KB

1import { z } from 'zod'
2import { anthropicText } from '@tanstack/ai-anthropic'
3import { chat } from '@tanstack/ai'
4​
5export const judgeSchema = z.object({
6 accuracy: z.number().min(1).max(10).describe('Accuracy rating from 1-10'),
7 comprehensiveness: z
8 .number()
9 .min(1)
10 .max(10)
11 .describe('How completely the response answers the user query (1-10)'),
12 typescriptQuality: z
13 .number()
14 .min(1)
15 .max(10)
16 .describe('TypeScript quality rating from 1-10'),
17 codeModeEfficiency: z
18 .number()
19 .min(1)
20 .max(10)
21 .describe('Code-mode efficiency rating from 1-10'),
22 summary: z.string().describe('Two sentence summary of the evaluation'),
23})
24​
25export type JudgeResult = z.infer<typeof judgeSchema>
26​
27const JUDGE_SYSTEM_PROMPT = `You are a strict evaluator comparing an AI-generated data analysis report against a gold-standard reference report.
28​
29You will receive:
301. The original user query
312. The gold-standard report (the ideal answer)
323. The candidate report to evaluate
33​
34Rate the candidate on four dimensions, each on a scale from 1 to 10 (10 = best):
35​
36- **Accuracy**: Are the numbers, rankings, and factual claims correct compared to the gold standard?
37- **Comprehensiveness**: Does the candidate fully answer the user query and include all requested outputs? Use the query as the primary rubric. Use the gold report only as optional context, not as a strict checklist.
38- **TypeScript Quality**: Is the generated TypeScript clear, type-safe, and idiomatic? Penalize brittle code, unnecessary \`any\`, and confusing structure.
39- **Code-Mode Efficiency**: Did the model accomplish the task in a reasonable number of steps without unnecessary tool calls or retries?
40​
41Provide a two sentence summary of your evaluation.`
42​
43/**
44 * Compare a candidate assistant report to the gold-standard report using Claude Opus (structured output).
45 */
46export async function judgeReports(params: {
47 query: string
48 goldReport: string
49 candidateReport: string
50 typescriptEvidence: string
51}): Promise<JudgeResult> {
52 const { query, goldReport, candidateReport, typescriptEvidence } = params
53​
54 const adapter = anthropicText('claude-opus-4-6')
55​
56 const result = await chat({
57 adapter,
58 messages: [
59 {
60 role: 'user' as const,
61 content: `## Query\n${query}\n\n## Gold-Standard Report\n${goldReport}\n\n## Candidate Report\n${candidateReport}\n\n## TypeScript Evidence\n${typescriptEvidence}`,
62 },
63 ],
64 systemPrompts: [JUDGE_SYSTEM_PROMPT],
65 outputSchema: judgeSchema,
66 modelOptions: { max_tokens: 512 },
67 })
68​
69 return result as JudgeResult