Skip to content

Instantly share code, notes, and snippets.

@zhu327
Created April 15, 2024 08:02
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save zhu327/4bacd6513497ee56c413eaa208d9cdd5 to your computer and use it in GitHub Desktop.
Save zhu327/4bacd6513497ee56c413eaa208d9cdd5 to your computer and use it in GitHub Desktop.
groq api proxy with deno deploy
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