Implement Phase 12 feedback, Phase 13 Semantic Chunker, Phase 13-B Reranker, Bug 5 thinking fix
- Phase 12: FeedbackRepository + td_feedback 테이블, Gradio 👍/👎 이벤트, run_id 추적, LangSmith create_feedback() 연동 - Phase 13: 커스텀 _SemanticSplitter 제거 → langchain_experimental.SemanticChunker 교체, buffer_size/threshold_type 환경변수 적용 - Phase 13-B: RerankService (Cross-Encoder), RetrieverService.search()에 reranker 통합, tools.py as_retriever() → search() 전환 - Bug 5: mlx_chat_model enable_thinking 런타임 오버라이드, agent_service stream_mode=["messages","custom"] 이중 스트림, thinking 토큰 custom 이벤트로 emit - ROADMAP: LLM 모델명 8B 반영, RAG에 Reranker 추가, 추천 진행 순서 갱신 Co-Authored-By: Claude Sonnet 4.6 <noreply@anthropic.com>
This commit is contained in:
@@ -6,6 +6,7 @@ from typing import AsyncIterator
|
||||
from langchain_core.messages import AIMessage, AIMessageChunk, HumanMessage, SystemMessage
|
||||
from langchain_core.runnables import RunnableConfig
|
||||
from langgraph.checkpoint.memory import MemorySaver
|
||||
from langgraph.config import get_stream_writer
|
||||
from langgraph.graph import START, MessagesState, StateGraph
|
||||
from langgraph.prebuilt import ToolNode, tools_condition
|
||||
|
||||
@@ -43,6 +44,7 @@ class AgentService:
|
||||
self._conv_id: int | None = None
|
||||
self._pending_history: list = []
|
||||
self._user_id = user_id
|
||||
self._last_run_id: str | None = None
|
||||
|
||||
if conversation_repository:
|
||||
try:
|
||||
@@ -107,10 +109,19 @@ class AgentService:
|
||||
system_content += f"\n\n## 사용자 정보 (이전 대화에서 기억된 내용)\n" + "\n".join(lines)
|
||||
msgs = [SystemMessage(content=system_content)] + state["messages"]
|
||||
thinking_acc, content_acc, tool_calls_acc = "", "", []
|
||||
async for chunk in llm_with_tools.astream(msgs, config):
|
||||
try:
|
||||
writer = get_stream_writer()
|
||||
except Exception:
|
||||
writer = None
|
||||
# 체크박스 값을 모델의 enable_thinking으로 전달 (런타임 오버라이드)
|
||||
show_thinking = config.get("configurable", {}).get("show_thinking", False)
|
||||
_llm = llm_with_tools.bind(enable_thinking=show_thinking) if show_thinking != chat_model.enable_thinking else llm_with_tools
|
||||
async for chunk in _llm.astream(msgs, config):
|
||||
t = chunk.additional_kwargs.get("thinking", "")
|
||||
if t:
|
||||
thinking_acc += t
|
||||
if writer:
|
||||
writer({"__thinking": t})
|
||||
if chunk.content and isinstance(chunk.content, str):
|
||||
content_acc += chunk.content
|
||||
if chunk.tool_calls:
|
||||
@@ -132,13 +143,18 @@ class AgentService:
|
||||
self._agent = builder.compile(checkpointer=MemorySaver())
|
||||
|
||||
@property
|
||||
def _config(self) -> dict:
|
||||
return {"configurable": {"thread_id": self._thread_id}}
|
||||
def last_run_id(self) -> str | None:
|
||||
return self._last_run_id
|
||||
|
||||
def _make_config(self, show_thinking: bool = False) -> dict:
|
||||
return {"configurable": {"thread_id": self._thread_id, "show_thinking": show_thinking}}
|
||||
|
||||
async def stream_response(self, user_input: str, show_thinking: bool | None = None) -> AsyncIterator[str]:
|
||||
"""사용자 입력을 받아 응답 토큰을 순서대로 yield한다."""
|
||||
_think_verbose = show_thinking if show_thinking is not None else self._think_verbose
|
||||
self._source_buffer.clear()
|
||||
run_id = uuid.uuid4()
|
||||
run_config = {**self._make_config(_think_verbose), "run_id": str(run_id)}
|
||||
|
||||
# 재시작 후 첫 호출 시 MySQL 이력을 초기 상태에 주입
|
||||
if self._pending_history:
|
||||
@@ -155,13 +171,42 @@ class AgentService:
|
||||
content_started = False # 노드 당 레이블 1회 출력 제어
|
||||
start_time = time.perf_counter()
|
||||
|
||||
async for chunk, metadata in self._agent.astream(
|
||||
messages, self._config, stream_mode="messages"
|
||||
async for stream_event in self._agent.astream(
|
||||
messages, run_config, stream_mode=["messages", "custom"]
|
||||
):
|
||||
mode, data = stream_event
|
||||
|
||||
# ── custom 이벤트 — call_model writer가 emit한 thinking 토큰 ──
|
||||
if mode == "custom":
|
||||
if isinstance(data, dict) and "__thinking" in data:
|
||||
# thinking 첫 토큰 도착 시 agent 레이블 + prev_node 갱신
|
||||
if "agent" != prev_node:
|
||||
if thinking_open:
|
||||
yield "\n[/사고 과정]\n"
|
||||
thinking_open = False
|
||||
content_started = False
|
||||
if lg:
|
||||
elapsed = time.perf_counter() - start_time
|
||||
label = "agent: 검색 결과 반영 중" if prev_node == "tools" else "agent: 질문 분석 중"
|
||||
yield f"\n[LangGraph → {label}] ({elapsed:.2f}s)\n"
|
||||
prev_node = "agent"
|
||||
if _think_verbose:
|
||||
if not thinking_open:
|
||||
yield "\n[사고 과정]\n"
|
||||
thinking_open = True
|
||||
yield data["__thinking"]
|
||||
continue
|
||||
|
||||
# ── messages 이벤트 ──────────────────────────────────────
|
||||
chunk, metadata = data
|
||||
node = metadata.get("langgraph_node", "")
|
||||
|
||||
# ── 노드 전환 시 플래그 리셋 + 레이블 출력 ──────────────
|
||||
# (agent 레이블은 custom 이벤트 핸들러에서 이미 처리될 수 있으므로 중복 방지)
|
||||
if node != prev_node:
|
||||
if thinking_open:
|
||||
yield "\n[/사고 과정]\n"
|
||||
thinking_open = False
|
||||
content_started = False
|
||||
if lg:
|
||||
if node == "agent":
|
||||
@@ -175,13 +220,6 @@ class AgentService:
|
||||
|
||||
# ── agent 노드 — AIMessageChunk만 처리 (중복 방지) ──────
|
||||
if node == "agent" and isinstance(chunk, AIMessageChunk):
|
||||
thinking = chunk.additional_kwargs.get("thinking", "")
|
||||
if thinking and _think_verbose:
|
||||
if not thinking_open:
|
||||
yield "\n[사고 과정]\n"
|
||||
thinking_open = True
|
||||
yield thinking
|
||||
|
||||
if chunk.tool_calls:
|
||||
if thinking_open:
|
||||
yield "\n[/사고 과정]\n"
|
||||
@@ -213,7 +251,7 @@ class AgentService:
|
||||
elif node == "agent" and isinstance(chunk, AIMessage):
|
||||
if not content_started and not thinking_open:
|
||||
thinking = chunk.additional_kwargs.get("thinking", "")
|
||||
if thinking and self._think_verbose:
|
||||
if thinking and _think_verbose:
|
||||
yield "\n[사고 과정]\n"
|
||||
yield thinking
|
||||
yield "\n[/사고 과정]\n"
|
||||
@@ -247,6 +285,8 @@ class AgentService:
|
||||
if thinking_open:
|
||||
yield "\n[/사고 과정]\n"
|
||||
|
||||
self._last_run_id = str(run_id)
|
||||
|
||||
# 대화 내용을 MySQL에 저장
|
||||
if self._conv_repo and self._conv_id and response_content:
|
||||
try:
|
||||
|
||||
Reference in New Issue
Block a user