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
2 changes: 2 additions & 0 deletions src/agents/sql_agent_graph.py
Original file line number Diff line number Diff line change
Expand Up @@ -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']
})

Expand Down Expand Up @@ -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)
Expand Down
3 changes: 2 additions & 1 deletion src/api/v1/endpoints/chat.py
Original file line number Diff line number Diff line change
Expand Up @@ -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)
9 changes: 8 additions & 1 deletion src/api/v1/schemas/chatbot_schemas.py
Original file line number Diff line number Diff line change
@@ -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
answer: str
4 changes: 4 additions & 0 deletions src/prompts/v1/sql_agent/db_classifier.yaml
Original file line number Diff line number Diff line change
@@ -1,13 +1,17 @@
_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?
Please respond with only the database name.

Available databases:
{db_options}

conversation History:
{chat_history}

User Question:
{question}
Expand Down
4 changes: 4 additions & 0 deletions src/prompts/v1/sql_agent/response_synthesizer.yaml
Original file line number Diff line number Diff line change
@@ -1,6 +1,7 @@
_type: prompt
input_variables:
- question
- chat_history
- context_message
template: |
You are a friendly and helpful database assistant chatbot.
Expand All @@ -11,6 +12,9 @@ template: |

Context:
{context_message}

conversation History:
{chat_history}

Instructions:
- If the process was successful:
Expand Down
30 changes: 20 additions & 10 deletions src/services/chatbot_service.py
Original file line number Diff line number Diff line change
@@ -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
return final_state['final_response']