aboutsummaryrefslogtreecommitdiffstats
path: root/tools/compare-models/src/bookmarkProcessor.ts
blob: 910957fe725206a48bf18dc389e2846addcfee97 (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
import type { InferenceClient } from "./inferenceClient";
import type { Bookmark } from "./types";

export async function extractBookmarkContent(
  bookmark: Bookmark,
): Promise<string> {
  if (bookmark.content.type === "link") {
    const parts = [];

    if (bookmark.content.url) {
      parts.push(`URL: ${bookmark.content.url}`);
    }

    if (bookmark.title) {
      parts.push(`Title: ${bookmark.title}`);
    }

    if (bookmark.content.description) {
      parts.push(`Description: ${bookmark.content.description}`);
    }

    if (bookmark.content.htmlContent) {
      parts.push(`Content: ${bookmark.content.htmlContent}`);
    }

    return parts.join("\n");
  }

  if (bookmark.content.type === "text" && bookmark.content.text) {
    return bookmark.content.text;
  }

  return "";
}

export async function runTaggingForModel(
  bookmark: Bookmark,
  model: string,
  inferenceClient: InferenceClient,
  lang: string = "english",
): Promise<string[]> {
  const content = await extractBookmarkContent(bookmark);

  if (!content) {
    return [];
  }

  try {
    const tags = await inferenceClient.inferTags(content, model, lang, []);
    return tags;
  } catch (error) {
    throw new Error(
      `Failed to generate tags with ${model}: ${error instanceof Error ? error.message : String(error)}`,
    );
  }
}