File size: 4,321 Bytes
3a17e83
4e6658c
 
7bb4d19
 
fb77726
bdd842c
af38c19
 
 
a53437c
d1f0f2d
fb77726
af38c19
 
 
 
4b68ad4
 
 
 
7bb4d19
 
 
 
 
 
 
 
 
 
 
 
 
af38c19
541cc7d
7bb4d19
 
fb77726
 
 
 
bdd842c
68f11c9
bdd842c
 
ac03bb3
af38c19
ac03bb3
4a8deab
ac03bb3
 
 
c98264e
 
 
 
65e47ec
 
 
 
ac03bb3
c60e6fd
65e47ec
ac03bb3
c98264e
ac03bb3
c98264e
7bb4d19
ac03bb3
 
c60e6fd
65e47ec
ac03bb3
c98264e
ac03bb3
c98264e
ac03bb3
 
7bb4d19
af38c19
7bb4d19
 
077080c
af38c19
077080c
 
4b79bca
 
 
 
7bb4d19
 
 
 
 
 
4b79bca
7bb4d19
 
4272b88
7bb4d19
 
 
 
011422d
7bb4d19
 
 
af38c19
 
 
 
 
 
4272b88
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
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
import { Messager, MessagerList } from "./messager.js";
import { transform_footnote } from "../networks/stream_jsonizer.js";
import { screen_scroller } from "./screen_scroller.js";

let messagers_container = $("#messagers-container");
let available_models_select = $("#available-models-select");
let temperature_select = $("#temperature-select");

let messager_list = new MessagerList(messagers_container);
let chat_history = [messager_list];
let md_to_html_converter = new showdown.Converter();
md_to_html_converter.setFlavor("github");

export function get_active_messager_list() {
    return chat_history[chat_history.length - 1];
}

export function get_latest_messager() {
    return get_active_messager_list().messagers.slice(-1)[0];
}

export function create_messager(
    role,
    content = "",
    model = "",
    temperature = ""
) {
    let message = {
        role: role,
        content: content,
        model: model,
        temperature: temperature,
    };
    let messager = new Messager(message);
    get_active_messager_list().push(messager);
    screen_scroller.scroll_to_bottom();
}

export function get_selected_llm_model() {
    return available_models_select.val();
}

export function get_selected_temperature() {
    return Number(temperature_select.val());
}

export function get_latest_messager_container() {
    return get_active_messager_list().messagers_container.children().last();
}
export function get_latest_message_content_displayer() {
    return get_latest_messager_container().find(".content-displayer");
}

export function get_latest_user_messager() {
    return $(".message-user").last();
}

export function get_latest_assistant_messager() {
    return $(".message-assistant").last();
}

export function start_latest_message_animation() {
    get_latest_assistant_messager().addClass("inferring");
    get_latest_assistant_messager()
        .find(".button-group")
        .find(".regenerate-button")
        .find("i")
        .addClass("fa-spin-fast");
}

export function stop_latest_message_animation() {
    get_latest_assistant_messager().removeClass("inferring");
    get_latest_assistant_messager()
        .find(".button-group")
        .find(".regenerate-button")
        .find("i")
        .removeClass("fa-spin-fast");
}

export function get_request_messages() {
    return get_active_messager_list().get_request_messages();
}

export function pop_messager(n = 2) {
    return get_active_messager_list().pop(n);
}

export function update_message(json_chunks, content_displayer_updater = null) {
    if (content_displayer_updater === null) {
        content_displayer_updater = new ContentDisplayerUpdater();
    }
    json_chunks.forEach(function (item) {
        let choice = item.choices[0];
        let content = choice.delta.content;
        let role = choice.delta.role;
        let finish_reason = choice.finish_reason;
        if (role) {
            // console.log("role: " + role);
        }
        if (content) {
            content_displayer_updater.update_with_chunk_content(content);
        }
        if (finish_reason === "stop") {
            console.log("[STOP]");
        }
        // console.log(item);
    });
    return json_chunks;
}

export function create_new_chat_session() {
    let new_messager_list = new MessagerList(messagers_container);
    chat_history.push(new_messager_list);
    messagers_container.empty();
}

export class ContentDisplayerUpdater {
    constructor(content_displayer = null) {
        if (content_displayer === null) {
            self.content_displayer = get_latest_message_content_displayer();
        } else {
            self.content_displayer = content_displayer;
        }
    }
    update_with_chunk_content(content) {
        self.content_displayer.data(
            "raw_content",
            self.content_displayer.data("raw_content") + content
        );
        get_active_messager_list().messagers.slice(-1)[0].message.content +=
            content;
        self.content_displayer.html(
            md_to_html_converter.makeHtml(
                transform_footnote(self.content_displayer.data("raw_content"))
            )
        );
        self.content_displayer
            .find("table")
            .addClass("table table-bordered table-hover");
        screen_scroller.scroll_to_bottom();
    }
}