File size: 16,922 Bytes
3c6f1bf
 
 
 
 
 
 
204dd4d
3c6f1bf
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
204dd4d
3c6f1bf
 
 
204dd4d
3c6f1bf
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
204dd4d
 
 
 
 
 
3c6f1bf
 
 
 
 
 
 
 
 
204dd4d
 
3c6f1bf
 
 
 
204dd4d
 
3c6f1bf
 
204dd4d
3c6f1bf
204dd4d
 
 
 
 
 
 
 
3c6f1bf
204dd4d
 
 
 
 
 
 
 
 
 
 
 
3c6f1bf
204dd4d
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
3c6f1bf
204dd4d
 
 
 
3c6f1bf
204dd4d
 
 
 
3c6f1bf
204dd4d
 
 
 
 
 
 
 
3c6f1bf
204dd4d
 
 
 
3c6f1bf
204dd4d
 
 
 
3c6f1bf
 
 
 
 
 
75df934
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
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
<!doctype html>
<html>

<head>
    <title>Python Project</title>
    <meta charset="UTF-8">
    <meta name="viewport" content="width=device-width, initial-scale=1.0">
    <meta name="csrf-token" content="{{ csrf_token }}">
    <script src="https://cdn.tailwindcss.com"></script>
    <!-- <script>
        // const roomdata = JSON.parse('{{ roomdata|safe }}');
        var roomdata = JSON.parse('{{ roomdata|safe }}');
        console.log(roomdata)
        function call() {
            return roomdata;
        }
    </script> -->
    <style>
        body {
            margin: 0;
            padding: 0;
            background-color: #2A2A2A;
            color: white;
        }

        .container {
            display: flex;
            height: 100vh;
        }

        .left-column {
            width: 25%;
            background-color: #3A3A3A;
            padding: 20px;
            overflow-y: auto;
        }

        .right-column {
            width: 75%;
            background-color: #2E2E2E;
            padding: 20px;
            overflow-y: auto;
        }
        .logout-button {
            margin-left: 30px; /* Adjust this value for more space */
        }
    </style>
</head>
<script>
    function textAreaAdjust(element) {
        element.style.height = "1px";
        element.style.height = (25 + element.scrollHeight) + "px";
    }
</script>

<body>
    <div id="app" class="container">
        <div class="left-column">
            <h2 class="text-xl font-medium mb-4">Lịch sử đoạn chat<button @click="logout"  class="bg-blue-500 text-white px-2 py-1 rounded mt-2 logout-button">
                <a :href="`/users/logout`">Logout</a>
              </button></h2>
            <ul class="list-none">
                {% for x in rooms %}
                    <li class="p-2 bg-[#4A4A4A] mb-2 rounded flex justify-between items-center">
                        <a :href="`/chat?id={{x.id}}`" class="cursor-pointer">{{ x.name }}</a>
                        <a :href="`/chat/delete?id={{x.id}}`" class="bg-red-500 text-white px-2 py-1 rounded">
                            Xoá
                        </a>
                    </li>
                {% endfor %}
            </ul>
            
        </div>

        <div class="right-column flex flex-col md:mx-auto mx-0 bg-[#2E2E2E] w-full h-screen">

            <div class="flex flex-col h-full">
                <!-- head -->
                <div class="flex flex-row justify-between items-center p-4 select-none">
                    <h1 class="text-2xl font-medium text-center">
                        TinyChat
                    </h1>
                    <div>
                        <svg width="24" height="24" viewBox="0 0 24 24" fill="none" xmlns="http://www.w3.org/2000/svg"
                            class="cursor-pointer" :class="{'text-white':!showConfig,'text-[#919191]':showConfig}"
                            @click="showConfig=!showConfig">
                            <path
                                d="M11.6439 3C10.9352 3 10.2794 3.37508 9.92001 3.98596L9.49643 4.70605C8.96183 5.61487 7.98937 6.17632 6.935 6.18489L6.09966 6.19168C5.39095 6.19744 4.73822 6.57783 4.38385 7.19161L4.02775 7.80841C3.67338 8.42219 3.67031 9.17767 4.01968 9.7943L4.4315 10.5212C4.95126 11.4386 4.95126 12.5615 4.4315 13.4788L4.01968 14.2057C3.67031 14.8224 3.67338 15.5778 4.02775 16.1916L4.38385 16.8084C4.73822 17.4222 5.39095 17.8026 6.09965 17.8083L6.93501 17.8151C7.98938 17.8237 8.96184 18.3851 9.49644 19.294L9.92001 20.014C10.2794 20.6249 10.9352 21 11.6439 21H12.3561C13.0648 21 13.7206 20.6249 14.08 20.014L14.5035 19.294C15.0381 18.3851 16.0106 17.8237 17.065 17.8151L17.9004 17.8083C18.6091 17.8026 19.2618 17.4222 19.6162 16.8084L19.9723 16.1916C20.3267 15.5778 20.3298 14.8224 19.9804 14.2057L19.5686 13.4788C19.0488 12.5615 19.0488 11.4386 19.5686 10.5212L19.9804 9.7943C20.3298 9.17767 20.3267 8.42219 19.9723 7.80841L19.6162 7.19161C19.2618 6.57783 18.6091 6.19744 17.9004 6.19168L17.065 6.18489C16.0106 6.17632 15.0382 5.61487 14.5036 4.70605L14.08 3.98596C13.7206 3.37508 13.0648 3 12.3561 3H11.6439Z"
                                stroke="white" stroke-width="2" stroke-linejoin="round" />
                            <path
                                d="M12 14.5C13.3807 14.5 14.5 13.3807 14.5 12C14.5 10.6193 13.3807 9.5 12 9.5C10.6193 9.5 9.5 10.6193 9.5 12C9.5 13.3807 10.6193 14.5 12 14.5Z"
                                stroke="white" stroke-width="2" />
                        </svg>

                    </div>
                </div>
                <!-- config -->
                <div v-show="showConfig" class="flex flex-col gap-4 p-4 select-none">
                    <!-- render configFields -->
                    <div v-for="field in configFields" :key="field.name" class="flex flex-col gap-2">
                        <label class="text-sm text-gray-400">
                            {{ field.label }}
                        </label>
                        <input v-if="field.type=='text'" type="text"
                            class="bg-transparent border border-[#919191] rounded-lg p-2"
                            v-model="config[field.name]" />
                        <input v-if="field.type=='number'" type="number"
                            class="bg-transparent border border-[#919191] rounded-lg p-2"
                            v-model="config[field.name]" />
                    </div>
                </div>
                <!-- messages -->
                <div class="flex flex-col overflow-y-auto scroll-smooth" id="messages">
                    <div v-for="(message,index) in messages" :key="message.id"
                        class="flex flex-col odd:bg-[#F7F7F7]/10 group">
                        <div class="p-4 flex flex-col justify-between gap-4">
                            <div class="flex flex-row gap-4 capitalize">
                                <div class="text-gray-400 text-sm">
                                    <textarea
                                        class="text-gray-400 text-sm size-full outline-0 bg-transparent border-none text-white rounded-lg resize-none"
                                        :rows="message.role" v-model="message.role"></textarea>
                                </div>
                                <div class="flex flex-col w-full">
                                    <textarea onkeyup="textAreaAdjust(this)" style="overflow:hidden"
                                        class="size-full outline-0 bg-transparent border-none text-white rounded-lg resize-y"
                                        :rows="message.content.split('\n').length" v-model="message.content"></textarea>
                                    <div class="mt-4" v-if="index==messages.length-1 && message.role=='assistant'">
                                        <svg stroke="currentColor" fill="none" stroke-width="1.5" viewBox="0 0 24 24"
                                            stroke-linecap="round" stroke-linejoin="round" height="1em" width="1em"
                                            xmlns="http://www.w3.org/2000/svg" v-if="index==messages.length-1"
                                            class="group-hover:block cursor-pointer h-3 w-3" @click="refresh"
                                            :class="{'animate-spin':isLoading}">
                                            >
                                            >
                                            >
                                            <polyline points="1 4 1 10 7 10"></polyline>
                                            <polyline points="23 20 23 14 17 14"></polyline>
                                            <path
                                                d="M20.49 9A9 9 0 0 0 5.64 5.64L1 10m22 4l-4.64 4.36A9 9 0 0 1 3.51 15">
                                            </path>

                                        </svg>
                                    </div>
                                </div>
                            </div>
                        </div>
                    </div>
                </div>
            </div>
            <!-- send message -->
            <div class="flex flex-row justify-between items-center p-4 sticky bottom-0 bg-[#2E2E2E]" v-if="!showConfig">
                <div class="w-full flex flex-row border border-1 border-[#919191] rounded-xl px-4 py-6 items-center">
                    <textarea
                        class="w-full h-full outline-0 bg-transparent border-none text-white resize-none text-md max-h-[100px]"
                        v-on:keyup.enter="sendMessageAPI" placeholder="Type a message" :rows="message.split('\n').length"
                        v-model="message"></textarea>
                    <div class="flex flex-col justify-end gap-4 text-[#2E2E2E] rounded-md p-1"
                        :class="{'bg-white':message!='','bg-white/10':message==''}">
                        <button @click="sendMessageAPI">
                            <svg class="w-6 h-6" fill="none" stroke="currentColor" viewBox="0 0 24 24"
                                xmlns="http://www.w3.org/2000/svg">
                                <path stroke-linecap="round" stroke-linejoin="round" stroke-width="2"
                                    d="M12 19l9 2-9-18-9 18 9-2zm0 0v-8" />
                            </svg>
                        </button>
                    </div>
                </div>
            </div>

        </div>
        
        <script type="module">
            import { createApp } from 'https://unpkg.com/vue@3/dist/vue.esm-browser.js'
            // import { SSE } from "https://cdn.jsdelivr.net/npm/sse.js@2.1.0/lib/sse.min.js"
            
            var roomdata = {{ roomdata|safe }};
            
            createApp({
                data() {
                    return {
                        showConfig: false,
                        configFields: [
                            {
                                name: "endPoint",
                                type: "text",
                                label: "End Point"
                            },
                            {
                                name: "apiKey",
                                type: "text",
                                label: "API Key"
                            },
                            {
                                name: "model",
                                type: "text",
                                label: "Model"
                            },
                            {
                                name: "temperature",
                                type: "number",
                                label: "Temperature"
                            },
                            {
                                name: "presence_penalty",
                                type: "number",
                                label: "Presence Penalty"
                            },
                            {
                                name: "top_p",
                                type: "number",
                                label: "Top P"
                            },
                            {
                                name: "frequency_penalty",
                                type: "number",
                                label: "Frequency Penalty"
                            },
                        ],
                        isLoading: false,
                        message: '',
                        messages: roomdata.messages,
                        // cs:roomdata,
                        chatSocket: null,
                        config: {
                            endPoint: "",
                            apiKey: "",
                            model: "gpt-3.5-turbo",
                            temperature: 1,
                            presence_penalty: 0,
                            top_p: 1,
                            frequency_penalty: 0,
                            stream: true
                        },
                        // roomdata: roomdata
                    }

                },
                created() {
                    // const roomdata = JSON.parse('roomdata');
                    const params = new URLSearchParams(window.location.search);
                    this.id = params.get('id');//chat id
                    if (this.id == null) {
                        return
                    }
                    // this.connect()
                    this.ques = params.get('ques');
                    if (this.ques != null) {
                        this.message = this.ques;
                        this.sendMessageAPI();
                    }
                },
                methods: {
                    refresh() {
                        this.send(true);
                    },
                    deleteChat(id) {
                        window.location.href = '/chat/delete?id=' + id;
                        return;
                    },
                    sendMessageAPI() {
                        console.log("call sendMesageAPI");
                        if (this.id == null) {
                            window.location.href = '/chat/newchat?ques=' + this.message;
                            return;
                        }
                        this.ques = this.message;
                        this.message = "";
                        this.messages.push({
                            role: "user",
                            content: this.ques
                        });
                        fetch('/chat/messages/', {
                            method: 'POST',
                            headers: {
                            'Content-Type': 'application/json'
                            },
                            body: JSON.stringify({
                            'roomid': this.id,
                            'message': this.ques,
                            'messages': this.messages,
                            })
                        })
                        .then(response => response.json())
                        .then(data => {
                            this.messages.push({
                                role: "model",
                                content: data.message
                            });
                        })
                        .catch(error => {
                            console.error('Error sending or fetching messages:', error);
                        });
                    },

                    // sendMessage() {
                    //     if (this.id == null) {
                    //         window.location.href = '/chat/newchat?ques=' + this.message;
                    //         return;
                    //     }
                    //     this.messages.push({
                    //         role: "user",
                    //         content: this.message
                    //     });
                    //     this.chatSocket.send(JSON.stringify({
                    //         'roomid':this.id,
                    //         'message': this.message,
                    //         'messages': this.messages,
                    //     }));
                    //     console.log(JSON.stringify({
                    //         'messages': this.messages
                    //     }))
                    //     this.message = "";
                    // },
                    
                    // connect() {
                    //     this.chatSocket = new WebSocket(
                    //         'wss://' + window.location.host + '/wss/chat'
                    //     );

                    //     this.chatSocket.onopen = () => {
                    //         this.connectionStatus = 'Connected';
                    //         console.log('WebSocket connected');
                    //     };

                    //     this.chatSocket.onmessage = (event) => {
                    //         var mes = JSON.parse(event.data)
                    //         this.messages.push({
                    //             role: "model",
                    //             content: mes.message
                    //         });
                    //         console.log('Message received: ', event.data);
                    //     };

                    //     this.chatSocket.onclose = () => {
                    //         this.connectionStatus = 'Disconnected';
                    //         console.log('WebSocket disconnected');
                    //     };

                    //     this.chatSocket.onerror = (error) => {
                    //         console.error('WebSocket Error: ', error);
                    //     };
                    // }
                },
            }).mount('#app')
        </script>
    </div>
</body>

</html>