-
-
Notifications
You must be signed in to change notification settings - Fork 229
/
Copy pathbatchTagWithGpt.ts
93 lines (85 loc) · 2.82 KB
/
batchTagWithGpt.ts
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
import * as db from "../db/db.js"
import { getGptTopicSuggestions } from "../db/model/Chart.js"
import yargs from "yargs"
import { hideBin } from "yargs/helpers"
interface BatchTagWithGptArgs {
debug?: boolean
limit?: number
}
/*
Add GPT topics to eligible charts (and later posts, explorers).
Usage:
$ yarn batchTagWithGpt --help
Example: yarn batchTagWithGpt --debug --limit 1
*/
const batchTagChartsWithGpt = async (
knex: db.KnexReadonlyTransaction,
{ debug, limit }: BatchTagWithGptArgs = {}
) => {
// Identify all charts that need tagging. Get all charts that aren't tagged
// with a topic tag or the "Unlisted" tag. This includes charts that have no
// tags at all)
const chartsToTag = await db.knexRaw<{ id: number }>(
knex,
`-- sql
SELECT id
FROM charts
WHERE id
NOT IN (
SELECT chartId
FROM chart_tags
JOIN tags ON chart_tags.tagId = tags.id
WHERE tags.slug IS NOT NULL OR tags.name = 'Unlisted'
)
GROUP BY id
${limit ? `LIMIT ${limit}` : ""}
`
)
// Iterate through the charts and tag them with GPT-suggested topics
for (const chart of chartsToTag) {
const gptTopicSuggestions = await getGptTopicSuggestions(knex, chart.id)
for (const tag of gptTopicSuggestions) {
if (debug) console.log("Tagging chart", chart.id, "with", tag.id)
// Insert the suggested chart-tag association if it doesn't already
// exist, giving priority to the existing tags. This is to make sure
// already curated tags and their associated key chart levels and
// validation statuses are preserved.
await db.knexRaw(
knex,
`-- sql
INSERT IGNORE into chart_tags (chartId, tagId) VALUES (${chart.id},${tag.id})
`
)
}
}
}
if (require.main === module) {
void yargs(hideBin(process.argv))
.command<BatchTagWithGptArgs>(
"$0",
"Batch tag charts with GPT topics",
(yargs) => {
yargs
.option("debug", {
alias: "d",
type: "boolean",
description: "Enable debug mode",
default: false,
})
.option("limit", {
alias: "l",
type: "number",
description: "Limit the number of items processed",
})
},
async (argv) => {
await db.knexReadonlyTransaction(
(trx) => batchTagChartsWithGpt(trx, argv),
db.TransactionCloseMode.Close
)
}
)
.help()
.alias("help", "h")
.strict().argv
}