LLaMA 8B Floating Chat (Monkey Icon)

Adds a draggable ChatGPT button to Discord with an inbuilt ChatGPT chatbox

当前为 2025-08-14 提交的版本,查看 最新版本

// ==UserScript==
// @name         LLaMA 8B Floating Chat (Monkey Icon)
// @version      1.2
// @match        *://*/*
// @grant        GM_xmlhttpRequest
// @namespace https://gf.qytechs.cn/
// @description Adds a draggable ChatGPT button to Discord with an inbuilt ChatGPT chatbox
// ==/UserScript==

(function() {
    'use strict';

    // ===== Floating Button =====
    const btn = document.createElement("img");
    btn.src = "https://i.imgur.com/3PW2vTM.jpg"; // Monkey image
    btn.style.position = "fixed";
    btn.style.bottom = "20px";
    btn.style.right = "20px";
    btn.style.width = "60px";
    btn.style.height = "60px";
    btn.style.borderRadius = "50%";
    btn.style.cursor = "pointer";
    btn.style.zIndex = 10000;
    btn.style.boxShadow = "0 4px 8px rgba(0,0,0,0.3)";
    document.body.appendChild(btn);

    // ===== Chat Box =====
    const chatBox = document.createElement("div");
    chatBox.style.position = "fixed";
    chatBox.style.bottom = "90px";
    chatBox.style.right = "20px";
    chatBox.style.width = "300px";
    chatBox.style.height = "400px";
    chatBox.style.background = "#fff";
    chatBox.style.border = "1px solid #ccc";
    chatBox.style.borderRadius = "10px";
    chatBox.style.padding = "10px";
    chatBox.style.display = "none";
    chatBox.style.flexDirection = "column";
    chatBox.style.zIndex = 10000;
    chatBox.style.overflowY = "auto";
    chatBox.style.fontFamily = "sans-serif";
    document.body.appendChild(chatBox);

    const input = document.createElement("input");
    input.type = "text";
    input.placeholder = "Type your message...";
    input.style.width = "100%";
    input.style.padding = "5px";
    input.style.marginTop = "auto";
    chatBox.appendChild(input);

    // ===== Toggle Chat =====
    btn.addEventListener("click", () => {
        chatBox.style.display = chatBox.style.display === "none" ? "flex" : "none";
    });

    // ===== Send Message =====
    input.addEventListener("keypress", function(e) {
        if (e.key === "Enter" && input.value.trim() !== "") {
            const userMessage = input.value;
            input.value = "";

            const messageDiv = document.createElement("div");
            messageDiv.textContent = "You: " + userMessage;
            chatBox.appendChild(messageDiv);

            // Typing indicator
            const loadingDiv = document.createElement("div");
            loadingDiv.textContent = "LLaMA is typing...";
            chatBox.appendChild(loadingDiv);
            chatBox.scrollTop = chatBox.scrollHeight;

            GM_xmlhttpRequest({
                method: "POST",
                url: "https://api-inference.huggingface.co/models/meta-llama/Llama-3.1-8B-Instruct",
                headers: {
                    "Authorization": "Bearer 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", // Replace with your token
                    "Content-Type": "application/json"
                },
                data: JSON.stringify({
                    inputs: userMessage,
                    options: { wait_for_model: true }
                }),
                onload: function(response) {
                    try {
                        const data = JSON.parse(response.responseText);
                        let llamaResponse = "Error getting response";

                        if (Array.isArray(data) && data.length > 0) {
                            if (data[0].generated_text) {
                                llamaResponse = data[0].generated_text;
                            } else if (data[0].content && data[0].content[0] && data[0].content[0].text) {
                                llamaResponse = data[0].content[0].text;
                            }
                        } else if (data.error) {
                            llamaResponse = "Error: " + data.error;
                        }

                        loadingDiv.textContent = "LLaMA: " + llamaResponse;
                        chatBox.scrollTop = chatBox.scrollHeight;

                    } catch(e) {
                        loadingDiv.textContent = "Failed to parse response: " + e.message;
                    }
                },
                onerror: function(err) {
                    loadingDiv.textContent = "Request failed";
                }
            });
        }
    });
})();

QingJ © 2025

镜像随时可能失效,请加Q群300939539或关注我们的公众号极客氢云获取最新地址