From 7ad1160111a54cd655102b863e3a48d059a47430 Mon Sep 17 00:00:00 2001 From: tianzhou Date: Fri, 1 Sep 2023 19:07:32 +0800 Subject: [PATCH] feat: add NEXT_PUBLIC_ALLOW_SELF_OPENAI_KEY --- README.es-ES.md | 2 + README.md | 1 + README.zh-CN.md | 1 + process.d.ts | 2 + src/components/OpenAIApiConfigView.tsx | 66 ++++++++++++++------------ src/components/QuotaOverflowBanner.tsx | 9 ++-- src/components/QuotaView.tsx | 3 +- src/utils/openai.ts | 4 ++ 8 files changed, 54 insertions(+), 34 deletions(-) diff --git a/README.es-ES.md b/README.es-ES.md index cd15af9..25518c2 100644 --- a/README.es-ES.md +++ b/README.es-ES.md @@ -51,6 +51,8 @@ docker run --name sqlchat --platform linux/amd64 -env NEXTAUTH_SECRET=xxx -p 300 ### Variables relacionadas con OpenAI: +- `NEXT_PUBLIC_ALLOW_SELF_OPENAI_KEY`: Establezca en "verdadero" para permitir a los usuarios traer su propia clave API de OpenAI. + - `OPENAI_API_KEY`: Clave API de OpenAI. Puedes conseguir una [aquí](https://beta.openai.com/docs/developer-quickstart/api-keys). - `OPENAI_API_ENDPOINT`: Endpoint de la API de OpenAI. El predeterminado es `https://api.openai.com`. diff --git a/README.md b/README.md index beb1ab3..2308056 100644 --- a/README.md +++ b/README.md @@ -51,6 +51,7 @@ docker run --name sqlchat --platform linux/amd64 -env NEXTAUTH_SECRET=xxx -p 300 ### OpenAI related variables: +- `NEXT_PUBLIC_ALLOW_SELF_OPENAI_KEY`: Set to `true` to allow users to bring their own OpenAI API key. - `OPENAI_API_KEY`: OpenAI API key. You can get one from [here](https://beta.openai.com/docs/developer-quickstart/api-keys). - `OPENAI_API_ENDPOINT`: OpenAI API endpoint. Defaults to `https://api.openai.com`. diff --git a/README.zh-CN.md b/README.zh-CN.md index f3252e6..9e187e1 100644 --- a/README.zh-CN.md +++ b/README.zh-CN.md @@ -51,6 +51,7 @@ docker run --name sqlchat --platform linux/amd64 -env NEXTAUTH_SECRET=xxx -p 300 ### OpenAI 相关变量: +- `NEXT_PUBLIC_ALLOW_SELF_OPENAI_KEY`: 设置为 `true` 如果你允许用户提供自己的 OpenAI API key. - `OPENAI_API_KEY`: OpenAI API Key,通过[这里](https://beta.openai.com/docs/developer-quickstart/api-keys)申请。 - `OPENAI_API_ENDPOINT`: OpenAI API 端点,默认为 `https://api.openai.com`。 diff --git a/process.d.ts b/process.d.ts index ad5d652..4b7cf5a 100644 --- a/process.d.ts +++ b/process.d.ts @@ -6,6 +6,8 @@ declare namespace NodeJS { // We can't prefix DATABASE_URL with NEXT_PUBLIC_ because it contains sensitive information that // should not be exposed to the client. NEXT_PUBLIC_DATABASE_LESS: string; + // Optional. Set to "true" to allow users to bring their own OpenAI API key. + NEXT_PUBLIC_ALLOW_SELF_OPENAI_KEY: string; // Required if NEXT_PUBLIC_DATABASE_LESS is false. Postgres database connection string to store // the data. e.g. postgresql://postgres:YOUR_PASSWORD@localhost:5432/sqlchat?schema=sqlchat DATABASE_URL: string; diff --git a/src/components/OpenAIApiConfigView.tsx b/src/components/OpenAIApiConfigView.tsx index 56334f6..a9e00cd 100644 --- a/src/components/OpenAIApiConfigView.tsx +++ b/src/components/OpenAIApiConfigView.tsx @@ -3,9 +3,11 @@ import { useTranslation } from "react-i18next"; import { useDebounce } from "react-use"; import { useSettingStore } from "@/store"; import { OpenAIApiConfig } from "@/types"; +import { allowSelfOpenAIKey } from "@/utils"; import Radio from "./kit/Radio"; import TextField from "./kit/TextField"; import Tooltip from "./kit/Tooltip"; + const OpenAIApiConfigView = () => { const { t } = useTranslation(); const settingStore = useSettingStore(); @@ -75,7 +77,7 @@ const OpenAIApiConfigView = () => {
-

{t("setting.openai-api-configuration.model-description")}

+ {allowSelfOpenAIKey() &&

{t("setting.openai-api-configuration.model-description")}

}
{models.map((model) => @@ -90,35 +92,39 @@ const OpenAIApiConfigView = () => {
-
- -

{t("setting.openai-api-configuration.key-description")}

- handleSetOpenAIApiConfig({ key: value })} - /> -
-
- -
-

{t("setting.openai-api-configuration.endpoint-description")}

- - {t("setting.openai-api-configuration.find-my-key")} - -
- handleSetOpenAIApiConfig({ endpoint: value })} - /> -
+ {allowSelfOpenAIKey() && ( + <> +
+ +

{t("setting.openai-api-configuration.key-description")}

+ handleSetOpenAIApiConfig({ key: value })} + /> +
+
+ +
+

{t("setting.openai-api-configuration.endpoint-description")}

+ + {t("setting.openai-api-configuration.find-my-key")} + +
+ handleSetOpenAIApiConfig({ endpoint: value })} + /> +
+ + )}
); diff --git a/src/components/QuotaOverflowBanner.tsx b/src/components/QuotaOverflowBanner.tsx index 685f257..791b903 100644 --- a/src/components/QuotaOverflowBanner.tsx +++ b/src/components/QuotaOverflowBanner.tsx @@ -1,5 +1,6 @@ import { useTranslation } from "react-i18next"; import { useLocalStorage } from "react-use"; +import { allowSelfOpenAIKey } from "@/utils"; import Link from "next/link"; import Icon from "./Icon"; @@ -22,9 +23,11 @@ const QuotaOverflowBanner = (props: Props) => { >
{t("banner.quota-overflow")}{" "} - - {t("banner.use-my-key")} - + {allowSelfOpenAIKey() && ( + + {t("banner.use-my-key")} + + )}
))} - {!!showSupplyOwnKey && ( + {allowSelfOpenAIKey() && !!showSupplyOwnKey && ( {t("banner.use-my-key")} diff --git a/src/utils/openai.ts b/src/utils/openai.ts index 375a8d8..b5c4363 100644 --- a/src/utils/openai.ts +++ b/src/utils/openai.ts @@ -50,3 +50,7 @@ export function generateDbPromptFromContext( } return promptGenerator(engine, finalTableList.join("\n\n")); } + +export function allowSelfOpenAIKey() { + return process.env.NEXT_PUBLIC_ALLOW_SELF_OPENAI_KEY == "true"; +}