common.ts 5.9 KB

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