Featured image of post 在cloudflare部署hugging face的免费api,可对接oneapi/newapi,免费使用Qwen2.5 72B等模型

在cloudflare部署hugging face的免费api,可对接oneapi/newapi,免费使用Qwen2.5 72B等模型

感谢大佬的开源
自部署 hugging face 的免费 api,拉到 newapi 内使用, Qwen2.5 70B 等免费 - 开发调优 - LINUX DO

顺手打包了镜像需要的可以看看

1
docker pull oozzbb/hg2api:latest

运行命令可参考

1
2
3
4
HUGGINGFACE_API_KEY ,去hugging face申请
API_KEY ,对接one-api/new-api使用

docker run --name hg2api --restart always -p 5023:5000 -e HUGGINGFACE_API_KEY=hg_xxx -e  API_KEY=sk-1234567890 oozzbb/hg2api:latest

正文开始
因国内部署无法访问 hugging face,所以在大佬的基础上改造成能部署到 cloudflare workers

准备工作
1、注册 cloudflare 28
2、注册 hugging face 并申请 api key,申请 api key 地址 147
3、复制以下代码部署到 cloudflare workers 中即可
4、支持在 oneapi/newapi 点击 “获取模型列表” 一键添加可用模型

我是隐藏代码

  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
//对接one-api/new-api使用
const API_KEY = "sk-1234567890";

//你的hugging face api key去hugging face申请
const HUGGINGFACE_API_KEY = "hf_xxxxxxxxxxx";

//目前发现的可用模型,请求时如模型不在该列表内,则使用你请求的模型
const CUSTOMER_MODEL_MAP = {
    "qwen2.5-72b-instruct": "Qwen/Qwen2.5-72B-Instruct",
    "gemma2-2b-it": "google/gemma-2-2b-it",
    "gemma2-27b-it": "google/gemma-2-27b-it",
    "llama-3-8b-instruct": "meta-llama/Meta-Llama-3-8B-Instruct",
    "llama-3.2-1b-instruct": "meta-llama/Llama-3.2-1B-Instruct",
    "llama-3.2-3b-instruct": "meta-llama/Llama-3.2-3B-Instruct",
    "phi-3.5": "microsoft/Phi-3.5-mini-instruct"
};

async function handleRequest(request) {
    try {
        if (request.method === "OPTIONS") {
            return getResponse("", 204);
        }

        const authHeader = request.headers.get("Authorization");
        if (!authHeader || !authHeader.startsWith("Bearer ") || authHeader.split(" ")[1] !== API_KEY) {
            return getResponse("Unauthorized", 401);
        }

        if (request.url.endsWith("/v1/models")) {
            const arrs = [];
            Object.keys(CUSTOMER_MODEL_MAP).map(element => arrs.push({ id: element, object: "model" }))
            const response = {
                data: arrs,
                success: true
            };

            return getResponse(JSON.stringify(response), 200);
        }

        if (request.method !== "POST") {
            return getResponse("Only POST requests are allowed", 405);
        }

        if (!request.url.endsWith("/v1/chat/completions")) {
            return getResponse("Not Found", 404);
        }

        const data = await request.json();
        const messages = data.messages || [];
        const model = CUSTOMER_MODEL_MAP[data.model] || data.model;
        const temperature = data.temperature || 0.7;
        const max_tokens = data.max_tokens || 8196;
        const top_p = Math.min(Math.max(data.top_p || 0.9, 0.0001), 0.9999);
        const stream = data.stream || false;

        const requestBody = {
            model: model,
            stream: stream,
            temperature: temperature,
            max_tokens: max_tokens,
            top_p: top_p,
            messages: messages
        };

        const apiUrl = `https://api-inference.huggingface.co/models/${model}/v1/chat/completions`;
        const response = await fetch(apiUrl, {
            method: 'POST',
            headers: {
                'Authorization': `Bearer ${HUGGINGFACE_API_KEY}`,
                'Content-Type': 'application/json'
            },
            body: JSON.stringify(requestBody)
        });

        if (!response.ok) {
            const errorText = await response.text();
            return getResponse(`Error from API: ${response.statusText} - ${errorText}`, response.status);
        }

        const newResponse = new Response(response.body, {
            status: response.status,
            headers: {
                ...Object.fromEntries(response.headers),
                'Access-Control-Allow-Origin': '*',
                'Access-Control-Allow-Methods': '*',
                'Access-Control-Allow-Headers': '*'
            }
        });

        return newResponse;
    } catch (error) {
        return getResponse(JSON.stringify({
            error: `处理请求失败: ${error.message}`
        }), 500);
    }
}

function getResponse(resp, status) {
    return new Response(resp, {
        status: status,
        headers: {
            "Content-Type": "application/json",
            "Access-Control-Allow-Origin": "*",
            "Access-Control-Allow-Methods": "*",
            "Access-Control-Allow-Headers": "*"
        }
    });
}

addEventListener('fetch', event => {
    event.respondWith(handleRequest(event.request))
})

本文出自:在cloudflare部署hugging face的免费api,可对接oneapi/newapi,免费使用Qwen2.5 72B等模型 - 资源荟萃 - LINUX DO

RSS Feed 使用 Hugo 构建
主题 StackJimmy 设计