diff --git a/app/api/common.ts b/app/api/common.ts index 53ab18ed6..7fb9ff89a 100644 --- a/app/api/common.ts +++ b/app/api/common.ts @@ -1,15 +1,21 @@ import { NextRequest } from "next/server"; const OPENAI_URL = "api.openai.com"; +const AZURE_OPENAI_URL = "azure-openai-gpt.openai.azure.com"; const DEFAULT_PROTOCOL = "https"; const PROTOCOL = process.env.PROTOCOL ?? DEFAULT_PROTOCOL; -const BASE_URL = process.env.BASE_URL ?? OPENAI_URL; export async function requestOpenai(req: NextRequest) { const apiKey = req.headers.get("token"); const openaiPath = req.headers.get("path"); - let baseUrl = BASE_URL; + let baseUrl = OPENAI_URL; + if (openaiPath?.includes("/deployments/")) { + baseUrl = AZURE_OPENAI_URL; + } + if (process.env.BASE_URL) { + baseUrl = process.env.BASE_URL; + } if (!baseUrl.startsWith("http")) { baseUrl = `${PROTOCOL}://${baseUrl}`; @@ -22,6 +28,7 @@ export async function requestOpenai(req: NextRequest) { headers: { "Content-Type": "application/json", Authorization: `Bearer ${apiKey}`, + "api-key": apiKey || "", }, method: req.method, body: req.body, diff --git a/app/components/settings.tsx b/app/components/settings.tsx index d81b5b358..c0e144876 100644 --- a/app/components/settings.tsx +++ b/app/components/settings.tsx @@ -23,6 +23,7 @@ import { useUpdateStore, useAccessStore, ModalConfigValidator, + AZURE_API_VERSION, } from "../store"; import { Avatar } from "./chat"; @@ -466,6 +467,34 @@ export function Settings(props: { closeSettings: () => void }) { <> )} + + { + accessStore.switchAOAI(e.currentTarget.checked); + }} + > + + + {accessStore.enableAOAI ? ( + + { + accessStore.updateDeployName(e.currentTarget.value); + }} + /> + + ) : ( + <> + )} + void }) { ); }} > - {ALL_MODELS.map((v) => ( - - ))} + {(accessStore.enableAOAI ? AZURE_API_VERSION : ALL_MODELS).map( + (v) => ( + + ), + )} fetch("/api/openai?_vercel_no_cache=1", { @@ -64,7 +75,7 @@ export function requestOpenaiClient(path: string) { export async function requestChat(messages: Message[]) { const req: ChatRequest = makeRequestParam(messages, { filterBot: true }); - const res = await requestOpenaiClient("v1/chat/completions")(req); + const res = await requestOpenaiClient(getRequestPath())(req); try { const response = (await res.json()) as ChatResponse; @@ -149,7 +160,7 @@ export async function requestChatStream( method: "POST", headers: { "Content-Type": "application/json", - path: "v1/chat/completions", + path: getRequestPath(), ...getHeaders(), }, body: JSON.stringify(req), diff --git a/app/store/access.ts b/app/store/access.ts index aed131684..3f132097b 100644 --- a/app/store/access.ts +++ b/app/store/access.ts @@ -5,10 +5,15 @@ export interface AccessControlStore { accessCode: string; token: string; + enableAOAI: boolean; + azureDeployName: string; + needCode: boolean; updateToken: (_: string) => void; updateCode: (_: string) => void; + updateDeployName: (_: string) => void; + switchAOAI: (_: boolean) => void; enabledAccessControl: () => boolean; isAuthorized: () => boolean; fetch: () => void; @@ -23,6 +28,8 @@ export const useAccessStore = create()( (set, get) => ({ token: "", accessCode: "", + azureDeployName: "", + enableAOAI: false, needCode: true, enabledAccessControl() { get().fetch(); @@ -35,8 +42,18 @@ export const useAccessStore = create()( updateToken(token: string) { set((state) => ({ token })); }, + updateDeployName(azureDeployName: string) { + set((state) => ({ azureDeployName })); + }, + switchAOAI(switchStatus: boolean) { + set((state) => ({ enableAOAI: switchStatus })); + }, isAuthorized() { // has token or has code or disabled access control + if (get().enableAOAI) { + return !!get().azureDeployName && !!get().token; + } + return ( !!get().token || !!get().accessCode || !get().enabledAccessControl() ); diff --git a/app/store/app.ts b/app/store/app.ts index 8d875fee5..e9f7f7134 100644 --- a/app/store/app.ts +++ b/app/store/app.ts @@ -71,6 +71,13 @@ export const ROLES: Message["role"][] = ["system", "user", "assistant"]; const ENABLE_GPT4 = true; +export const AZURE_API_VERSION = [ + { + name: "2023-03-15-preview", + available: true, + }, +]; + export const ALL_MODELS = [ { name: "gpt-4",