File size: 5,532 Bytes
6e6dab9
 
 
4e6658c
451c088
 
 
 
fb77726
68f11c9
3a17e83
6e6dab9
 
38880bf
6e6dab9
fb77726
38880bf
 
 
 
6e6dab9
3132327
 
 
38880bf
 
7626743
6e6dab9
 
3132327
 
 
 
 
 
7626743
 
 
 
 
6e6dab9
eb28af4
6e6dab9
 
7626743
 
 
 
 
 
 
 
 
 
38880bf
7626743
 
6e6dab9
 
 
7626743
 
 
6e6dab9
7626743
 
6e6dab9
7626743
6e6dab9
 
ed18d7d
451c088
 
ed18d7d
045ac52
451c088
045ac52
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
6e6dab9
 
 
 
 
 
2fe3cee
cf6dabc
ae31beb
 
cf6dabc
 
2fe3cee
cf6dabc
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
5089a01
 
2fe3cee
 
cf6dabc
 
2fe3cee
 
 
 
 
cf6dabc
 
 
6e6dab9
cf6dabc
 
 
 
6e6dab9
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
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
import {
    jsonize_stream_data,
    stringify_stream_bytes,
} from "./stream_jsonizer.js";
import {
    update_message,
    create_messager,
    get_request_messages,
    get_selected_llm_model,
    get_selected_temperature,
} from "../components/chat_operator.js";

export class ChatCompletionsRequester {
    constructor({
        prompt,
        model = null,
        temperature = 0.5,
        top_p = 0.95,
        openai_endpoint = null,
    } = {}) {
        this.prompt = prompt;
        this.openai_endpoint =
            openai_endpoint || this.extract_endpoint_and_model()[0];
        this.model = model || this.extract_endpoint_and_model()[1];
        this.temperature = temperature;
        this.top_p = top_p;
        this.backend_request_endpoint = "/chat/completions";
        this.controller = new AbortController();
    }
    extract_endpoint_and_model() {
        let model_id_with_endpoint = get_selected_llm_model();
        this.openai_endpoint = model_id_with_endpoint.split("|")[0];
        this.model = model_id_with_endpoint.split("|")[1];
        return [this.openai_endpoint, this.model];
    }
    construct_openai_request_headers() {
        this.backend_request_headers = {
            "Content-Type": "application/json",
        };
        this.openai_request_headers = {
            "Content-Type": "application/json",
            Authorization: `Bearer ${localStorage.getItem("openai_api_key")}`,
        };
    }
    construct_backend_request_body() {
        this.openai_request_messages = get_request_messages();
        this.backend_request_body = {
            openai_endpoint: this.openai_endpoint,
            openai_request_method: "POST",
            openai_request_headers: this.openai_request_headers,
            openai_request_body: {
                model: this.model,
                messages: this.openai_request_messages,
                temperature: this.temperature,
                top_p: this.top_p,
                stream: true,
            },
        };
    }
    construct_request_params() {
        this.construct_openai_request_headers();
        this.construct_backend_request_body();
        this.backend_request_params = {
            method: "POST",
            headers: this.backend_request_headers,
            body: JSON.stringify(this.backend_request_body),
            signal: this.controller.signal,
            stream: true,
        };
    }
    create_messager_components() {
        create_messager("user", this.prompt);
        create_messager("assistant", "", this.model, this.temperature);
    }
    async post() {
        this.construct_request_params();
        const response = await fetch(
            this.backend_request_endpoint,
            this.backend_request_params
        );
        const reader = response.body.getReader();
        let buffer = "";
        return reader.read().then(function process({ done, value }) {
            if (done) {
                return;
            }
            buffer += stringify_stream_bytes(value);
            let boundary = buffer.lastIndexOf("\n");
            if (boundary !== -1) {
                let input = buffer.substring(0, boundary);
                buffer = buffer.substring(boundary + 1);
                let json_chunks = jsonize_stream_data(input);
                update_message(json_chunks);
            }
            return reader.read().then(process);
        });
    }
    stop() {
        this.controller.abort();
    }
}

// export var available_models = { "user-customized": ["notes"] };
export class AvailableModelsRequester {
    constructor(openai_endpoint) {
        this.openai_endpoint = openai_endpoint;
        this.backend_request_endpoint = "/models";
        this.controller = new AbortController();
        this.available_models = [];
    }
    construct_openai_request_headers() {
        this.backend_request_headers = {
            "Content-Type": "application/json",
        };
        this.openai_request_headers = {
            "Content-Type": "application/json",
        };
    }
    construct_backend_request_body() {
        this.backend_request_body = {
            openai_endpoint: this.openai_endpoint,
            openai_request_method: "GET",
            openai_request_headers: this.openai_request_headers,
        };
    }
    construct_request_params() {
        this.construct_openai_request_headers();
        this.construct_backend_request_body();
        this.backend_request_params = {
            method: "POST",
            headers: this.backend_request_headers,
            body: JSON.stringify(this.backend_request_body),
            signal: this.controller.signal,
        };
    }
    get() {
        this.construct_request_params();
        return fetch(this.backend_request_endpoint, this.backend_request_params)
            .then((response) => response.json())
            .then((response_json) => {
                let data = response_json.data;
                data.forEach((item) => {
                    if (!this.available_models.includes(item.id)) {
                        this.available_models.push(item.id);
                    }
                });
                console.log(
                    `get available_models of ${this.openai_endpoint}:`,
                    this.available_models
                );
                return this.available_models;
            })
            .catch((error) => {
                console.error("Error:", error);
            });
    }
    stop() {
        this.controller.abort();
    }
}