From 06ef13963101667e4b8210ef2b6c7401b87e8fdd Mon Sep 17 00:00:00 2001 From: CheckerBoard Date: Thu, 14 Aug 2025 02:25:32 +0900 Subject: [PATCH 1/2] =?UTF-8?q?docs:=20=ED=94=84=EB=A1=AC=ED=94=84?= =?UTF-8?q?=ED=8A=B8=EC=97=90=20=EC=B1=84=ED=8C=85=20=EB=82=B4=EC=97=AD=20?= =?UTF-8?q?=EC=B6=94=EA=B0=80?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- src/prompts/v1/sql_agent/db_classifier.yaml | 4 ++++ src/prompts/v1/sql_agent/response_synthesizer.yaml | 4 ++++ 2 files changed, 8 insertions(+) diff --git a/src/prompts/v1/sql_agent/db_classifier.yaml b/src/prompts/v1/sql_agent/db_classifier.yaml index b4e2270..aa89aaa 100644 --- a/src/prompts/v1/sql_agent/db_classifier.yaml +++ b/src/prompts/v1/sql_agent/db_classifier.yaml @@ -1,6 +1,7 @@ _type: "prompt" input_variables: - db_options + - chat_history - question template: | Based on the user's question, which of the following databases is most likely to contain the answer? @@ -8,6 +9,9 @@ template: | Available databases: {db_options} + + conversation History: + {chat_history} User Question: {question} diff --git a/src/prompts/v1/sql_agent/response_synthesizer.yaml b/src/prompts/v1/sql_agent/response_synthesizer.yaml index 94c54b3..2fd970c 100644 --- a/src/prompts/v1/sql_agent/response_synthesizer.yaml +++ b/src/prompts/v1/sql_agent/response_synthesizer.yaml @@ -1,6 +1,7 @@ _type: prompt input_variables: - question + - chat_history - context_message template: | You are a friendly and helpful database assistant chatbot. @@ -11,6 +12,9 @@ template: | Context: {context_message} + + conversation History: + {chat_history} Instructions: - If the process was successful: From 3a80f615d20b0dd2086aeab04f875ada33200fb2 Mon Sep 17 00:00:00 2001 From: CheckerBoard Date: Thu, 14 Aug 2025 02:27:25 +0900 Subject: [PATCH 2/2] =?UTF-8?q?feat:=20=EC=B1=97=EB=B4=87=20=EB=A9=80?= =?UTF-8?q?=ED=8B=B0=ED=84=B4=20=EA=B5=AC=ED=98=84?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- src/agents/sql_agent_graph.py | 2 ++ src/api/v1/endpoints/chat.py | 3 ++- src/api/v1/schemas/chatbot_schemas.py | 9 +++++++- src/services/chatbot_service.py | 30 ++++++++++++++++++--------- 4 files changed, 32 insertions(+), 12 deletions(-) diff --git a/src/agents/sql_agent_graph.py b/src/agents/sql_agent_graph.py index 8ce18b8..71d59b0 100644 --- a/src/agents/sql_agent_graph.py +++ b/src/agents/sql_agent_graph.py @@ -91,6 +91,7 @@ def db_classifier_node(state: SqlAgentState): chain = DB_CLASSIFIER_PROMPT | llm_instance | StrOutputParser() selected_db_name = chain.invoke({ "db_options": db_options, + "chat_history": state['chat_history'], "question": state['question'] }) @@ -199,6 +200,7 @@ def response_synthesizer_node(state: SqlAgentState): prompt = RESPONSE_SYNTHESIZER_PROMPT.format( question=state['question'], + chat_history=state['chat_history'], context_message=context_message ) response = llm_instance.invoke(prompt) diff --git a/src/api/v1/endpoints/chat.py b/src/api/v1/endpoints/chat.py index 09aa2d3..69ee393 100644 --- a/src/api/v1/endpoints/chat.py +++ b/src/api/v1/endpoints/chat.py @@ -23,5 +23,6 @@ def handle_chat_request( Returns: ChatRespone: 챗봇 응답 """ - final_answer = service.handle_request(request.question) + final_answer = service.handle_request(request.question, request.chat_history) + return ChatResponse(answer=final_answer) \ No newline at end of file diff --git a/src/api/v1/schemas/chatbot_schemas.py b/src/api/v1/schemas/chatbot_schemas.py index 0db3a79..f3ae457 100644 --- a/src/api/v1/schemas/chatbot_schemas.py +++ b/src/api/v1/schemas/chatbot_schemas.py @@ -1,9 +1,16 @@ # src/api/v1/schemas/chatbot_schemas.py from pydantic import BaseModel +from typing import List, Optional + +class ChatMessage(BaseModel): + """대화 기록의 단일 메시지를 나타내는 모델""" + role: str # "user" 또는 "assistant" + content: str class ChatRequest(BaseModel): question: str + chat_history: Optional[List[ChatMessage]] = None class ChatResponse(BaseModel): - answer: str \ No newline at end of file + answer: str diff --git a/src/services/chatbot_service.py b/src/services/chatbot_service.py index 8f9c8a0..3a17cfc 100644 --- a/src/services/chatbot_service.py +++ b/src/services/chatbot_service.py @@ -1,24 +1,34 @@ # src/services/chatbot_service.py from agents.sql_agent_graph import sql_agent_app -from core.db_manager import schema_instance +from api.v1.schemas.chatbot_schemas import ChatMessage # --- 추가된 부분 --- +from langchain_core.messages import HumanMessage, AIMessage, BaseMessage # --- 추가된 부분 --- +from typing import List, Optional # --- 추가된 부분 --- +#from core.db_manager import schema_instance class ChatbotService(): - def __init__(self): - self.db_schema = schema_instance + # TODO: schema API 요청 + # def __init__(self): + # self.db_schema = schema_instance - def handle_request(self, user_question: str) -> str: - # 1. 에이전트 그래프에 전달할 초기 상태 구성 + def handle_request(self, user_question: str, chat_history: Optional[List[ChatMessage]] = None) -> dict: + + langchain_messages: List[BaseMessage] = [] + if chat_history: + for message in chat_history: + if message.role == 'user': + langchain_messages.append(HumanMessage(content=message.content)) + elif message.role == 'assistant': + langchain_messages.append(AIMessage(content=message.content)) + initial_state = { "question": user_question, - "chat_history": [], - "db_schema": self.db_schema, + "chat_history": langchain_messages, + # "db_schema": self.db_schema, "validation_error_count": 0, "execution_error_count": 0 } - # 2. 그래프 실행 final_state = sql_agent_app.invoke(initial_state) - final_response = final_state['final_response'] - return final_response \ No newline at end of file + return final_state['final_response'] \ No newline at end of file