diff --git a/src/api/chatbot.js b/src/api/chatbot.js new file mode 100644 index 0000000..0255949 --- /dev/null +++ b/src/api/chatbot.js @@ -0,0 +1,46 @@ +import axios from 'axios'; + +const apiKey = import.meta.env.VITE_OPENAI_API_KEY; + +const delay = (ms) => new Promise(resolve => setTimeout(resolve, ms)); + +export const sendMessageToChatGPT = async (message, retries = 3) => { + let attempt = 0; + const maxDelay = 32000; // 최대 지연 시간 (32초) + + while (attempt < retries) { + try { + console.log(apiKey) + const response = await axios.post( + 'https://api.openai.com/v1/chat/completions', + { + model: 'gpt-3.5-turbo', // 또는 다른 사용 가능한 모델로 변경 + messages: [ + { role: 'system', content: 'You are a tour guide for those who want to travel to Korea. Answer only questions related to your trip to Korea.' }, + { role: 'user', content: message } + ], + }, + { + headers: { + 'Content-Type': 'application/json', + 'Authorization': `Bearer ${apiKey}`, + }, + } + ); + + return response.data.choices[0].message.content; + } catch (error) { + if (error.response && error.response.status === 429) { + attempt++; + const backoffTime = Math.min(1000 * 2 ** attempt, maxDelay); // 지수 백오프 + console.warn(`Rate limit exceeded. Retrying in ${backoffTime / 1000} seconds...`); + await delay(backoffTime); + } else { + console.error('Error calling OpenAI API:', error); + throw error; + } + } + } + + throw new Error('Exceeded maximum retry attempts'); +}; diff --git a/src/components/chatbot/TravelChatBot.vue b/src/components/chatbot/TravelChatBot.vue new file mode 100644 index 0000000..8b1581b --- /dev/null +++ b/src/components/chatbot/TravelChatBot.vue @@ -0,0 +1,103 @@ + + + + + + \ No newline at end of file