common.ts 5.9 KB

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