|
| 1 | +import { v } from "convex/values"; |
| 2 | +import { internalAction, internalMutation, query } from "./_generated/server"; |
| 3 | +import { internal } from "./_generated/api"; |
| 4 | +import { buildMatchingMap, type AAModel } from "./lib/model_matching"; |
| 5 | + |
| 6 | +type AAModelsResponse = { |
| 7 | + data?: AAModel[]; |
| 8 | +}; |
| 9 | + |
| 10 | +type OpenRouterModel = { |
| 11 | + id: string; |
| 12 | +}; |
| 13 | + |
| 14 | +type OpenRouterModelsResponse = { |
| 15 | + data?: OpenRouterModel[]; |
| 16 | +}; |
| 17 | + |
| 18 | +const benchmarkValidator = v.object({ |
| 19 | + openRouterModelId: v.string(), |
| 20 | + aaSlug: v.string(), |
| 21 | + aaCreatorName: v.string(), |
| 22 | + intelligenceIndex: v.optional(v.float64()), |
| 23 | + codingIndex: v.optional(v.float64()), |
| 24 | + mathIndex: v.optional(v.float64()), |
| 25 | + mmluPro: v.optional(v.float64()), |
| 26 | + gpqa: v.optional(v.float64()), |
| 27 | + scicode: v.optional(v.float64()), |
| 28 | + livecodebench: v.optional(v.float64()), |
| 29 | + math500: v.optional(v.float64()), |
| 30 | + aime: v.optional(v.float64()), |
| 31 | +}); |
| 32 | + |
| 33 | +export const fetchAndStoreBenchmarks = internalAction({ |
| 34 | + args: {}, |
| 35 | + handler: async (ctx) => { |
| 36 | + const apiKey = process.env.ARTIFICIAL_ANALYSIS_API_KEY; |
| 37 | + if (!apiKey) { |
| 38 | + console.warn("ARTIFICIAL_ANALYSIS_API_KEY is not set; skipping benchmark refresh"); |
| 39 | + return; |
| 40 | + } |
| 41 | + |
| 42 | + try { |
| 43 | + const openRouterResponse = await fetch("https://openrouter.ai/api/v1/models"); |
| 44 | + if (!openRouterResponse.ok) { |
| 45 | + throw new Error(`Failed to fetch OpenRouter models: ${openRouterResponse.status}`); |
| 46 | + } |
| 47 | + const openRouterPayload = (await openRouterResponse.json()) as OpenRouterModelsResponse; |
| 48 | + const openRouterIds = (openRouterPayload.data ?? []).map((m) => m.id); |
| 49 | + console.log(`Fetched ${openRouterIds.length} OpenRouter model IDs`); |
| 50 | + |
| 51 | + const response = await fetch("https://artificialanalysis.ai/api/v2/data/llms/models", { |
| 52 | + headers: { |
| 53 | + "x-api-key": apiKey, |
| 54 | + }, |
| 55 | + }); |
| 56 | + |
| 57 | + if (!response.ok) { |
| 58 | + throw new Error(`Failed to fetch Artificial Analysis models: ${response.status}`); |
| 59 | + } |
| 60 | + |
| 61 | + const payload = (await response.json()) as AAModelsResponse; |
| 62 | + const aaModels = Array.isArray(payload.data) ? payload.data : []; |
| 63 | + const matchingMap = buildMatchingMap(aaModels, openRouterIds); |
| 64 | + |
| 65 | + const benchmarks = aaModels.flatMap((model) => { |
| 66 | + const openRouterModelId = matchingMap.get(model.slug); |
| 67 | + if (!openRouterModelId) return []; |
| 68 | + |
| 69 | + const evaluations = model.evaluations ?? {}; |
| 70 | + |
| 71 | + return [{ |
| 72 | + openRouterModelId, |
| 73 | + aaSlug: model.slug, |
| 74 | + aaCreatorName: model.model_creator.name, |
| 75 | + intelligenceIndex: evaluations.artificial_analysis_intelligence_index ?? undefined, |
| 76 | + codingIndex: evaluations.artificial_analysis_coding_index ?? undefined, |
| 77 | + mathIndex: evaluations.artificial_analysis_math_index ?? undefined, |
| 78 | + mmluPro: evaluations.mmlu_pro ?? undefined, |
| 79 | + gpqa: evaluations.gpqa ?? undefined, |
| 80 | + scicode: evaluations.scicode ?? undefined, |
| 81 | + livecodebench: evaluations.livecodebench ?? undefined, |
| 82 | + math500: evaluations.math_500 ?? undefined, |
| 83 | + aime: evaluations.aime ?? undefined, |
| 84 | + }]; |
| 85 | + }); |
| 86 | + |
| 87 | + await ctx.runMutation((internal as any).benchmarks.storeBenchmarks, { benchmarks }); |
| 88 | + } catch (error) { |
| 89 | + console.error("Failed to refresh Artificial Analysis benchmarks", error); |
| 90 | + } |
| 91 | + }, |
| 92 | +}); |
| 93 | + |
| 94 | +export const storeBenchmarks = internalMutation({ |
| 95 | + args: { |
| 96 | + benchmarks: v.array(benchmarkValidator), |
| 97 | + }, |
| 98 | + handler: async (ctx, args) => { |
| 99 | + const lastUpdated = Date.now(); |
| 100 | + |
| 101 | + for (const benchmark of args.benchmarks) { |
| 102 | + const existing = await ctx.db |
| 103 | + .query("benchmarks") |
| 104 | + .withIndex("by_openrouter_id", (q) => q.eq("openRouterModelId", benchmark.openRouterModelId)) |
| 105 | + .first(); |
| 106 | + |
| 107 | + if (existing) { |
| 108 | + await ctx.db.patch(existing._id, { |
| 109 | + ...benchmark, |
| 110 | + lastUpdated, |
| 111 | + }); |
| 112 | + continue; |
| 113 | + } |
| 114 | + |
| 115 | + await ctx.db.insert("benchmarks", { |
| 116 | + ...benchmark, |
| 117 | + lastUpdated, |
| 118 | + }); |
| 119 | + } |
| 120 | + }, |
| 121 | +}); |
| 122 | + |
| 123 | +export const getBenchmarkByOpenRouterId = query({ |
| 124 | + args: { |
| 125 | + openRouterModelId: v.string(), |
| 126 | + }, |
| 127 | + handler: async (ctx, args) => { |
| 128 | + return await ctx.db |
| 129 | + .query("benchmarks") |
| 130 | + .withIndex("by_openrouter_id", (q) => q.eq("openRouterModelId", args.openRouterModelId)) |
| 131 | + .first(); |
| 132 | + }, |
| 133 | +}); |
| 134 | + |
| 135 | +export const getAllBenchmarks = query({ |
| 136 | + args: {}, |
| 137 | + handler: async (ctx) => { |
| 138 | + return await ctx.db.query("benchmarks").collect(); |
| 139 | + }, |
| 140 | +}); |
| 141 | + |
| 142 | + |
0 commit comments