aboutsummaryrefslogtreecommitdiffstats
path: root/tools/compare-models/src/index.ts
blob: 88fc9249c4e10cbb83841557cd888a6f694c3209 (plain) (blame)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
import chalk from "chalk";

import type { ComparisonResult } from "./types";
import { KarakeepAPIClient } from "./apiClient";
import { runTaggingForModel } from "./bookmarkProcessor";
import { config } from "./config";
import { createInferenceClient } from "./inferenceClient";
import {
  askQuestion,
  clearProgress,
  close,
  displayComparison,
  displayError,
  displayFinalResults,
  displayProgress,
} from "./interactive";

interface VoteCounters {
  model1Votes: number;
  model2Votes: number;
  skipped: number;
  errors: number;
  total: number;
}

interface ShuffleResult {
  modelA: string;
  modelB: string;
  modelAIsModel1: boolean;
}

async function main() {
  console.log(chalk.cyan("\n🚀 Karakeep Model Comparison Tool\n"));

  const isExistingMode = config.COMPARISON_MODE === "model-vs-existing";

  if (isExistingMode) {
    console.log(
      chalk.yellow(
        `Mode: Comparing ${config.MODEL1_NAME} against existing AI tags\n`,
      ),
    );
  } else {
    if (!config.MODEL2_NAME) {
      console.log(
        chalk.red(
          "\n✗ Error: MODEL2_NAME is required for model-vs-model comparison mode\n",
        ),
      );
      process.exit(1);
    }
    console.log(
      chalk.yellow(
        `Mode: Comparing ${config.MODEL1_NAME} vs ${config.MODEL2_NAME}\n`,
      ),
    );
  }

  const apiClient = new KarakeepAPIClient();

  displayProgress("Fetching bookmarks from Karakeep...");
  let bookmarks = await apiClient.fetchBookmarks(config.COMPARE_LIMIT);
  clearProgress();

  // Filter bookmarks with AI tags if in existing mode
  if (isExistingMode) {
    bookmarks = bookmarks.filter(
      (b) => b.tags.some((t) => t.attachedBy === "ai"),
    );
    console.log(
      chalk.green(
        `✓ Fetched ${bookmarks.length} link bookmarks with existing AI tags\n`,
      ),
    );
  } else {
    console.log(chalk.green(`✓ Fetched ${bookmarks.length} link bookmarks\n`));
  }

  if (bookmarks.length === 0) {
    console.log(
      chalk.yellow(
        "\n⚠ No bookmarks found with AI tags. Please add some bookmarks with AI tags first.\n",
      ),
    );
    return;
  }

  const counters: VoteCounters = {
    model1Votes: 0,
    model2Votes: 0,
    skipped: 0,
    errors: 0,
    total: bookmarks.length,
  };

  const detailedResults: ComparisonResult[] = [];

  for (let i = 0; i < bookmarks.length; i++) {
    const bookmark = bookmarks[i];

    displayProgress(
      `[${i + 1}/${bookmarks.length}] Running inference on: ${bookmark.title || bookmark.content.title || "Untitled"}`,
    );

    let model1Tags: string[] = [];
    let model2Tags: string[] = [];

    // Get tags for model 1 (new model)
    try {
      const model1Client = createInferenceClient(config.MODEL1_NAME);
      model1Tags = await runTaggingForModel(
        bookmark,
        model1Client,
        "english",
        config.INFERENCE_CONTEXT_LENGTH,
      );
    } catch (error) {
      clearProgress();
      displayError(
        `${config.MODEL1_NAME} failed: ${error instanceof Error ? error.message : String(error)}`,
      );
      counters.errors++;
      continue;
    }

    // Get tags for model 2 or existing AI tags
    if (isExistingMode) {
      // Use existing AI tags from the bookmark
      model2Tags = bookmark.tags
        .filter((t) => t.attachedBy === "ai")
        .map((t) => t.name);
    } else {
      // Run inference with model 2
      try {
        const model2Client = createInferenceClient(config.MODEL2_NAME!);
        model2Tags = await runTaggingForModel(
          bookmark,
          model2Client,
          "english",
          config.INFERENCE_CONTEXT_LENGTH,
        );
      } catch (error) {
        clearProgress();
        displayError(
          `${config.MODEL2_NAME} failed: ${error instanceof Error ? error.message : String(error)}`,
        );
        counters.errors++;
        continue;
      }
    }

    clearProgress();

    const model2Label = isExistingMode
      ? "Existing AI Tags"
      : config.MODEL2_NAME!;

    const shuffleResult: ShuffleResult = {
      modelA: config.MODEL1_NAME,
      modelB: model2Label,
      modelAIsModel1: Math.random() < 0.5,
    };

    if (!shuffleResult.modelAIsModel1) {
      shuffleResult.modelA = model2Label;
      shuffleResult.modelB = config.MODEL1_NAME;
    }

    const comparison: ComparisonResult = {
      bookmark,
      modelA: shuffleResult.modelA,
      modelATags: shuffleResult.modelAIsModel1 ? model1Tags : model2Tags,
      modelB: shuffleResult.modelB,
      modelBTags: shuffleResult.modelAIsModel1 ? model2Tags : model1Tags,
    };

    displayComparison(i + 1, bookmarks.length, comparison, true);

    const answer = await askQuestion(
      "Which tags do you prefer? [1=Model A, 2=Model B, s=skip, q=quit] > ",
    );

    const normalizedAnswer = answer.toLowerCase();

    if (normalizedAnswer === "q" || normalizedAnswer === "quit") {
      console.log(chalk.yellow("\n⏸ Quitting early...\n"));
      break;
    }

    if (normalizedAnswer === "1") {
      comparison.winner = "modelA";
      if (shuffleResult.modelAIsModel1) {
        counters.model1Votes++;
      } else {
        counters.model2Votes++;
      }
      detailedResults.push(comparison);
    } else if (normalizedAnswer === "2") {
      comparison.winner = "modelB";
      if (shuffleResult.modelAIsModel1) {
        counters.model2Votes++;
      } else {
        counters.model1Votes++;
      }
      detailedResults.push(comparison);
    } else {
      comparison.winner = "skip";
      counters.skipped++;
      detailedResults.push(comparison);
    }
  }

  close();

  displayFinalResults({
    model1Name: config.MODEL1_NAME,
    model2Name: isExistingMode ? "Existing AI Tags" : config.MODEL2_NAME!,
    model1Votes: counters.model1Votes,
    model2Votes: counters.model2Votes,
    skipped: counters.skipped,
    errors: counters.errors,
    total: counters.total,
  });
}

main().catch((error) => {
  console.error(chalk.red(`\n✗ Fatal error: ${error}\n`));
  process.exit(1);
});