-
Notifications
You must be signed in to change notification settings - Fork 0
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
🔗 Integrate ChatGPT API with axios
- Loading branch information
Showing
3 changed files
with
54 additions
and
12 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1,16 +1,52 @@ | ||
import './App.css'; | ||
import React, { useState } from 'react'; | ||
import axios from 'axios'; | ||
import config from './config'; | ||
import ChatBubble from './components/ChatBubble'; | ||
import ChatInput from './components/ChatInput'; | ||
import './App.css'; | ||
|
||
const App = () => { | ||
const [messages, setMessages] = useState([]); | ||
|
||
const addMessage = (message, isUser) => { | ||
setMessages((prevMessages) => [...prevMessages, { message, isUser }]); | ||
}; | ||
|
||
const sendMessage = async (message) => { | ||
addMessage(message, true); | ||
|
||
try { | ||
const response = await axios.post( | ||
`${config.apiUrl}`, | ||
{ | ||
prompt: | ||
'only respond to new message. previous messages: ' + | ||
messages.map((msg) => msg.message).join(', ') + | ||
', new message:' + | ||
message, | ||
}, | ||
{ | ||
headers: { | ||
'Content-Type': 'application/json', | ||
'Authorization': `Bearer ${process.env.REACT_APP_OPENAI_API_KEY}`, | ||
}, | ||
} | ||
); | ||
|
||
const chatGptResponse = response.data; | ||
addMessage(chatGptResponse, false); | ||
} catch (error) { | ||
console.error('Error fetching ChatGPT response:', error); | ||
addMessage('Error: Unable to fetch response from ChatGPT.', false); | ||
} | ||
}; | ||
|
||
function App() { | ||
return ( | ||
<div className="App"> | ||
<header className="App-header"> | ||
<ChatBubble></ChatBubble> | ||
<ChatInput></ChatInput> | ||
</header> | ||
</div> | ||
); | ||
} | ||
return ( | ||
<div className="app"> | ||
<ChatBubble></ChatBubble> | ||
<ChatInput></ChatInput> | ||
</div> | ||
); | ||
}; | ||
|
||
export default App; |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,6 @@ | ||
const config = { | ||
apiUrl: 'https://localhost:3500/chat', | ||
}; | ||
|
||
export default config; | ||
|