-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathindex.js
129 lines (104 loc) · 4.17 KB
/
index.js
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
import { eventSource, event_types, is_send_press, sendSystemMessage, system_message_types } from "../../../../script.js";
import { getContext } from "../../../extensions.js";
import { registerSlashCommand } from "../../../slash-commands.js";
import { textgen_types } from '../../../textgen-settings.js';
function log(...args) {
console.log('[local-chatbot-arena]', ...args)
}
function getModels() {
return Array.from(document.querySelector("#connection_profiles")).filter(x => x.text.length > 0).filter(x => x.text.includes("arena"))
}
async function switchModel() {
const models = getModels().map(x => x.value);
const model = models.at(Math.floor(Math.random() * models.length))
document.querySelector("#connection_profiles").value = model;
document.querySelector("#connection_profiles").dispatchEvent(new Event('change'))
log("switching to model", model);
//toastr.info(`Switching to ${model}`)
}
function getLatestMessage() {
return getContext().chat.filter(x => !x.is_system).at(-1);
}
async function interceptMessage() {
// do not block main thread-loop
(async () => {
log("processing received message");
if (!event_types?.GENERATION_ENDED) {
log("GENERATION_ENDED event missing");
while (is_send_press) {
await new Promise(resolve => setTimeout(resolve, 1000))
}
}
const message = getLatestMessage();
if (message?.extra?.api !== textgen_types.TABBY) {
return;
}
switchModel();
// don't always change the model
// if(Math.random() < 0.8) {
// switchModel()
// }
})()
}
function getValue(value) {
const extras = getLatestMessage().extra
const storage = JSON.parse(localStorage.getItem("chatbot_arena_storage"))?.[`${extras.model}`]
return (storage?.[value] ?? null)
}
function storeKeyValue(key, value) {
const extras = getLatestMessage().extra
const storage = JSON.parse(localStorage.getItem("chatbot_arena_storage"));
localStorage.setItem("chatbot_arena_storage", JSON.stringify({
...storage, [`${extras.model}`]: {
...storage[`${extras.model}`],
[key]: value
}
}))
}
function reportGoodModel() {
const extras = getLatestMessage().extra
storeKeyValue("good", (getValue("good") ?? 0) + 1)
toastr.info(`recorded ${extras.model}`)
}
function reportBadModel() {
const extras = getLatestMessage().extra
storeKeyValue("bad", (getValue("bad") ?? 0) + 1)
toastr.info(`recorded ${extras.model}`)
}
function forceSwitchModel() {
switchModel()
}
function getStats() {
const storage = JSON.parse(localStorage.getItem("chatbot_arena_storage") ?? {});
if (!storage || Object.keys(storage).length === 0) {
sendSystemMessage(system_message_types.EMPTY, "No chatbot stats yet!");
return;
}
const statsBad = Object.entries(storage).filter(([_, val]) => !!val.bad).sort(([a_key, a], [b_key, b]) => b.bad - a.bad).map(([model, value]) => {
return `[ ${value.bad} ] ${model}`
})
sendSystemMessage(system_message_types.EMPTY, ["<b><u>Bad Ratings</u></b>", ...statsBad].join("<p><p/>"));
const statsGood = Object.entries(storage).filter(([_, val]) => !!val.good).sort(([a_key, a], [b_key, b]) => b.good - a.good).map(([model, value]) => {
return `[ ${value.good} ] ${model}`
})
sendSystemMessage(system_message_types.EMPTY, ["<b><u>Good Ratings</u></b>", ...statsGood].join("<p><p/>"));
}
function cleanStats() {
localStorage.setItem("chatbot_arena_storage", "{}")
toastr.info("Arena stats reset!")
}
function listModels() {
const models = getModels().map(x => x.text);
log("123", models)
sendSystemMessage(system_message_types.EMPTY, ["<b><u>Current arena models</u></b>", ...models].join("<p><p/>"));
}
registerSlashCommand('arenamodels', listModels, []);
registerSlashCommand('arenareset', cleanStats, []);
registerSlashCommand('arenastats', getStats, []);
registerSlashCommand('switchmodel', forceSwitchModel, ['sm']);
registerSlashCommand('goodmodel', reportGoodModel, ['gm']);
registerSlashCommand('badmodel', reportBadModel, ['bm']);
eventSource.on(event_types?.GENERATION_ENDED || event_types.MESSAGE_RECEIVED, interceptMessage);
if (!localStorage.getItem("chatbot_arena_storage")) {
localStorage.setItem("chatbot_arena_storage", JSON.stringify({}))
}