【尝鲜】llama 405B Copilot 公车 & cf 自部署

本来想发 万key 大车的,结果被发现了,一个不剩,不敢批量注册了,车只放了十几个 ,但是不用key也行(可能429),还是发出来水一下

{
  "github.copilot.advanced": {
    "debug.overrideProxyUrl": "https://override.kici.me",
    "debug.chatOverrideProxyUrl": "https://override.kici.me/chat/completions",
    "authProvider": "github-enterprise",
    "debug.overrideCAPIUrl": "https://override.kici.me/v1"
  },
  "github-enterprise.uri": "https://cocopilot.org/"
}

自部署代码, 有key 可以多key 轮询

const OPENAI_API_URL = 'https://api.deepinfra.com';

// 定义允许的模型列表
const allowedModels = [
    "meta-llama/Meta-Llama-3.1-70B-Instruct",
    "meta-llama/Meta-Llama-3.1-8B-Instruct",
    "meta-llama/Meta-Llama-3.1-405B-Instruct",
    "mistralai/Mixtral-8x22B-Instruct-v0.1",
    "mistralai/Mixtral-8x7B-Instruct-v0.1",
    "Qwen/Qwen2-72B-Instruct",
    "mistralai/Mistral-7B-Instruct-v0.3",
    "openchat/openchat-3.6-8b",
    "google/gemma-2-27b-it"
];

const tokens = []
addEventListener('fetch', event => {
  event.respondWith(handleRequest(event.request).catch(err => handleError(err)));
});
// 添加延时函数
function delay(ms) {
  return new Promise(resolve => setTimeout(resolve, ms));
}

// 添加防抖函数
function debounce(func, wait) {
  let timeout;
  return function executedFunction(...args) {
    return new Promise((resolve) => {
      const later = () => {
        clearTimeout(timeout);
        resolve(func(...args));
      };
      clearTimeout(timeout);
      timeout = setTimeout(later, wait);
    });
  };
}

async function handleRequest(request) {
  const url = new URL(request.url);
  const path = url.pathname;

  // Handle CORS
  const headers_Origin = request.headers.get("Origin") || "*";
  const corsHeaders = {
    "Access-Control-Allow-Origin": headers_Origin,
    "Access-Control-Allow-Methods": "GET, POST, OPTIONS",
    "Access-Control-Allow-Headers": "Content-Type, Authorization",
  };

  // Handle preflight requests
  if (request.method === "OPTIONS") {
    return new Response(null, { headers: corsHeaders });
  }

  const routes = getRoutes();
  const route = routes[path];
  if (!route) {
    return new Response('Not Found', { status: 404, headers: corsHeaders });
  }

  // 创建一个 AbortController 来处理取消请求
  const controller = new AbortController();
  const { signal } = controller;

  // 使用防抖函数包装路由处理器
  const debouncedHandler = debounce(async () => {
    if (route.waitTime > 0) {
      try {
        await delay(route.waitTime);
        console.log(`delay ${route.waitTime}ms done`);
        // 检查在等待结束后请求是否被取消
        if (signal.aborted) {
          console.log('Request was cancelled by the client during wait time.');
          return new Response('Request cancelled', { status: 499 });
        }
      } catch (error) {
        if (error.name === 'AbortError') {
          console.log('Request was cancelled by the client during wait time.');
          return new Response('Request cancelled', { status: 499 });
        }
        throw error;
      }
    }
    return route.handler(request, corsHeaders, route.waitTime, signal);
  }, 100); // 100ms 的防抖时间

  return debouncedHandler();
}

function getRoutes() {
    return {
        '/': {
            handler: handleRoot,
            waitTime: 0
        },
        '/v1/engines/copilot-codex/completions': {
            handler: handleCompletions,
            waitTime: 200 // 添加 200ms 延迟
        },
        '/v1/models': {
            handler: handleModels,
            waitTime: 0 
        },
        '/v1/agents': {
            handler: handleAgents,
            waitTime: 0 
        },
        '/v1/chat/completions': {
            handler: handleChatCompletions,
            waitTime: 0 
        },
        '/chat/completions': {
            handler: handleChatCompletions,
            waitTime: 0 
        },
        '/completions': {
            handler: handleCompletions,
            waitTime: 200 
        },
        '/v1/completions': {
            handler: handleCompletions,
            waitTime: 200 
        },
        '/v1/dashboard/billing/subscription': {
            handler: handleBillingSubscription,
            waitTime: 0 
        },
        '/dashboard/billing/subscription': {
            handler: handleBillingSubscription,
            waitTime: 0 
        },
        '/billing/subscription': {
            handler: handleBillingSubscription,
            waitTime: 0 
        },
        '/dashboard/billing/usage': {
            handler: handleBillingUsage,
            waitTime: 0 
        },
        '/v1/dashboard/billing/usage': {
            handler: handleBillingUsage,
            waitTime: 0 
        }
    };
}

async function handleRoot(request, corsHeaders, waitTime, signal) {
    return new Response("别打了" + request.headers.get('CF-Connecting-IP'), { status: 200, headers: corsHeaders });
}

async function handleChatCompletions(request, corsHeaders, waitTime, signal) {
    return handleOpenAIRequest(request, corsHeaders, '/v1/openai/chat/completions', signal);
}

async function handleCompletions(request, corsHeaders, waitTime, signal) {
    return handleOpenAIRequest(request, corsHeaders, '/v1/openai/completions', signal);
}

async function handleBillingSubscription(request, corsHeaders, waitTime, signal) {
    const subscriptionInfo = {
        "object": "billing_subscription",
        "has_payment_method": true,
        "soft_limit_usd": 1000,
        "hard_limit_usd": 1000,
        "system_hard_limit_usd": 1,
        "access_until": 0
    };
    return new Response(JSON.stringify(subscriptionInfo), {
        status: 200,
        headers: { ...corsHeaders, "Content-Type": "application/json" }
    });
}

async function handleBillingUsage(request, corsHeaders, waitTime, signal) {
    const usageInfo = {
        "object": "list",
        "total_usage": 0.00
    };
    return new Response(JSON.stringify(usageInfo), {
        status: 200,
        headers: { ...corsHeaders, "Content-Type": "application/json" }
    });
}

async function handleOpenAIRequest(request, corsHeaders, newPath, signal) {
    const url = new URL(request.url);
    url.pathname = newPath;
    url.host = OPENAI_API_URL.replace(/^https?:\/\//, '');

    const newHeaders = new Headers(request.headers);
    const newBody = await request.json();

    //  Cloudflare-related headers
    const cfHeaders = [
        'CF-Connecting-IP', 'CF-IPCountry', 'CF-Visitor', 'CF-Ray',
        'CF-Worker', 'CF-Device-Type', 'CF-Connecting-IP',
        'CF-IPCountry', 'CF-Visitor', 'CF-Ray', 'CF-Worker', 'CF-Device-Type'
    ];
    cfHeaders.forEach(header => newHeaders.delete(header));

    // Authorization header
    if (tokens.length > 0) {
        const randomToken = tokens[Math.floor(Math.random() * tokens.length)];
        newHeaders.set('authorization', `Bearer ${randomToken}`);
    } else {
        newHeaders.set('authorization', ` `);
    }

    if (newPath === '/v1/openai/completions') {
        newBody.stop = [
            "\n",
            "\n\n\n",
            "\n\n",
            "\n```"
        ]
        newBody.max_tokens = 500;
        newBody.stream = true;
        // Set other parameters
        newBody.n = 1;
    }
    newBody.model = "meta-llama/Meta-Llama-3.1-405B-Instruct";

    const modifiedRequest = new Request(url.toString(), {
        headers: newHeaders,
        method: request.method,
        body: JSON.stringify(newBody),
        redirect: 'follow',
        signal: signal // 添加 signal 以支持取消请求
    });

    try {
        const response = await fetch(modifiedRequest);
        const modifiedResponse = new Response(response.body, response);
        modifiedResponse.headers.set('Access-Control-Allow-Origin', corsHeaders["Access-Control-Allow-Origin"]);
        return modifiedResponse;
    } catch (err) {
        if (err.name === 'AbortError') {
            console.log('Request was cancelled by the client.');
            return new Response('Request cancelled', { status: 499 });
        }
        return handleError(err);
    }
}

function handleError(err) {
    console.error('Error occurred:', err);
    return new Response('Internal Server Error', { status: 500 });
}

function handleModels(request, corsHeaders, waitTime, signal) {
    const responseData = {
        data: [
            {
                capabilities: { family: "gpt-3.5-turbo", object: "model_capabilities", type: "chat" },
                id: "gpt-3.5-turbo",
                name: "GPT 3.5 Turbo",
                object: "model",
                version: "gpt-3.5-turbo-0613"
            },
            {
                capabilities: { family: "gpt-3.5-turbo", object: "model_capabilities", type: "chat" },
                id: "gpt-3.5-turbo-0613",
                name: "GPT 3.5 Turbo (2023-06-13)",
                object: "model",
                version: "gpt-3.5-turbo-0613"
            },
            {
                capabilities: { family: "gpt-4", object: "model_capabilities", type: "chat" },
                id: "gpt-4",
                name: "GPT 4",
                object: "model",
                version: "gpt-4-0613"
            },
            {
                capabilities: { family: "gpt-4", object: "model_capabilities", type: "chat" },
                id: "gpt-4-0613",
                name: "GPT 4 (2023-06-13)",
                object: "model",
                version: "gpt-4-0613"
            },
            {
                capabilities: { family: "gpt-4-turbo", object: "model_capabilities", type: "chat" },
                id: "gpt-4-0125-preview",
                name: "GPT 4 Turbo (2024-01-25 Preview)",
                object: "model",
                version: "gpt-4-0125-preview"
            },
            {
                capabilities: { family: "text-embedding-ada-002", object: "model_capabilities", type: "embeddings" },
                id: "text-embedding-ada-002",
                name: "Embedding V2 Ada",
                object: "model",
                version: "text-embedding-ada-002"
            },
            {
                capabilities: { family: "text-embedding-ada-002", object: "model_capabilities", type: "embeddings" },
                id: "text-embedding-ada-002-index",
                name: "Embedding V2 Ada (Index)",
                object: "model",
                version: "text-embedding-ada-002"
            },
            {
                capabilities: { family: "text-embedding-3-small", object: "model_capabilities", type: "embeddings" },
                id: "text-embedding-3-small",
                name: "Embedding V3 small",
                object: "model",
                version: "text-embedding-3-small"
            },
            {
                capabilities: { family: "text-embedding-3-small", object: "model_capabilities", type: "embeddings" },
                id: "text-embedding-3-small-inference",
                name: "Embedding V3 small (Inference)",
                object: "model",
                version: "text-embedding-3-small"
            }
        ],
        object: "list"
    };

    return new Response(JSON.stringify(responseData), {
        status: 200,
        headers: {
            'Content-Type': 'application/json',
            ...corsHeaders
        }
    });
}

function handleAgents(request, corsHeaders, waitTime, signal) {
    const responseData = { "agents": [] }
    return new Response(JSON.stringify(responseData), {
        status: 200,
        headers: {
            'Content-Type': 'application/json',
            ...corsHeaders
        }
    });
}

代码参考了https://linux.do/t/topic/160324 ,同时感谢 @fjiabinc 大佬的指导

17 Likes

感谢分享 :tieba_013:

1 Like

感谢分享 :tieba_013:

1 Like

感谢大佬分享

1 Like

感谢大佬分享

速度还挺快的,谢谢分享。

哇,这就上车

感谢分享,前排上车

感谢分享。。留个记号

速度好快 ,真牛逼

牛的,感谢分享

前排围观支持一下

佬 请问下如果部署在cf 上如何添加自己的校验码呢 这个会不会被fofo 扫到呢?

copolit 好像不允许其他验证,你可以尝试修改路径,

vscode 使用可能需要降级

这些模型支持FIM补全吗? github models里提供的好像不支持 是添加prompt了吗