测试密钥脚本

<!DOCTYPE html>
<html lang="zh-CN">
<head>
    <meta http-equiv="Content-Type" content="text/html; charset=UTF-8">
    <title>密钥测试</title>
    <link rel="shortcut icon" href="https://github.githubassets.com/favicons/favicon.svg">
    <style>
        body {
            font-family: Arial, sans-serif;
            width: 95%;
            margin: 0 auto;
        }

        textarea, input, button {
            display: block;
            margin: 15px 0;
            padding: 10px;
            width: 90%;
        }
    </style>
</head>
<body>
    <textarea id="textarea" placeholder="输入问题"></textarea>
    <input type="text" id="api_key" placeholder="输入API密钥">
    <button id="openai-btn" data-api-url="https://api.openai.com/v1/chat/completions">测试密钥1</button>
    <button id="openkey-btn" data-api-url="https://xxx.xxx/v1/chat/completions">测试密钥2</button>

    <script>
        let apiUrl = "";

        document.getElementById("openai-btn").addEventListener("click", function() {
            apiUrl = this.getAttribute("data-api-url");
            testAPI();
        });

        document.getElementById("openkey-btn").addEventListener("click", function() {
            apiUrl = this.getAttribute("data-api-url");
            testAPI();
        });

        function testAPI() {
            if (!apiUrl) {
                alert("请选择一个API URL");
                return;
            }

            const originalInput = document.getElementById('textarea').value;
            document.getElementById('textarea').value = "请稍后...";
            const apiKey = document.getElementById("api_key").value.trim();

            const data = {
                model: "gpt-3.5-turbo",
                messages: [
                    { role: "system", content: "You are a helpful assistant." },
                    { role: "user", content: originalInput }
                ]
            };

            const xhr = new XMLHttpRequest();
            xhr.open("POST", apiUrl, true);
            xhr.setRequestHeader("Content-Type", "application/json");
            xhr.setRequestHeader("Authorization", `Bearer ${apiKey}`);

            xhr.onreadystatechange = function() {
                if (this.readyState === XMLHttpRequest.DONE) {
                    if (this.status === 200) {
                        const response = JSON.parse(this.responseText);
                        document.getElementById('textarea').value = response.choices[0].message.content;
                    } else {
                        document.getElementById('textarea').value = "请求失败,请检查您的API密钥和其他设置。";
                    }
                }
            };

            xhr.send(JSON.stringify(data));
        }
    </script>
</body>
</html>

评论
成就一亿技术人!
拼手气红包6.0元
还能输入1000个字符
 
红包 添加红包
表情包 插入表情
 条评论被折叠 查看
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值