common.ts 2.3 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970717273747576777879808182838485868788899091
  1. import { NextRequest, NextResponse } from "next/server";
  2. export const OPENAI_URL = "api.openai.com";
  3. const DEFAULT_PROTOCOL = "https";
  4. const PROTOCOL = process.env.PROTOCOL ?? DEFAULT_PROTOCOL;
  5. const BASE_URL = process.env.BASE_URL ?? OPENAI_URL;
  6. const DISABLE_GPT4 = !!process.env.DISABLE_GPT4;
  7. export async function requestOpenai(req: NextRequest) {
  8. const controller = new AbortController();
  9. const authValue = req.headers.get("Authorization") ?? "";
  10. const openaiPath = `${req.nextUrl.pathname}${req.nextUrl.search}`.replaceAll(
  11. "/api/openai/",
  12. "",
  13. );
  14. let baseUrl = BASE_URL;
  15. if (!baseUrl.startsWith("http")) {
  16. baseUrl = `${PROTOCOL}://${baseUrl}`;
  17. }
  18. console.log("[Proxy] ", openaiPath);
  19. console.log("[Base Url]", baseUrl);
  20. if (process.env.OPENAI_ORG_ID) {
  21. console.log("[Org ID]", process.env.OPENAI_ORG_ID);
  22. }
  23. const timeoutId = setTimeout(() => {
  24. controller.abort();
  25. }, 10 * 60 * 1000);
  26. const fetchUrl = `${baseUrl}/${openaiPath}`;
  27. const fetchOptions: RequestInit = {
  28. headers: {
  29. "Content-Type": "application/json",
  30. Authorization: authValue,
  31. ...(process.env.OPENAI_ORG_ID && {
  32. "OpenAI-Organization": process.env.OPENAI_ORG_ID,
  33. }),
  34. },
  35. method: req.method,
  36. body: req.body,
  37. // @ts-ignore
  38. duplex: "half",
  39. signal: controller.signal,
  40. };
  41. // #1815 try to refuse gpt4 request
  42. if (DISABLE_GPT4 && req.body) {
  43. try {
  44. const clonedBody = await req.text();
  45. fetchOptions.body = clonedBody;
  46. const jsonBody = JSON.parse(clonedBody);
  47. if ((jsonBody?.model ?? "").includes("gpt-4")) {
  48. return NextResponse.json(
  49. {
  50. error: true,
  51. message: "you are not allowed to use gpt-4 model",
  52. },
  53. {
  54. status: 403,
  55. },
  56. );
  57. }
  58. } catch (e) {
  59. console.error("[OpenAI] gpt4 filter", e);
  60. }
  61. }
  62. try {
  63. const res = await fetch(fetchUrl, fetchOptions);
  64. // to prevent browser prompt for credentials
  65. const newHeaders = new Headers(res.headers);
  66. newHeaders.delete("www-authenticate");
  67. // to disable nginx buffering
  68. newHeaders.set("X-Accel-Buffering", "no");
  69. return new Response(res.body, {
  70. status: res.status,
  71. statusText: res.statusText,
  72. headers: newHeaders,
  73. });
  74. } finally {
  75. clearTimeout(timeoutId);
  76. }
  77. }