From 67156f4a7a6474171d2097c07a4afbf272f7f4c0 Mon Sep 17 00:00:00 2001 From: Wentao Lyu <35-wentao.lyu@users.noreply.git.stereye.tech> Date: Fri, 10 Mar 2023 21:05:35 +0800 Subject: [PATCH 1/2] feat: support config host name and proxy address --- api/.env.example | 6 ++++++ api/app/chatgpt-browser.js | 1 + api/app/chatgpt-client.js | 1 + api/app/chatgpt-custom.js | 1 + api/server/index.js | 7 ++++--- 5 files changed, 13 insertions(+), 3 deletions(-) diff --git a/api/.env.example b/api/.env.example index 47576c119a..14209cff03 100644 --- a/api/.env.example +++ b/api/.env.example @@ -1,7 +1,13 @@ OPENAI_KEY= +HOST= PORT=3080 NODE_ENV=development + # Change this to your MongoDB URI if different and I recommend appending chatgpt-clone MONGO_URI="mongodb://127.0.0.1:27017/chatgpt-clone" + +# Change this to proxy any request. +PROXY= + CHATGPT_TOKEN="" BING_TOKEN="" \ No newline at end of file diff --git a/api/app/chatgpt-browser.js b/api/app/chatgpt-browser.js index 442d2a731d..a17f277822 100644 --- a/api/app/chatgpt-browser.js +++ b/api/app/chatgpt-browser.js @@ -7,6 +7,7 @@ const clientOptions = { // Access token from https://chat.openai.com/api/auth/session accessToken: process.env.CHATGPT_TOKEN, // debug: true + proxy: process.env.PROXY || null, }; const browserClient = async ({ text, progressCallback, convo }) => { diff --git a/api/app/chatgpt-client.js b/api/app/chatgpt-client.js index ce3c0e2271..afd31e0a81 100644 --- a/api/app/chatgpt-client.js +++ b/api/app/chatgpt-client.js @@ -5,6 +5,7 @@ const clientOptions = { modelOptions: { model: 'gpt-3.5-turbo' }, + proxy: process.env.PROXY || null, debug: false }; diff --git a/api/app/chatgpt-custom.js b/api/app/chatgpt-custom.js index d31901c75f..a356ba4b1a 100644 --- a/api/app/chatgpt-custom.js +++ b/api/app/chatgpt-custom.js @@ -5,6 +5,7 @@ const clientOptions = { modelOptions: { model: 'gpt-3.5-turbo' }, + proxy: process.env.PROXY || null, debug: false }; diff --git a/api/server/index.js b/api/server/index.js index a98b5e54fc..be2feffd89 100644 --- a/api/server/index.js +++ b/api/server/index.js @@ -5,6 +5,7 @@ const cors = require('cors'); const routes = require('./routes'); const app = express(); const port = process.env.PORT || 3080; +const host = process.env.HOST || 'localhost' const projectPath = path.join(__dirname, '..', '..', 'client'); dbConnect().then(() => console.log('Connected to MongoDB')); @@ -23,6 +24,6 @@ app.use('/api/convos', routes.convos); app.use('/api/customGpts', routes.customGpts); app.use('/api/prompts', routes.prompts); -app.listen(port, () => { - console.log(`Server listening at http://localhost:${port}`); -}); \ No newline at end of file +app.listen(port, host, () => { + console.log(`Server listening at http://${host}:${port}`); +}); From 3a9b53224890e44d65ee0e69c041f8b64fdeb878 Mon Sep 17 00:00:00 2001 From: Wentao Lyu <35-wentao.lyu@users.noreply.git.stereye.tech> Date: Fri, 10 Mar 2023 21:06:13 +0800 Subject: [PATCH 2/2] fix: frontend api request shouldn't hard code the domain and port. --- client/src/components/Conversations/Conversation.jsx | 4 ++-- client/src/components/Conversations/DeleteButton.jsx | 2 +- client/src/components/Models/ModelDialog.jsx | 2 +- client/src/components/Models/ModelItem.jsx | 4 ++-- client/src/components/Models/ModelMenu.jsx | 2 +- client/src/components/Nav/ClearConvos.jsx | 4 ++-- client/src/components/Nav/index.jsx | 2 +- client/src/utils/handleSubmit.js | 2 +- 8 files changed, 11 insertions(+), 11 deletions(-) diff --git a/client/src/components/Conversations/Conversation.jsx b/client/src/components/Conversations/Conversation.jsx index 745671e1cc..ce4cf3c6be 100644 --- a/client/src/components/Conversations/Conversation.jsx +++ b/client/src/components/Conversations/Conversation.jsx @@ -23,8 +23,8 @@ export default function Conversation({ const { modelMap } = useSelector((state) => state.models); const inputRef = useRef(null); const dispatch = useDispatch(); - const { trigger } = manualSWR(`http://localhost:3080/api/messages/${id}`, 'get'); - const rename = manualSWR(`http://localhost:3080/api/convos/update`, 'post'); + const { trigger } = manualSWR(`/api/messages/${id}`, 'get'); + const rename = manualSWR(`/api/convos/update`, 'post'); const clickHandler = async () => { if (conversationId === id) { diff --git a/client/src/components/Conversations/DeleteButton.jsx b/client/src/components/Conversations/DeleteButton.jsx index 03c9f47f81..9b8e688cd0 100644 --- a/client/src/components/Conversations/DeleteButton.jsx +++ b/client/src/components/Conversations/DeleteButton.jsx @@ -9,7 +9,7 @@ import { setMessages } from '~/store/messageSlice'; export default function DeleteButton({ conversationId, renaming, cancelHandler }) { const dispatch = useDispatch(); const { trigger } = manualSWR( - `http://localhost:3080/api/convos/clear`, + `/api/convos/clear`, 'post', () => { dispatch(setMessages([])); diff --git a/client/src/components/Models/ModelDialog.jsx b/client/src/components/Models/ModelDialog.jsx index 47592d8a93..a1b05dfcdd 100644 --- a/client/src/components/Models/ModelDialog.jsx +++ b/client/src/components/Models/ModelDialog.jsx @@ -25,7 +25,7 @@ export default function ModelDialog({ mutate, setModelSave, handleSaveState }) { const [saveText, setSaveText] = useState('Save'); const [required, setRequired] = useState(false); const inputRef = useRef(null); - const updateCustomGpt = manualSWR(`http://localhost:3080/api/customGpts/`, 'post'); + const updateCustomGpt = manualSWR(`/api/customGpts/`, 'post'); const submitHandler = (e) => { if (chatGptLabel.length === 0) { diff --git a/client/src/components/Models/ModelItem.jsx b/client/src/components/Models/ModelItem.jsx index 8b7d8d082d..494b20a2ac 100644 --- a/client/src/components/Models/ModelItem.jsx +++ b/client/src/components/Models/ModelItem.jsx @@ -15,8 +15,8 @@ export default function ModelItem({ modelName, value, onSelect }) { const [currentName, setCurrentName] = useState(modelName); const [modelInput, setModelInput] = useState(modelName); const inputRef = useRef(null); - const rename = manualSWR(`http://localhost:3080/api/customGpts`, 'post'); - const deleteCustom = manualSWR(`http://localhost:3080/api/customGpts/delete`, 'post'); + const rename = manualSWR(`/api/customGpts`, 'post'); + const deleteCustom = manualSWR(`/api/customGpts/delete`, 'post'); if (value === 'chatgptCustom') { return ( diff --git a/client/src/components/Models/ModelMenu.jsx b/client/src/components/Models/ModelMenu.jsx index 607b48921f..94e9dd5c86 100644 --- a/client/src/components/Models/ModelMenu.jsx +++ b/client/src/components/Models/ModelMenu.jsx @@ -27,7 +27,7 @@ export default function ModelMenu() { const [menuOpen, setMenuOpen] = useState(false); const { model, customModel } = useSelector((state) => state.submit); const { models, modelMap, initial } = useSelector((state) => state.models); - const { trigger } = manualSWR(`http://localhost:3080/api/customGpts`, 'get', (res) => { + const { trigger } = manualSWR(`/api/customGpts`, 'get', (res) => { const fetchedModels = res.map((modelItem) => ({ ...modelItem, name: modelItem.chatGptLabel diff --git a/client/src/components/Nav/ClearConvos.jsx b/client/src/components/Nav/ClearConvos.jsx index d262226bb9..e6402b0c40 100644 --- a/client/src/components/Nav/ClearConvos.jsx +++ b/client/src/components/Nav/ClearConvos.jsx @@ -10,10 +10,10 @@ export default function ClearConvos() { const dispatch = useDispatch(); const { mutate } = useSWRConfig(); - const { trigger } = manualSWR(`http://localhost:3080/api/convos/clear`, 'post', () => { + const { trigger } = manualSWR(`/api/convos/clear`, 'post', () => { dispatch(setMessages([])); dispatch(setNewConvo()); - mutate(`http://localhost:3080/api/convos`); + mutate(`/api/convos`); }); const clickHandler = () => { diff --git a/client/src/components/Nav/index.jsx b/client/src/components/Nav/index.jsx index 2ae96d22cc..fa74e069e9 100644 --- a/client/src/components/Nav/index.jsx +++ b/client/src/components/Nav/index.jsx @@ -17,7 +17,7 @@ export default function Nav() { }; const { data, isLoading, mutate } = swr( - `http://localhost:3080/api/convos?pageNumber=${pageNumber}`, + `/api/convos?pageNumber=${pageNumber}`, onSuccess ); const containerRef = useRef(null); diff --git a/client/src/utils/handleSubmit.js b/client/src/utils/handleSubmit.js index 09e1aa52b6..4ba262ac08 100644 --- a/client/src/utils/handleSubmit.js +++ b/client/src/utils/handleSubmit.js @@ -11,7 +11,7 @@ export default function handleSubmit({ chatGptLabel, promptPrefix }) { - const endpoint = `http://localhost:3080/api/ask`; + const endpoint = `/api/ask`; let payload = { model, text, chatGptLabel, promptPrefix }; if (convo.conversationId && convo.parentMessageId) { payload = {