4b86b2a660
- server-dev start/stop/deploy 및 Gitea push 자동 배포 - local-dev 로컬 개발 환경 Co-authored-by: Cursor <cursoragent@cursor.com>
325 lines
12 KiB
Python
325 lines
12 KiB
Python
"""
|
|
Local + remote tool execution for the agent loop.
|
|
"""
|
|
|
|
import json
|
|
import re
|
|
from datetime import datetime, timezone
|
|
from typing import Any, Dict, List, Optional
|
|
|
|
from agent.chatbot_tool_client import ChatbotToolClient
|
|
|
|
RAG_SEARCH_TOOL = {
|
|
"type": "function",
|
|
"function": {
|
|
"name": "rag_search",
|
|
"description": (
|
|
"한국도로공사 FAQ/상담 지식베이스(Qdrant)에서 질문과 유사한 Q&A를 검색합니다. "
|
|
"통행료 안내, Hi-pass, 환불 절차, 민원, 일반 상담 FAQ 등 정적 지식 질문에 사용하세요."
|
|
),
|
|
"parameters": {
|
|
"type": "object",
|
|
"properties": {
|
|
"query": {"type": "string", "description": "검색할 질문 문장"},
|
|
},
|
|
"required": ["query"],
|
|
},
|
|
},
|
|
}
|
|
|
|
ASK_USER_TOOL = {
|
|
"type": "function",
|
|
"function": {
|
|
"name": "ask_user",
|
|
"description": (
|
|
"조회에 필요한 정보(차량번호, IC명, 휴게소명 등)가 부족할 때 사용자에게 "
|
|
"추가 질문을 합니다. 최종 답변 대신 clarification이 필요할 때만 사용하세요."
|
|
),
|
|
"parameters": {
|
|
"type": "object",
|
|
"properties": {
|
|
"question": {"type": "string", "description": "사용자에게 되물을 질문"},
|
|
"intentType": {
|
|
"type": "string",
|
|
"description": "되묻는 대상 intent (예: FARE_SEARCH, FARE_UNPAID). 첫 턴 clarify 시 필수.",
|
|
},
|
|
},
|
|
"required": ["question"],
|
|
},
|
|
},
|
|
}
|
|
|
|
|
|
class ToolExecutor:
|
|
"""Executes rag_search locally and domain tools via chatbotApi."""
|
|
|
|
_KEYWORD_RETRY_LIMIT = 3
|
|
_KEYWORD_RETRY_FIELDS = ("q", "a", "question", "answer", "category", "source")
|
|
_KEYWORD_RETRY_MAX_SCAN = 10000
|
|
_GENERIC_KEYWORD_QUERIES = {
|
|
"요금",
|
|
"통행료",
|
|
"할인",
|
|
"감면",
|
|
"환불",
|
|
"신청",
|
|
"방법",
|
|
"절차",
|
|
"문의",
|
|
"안내",
|
|
"고속도로",
|
|
"휴게소",
|
|
"하이패스",
|
|
}
|
|
|
|
def __init__(
|
|
self,
|
|
search_handler,
|
|
config,
|
|
chatbot_tool_client: Optional[ChatbotToolClient] = None,
|
|
):
|
|
self.search_handler = search_handler
|
|
self.config = config
|
|
self.chatbot_tool_client = chatbot_tool_client or ChatbotToolClient()
|
|
self.last_domain_result: Optional[Dict[str, Any]] = None
|
|
self.last_rag_result: Optional[Dict[str, Any]] = None
|
|
# 한 턴에 여러 domain tool이 성공한 경우(복합 질의) 모두 누적
|
|
self.domain_results: List[Dict[str, Any]] = []
|
|
self.tool_trace: List[Dict[str, Any]] = []
|
|
|
|
def reset_state(self) -> None:
|
|
"""Clear per-turn domain result and tool trace."""
|
|
self.last_domain_result = None
|
|
self.last_rag_result = None
|
|
self.domain_results = []
|
|
self.tool_trace = []
|
|
|
|
def load_remote_tools(self) -> List[Dict[str, Any]]:
|
|
try:
|
|
return self.chatbot_tool_client.list_tool_definitions()
|
|
except Exception as exc:
|
|
print(f"[ToolExecutor] remote tool definitions unavailable: {exc}")
|
|
return []
|
|
|
|
def all_tools(self) -> List[Dict[str, Any]]:
|
|
return [RAG_SEARCH_TOOL, ASK_USER_TOOL] + self.load_remote_tools()
|
|
|
|
def execute(
|
|
self,
|
|
tool_name: str,
|
|
arguments: Dict[str, Any],
|
|
*,
|
|
bot_id: Optional[str] = None,
|
|
user_input: Optional[str] = None,
|
|
) -> str:
|
|
started = datetime.now(timezone.utc).isoformat()
|
|
try:
|
|
if tool_name == "rag_search":
|
|
result = self._execute_rag_search(arguments)
|
|
elif tool_name == "ask_user":
|
|
result = {"status": True, "clarificationQuestion": arguments.get("question")}
|
|
else:
|
|
payload = self.chatbot_tool_client.execute_tool(
|
|
tool_name,
|
|
arguments,
|
|
bot_id=bot_id,
|
|
user_input=user_input,
|
|
)
|
|
if isinstance(payload, dict):
|
|
if payload.get("needsClarification") or payload.get("status") is True:
|
|
self.last_domain_result = payload
|
|
if payload.get("status") is True and payload.get("intentType"):
|
|
self.domain_results.append(payload)
|
|
result = payload
|
|
success = not (
|
|
isinstance(result, dict)
|
|
and result.get("status") is False
|
|
and not result.get("needsClarification")
|
|
)
|
|
self.tool_trace.append(
|
|
{
|
|
"tool": tool_name,
|
|
"arguments": arguments,
|
|
"startedAt": started,
|
|
"status": success,
|
|
}
|
|
)
|
|
return json.dumps(result, ensure_ascii=False)
|
|
except Exception as exc:
|
|
self.tool_trace.append(
|
|
{
|
|
"tool": tool_name,
|
|
"arguments": arguments,
|
|
"startedAt": started,
|
|
"status": False,
|
|
"error": str(exc),
|
|
}
|
|
)
|
|
return json.dumps({"status": False, "error": str(exc)}, ensure_ascii=False)
|
|
|
|
def _execute_rag_search(self, arguments: Dict[str, Any]) -> Dict[str, Any]:
|
|
query = (arguments or {}).get("query") or ""
|
|
ts = datetime.now().strftime("%H:%M:%S")
|
|
query_vec = self.search_handler.embed_query(query, ts)
|
|
if query_vec is None:
|
|
return {"status": False, "error": "embedding_failed", "references": []}
|
|
|
|
search_results = self.search_handler.search(query_vec, self.config.threshold, ts)
|
|
# no-match 시 완화된 threshold로 재검색 (Legacy /ask recall parity)
|
|
if not search_results:
|
|
retry_threshold = getattr(self.config, "threshold_rewrite", None)
|
|
if retry_threshold is not None and retry_threshold < self.config.threshold:
|
|
print(f"[ToolExecutor] rag_search no-match → threshold {retry_threshold} 재검색")
|
|
search_results = self.search_handler.search(query_vec, retry_threshold, ts)
|
|
# search 결과는 {"meta": {...}, "score": ...} 형태 → legacy /ask와 동일하게 meta 언랩
|
|
candidates = [r["meta"] for r in search_results if isinstance(r, dict) and r.get("meta")]
|
|
top_results, scores, _, _ = self.search_handler.rerank(query, candidates, ts)
|
|
references = self._build_references(top_results, scores)
|
|
search_info = getattr(self.search_handler, "last_search_info", {}) or {}
|
|
search_mode = search_info.get("searchMode") or "vector"
|
|
keyword_retry_used = False
|
|
keyword_retry_accepted = False
|
|
|
|
# 최종 guidance 직전 보조 검색: 벡터/완화 재검색이 모두 실패한 경우에만,
|
|
# 문자열 포함 결과 최대 3건을 reranker로 검증해 충분히 맞을 때만 근거로 채택한다.
|
|
if not references:
|
|
keyword_retry_used = self._keyword_retry_allowed(query)
|
|
if keyword_retry_used:
|
|
keyword_references = self._keyword_retry_with_rerank(query, ts)
|
|
if keyword_references:
|
|
references = keyword_references
|
|
search_mode = "keyword_retry"
|
|
keyword_retry_accepted = True
|
|
|
|
result = {
|
|
"status": True,
|
|
"query": query,
|
|
"references": references,
|
|
"referenceCount": len(references),
|
|
"searchMode": search_mode,
|
|
"candidateCount": search_info.get("candidateCount"),
|
|
"keywordRetryUsed": keyword_retry_used,
|
|
"keywordRetryAccepted": keyword_retry_accepted,
|
|
}
|
|
self.last_rag_result = result
|
|
return result
|
|
|
|
def _build_references(
|
|
self, top_results: List[Dict[str, Any]], scores: List[Any], limit: int = 5
|
|
) -> List[Dict[str, Any]]:
|
|
references = []
|
|
for item, score in zip((top_results or [])[:limit], (scores or [])[:limit]):
|
|
references.append(
|
|
{
|
|
"question": item.get("q") or item.get("question"),
|
|
"answer": item.get("a") or item.get("answer"),
|
|
"score": score,
|
|
"category": item.get("category"),
|
|
"url": item.get("url"),
|
|
"searchSource": item.get("_search_source"),
|
|
"denseScore": item.get("_dense_score"),
|
|
"sparseScore": item.get("_sparse_score"),
|
|
}
|
|
)
|
|
return references
|
|
|
|
def _keyword_retry_allowed(self, query: str) -> bool:
|
|
text = (query or "").strip()
|
|
if not text:
|
|
return False
|
|
|
|
normalized = re.sub(r"\s+", "", text).lower()
|
|
if len(normalized) < 3:
|
|
return False
|
|
|
|
if not re.search(r"[0-9a-zA-Z가-힣]", normalized):
|
|
return False
|
|
|
|
tokens = re.findall(r"[0-9a-zA-Z가-힣]+", text.lower())
|
|
if not tokens:
|
|
return False
|
|
|
|
compact_tokens = [re.sub(r"\s+", "", token) for token in tokens if token.strip()]
|
|
if len(compact_tokens) == 1 and compact_tokens[0] in self._GENERIC_KEYWORD_QUERIES:
|
|
return False
|
|
|
|
compact_query = "".join(compact_tokens)
|
|
if compact_query in self._GENERIC_KEYWORD_QUERIES:
|
|
return False
|
|
|
|
return True
|
|
|
|
def _keyword_retry_with_rerank(self, query: str, ts: str) -> List[Dict[str, Any]]:
|
|
vector_store = getattr(self.search_handler, "vector_store", None)
|
|
keyword_search = getattr(vector_store, "keyword_search", None)
|
|
if not callable(keyword_search):
|
|
return []
|
|
|
|
try:
|
|
keyword_items, total, exhausted = keyword_search(
|
|
query,
|
|
fields=self._KEYWORD_RETRY_FIELDS,
|
|
limit=self._KEYWORD_RETRY_LIMIT,
|
|
max_scan=self._KEYWORD_RETRY_MAX_SCAN,
|
|
count_total=False,
|
|
)
|
|
except TypeError:
|
|
# 이전 시그니처/테스트 더블 호환: count_total을 받지 못하면 기본 호출로 재시도
|
|
keyword_items, total, exhausted = keyword_search(
|
|
query,
|
|
fields=self._KEYWORD_RETRY_FIELDS,
|
|
limit=self._KEYWORD_RETRY_LIMIT,
|
|
)
|
|
except Exception as exc:
|
|
print(f"[ToolExecutor] keyword retry 실패: {exc}")
|
|
return []
|
|
|
|
if not isinstance(keyword_items, list) or not keyword_items:
|
|
print(f"[ToolExecutor] keyword retry no-match: query={query}")
|
|
return []
|
|
|
|
candidates = [
|
|
item.get("meta")
|
|
for item in keyword_items[: self._KEYWORD_RETRY_LIMIT]
|
|
if isinstance(item, dict) and isinstance(item.get("meta"), dict)
|
|
]
|
|
if not candidates:
|
|
return []
|
|
|
|
print(
|
|
f"[ToolExecutor] keyword retry → rerank 검증: "
|
|
f"query={query}, candidates={len(candidates)}, total_seen={total}, exhausted={exhausted}"
|
|
)
|
|
top_results, scores, rerank_used, _ = self.search_handler.rerank(query, candidates, ts)
|
|
if not top_results or not scores or scores[0] is None:
|
|
print("[ToolExecutor] keyword retry 거부: rerank 점수 없음")
|
|
return []
|
|
|
|
threshold = self._low_confidence_threshold()
|
|
try:
|
|
top_score = float(scores[0])
|
|
except (TypeError, ValueError):
|
|
print(f"[ToolExecutor] keyword retry 거부: 잘못된 rerank 점수={scores[0]}")
|
|
return []
|
|
|
|
if not rerank_used or top_score < threshold:
|
|
print(
|
|
f"[ToolExecutor] keyword retry 거부: score={top_score:.4f}, "
|
|
f"threshold={threshold:.4f}, rerank_used={rerank_used}"
|
|
)
|
|
return []
|
|
|
|
print(f"[ToolExecutor] keyword retry 채택: score={top_score:.4f}")
|
|
return self._build_references(
|
|
top_results[: self._KEYWORD_RETRY_LIMIT],
|
|
scores[: self._KEYWORD_RETRY_LIMIT],
|
|
limit=self._KEYWORD_RETRY_LIMIT,
|
|
)
|
|
|
|
def _low_confidence_threshold(self) -> float:
|
|
value = getattr(self.config, "low_confidence_threshold", 0.65)
|
|
try:
|
|
return float(value)
|
|
except (TypeError, ValueError):
|
|
return 0.65
|