无服务器部署ChatGPT聊天界面

使用Cloudflare部署Ai聊天前端界面,无需置备服务器,只要一个可以接入cloudflare的域名即可。

代码摘自开源项目 https://github.com/opentdp/openai-chat

  • 支持基于gpt-3.5-turbo模型的对话
  • 支持批量查询api到期时间和剩余额度

安装步骤

  • 将代码部署到Cloudflare的worker平台(其他支持worker的平台也可)
/**
 * @auther Rehiy
 * @url https://github.com/opentdp/openai-chat
 */

const GITHUB_URL = 'https://raw.githubusercontent.com/opentdp/openai-chat/master';

async function github_proxy(request) {
    const url = new URL(request.url);

    let backend = GITHUB_URL + url.pathname;
    if (url.pathname.endsWith('/')) {
        backend += 'index.html';
    }

    const res = await fetch(backend, {
        method: request.method,
        headers: {
            'User-Agent': request.headers.get('User-Agent'),
        },
    });

    const headers = new Headers();
    headers.set('Content-Type', file_type(backend));
    headers.set('Cache-Control', 'public, max-age=86400');

    return new Response(res.body, {
        status: res.status,
        headers,
    });
}

function file_type(url) {
    const ext = url.split('?').shift().split('.').pop();
    const mines = {
        'json': 'application/json',
        'js': 'application/javascript',
        'css': 'text/css',
        'xml': 'text/xml',
        'html': 'text/html',
        'webm': 'video/webm',
        'mp3': 'audio/mpeg',
        'mp4': 'video/mp4',
        'webp': 'image/webp',
        'gif': 'image/gif',
        'png': 'image/png',
        'jpg': 'image/jpeg',
        'jpeg': 'image/jpeg',
        'svg': 'image/svg+xml',
        'ico': 'image/x-icon',
    };
    return mines[ext] || 'text/plain';
}

// esmodule

export default {
    async fetch(request, env) {
        return github_proxy(request);
    }
}
  • ai
  • cloudflare
  • worker
  • chatgpt

6 评论

鹰瑶 回复

很久前码的一小段代码,偶然翻出来了。带注释分享一下,仅供大家研究学习,切勿用于其他用途

小东 回复

很久没更新这个系列的文章了,想来按照之前教程部署的集群也都到了该升级的时候,今天我们来探讨下如何升级k3s集群

荒年 回复

在和上可以通过删除“设置账户邮件和账户”里的登录账号来达到目的。但是在上尝试了各种删除的办法都是没用

梦屿 回复

MariaDB 安装时,默认会创建一个特权用户。如果忘记超级管理员可以使用这个账号登录后修改密码或者主机

蛋黄 回复

使用Cloudflare部署Ai聊天前端界面,无需置备服务器,只要一个可以接入cloudflare的域名即可

king 回复

前言 不得不吐槽下,将一个现有域名添加到Cloudflare时,系统自动导入的几百个不那么正确的解析记录,让人抓狂

回复题主

您的电子邮件地址将不会被公布。必填字段已标记 *