Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

refactor[BE,FE]: api comm probelm resolve #111

Merged
merged 5 commits into from
Dec 5, 2024
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
Binary file modified agentPersona/__pycache__/config.cpython-311.pyc
Binary file not shown.
Binary file modified agentPersona/__pycache__/routes.cpython-311.pyc
Binary file not shown.
41 changes: 24 additions & 17 deletions agentPersona/routes.py
Original file line number Diff line number Diff line change
Expand Up @@ -67,7 +67,7 @@ def plan():
'''사용자 입력을 받아 여행 계획을 생성'''
data = request.json
user_id = data.get("user_id") # 사용자 ID
# user_id = 1 # 사용자 ID
# user_id = 1 사용자 ID
travel_date = data.get("travel_date")
travel_days = data.get("travel_days")
travel_mate = data.get("travel_mate")
Expand Down Expand Up @@ -121,19 +121,24 @@ def plan():
print(travel_info)
print(plan_response)
print(location_response)
location_response = json.loads(location_response)

existing_plan = TravelPlan.query.filter_by(user_id=user_id).first() # user_id를 기준으로 조회

# 여행 계획 테이블에 세션 컨셉으로 저장
if TravelPlan.query.get(user_id):
existing_plan = TravelPlan.query.get(user_id)
existing_plan.travel_info = json.dumps(travel_info)

if existing_plan:
# 기존 데이터가 있으면 업데이트
existing_plan.travel_info = json.dumps(travel_info, ensure_ascii=False)
existing_plan.plan_response = plan_response
existing_plan.location_info = location_response
existing_plan.location_info = json.dumps(location_response, ensure_ascii=False)
else:
db.session.add(TravelPlan(user_id=user_id,
travel_info=json.dumps(travel_info),
plan_response=plan_response,
location_info=json.dumps(location_response))
)
# 기존 데이터가 없으면 새로 추가
db.session.add(TravelPlan(
user_id=user_id,
travel_info=json.dumps(travel_info, ensure_ascii=False),
plan_response=plan_response,
location_info=json.dumps(location_response, ensure_ascii=False)
))

db.session.commit()
follow_up_message = "여행 계획이 생성되었습니다. 수정하고 싶은 부분이 있으면 말씀해주세요! 😊"
Expand Down Expand Up @@ -193,7 +198,7 @@ def plan():
# status=500
# )

location_response = json.loads(location_response)
# location_response = json.loads(location_response)
print(type(plan_response))
print(type(travel_info))
print(type(location_response))
Expand Down Expand Up @@ -262,16 +267,16 @@ def modify3():
print(f"Condition not met. Cleaned Intent Value: '{intent_cleaned}'")

# 데이터베이스에서 여행 계획 가져오기
travel_plan = TravelPlan.query.get(user_id) # 특정 ID에 해당하는 행 가져오기
existing_plan = TravelPlan.query.filter_by(user_id=user_id).first()

if not travel_plan:
if not existing_plan:
return jsonify({"error": "No travel plan found with the provided ID"}), 404

output_parser = StrOutputParser()
modify_chain = modify_prompt | plan_model | output_parser

input_data = {
"current_plan": travel_plan.plan_response,
"current_plan": existing_plan.plan_response,
"modification_request": modification_request
}

Expand All @@ -297,7 +302,9 @@ def modify3():
print(modification_response)
print(location_response)

existing_plan = TravelPlan.query.get(user_id)
location_response = json.loads(location_response)

existing_plan = TravelPlan.query.filter_by(user_id=user_id).first()

existing_plan.plan_response = modification_response
existing_plan.location_info = location_response
Expand All @@ -307,7 +314,7 @@ def modify3():
travel_info = TravelPlan.query.get(user_id).travel_info
follow_up_message = "수정이 완료되었습니다. 추가 수정이 필요하면 말씀해주세요! 😊"

location_response = json.loads(location_response)
# location_response = json.loads(location_response)
# JSON 응답 생성
modify_response_data = {
"response": modification_response,
Expand Down
5 changes: 2 additions & 3 deletions frontend/src/api/chatApi.js
Original file line number Diff line number Diff line change
@@ -1,4 +1,5 @@
import axios from "axios";
import {useEffect, useState} from "react";

// 환경 변수에서 Base URL 가져오기
const API_BASE_URL = process.env.REACT_APP_API_BASE_URL;
Expand Down Expand Up @@ -32,9 +33,7 @@ export const getTravelPlan = async (requestData) => {
// Modify API
export const modifyTravelPlan = async (modifyRequest) => {
try {
const response = await axios.post(`${API_BASE_URL}/modify`, {
modify_request: modifyRequest,
});
const response = await axios.post(`${API_BASE_URL}/modify`, modifyRequest);
return response.data;
} catch (error) {
console.error("Modify API Error:", error.response?.data || error.message);
Expand Down
12 changes: 10 additions & 2 deletions frontend/src/pages/NewChat.js
Original file line number Diff line number Diff line change
Expand Up @@ -62,16 +62,18 @@ function NewChat() {
const iconUserProfile = "/icon_userprofile.png";

const [userInfo, setUserInfo] = useState({
userInfo: "", // 기본 user_id
nickname: "", // 기본 닉네임
profileImage: iconUserProfile, // 기본 이미지
});

// 사용자 정보 로드
useEffect(() => {
const userId = localStorage.getItem("userId") || "default_user_id";
const nickname = localStorage.getItem("nickname") || "닉네임 없음";
const profileImage =
localStorage.getItem("profileImage") || "/icon_userprofile.png";
setUserInfo({ nickname, profileImage });
setUserInfo({ userId, nickname, profileImage });
}, []);

// 상태를 sessionStorage에 저장
Expand Down Expand Up @@ -234,6 +236,7 @@ function NewChat() {
);

const requestData = {
user_id: userInfo.userId,
travel_date: `${dateRange[0].toLocaleDateString()} ~ ${dateRange[1].toLocaleDateString()}`,
travel_days: travelDays,
travel_mate: selectedCompanion,
Expand Down Expand Up @@ -283,12 +286,17 @@ function NewChat() {
const handleModifyRequest = async (modifyRequest) => {
setIsGenerating(true); // 로딩 시작

const modifyRrequest = {
user_id: userInfo.userId,
modify_request: modifyRequest,
}

try {
const {
response: modifyResponse,
follow_up: followUp,
location_info,
} = await modifyTravelPlan(modifyRequest); // Modify API 호출
} = await modifyTravelPlan(modifyRrequest); // Modify API 호출

// 장소 데이터 처리 및 상태 업데이트
if (location_info?.places) {
Expand Down