11import { NextRequest , NextResponse } from "next/server" ;
22import { getServerSideConfig } from "../config/server" ;
33import { DEFAULT_MODELS , OPENAI_BASE_URL } from "../constant" ;
4- import { collectModelTable } from "../utils/model" ;
5- import { makeAzurePath } from "../azure" ;
4+ import { collectModelTable , collectModels } from "../utils/model" ;
65
76const serverConfig = getServerSideConfig ( ) ;
87
98export async function requestOpenai ( req : NextRequest ) {
109 const controller = new AbortController ( ) ;
11-
1210 const authValue = req . headers . get ( "Authorization" ) ?? "" ;
13- const authHeaderName = serverConfig . isAzure ? "api-key" : "Authorization" ;
14-
15- let path = `${ req . nextUrl . pathname } ${ req . nextUrl . search } ` . replaceAll (
11+ const openaiPath = `${ req . nextUrl . pathname } ${ req . nextUrl . search } ` . replaceAll (
1612 "/api/openai/" ,
1713 "" ,
1814 ) ;
1915
20- let baseUrl =
21- serverConfig . azureUrl || serverConfig . baseUrl || OPENAI_BASE_URL ;
16+ let baseUrl = serverConfig . baseUrl ?? OPENAI_BASE_URL ;
2217
2318 if ( ! baseUrl . startsWith ( "http" ) ) {
2419 baseUrl = `https://${ baseUrl } ` ;
@@ -28,12 +23,9 @@ export async function requestOpenai(req: NextRequest) {
2823 baseUrl = baseUrl . slice ( 0 , - 1 ) ;
2924 }
3025
31- console . log ( "[Proxy] " , path ) ;
26+ console . log ( "[Proxy] " , openaiPath ) ;
3227 console . log ( "[Base Url]" , baseUrl ) ;
33- // this fix [Org ID] undefined in server side if not using custom point
34- if ( serverConfig . openaiOrgId !== undefined ) {
35- console . log ( "[Org ID]" , serverConfig . openaiOrgId ) ;
36- }
28+ console . log ( "[Org ID]" , serverConfig . openaiOrgId ) ;
3729
3830 const timeoutId = setTimeout (
3931 ( ) => {
@@ -42,24 +34,14 @@ export async function requestOpenai(req: NextRequest) {
4234 10 * 60 * 1000 ,
4335 ) ;
4436
45- if ( serverConfig . isAzure ) {
46- if ( ! serverConfig . azureApiVersion ) {
47- return NextResponse . json ( {
48- error : true ,
49- message : `missing AZURE_API_VERSION in server env vars` ,
50- } ) ;
51- }
52- path = makeAzurePath ( path , serverConfig . azureApiVersion ) ;
53- }
54-
55- const fetchUrl = `${ baseUrl } /${ path } ` ;
37+ const fetchUrl = `${ baseUrl } /${ openaiPath } ` ;
5638 const fetchOptions : RequestInit = {
5739 headers : {
5840 "Content-Type" : "application/json" ,
5941 "Cache-Control" : "no-store" ,
60- [ authHeaderName ] : authValue ,
61- ...( serverConfig . openaiOrgId && {
62- "OpenAI-Organization" : serverConfig . openaiOrgId ,
42+ Authorization : authValue ,
43+ ...( process . env . OPENAI_ORG_ID && {
44+ "OpenAI-Organization" : process . env . OPENAI_ORG_ID ,
6345 } ) ,
6446 } ,
6547 method : req . method ,
@@ -84,7 +66,7 @@ export async function requestOpenai(req: NextRequest) {
8466 const jsonBody = JSON . parse ( clonedBody ) as { model ?: string } ;
8567
8668 // not undefined and is false
87- if ( modelTable [ jsonBody ?. model ?? "" ] . available === false ) {
69+ if ( modelTable [ jsonBody ?. model ?? "" ] === false ) {
8870 return NextResponse . json (
8971 {
9072 error : true ,
0 commit comments