Skip to content
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
23 changes: 22 additions & 1 deletion app/api/routes/chat.py
Original file line number Diff line number Diff line change
Expand Up @@ -155,4 +155,25 @@ async def delete_session(
code="COMMON200",
message="채팅이 삭제되었습니다."
)
return None
return None


@router.get("/evaluate/result/{session_id}/",
response_model=ResponseModel,
summary="채팅방 결과 조회",
description="유저의 채팅 결과를 조회하는 API입니다.",
)
async def chat_list(
request: Request,
session_id: Annotated[str, Path(
description="종료된 상담을 조회하는 API입니다.",
example="10-73cbf3c058274f138913c43c40be19f0"
)]
):
user_id = request.headers.get("X-USER-ID")
model= await ChatService.get_evaluation_result(session_id, user_id)
return ResponseModel(
code="COMMON200",
message="채팅 기록 반환에 성공했습니다.",
result = model
)
41 changes: 34 additions & 7 deletions app/service/chat_service.py
Original file line number Diff line number Diff line change
@@ -1,7 +1,8 @@
import datetime
from typing import Optional, List

from app.core.config import settings
from app.Exception.exceptions import CustomException
from app.core.config import settings
from app.database.CustomMongoChat import CustomMongoDBChatMessageHistory
from app.database.MongoDB import get_db
from app.prompt.counselorAI import chain_with_history
Expand Down Expand Up @@ -129,6 +130,7 @@ async def evaluate_user(session_id: str, user_id: str):
config=config,
)
try:
print(response)
block = parse_json_block(response)
result = await update_after_keyword_and_change_status(session_id, block)
if result == "OK":
Expand Down Expand Up @@ -161,9 +163,21 @@ async def delete_chat(session_id: str, user_id: str, status: bool):
raise CustomException(500, "CHAT002", "서버 내부 오류입니다.")


@staticmethod
async def get_evaluation_result(session_id: str, user_id:str):
await SessionManager.validate_session(session_id, user_id, status=False)

info = await ChatRepository.find_session_info(session_id, False)
evaluation_result = {
"suggest_comment": info.get("suggest_comment"),
"total_comment": info.get("total_comment"),
"session_id": info.get("session_id"),
"chat_title": info.get("chat_title"),
"category": info.get("category"),
"after_keyword": info.get("after_keyword")
}



return evaluation_result



Expand All @@ -178,22 +192,35 @@ def parse_json_block(response_text: str) -> dict:

async def update_after_keyword_and_change_status(session_id: str, data_dict: dict):
try:
analytics_data = data_dict.get('analytics', [])
analytics_emotion = data_dict.get('analytics', [])
total_comment_data = data_dict.get('totalComment', {})
suggest_data = data_dict.get('suggest', {})

keywords_to_add = []
for item in analytics_data:
for item in analytics_emotion:
keyword_entry = {
"emotion": item.get('emotion'),
"score": item.get('score'),
"comment": item.get('comment')
}
keywords_to_add.append(keyword_entry)



db = get_db()
collection = db[settings.MONGODB_COLLECTION]
query = {"session_id": session_id}
update = {"$push": {"after_keyword": {"$each": keywords_to_add}}, "$set": {"worry_state": False}}
result = await collection.update_one(query, update)
update_fields = {
"$push": {"after_keyword": {"$each": keywords_to_add}}, # 감정 분석 결과는 after_keyword 배열에 추가
"$set": {
"worry_state": False,
"total_comment": total_comment_data.get('comment'), # totalComment의 comment를 'total_comment' 필드에 저장
"suggest_comment": suggest_data.get('comment'), # suggest의 comment를 'suggest_comment' 필드에 저장
"last_updated": datetime.datetime.now() # 마지막 업데이트 시각 기록 (옵션)
}
}
result = await collection.update_one(query, update_fields)

if result.modified_count > 0 or result.upserted_id is not None:
return "OK"
return None
Expand Down