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
# It fetches ai-model-list from model-store of ragit.baehyunsol.com, and updates
# `models.json` in the repository. I'm doing this because I don't want to have
# 2 separate source of truth.
#
# I had 2 choices:
# 1. Manually update `models.json` in the repository and automatically update
# model-store according to the file.
# 2. Manually update model-store and automatically update `models.json` in
# the repository.
#
# I chose #2 because, in order to implement #1, I need a bot that regularly
# checks the repository and updates the model-store, and I don't want to
# implement another bot. I prefer doing it manually: update model-store ->
# run this script -> git commit & push
import json
import os
from server import get_json
from utils import goto_root
paths = [
"./models.json",
"./ragithub/backend/models.json",
"./crates/api/models.json",
]
goto_root()
for path in paths:
assert os.path.exists(path)
offset = 0
models = []
while True:
models_ = get_json(
url=f"https://ragit.baehyunsol.com/ai-model-list",
raw_url=True,
query={
"limit": 50,
"offset": offset,
},
)
offset += 50
models += models_
if len(models_) < 50:
break
models = [
{
# The original models.json is sorted in this order and I like this order.
k: model[k] for k in [
"name",
"api_name",
"can_read_images",
"api_provider",
"api_url",
"input_price",
"output_price",
"explanation",
"tags",
"api_env_var",
]
} for model in models
]
# Models are sorted by api_env_var because a group of models share api_env_var.
models.sort(key=lambda m: m["name"]) # break tie
models.sort(key=lambda m: (m["api_env_var"] or "ZZZ"))
for path in paths:
with open(path, "w") as f:
f.write(json.dumps(models, indent=4))