From e4468fd93fd265854f711a815f3ab57e2e5a48d5 Mon Sep 17 00:00:00 2001 From: Jing Hua Date: Sun, 2 Apr 2023 20:43:54 +0800 Subject: [PATCH] support environment variables fixes #159 --- .env.example | 5 +++++ .gitignore | 2 ++ src/components/ApiPopup/ApiPopup.tsx | 4 +++- src/constants/auth.ts | 10 +++++----- src/constants/chat.ts | 4 +++- src/store/auth-slice.ts | 1 + 6 files changed, 19 insertions(+), 7 deletions(-) create mode 100644 .env.example diff --git a/.env.example b/.env.example new file mode 100644 index 0000000..2a3d554 --- /dev/null +++ b/.env.example @@ -0,0 +1,5 @@ +# All options are optional, remove those you do not need +VITE_CUSTOM_API_ENDPOINT= +VITE_DEFAULT_API_ENDPOINT= +VITE_OPENAI_API_KEY= +VITE_DEFAULT_SYSTEM_MESSAGE= # Remove this line if you want to use the default system message of Better ChatGPT \ No newline at end of file diff --git a/.gitignore b/.gitignore index 03fe3a9..7912dae 100644 --- a/.gitignore +++ b/.gitignore @@ -24,3 +24,5 @@ dist-ssr *.sw? release/ + +.env \ No newline at end of file diff --git a/src/components/ApiPopup/ApiPopup.tsx b/src/components/ApiPopup/ApiPopup.tsx index 5425a91..42f42ae 100644 --- a/src/components/ApiPopup/ApiPopup.tsx +++ b/src/components/ApiPopup/ApiPopup.tsx @@ -14,7 +14,9 @@ const ApiPopup = () => { const setFirstVisit = useStore((state) => state.setFirstVisit); const [_apiKey, _setApiKey] = useState(apiKey || ''); - const [isModalOpen, setIsModalOpen] = useState(firstVisit); + const [isModalOpen, setIsModalOpen] = useState( + !apiKey && firstVisit + ); const [error, setError] = useState(''); const handleConfirm = () => { diff --git a/src/constants/auth.ts b/src/constants/auth.ts index f960bdd..195251e 100644 --- a/src/constants/auth.ts +++ b/src/constants/auth.ts @@ -1,7 +1,7 @@ export const officialAPIEndpoint = 'https://api.openai.com/v1/chat/completions'; -export const defaultAPIEndpoint = officialAPIEndpoint; +const customAPIEndpoint = + import.meta.env.VITE_CUSTOM_API_ENDPOINT || 'https://chatgpt-api.shn.hk/v1/'; +export const defaultAPIEndpoint = + import.meta.env.VITE_DEFAULT_API_ENDPOINT || officialAPIEndpoint; -export const availableEndpoints = [ - officialAPIEndpoint, - 'https://chatgpt-api.shn.hk/v1/', -]; +export const availableEndpoints = [officialAPIEndpoint, customAPIEndpoint]; diff --git a/src/constants/chat.ts b/src/constants/chat.ts index 9d70104..72d30d8 100644 --- a/src/constants/chat.ts +++ b/src/constants/chat.ts @@ -11,7 +11,9 @@ const dateString = ('0' + date.getDate()).slice(-2); // default system message obtained using the following method: https://twitter.com/DeminDimin/status/1619935545144279040 -export const _defaultSystemMessage = `You are ChatGPT, a large language model trained by OpenAI. +export const _defaultSystemMessage = + import.meta.env.VITE_DEFAULT_SYSTEM_MESSAGE ?? + `You are ChatGPT, a large language model trained by OpenAI. Carefully heed the user's instructions. Respond using Markdown.`; diff --git a/src/store/auth-slice.ts b/src/store/auth-slice.ts index b11a6c8..5208890 100644 --- a/src/store/auth-slice.ts +++ b/src/store/auth-slice.ts @@ -11,6 +11,7 @@ export interface AuthSlice { } export const createAuthSlice: StoreSlice = (set, get) => ({ + apiKey: import.meta.env.VITE_OPENAI_API_KEY || undefined, apiEndpoint: defaultAPIEndpoint, firstVisit: true, setApiKey: (apiKey: string) => {