File size: 4,332 Bytes
f290067 |
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 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 144 145 146 147 148 |
import { serve } from "https://deno.land/std/http/server.ts";
const handler = async (request) => {
const url = new URL(request.url);
// Handle CORS preflight requests
if (request.method === "OPTIONS") {
return new Response(null, {
headers: {
"Access-Control-Allow-Origin": "*",
"Access-Control-Allow-Methods": "POST, GET, OPTIONS",
"Access-Control-Allow-Headers": "Content-Type, Authorization",
"Access-Control-Max-Age": "86400",
},
});
}
const TOKEN = Deno.env.get("TOKEN");
// Authentication check
if (TOKEN) {
const authHeader = request.headers.get("Authorization");
if (!authHeader || authHeader !== `Bearer ${TOKEN}`) {
return new Response("Unauthorized", {
status: 401,
headers: {
"Content-Type": "application/json",
"Access-Control-Allow-Origin": "*",
},
});
}
}
// Handle /v1/models endpoint
if (url.pathname === "/v1/models" && request.method === "GET") {
const models = {
object: "list",
data: [
{
id: "deepseek-ai/DeepSeek-R1-0528-Turbo",
object: "model",
created: 1624980000,
owned_by: "deepseek-ai",
},
{
id: "deepseek-ai/DeepSeek-V3-0324-Turbo",
object: "model",
created: 1632000000,
owned_by: "deepseek-ai",
},
{
id: "deepseek-ai/DeepSeek-R1-Distill-Llama-70B",
object: "model",
created: 1640000000,
owned_by: "deepseek-ai",
},
{
id: "zai-org/GLM-4.6",
object: "model",
created: 1720000000,
owned_by: "zai-org",
},
{
id: "moonshotai/Kimi-K2-Instruct",
object: "model",
created: 1721000000,
owned_by: "moonshotai",
},
{
id: "deepseek-ai/DeepSeek-V3.2-Exp",
object: "model",
created: 1722000000,
owned_by: "deepseek-ai",
},
{
id: "deepseek-ai/DeepSeek-V3.1-Terminus",
object: "model",
created: 1723000000,
owned_by: "deepseek-ai",
},
],
};
return new Response(JSON.stringify(models), {
headers: {
"Content-Type": "application/json",
"Access-Control-Allow-Origin": "*",
},
});
}
// Handle chat completions
if (url.pathname === "/v1/chat/completions" && request.method === "POST") {
try {
const body = await request.json();
const headers = new Headers({
"User-Agent": "Mozilla/5.0 (Windows NT 10.0; Win64; x64) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/133.0.0.0 Safari/537.36 Edg/133.0.0.0",
"Accept": "text/event-stream",
"Accept-Encoding": "gzip, deflate, br, zstd",
"Content-Type": "application/json",
"sec-ch-ua-platform": "Windows",
"X-Deepinfra-Source": "web-page",
"sec-ch-ua": '"Not(A:Brand";v="99", "Microsoft Edge";v="133", "Chromium";v="133"',
"sec-ch-ua-mobile": "?0",
"Origin": "https://deepinfra.com",
"Sec-Fetch-Site": "same-site",
"Sec-Fetch-Mode": "cors",
"Sec-Fetch-Dest": "empty",
"Referer": "https://deepinfra.com/",
});
const response = await fetch("https://api.deepinfra.com/v1/openai/chat/completions", {
method: "POST",
headers: headers,
body: JSON.stringify(body),
});
return new Response(response.body, {
status: response.status,
statusText: response.statusText,
headers: {
"Access-Control-Allow-Origin": "*",
"Content-Type": response.headers.get("Content-Type") || "application/json",
},
});
} catch (error) {
return new Response(JSON.stringify({ error: error.message }), {
status: 500,
headers: {
"Content-Type": "application/json",
"Access-Control-Allow-Origin": "*",
},
});
}
}
return new Response("Not Found", {
status: 404,
headers: {
"Access-Control-Allow-Origin": "*",
}
});
};
const port = Deno.env.get("PORT") ? parseInt(Deno.env.get("PORT")) : 7860;
console.log(`HTTP server running. Access it at: http://localhost:${port}/`);
serve(handler, { port });
|