현재 여행지 추천로직에서 ChatGPT를 도입했다
그런데 응답 속도가 평균 약 35초가 걸린다.. 너무 느리다
AI 응답 속도를 개선해 보자
지금 사용하고 있는 모델은 GPT-4o-mini 이다

포스트맨으로 요청했을때 34초 정도
내가 최적화한 방법
- 응답 길이를 줄이자
- 필요한 데이터만 요청
- 가공할 수 있는 데이터는 직접 코드를 짜자
응답 길이를 줄이자
현재 응답 포멧이다
const jsonFormat = `{
transportOption : car | publicTransport ,
routes: {
date : 'xxxx-xx-xx'
detail : { address: string;
placeTitle : string
routeIndex : number;
placeImage : string;
mapx : number
mapy : number
day : number
distance: '약 10Km';
estimatedTime: '약 10분';
playTime : '2시간'
mapLink : "http://map.naver.com/index.nhn?slng=&slat=&stext=&elng=&elat=&etext=&menu=route&pathType=1"
}[]
}[]
accommodation: {
day : number
address: string;
title: string;
reservationLink : https://www.yeogi.com/domestic-accommodations?searchType=KEYWORD&keyword=서울+강남&checkIn=2024-08-06&checkOut=2024-08-07&freeForm=true
}[]
}
`;
시작 날과 끝나는 날의 차이가 클수록 응답해야하는 데이터 형식이 길어지므로
그에 따라 응답 속도도 느려지게 된다
1차 포멧 형식 개선
const jsonFormat = `{
transportOption : car | publicTransport ,
routes: {
date : 'xxxx-xx-xx'
detail : { address: string;
placeTitle : string
routeIndex : number;
placeImage : string;
mapx : number
mapy : number
day : number
distance: '약 10Km';
estimatedTime: '약 10분';
playTime : '2시간'
mapLink : "http://map.naver.com/index.nhn?slng=&slat=&stext=&elng=&elat=&etext=&menu=route&pathType=1"
}[]
}[]
}
`;
숙소에 대한 데이터가 필요없으므로 삭제

약 10초 정도 줄어들었다
2차 포멧팅 개선
const jsonFormat = `{
transportOption : car | publicTransport ,
routes: {
date : 'xxxx-xx-xx'
detail : {
placeId : number;
routeIndex : number;
day : number
distance: '약 10Km';
estimatedTime: '약 10분';
playTime : '2시간'
mapLink : "http://map.naver.com/index.nhn?slng=&slat=&stext=&elng=&elat=&etext=&menu=route&pathType=1"
}[]
}[]
}
`;
place에 대한 데이터를 placeId 하나로 변경했다
나중에 placeId를 통해서 데이터 가공을 직접 할것이다

이렇게 약 8초정도 단축
필요한 데이터만 요청
내가 AI를 통해서 얻고싶은 데이터는
각각 여행지 마다 거리가 짧은 순과 여행 컨셉에 맞게
여행지와 경로를 추천받는 데이터이다
그러므로 필요없는 데이터는 삭제했다
const jsonFormat = `{
routes: {
date : 'xxxx-xx-xx'
detail : {
placeId : number;
routeIndex : number;
day : number
distance: '약 10Km';
estimatedTime: '약 10분';
playTime : '2시간'
}[]
}[]
}
`;
이렇게 내가 필요한 정보만 추천해달라고 변경

약 4초정도 단축
데이터 가공하기
const result = await this.gptClient.chat.completions.create({
model: 'gpt-4o-mini-2024-07-18',
messages: [
{
role: 'system',
content: `json형식으로 응답하는데 포맷은 ${jsonFormat} 이런 형식이야. 전체 일정을 다 채워야해`,
},
{
role: 'user',
content: `${travleRawsJson} 해당 여행지들 중에서 여행지 추천해줘 ${age ? `${age}대들이` : ''} ${people ? `${people}명이서` : ''} ${sdate} 부터 ${edate} 까지
${transportation}로 여행을 갈건데 ${concept ? `여행 컨셉은 ${concept}이고` : ''} 서로 거리가 가까운 순으로 추천해주고.
day는 날짜별로 정해줘. routeIndex는 day별로 1부터 시작할것. 날마다 최소 한곳은 가게해서 무조건 ${sdate} 부터 ${edate} 까지 일정을 다 채울것. 지역이 여러개 있으면 지역마다 들릴것.
day의 마지막 여행지의 distance, estimatedTime는 null로 해.`,
},
],
response_format: { type: 'json_object' },
temperature: 0.3,
});
const answer = JSON.parse(result.choices[0].message.content,) as Irecommendations;
const routes = answer.routes.map((day) => {
const detail = day.detail.map((route, i) => {
const currentPlace = travleRaws.find(
(place) => place.id === route.placeId,
);
let mapLink: string;
//마지막 목적지면 mapLink는 null
if (i === day.detail.length - 1) {
mapLink = null;
} else {
const { placeId } = day.detail[i + 1];
const nextPlace = travleRaws.find((place) => place.id === placeId);
const stext = currentPlace.title.split(' ').join('+');
const etext = nextPlace.title.split(' ').join('+');
mapLink = `http://map.naver.com/index.nhn?slng=${currentPlace.mapx}&slat=${currentPlace.mapy}&stext=${stext}&elng=${nextPlace.mapx}&elat=${nextPlace.mapy}&etext=${etext}&menu=route&pathType=1`;
}
return {
...route,
address: currentPlace.address,
placeTitle: currentPlace.title,
placeImage: currentPlace.image,
mapx: currentPlace.mapx,
mapy: currentPlace.mapy,
mapLink,
};
});
return { day: day.date, detail };
});
return { transportOption: transportation, routes };
}
AI가 추천해준 데이터를 바탕으로 필요한 데이터를 추가하는 작업을 진행했다

응답 형식이 잘 나오면서 속도를 34s => 12s 개선했다
마이리얼트립이나 마이로 처럼 여행지 추천해주는 사이트에서는
딸각하면 바로 추천해주던데 나의 경우에는
평균 10초대 라는거 부터가 조금 찝찝하긴 하다..
10초 이하로 줄이는 방법이 없을까..
'이것저것' 카테고리의 다른 글
| Nginx 502 반환 트러블 슈팅 (0) | 2025.07.09 |
|---|---|
| 동적 객체 생성 vs 정적 객체 생성 (0) | 2024.04.30 |
| 다마고치 응가 기능 어떻게 구현하지 (0) | 2024.04.24 |
| 쿠키, 세션, 토큰 (0) | 2024.03.28 |
| webpack 개념과 탄생 배경 (0) | 2024.03.21 |