//by Adriana Rincon and Tara Sarli
//hello world part of project to show that the API call works
//please check console to see output of our prompt (What's the capital of Ireland?)
const prompt= "What's the capital of Ireland?";
// MH edit
// const prompt= "What's the capital of France?";
const apiUrl = 'https://api.openai.com/v1/chat/completions';
const apiKey = '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';
// Code adapted and modified from OpenAI's CHATGPT API documentation
async function generateChatResponse(prompt) {
try {
const response = await fetch(apiUrl, {
method: 'POST',
headers: {
'Content-Type': 'application/json',
'Authorization': `Bearer ${apiKey}`,
},
body: JSON.stringify({
model: 'gpt-3.5-turbo',
messages: [{ role: 'user', content: prompt }],
max_tokens: 150,
}),
});
console.log(response);
if (!response.ok) {
throw new Error(`API request failed with status ${response.status}`);
}
const responseData = await response.json();
console.log(responseData.choices[0].message.content);
console.log(responseData);
} catch (error) {
console.error('Error making API request:', error.message);
}
}
// Credits to "Capture the Hat" by Tuomas Bazzan for the start function idea
function start()
{
console.log(generateChatResponse(prompt));
//generateChatResponse(prompt);
}
start();