Skip to content

Commit

Permalink
support environment variables
Browse files Browse the repository at this point in the history
fixes #159
  • Loading branch information
ztjhz committed Apr 2, 2023
1 parent dfd8cfc commit e4468fd
Show file tree
Hide file tree
Showing 6 changed files with 19 additions and 7 deletions.
5 changes: 5 additions & 0 deletions .env.example
Original file line number Diff line number Diff line change
@@ -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 changes: 2 additions & 0 deletions .gitignore
Original file line number Diff line number Diff line change
Expand Up @@ -24,3 +24,5 @@ dist-ssr
*.sw?

release/

.env
4 changes: 3 additions & 1 deletion src/components/ApiPopup/ApiPopup.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -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 = () => {
Expand Down
10 changes: 5 additions & 5 deletions src/constants/auth.ts
Original file line number Diff line number Diff line change
@@ -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];
4 changes: 3 additions & 1 deletion src/constants/chat.ts
Original file line number Diff line number Diff line change
Expand Up @@ -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.`;

Expand Down
1 change: 1 addition & 0 deletions src/store/auth-slice.ts
Original file line number Diff line number Diff line change
Expand Up @@ -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) => {
Expand Down

0 comments on commit e4468fd

Please sign in to comment.