|
| 1 | +// Copyright 2025 The Chromium Authors |
| 2 | +// Use of this source code is governed by a BSD-style license that can be |
| 3 | +// found in the LICENSE file. |
| 4 | + |
| 5 | +import assert from 'node:assert'; |
| 6 | + |
| 7 | +import {loadInstructions} from '../instructions/load.ts'; |
| 8 | +import type {Conversation} from '../types'; |
| 9 | + |
| 10 | +import {generateGeminiContent} from './gemini.ts'; |
| 11 | +import {getMarkdownConversation, getOutputs, type Output} from './outputs.ts'; |
| 12 | + |
| 13 | +abstract class Evaluator {} |
| 14 | + |
| 15 | +export class FunctionCalled extends Evaluator { |
| 16 | + static nameOnly(example: Conversation, funcName: string): boolean { |
| 17 | + return example.queries.some(q => { |
| 18 | + return q.response.functionCallRequests?.some(call => call.name === funcName); |
| 19 | + }); |
| 20 | + } |
| 21 | +} |
| 22 | + |
| 23 | +export class LLMComparison extends Evaluator { |
| 24 | + static async judge(example: Conversation, prompt: string): Promise<{score: number, reasons: string}> { |
| 25 | + const scoringInstructions = loadInstructions('scoring'); |
| 26 | + const exampleAsMarkdown = getMarkdownConversation(example); |
| 27 | + const response = await generateGeminiContent( |
| 28 | + `${scoringInstructions} |
| 29 | +
|
| 30 | + ${prompt}. |
| 31 | +
|
| 32 | +## Conversation to score: |
| 33 | +${exampleAsMarkdown}`, |
| 34 | + 'gemini-2.5-flash', { |
| 35 | + type: 'object', |
| 36 | + properties: { |
| 37 | + score: {type: 'number', description: 'A numerical score assigned by the AI.'}, |
| 38 | + reasons: {type: 'string', description: 'A string containing the reasons for the assigned score.'} |
| 39 | + }, |
| 40 | + required: ['score', 'reasons'] |
| 41 | + }); |
| 42 | + const r = JSON.parse(response) as {score: number, reasons: string}; |
| 43 | + return {score: r.score, reasons: r.reasons}; |
| 44 | + } |
| 45 | +} |
| 46 | + |
| 47 | +interface GroupTestState { |
| 48 | + store: ResultStore; |
| 49 | + outputsByDate: Partial<Record<string, Output[]>>; |
| 50 | +} |
| 51 | + |
| 52 | +let state: GroupTestState|null = null; |
| 53 | + |
| 54 | +export type ItEval = { |
| 55 | + test: string, |
| 56 | +}&({ |
| 57 | + succeed: (example: Conversation) => boolean, |
| 58 | +}|{ |
| 59 | + judge: (example: Conversation) => Promise<{score: number, reasons: string}>, |
| 60 | +}); |
| 61 | + |
| 62 | +export async function itEval(config: ItEval): Promise<void> { |
| 63 | + assert.ok(state); |
| 64 | + if ('succeed' in config) { |
| 65 | + for (const [date, outputs] of Object.entries(state.outputsByDate)) { |
| 66 | + if (!outputs) { |
| 67 | + continue; |
| 68 | + } |
| 69 | + |
| 70 | + const allDevToolsConversations = outputs.flatMap(o => o.contents.conversations); |
| 71 | + |
| 72 | + let total = 0; |
| 73 | + let succeeded = 0; |
| 74 | + for (const conversation of allDevToolsConversations) { |
| 75 | + total++; |
| 76 | + if (config.succeed(conversation)) { |
| 77 | + succeeded++; |
| 78 | + } |
| 79 | + state.store.saveResult(config.test, date, {type: 'BINARY', success: succeeded, total}); |
| 80 | + } |
| 81 | + } |
| 82 | + } else if ('judge' in config) { |
| 83 | + for (const [date, outputs] of Object.entries(state.outputsByDate)) { |
| 84 | + if (!outputs) { |
| 85 | + continue; |
| 86 | + } |
| 87 | + const allDevToolsConversations = outputs.flatMap(o => o.contents.conversations); |
| 88 | + const scores = await Promise.all(allDevToolsConversations.map(async example => { |
| 89 | + const result = await config.judge(example); |
| 90 | + return result.score; |
| 91 | + })); |
| 92 | + const totalOfAllScores = scores.reduce((acc: number, score: number) => acc + score, 0); |
| 93 | + const average = totalOfAllScores / scores.length; |
| 94 | + state.store.saveResult(config.test, date, {type: 'JUDGE', average, allScores: scores, total: totalOfAllScores}); |
| 95 | + } |
| 96 | + } |
| 97 | +} |
| 98 | + |
| 99 | +export interface GroupConfig { |
| 100 | + type: string; |
| 101 | + label: string; |
| 102 | +} |
| 103 | + |
| 104 | +export async function evalGroup(config: GroupConfig, cb: (() => Promise<void>)): Promise<void> { |
| 105 | + const store = new ResultStore(config.type, config.label); |
| 106 | + const outputs = await getOutputs(config.type, config.label); |
| 107 | + const outputsByDate = Object.groupBy(outputs, o => o.dateFolder); |
| 108 | + state = { |
| 109 | + store, |
| 110 | + outputsByDate, |
| 111 | + }; |
| 112 | + |
| 113 | + await cb(); |
| 114 | + printResults(state.store); |
| 115 | +} |
| 116 | + |
| 117 | +function log(indentation: number, message: string): void { |
| 118 | + console.log(`${' '.repeat(indentation)}${message}`); |
| 119 | +} |
| 120 | + |
| 121 | +function printResults(store: ResultStore): void { |
| 122 | + log(0, `Results for: ${store.type}/${store.label}`); |
| 123 | + |
| 124 | + // Structures the results in Date => <Test Name, Test Output>. |
| 125 | + const dataForTable: Record<string, Record<string, string>> = {}; |
| 126 | + |
| 127 | + for (const [test, dateToResult] of store.results) { |
| 128 | + for (const [date, result] of dateToResult) { |
| 129 | + dataForTable[date] ??= {}; |
| 130 | + switch (result.type) { |
| 131 | + case 'BINARY': |
| 132 | + dataForTable[date][test] = `${result.success} / ${result.total} passed`; |
| 133 | + break; |
| 134 | + case 'JUDGE': |
| 135 | + dataForTable[date][test] = `${result.average.toFixed(1)} average from ${result.allScores.length} inputs.`; |
| 136 | + break; |
| 137 | + default: |
| 138 | + throw new Error('Unknown result type!'); |
| 139 | + } |
| 140 | + } |
| 141 | + } |
| 142 | + console.table(dataForTable); |
| 143 | +} |
| 144 | + |
| 145 | +type Result = { |
| 146 | + type: 'BINARY', |
| 147 | + total: number, |
| 148 | + success: number, |
| 149 | +}|{ |
| 150 | + type: 'JUDGE', |
| 151 | + average: number, |
| 152 | + total: number, |
| 153 | + allScores: number[], |
| 154 | +}; |
| 155 | + |
| 156 | +class ResultStore { |
| 157 | + // Map of testName => YYYY-MM-DD => Result |
| 158 | + #results = new Map<string, Map<string, Result>>(); |
| 159 | + #type: string; |
| 160 | + #label: string; |
| 161 | + |
| 162 | + constructor(type: string, label: string) { |
| 163 | + this.#type = type; |
| 164 | + this.#label = label; |
| 165 | + } |
| 166 | + |
| 167 | + get type(): string { |
| 168 | + return this.#type; |
| 169 | + } |
| 170 | + get label(): string { |
| 171 | + return this.#label; |
| 172 | + } |
| 173 | + |
| 174 | + get results(): ReadonlyMap<string, ReadonlyMap<string, Result>> { |
| 175 | + return this.#results; |
| 176 | + } |
| 177 | + |
| 178 | + saveResult(testName: string, dateFolder: string, result: Result): void { |
| 179 | + const forTest = this.#results.get(testName) ?? new Map<string, Result>(); |
| 180 | + forTest.set(dateFolder, result); |
| 181 | + this.#results.set(testName, forTest); |
| 182 | + } |
| 183 | +} |
0 commit comments