common.ts 2.2 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687
  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. cache: "no-store",
  36. method: req.method,
  37. body: req.clone().body,
  38. signal: controller.signal,
  39. };
  40. // #1815 try to refuse gpt4 request
  41. if (DISABLE_GPT4) {
  42. try {
  43. const clonedBody = await req.clone().json();
  44. if ((clonedBody?.model ?? "").includes("gpt-4")) {
  45. return NextResponse.json(
  46. {
  47. error: true,
  48. message: "you are not allowed to use gpt-4 model",
  49. },
  50. {
  51. status: 403,
  52. },
  53. );
  54. }
  55. } catch (e) {
  56. console.error("[OpenAI] gpt4 filter", e);
  57. }
  58. }
  59. try {
  60. const res = await fetch(fetchUrl, fetchOptions);
  61. if (res.status === 401) {
  62. // to prevent browser prompt for credentials
  63. const newHeaders = new Headers(res.headers);
  64. newHeaders.delete("www-authenticate");
  65. return new Response(res.body, {
  66. status: res.status,
  67. statusText: res.statusText,
  68. headers: newHeaders,
  69. });
  70. }
  71. return res;
  72. } finally {
  73. clearTimeout(timeoutId);
  74. }
  75. }