Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Configurable Azure OpenAI (server side), updated and completed german translations #2505

Closed
wants to merge 2 commits into from
Closed
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
19 changes: 17 additions & 2 deletions .env.template
Original file line number Diff line number Diff line change
@@ -1,5 +1,5 @@

# Your openai api key. (required)
# Your openai or azure api key. (required)
OPENAI_API_KEY=sk-xxxx

# Access passsword, separated by comma. (optional)
Expand All @@ -10,9 +10,24 @@ PROXY_URL=http://localhost:7890

# Override openai api request base url. (optional)
# Default: https://api.openai.com
# Examples: http://your-openai-proxy.com
# Examples: http://your-openai-proxy.com, https://<deployment>.openai.azure.com
BASE_URL=

# Use OpenAI on Azure. Set to "1" for Azure
# Default: Empty
OPENAI_ON_AZURE=

# (optional)
# Azure API version, e.g. 2023-05-15
# Default: Empty
AZURE_OPENAI_APIVERSION=

# (optional)
# Azure deployment id
# Default: Empty
# Examples: gpt-3.5-turbo, MY_Azure_Deployment
AZURE_OPENAI_DEPLOYMENT_ID=

# Specify OpenAI organization ID.(optional)
# Default: Empty
# If you do not want users to input their own API key, set this value to 1.
Expand Down
13 changes: 12 additions & 1 deletion app/api/common.ts
Original file line number Diff line number Diff line change
@@ -1,4 +1,5 @@
import { NextRequest, NextResponse } from "next/server";
import { getServerSideConfig } from "../config/server";

export const OPENAI_URL = "api.openai.com";
const DEFAULT_PROTOCOL = "https";
Expand All @@ -7,6 +8,7 @@ const BASE_URL = process.env.BASE_URL || OPENAI_URL;
const DISABLE_GPT4 = !!process.env.DISABLE_GPT4;

export async function requestOpenai(req: NextRequest) {
const config = getServerSideConfig();
const controller = new AbortController();
const authValue = req.headers.get("Authorization") ?? "";
const openaiPath = `${req.nextUrl.pathname}${req.nextUrl.search}`.replaceAll(
Expand All @@ -31,11 +33,20 @@ export async function requestOpenai(req: NextRequest) {
controller.abort();
}, 10 * 60 * 1000);

const fetchUrl = `${baseUrl}/${openaiPath}`;
let fetchUrl: string;
if (config.openaiOnAzure === "1") {
const deploymentId = config.azureDeploymentId;
const apiVersion = config.azureApiVersion;
fetchUrl = `${baseUrl}/openai/deployments/${deploymentId}/chat/completions?api-version=${apiVersion}`;
} else {
fetchUrl = `${baseUrl}/${openaiPath}`;
}

const fetchOptions: RequestInit = {
headers: {
"Content-Type": "application/json",
"Cache-Control": "no-store",
"api-key": config.apiKey ? config.apiKey : "",
Authorization: authValue,
...(process.env.OPENAI_ORG_ID && {
"OpenAI-Organization": process.env.OPENAI_ORG_ID,
Expand Down
5 changes: 4 additions & 1 deletion app/api/openai/[...path]/route.ts
Original file line number Diff line number Diff line change
Expand Up @@ -32,7 +32,10 @@ async function handle(

const subpath = params.path.join("/");

if (!ALLOWD_PATH.has(subpath)) {
if (
!ALLOWD_PATH.has(subpath) &&
!subpath.startsWith("/openai/deployments/")
) {
console.log("[OpenAI Route] forbidden path ", subpath);
return NextResponse.json(
{
Expand Down
6 changes: 6 additions & 0 deletions app/config/server.ts
Original file line number Diff line number Diff line change
Expand Up @@ -6,6 +6,9 @@ declare global {
OPENAI_API_KEY?: string;
CODE?: string;
BASE_URL?: string;
OPENAI_ON_AZURE?: string;
AZURE_OPENAI_APIVERSION?: string;
AZURE_OPENAI_DEPLOYMENT_ID?: string;
PROXY_URL?: string;
VERCEL?: string;
HIDE_USER_API_KEY?: string; // disable user's api key input
Expand Down Expand Up @@ -39,6 +42,9 @@ export const getServerSideConfig = () => {

return {
apiKey: process.env.OPENAI_API_KEY,
openaiOnAzure: process.env.OPENAI_ON_AZURE,
azureApiVersion: process.env.AZURE_OPENAI_APIVERSION,
azureDeploymentId: process.env.AZURE_OPENAI_DEPLOYMENT_ID,
code: process.env.CODE,
codes: ACCESS_CODES,
needCode: ACCESS_CODES.size > 0,
Expand Down
Loading