Hello! I am following one tutorial and I have noticed that in the chat screen the following code
is placed:
const generateText = () => {
setIsTyping(true)
const message = {
_id: Math.random().toString(36).substring(7),
text: inputMessage,
createAt: new Date(),
user: { _id: 1 },
}
setMessages((previousMessage) =>
GiftedChat.append(previousMessage, [message])
)
/**
* Always put your api key in an environment file
*/
fetch('https://api.openai.com/v1/chat/completions', {
method: 'POST',
headers: {
'Content-Type': 'application/json',
Authorization: 'Bearer ---------------------------------------------',
},
body: JSON.stringify({
model: 'gpt-3.5-turbo',
messages: [
{
role: 'user',
content: inputMessage,
},
],
}),
})
.then((response) => response.json())
.then((data) => {
console.log(data.choices[0].message.content)
setInputMessage('')
setOutputMessage(data.choices[0].message.content.trim())
const message = {
_id: Math.random().toString(36).substring(7),
text: data.choices[0].message.content.trim(),
createAt: new Date(),
user: { _id: 2, name: 'ChatGPT' },
}
setIsTyping(false)
setMessages((previousMessage) =>
GiftedChat.append(previousMessage, [message])
)
})
}
const handleInputText = (text) => {
setInputMessage(text);
}
After seeing the documentation I don’t think that this is safe approach. Any advices? Most of the tutorials I have seen use similar approach…