XFE Git
XFE Studio Git
Git 首页 全局搜索
XFE 主站 文档 NuGet
公开
关注 0 Fork 0 Star 1
返回提交历史

XFEstudio/gpt4free

Improve gui handling, Improve count tokens

20ab17f3
Heiner Lohaus <hlohaus@users.noreply.github.com>
提交于

代码差异

1 个文件 +28 -22
Modified g4f/gui/client/js/chat.v1.js +28 -22
@@ -5,6 +5,8 @@ const message_input = document.getElementById(`message-input`);
5 5 const box_conversations = document.querySelector(`.top`);
6 6 const stop_generating = document.querySelector(`.stop_generating`);
7 7 const regenerate = document.querySelector(`.regenerate`);
8 const sidebar = document.querySelector(".conversations");
9 const sidebar_button = document.querySelector(".mobile-sidebar");
8 10 const send_button = document.getElementById("send-button");
9 11 const imageInput = document.getElementById("image");
10 12 const cameraInput = document.getElementById("camera");
@@ -65,6 +67,13 @@ const register_remove_message = async () => {
65 67
66 68 const delete_conversations = async () => {
67 69 localStorage.clear();
70 for (let i = 0; i < localStorage.length; i++){
71 let key = localStorage.key(i);
72 if (key.startsWith("conversation:")) {
73 localStorage.removeItem(key);
74 }
75 }
76 hide_sidebar();
68 77 await new_conversation();
69 78 };
70 79
@@ -395,7 +404,8 @@ const set_conversation = async (conversation_id) => {
395 404
396 405 await clear_conversation();
397 406 await load_conversation(conversation_id);
398 await load_conversations();
407 load_conversations();
408 hide_sidebar();
399 409 };
400 410
401 411 const new_conversation = async () => {
@@ -403,9 +413,9 @@ const new_conversation = async () => {
403 413 window.conversation_id = uuid();
404 414
405 415 await clear_conversation();
406 await load_conversations();
407
408 await say_hello()
416 load_conversations();
417 hide_sidebar();
418 say_hello();
409 419 };
410 420
411 421 const load_conversation = async (conversation_id) => {
@@ -419,7 +429,7 @@ const load_conversation = async (conversation_id) => {
419 429 let next_i = parseInt(i) + 1;
420 430 let next_provider = item.provider ? item.provider : (messages.length > next_i ? messages[next_i].provider : null);
421 431
422 let provider_link = item?.provider?.name ? `<a href="${item?.provider?.url}" target="_blank">${item.provider.name}</a>` : "";
432 let provider_link = item.provider?.name ? `<a href="${item.provider?.url}" target="_blank">${item.provider.name}</a>` : "";
423 433 let provider = provider_link ? `
424 434 <div class="provider">
425 435 ${provider_link}
@@ -428,7 +438,7 @@ const load_conversation = async (conversation_id) => {
428 438 ` : "";
429 439 elements += `
430 440 <div class="message" data-index="${i}">
431 <div class=${item.role == "assistant" ? "assistant" : "user"}>
441 <div class="${item.role}">
432 442 ${item.role == "assistant" ? gpt_image : user_image}
433 443 <i class="fa-solid fa-xmark"></i>
434 444 ${item.role == "assistant"
@@ -450,13 +460,13 @@ const load_conversation = async (conversation_id) => {
450 460 last_model = last_model?.startsWith("gpt-4") ? "gpt-4" : "gpt-3.5-turbo"
451 461 let count_total = GPTTokenizer_cl100k_base?.encodeChat(filtered, last_model).length
452 462 if (count_total > 0) {
453 elements += `<div class="count_total">(${count_total} tokens total)</div>`;
463 elements += `<div class="count_total">(${count_total} tokens used)</div>`;
454 464 }
455 465 }
456 466
457 467 message_box.innerHTML = elements;
458 468
459 await register_remove_message();
469 register_remove_message();
460 470 highlight(message_box);
461 471
462 472 message_box.scrollTo({ top: message_box.scrollHeight, behavior: "smooth" });
@@ -485,8 +495,9 @@ function count_tokens(model, text) {
485 495 }
486 496
487 497 function count_words_and_tokens(text, model) {
488 const tokens_count = model ? `, ${count_tokens(model, text)} tokens` : "";
489 return `(${count_words(text)} words${tokens_count})`
498 const tokens_count = model ? count_tokens(model, text) : null;
499 const tokens_append = tokens_count ? `, ${tokens_count} tokens` : "";
500 return `(${count_words(text)} words${tokens_append})`
490 501 }
491 502
492 503 const get_conversation = async (conversation_id) => {
@@ -629,15 +640,17 @@ const message_id = () => {
629 640 return BigInt(`0b${unix}${random_bytes}`).toString();
630 641 };
631 642
632 document.querySelector(".mobile-sidebar").addEventListener("click", (event) => {
633 const sidebar = document.querySelector(".conversations");
643 async function hide_sidebar() {
644 sidebar.classList.remove("shown");
645 sidebar_button.classList.remove("rotated");
646 }
634 647
648 sidebar_button.addEventListener("click", (event) => {
635 649 if (sidebar.classList.contains("shown")) {
636 sidebar.classList.remove("shown");
637 event.target.classList.remove("rotated");
650 hide_sidebar();
638 651 } else {
639 652 sidebar.classList.add("shown");
640 event.target.classList.add("rotated");
653 sidebar_button.classList.add("rotated");
641 654 }
642 655
643 656 window.scrollTo(0, 0);
@@ -740,13 +753,6 @@ message_input.addEventListener("keyup", count_input);
740 753 window.onload = async () => {
741 754 setTheme();
742 755
743 let conversations = 0;
744 for (let i = 0; i < localStorage.length; i++) {
745 if (localStorage.key(i).startsWith("conversation:")) {
746 conversations += 1;
747 }
748 }
749
750 756 count_input();
751 757
752 758 if (/\/chat\/.+/.test(window.location.href)) {