diff --git a/src/api/chatbot.js b/src/api/chatbot.js index 0255949..c187f7d 100644 --- a/src/api/chatbot.js +++ b/src/api/chatbot.js @@ -1,46 +1,63 @@ -import axios from 'axios'; +// import axios from 'axios'; -const apiKey = import.meta.env.VITE_OPENAI_API_KEY; +// const apiKey = import.meta.env.VITE_OPENAI_API_KEY; -const delay = (ms) => new Promise(resolve => setTimeout(resolve, ms)); +// const delay = (ms) => new Promise(resolve => setTimeout(resolve, ms)); -export const sendMessageToChatGPT = async (message, retries = 3) => { - let attempt = 0; - const maxDelay = 32000; // 최대 지연 시간 (32초) +// 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}`, - }, - } - ); +// 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; - } - } +// 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'); +// }; + +import { localAxios } from '@/utils/http-commons'; + +const local = localAxios; + +const sendMessageToChatGPT = async (message) => { + try { + console.log(message) + const response = await local.post('/api/chatgpt/message', { message }); + return response.data; + } catch (error) { + console.error('Error sending message to ChatGPT:', error); + return 'Sorry, there was an error processing your request.'; } - - throw new Error('Exceeded maximum retry attempts'); }; + +export default sendMessageToChatGPT; \ No newline at end of file diff --git a/src/components/chatbot/TravelChatBot.vue b/src/components/chatbot/TravelChatBot.vue index 8b1581b..85320a5 100644 --- a/src/components/chatbot/TravelChatBot.vue +++ b/src/components/chatbot/TravelChatBot.vue @@ -1,4 +1,4 @@ -