Skip to content

Commit

Permalink
plugged in gpt
Browse files Browse the repository at this point in the history
  • Loading branch information
wlicb committed May 27, 2024
1 parent 3ced3d7 commit c2c4f2f
Showing 1 changed file with 32 additions and 22 deletions.
54 changes: 32 additions & 22 deletions src/panel/chat.ts
Original file line number Diff line number Diff line change
Expand Up @@ -52,28 +52,38 @@ document.getElementById('send-button')?.addEventListener('click', async () => {
displayMessage('You', inputValue);
const inputField = document.getElementById('message-input') as HTMLInputElement;
inputField.value = '';
// try {
// const response = await fetch('https://api.openai.com/v1/chat/completions', {
// method: 'POST',
// headers: {
// 'Content-Type': 'application/json',
// 'Authorization': `Bearer 123`
// },
// body: JSON.stringify({
// prompt: inputValue,
// max_tokens: 150
// })
// });

// const data = await response.json();
// const botMessage = data.choices[0].text.trim();
// console.log(botMessage);
// if (currentName) {
// displayMessage(currentName, botMessage);
// }
// } catch (error) {
// console.log('Error:', error);
// }
const data = {
model: "gpt-3.5-turbo",
messages: [{
role: "system",
content: "You are a virtual pet for students to learn programming. You should talk in a cute way and give the student emotional support and encouragement."
}, {
role: "user",
content: inputValue
}],
temperature: 0.7,
max_tokens: 50
};

try {
const response = await fetch('https://api.openai.com/v1/chat/completions', {
method: 'POST',
headers: {
'Content-Type': 'application/json',
'Authorization': 'Bearer 123'
},
body: JSON.stringify(data)
});
if (!response.ok) {
throw new Error('Failed to fetch AI response');
}
const responseData = await response.json();
const aiText = responseData.choices[0].message.content;
displayMessage(currentName, aiText);
} catch (error) {
console.error('Error fetching response from OpenAI:', error);
displayMessage(currentName, 'Sorry, there was an error processing your request.');
}
});


Expand Down

0 comments on commit c2c4f2f

Please sign in to comment.