common.ts 5.9 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192
  1. import { NextRequest, NextResponse } from "next/server";
  2. import { getServerSideConfig } from "../config/server";
  3. import {
  4. DEFAULT_MODELS,
  5. OPENAI_BASE_URL,
  6. GEMINI_BASE_URL,
  7. ServiceProvider,
  8. } from "../constant";
  9. import { isModelAvailableInServer } from "../utils/model";
  10. import { cloudflareAIGatewayUrl } from "../utils/cloudflare";
  11. const serverConfig = getServerSideConfig();
  12. export async function requestOpenai(req: NextRequest) {
  13. const controller = new AbortController();
  14. const isAzure = req.nextUrl.pathname.includes("azure/deployments");
  15. var authValue,
  16. authHeaderName = "";
  17. if (isAzure) {
  18. authValue =
  19. req.headers
  20. .get("Authorization")
  21. ?.trim()
  22. .replaceAll("Bearer ", "")
  23. .trim() ?? "";
  24. authHeaderName = "api-key";
  25. } else {
  26. authValue = req.headers.get("Authorization") ?? "";
  27. authHeaderName = "Authorization";
  28. }
  29. let path = `${req.nextUrl.pathname}`.replaceAll("/api/openai/", "");
  30. let baseUrl =
  31. (isAzure ? serverConfig.azureUrl : serverConfig.baseUrl) || OPENAI_BASE_URL;
  32. if (!baseUrl.startsWith("http")) {
  33. baseUrl = `https://${baseUrl}`;
  34. }
  35. if (baseUrl.endsWith("/")) {
  36. baseUrl = baseUrl.slice(0, -1);
  37. }
  38. console.log("[Proxy] ", path);
  39. console.log("[Base Url]", baseUrl);
  40. const timeoutId = setTimeout(
  41. () => {
  42. controller.abort();
  43. },
  44. 10 * 60 * 1000,
  45. );
  46. if (isAzure) {
  47. const azureApiVersion =
  48. req?.nextUrl?.searchParams?.get("api-version") ||
  49. serverConfig.azureApiVersion;
  50. baseUrl = baseUrl.split("/deployments").shift() as string;
  51. path = `${req.nextUrl.pathname.replaceAll(
  52. "/api/azure/",
  53. "",
  54. )}?api-version=${azureApiVersion}`;
  55. // Forward compatibility:
  56. // if display_name(deployment_name) not set, and '{deploy-id}' in AZURE_URL
  57. // then using default '{deploy-id}'
  58. if (serverConfig.customModels && serverConfig.azureUrl) {
  59. const modelName = path.split("/")[1];
  60. let realDeployName = "";
  61. serverConfig.customModels
  62. .split(",")
  63. .filter((v) => !!v && !v.startsWith("-") && v.includes(modelName))
  64. .forEach((m) => {
  65. const [fullName, displayName] = m.split("=");
  66. const [_, providerName] = fullName.split("@");
  67. if (providerName === "azure" && !displayName) {
  68. const [_, deployId] = (serverConfig?.azureUrl ?? "").split(
  69. "deployments/",
  70. );
  71. if (deployId) {
  72. realDeployName = deployId;
  73. }
  74. }
  75. });
  76. if (realDeployName) {
  77. console.log("[Replace with DeployId", realDeployName);
  78. path = path.replaceAll(modelName, realDeployName);
  79. }
  80. }
  81. }
  82. const fetchUrl = cloudflareAIGatewayUrl(`${baseUrl}/${path}`);
  83. console.log("fetchUrl", fetchUrl);
  84. const fetchOptions: RequestInit = {
  85. headers: {
  86. "Content-Type": "application/json",
  87. "Cache-Control": "no-store",
  88. [authHeaderName]: authValue,
  89. ...(serverConfig.openaiOrgId && {
  90. "OpenAI-Organization": serverConfig.openaiOrgId,
  91. }),
  92. },
  93. method: req.method,
  94. body: req.body,
  95. // to fix #2485: https://stackoverflow.com/questions/55920957/cloudflare-worker-typeerror-one-time-use-body
  96. redirect: "manual",
  97. // @ts-ignore
  98. duplex: "half",
  99. signal: controller.signal,
  100. };
  101. // #1815 try to refuse gpt4 request
  102. if (serverConfig.customModels && req.body) {
  103. try {
  104. const clonedBody = await req.text();
  105. fetchOptions.body = clonedBody;
  106. const jsonBody = JSON.parse(clonedBody) as { model?: string };
  107. // not undefined and is false
  108. if (
  109. isModelAvailableInServer(
  110. serverConfig.customModels,
  111. jsonBody?.model as string,
  112. ServiceProvider.OpenAI as string,
  113. ) ||
  114. isModelAvailableInServer(
  115. serverConfig.customModels,
  116. jsonBody?.model as string,
  117. ServiceProvider.Azure as string,
  118. )
  119. ) {
  120. return NextResponse.json(
  121. {
  122. error: true,
  123. message: `you are not allowed to use ${jsonBody?.model} model`,
  124. },
  125. {
  126. status: 403,
  127. },
  128. );
  129. }
  130. } catch (e) {
  131. console.error("[OpenAI] gpt4 filter", e);
  132. }
  133. }
  134. try {
  135. const res = await fetch(fetchUrl, fetchOptions);
  136. // Extract the OpenAI-Organization header from the response
  137. const openaiOrganizationHeader = res.headers.get("OpenAI-Organization");
  138. // Check if serverConfig.openaiOrgId is defined and not an empty string
  139. if (serverConfig.openaiOrgId && serverConfig.openaiOrgId.trim() !== "") {
  140. // If openaiOrganizationHeader is present, log it; otherwise, log that the header is not present
  141. console.log("[Org ID]", openaiOrganizationHeader);
  142. } else {
  143. console.log("[Org ID] is not set up.");
  144. }
  145. // to prevent browser prompt for credentials
  146. const newHeaders = new Headers(res.headers);
  147. newHeaders.delete("www-authenticate");
  148. // to disable nginx buffering
  149. newHeaders.set("X-Accel-Buffering", "no");
  150. // Conditionally delete the OpenAI-Organization header from the response if [Org ID] is undefined or empty (not setup in ENV)
  151. // Also, this is to prevent the header from being sent to the client
  152. if (!serverConfig.openaiOrgId || serverConfig.openaiOrgId.trim() === "") {
  153. newHeaders.delete("OpenAI-Organization");
  154. }
  155. // The latest version of the OpenAI API forced the content-encoding to be "br" in json response
  156. // So if the streaming is disabled, we need to remove the content-encoding header
  157. // Because Vercel uses gzip to compress the response, if we don't remove the content-encoding header
  158. // The browser will try to decode the response with brotli and fail
  159. newHeaders.delete("content-encoding");
  160. return new Response(res.body, {
  161. status: res.status,
  162. statusText: res.statusText,
  163. headers: newHeaders,
  164. });
  165. } finally {
  166. clearTimeout(timeoutId);
  167. }
  168. }