Created
April 15, 2024 08:02
-
-
Save zhu327/4bacd6513497ee56c413eaa208d9cdd5 to your computer and use it in GitHub Desktop.
groq api proxy with deno deploy
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
import { serve } from "https://deno.land/std@0.181.0/http/server.ts"; | |
async function handleRequest(request: Request): Promise<Response> { | |
if (request.method === "OPTIONS") { | |
return handleOPTIONS(request); | |
} | |
const url = new URL(request.url); | |
url.host = "api.groq.com"; | |
url.pathname = "/openai" + url.pathname; | |
let body; | |
if (request.method === "POST") { | |
body = await request.json(); | |
body.model = "mixtral-8x7b-32768"; | |
} | |
const modifiedRequest = new Request(url.toString(), { | |
headers: request.headers, | |
method: request.method, | |
body: typeof body === "object" ? JSON.stringify(body) : null, | |
redirect: "follow", | |
}); | |
return fetch(modifiedRequest); | |
} | |
function handleOPTIONS(request: Request): Response { | |
return new Response(null, { | |
headers: { | |
"Access-Control-Allow-Origin": "*", | |
"Access-Control-Allow-Methods": "*", | |
"Access-Control-Allow-Headers": "*", | |
}, | |
}); | |
} | |
serve(handleRequest); |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment