support environment variables

fixes #159
This commit is contained in:
Jing Hua 2023-04-02 20:43:54 +08:00
parent dfd8cfc32a
commit e4468fd93f
6 changed files with 19 additions and 7 deletions

5
.env.example Normal file
View file

@ -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

2
.gitignore vendored
View file

@ -24,3 +24,5 @@ dist-ssr
*.sw?
release/
.env

View file

@ -14,7 +14,9 @@ const ApiPopup = () => {
const setFirstVisit = useStore((state) => state.setFirstVisit);
const [_apiKey, _setApiKey] = useState<string>(apiKey || '');
const [isModalOpen, setIsModalOpen] = useState<boolean>(firstVisit);
const [isModalOpen, setIsModalOpen] = useState<boolean>(
!apiKey && firstVisit
);
const [error, setError] = useState<string>('');
const handleConfirm = () => {

View file

@ -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];

View file

@ -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.`;

View file

@ -11,6 +11,7 @@ export interface AuthSlice {
}
export const createAuthSlice: StoreSlice<AuthSlice> = (set, get) => ({
apiKey: import.meta.env.VITE_OPENAI_API_KEY || undefined,
apiEndpoint: defaultAPIEndpoint,
firstVisit: true,
setApiKey: (apiKey: string) => {