Spaces:
Sleeping
Sleeping
File size: 7,582 Bytes
b3b0ccd 45b0535 b3b0ccd 45b0535 b3b0ccd 45b0535 b3b0ccd 45b0535 b3b0ccd 45b0535 b3b0ccd 45b0535 b3b0ccd | 1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 144 145 146 147 148 149 150 151 152 153 154 155 156 157 158 159 160 161 162 163 164 165 166 167 168 169 170 171 172 173 174 175 176 177 178 179 180 181 182 | import json
import re
from langchain_core.messages import HumanMessage, AIMessageChunk, ToolMessage
from core.execution_logger import log_chat_end, log_chat_start, log_error
SYSTEM_NODES = {"summarize_history", "rewrite_query"}
FINAL_RESPONSE_NODES = {"aggregate_answers"}
SYSTEM_NODE_CONFIG = {
"rewrite_query": {"title": "π Query Analysis & Rewriting"},
"summarize_history": {"title": "π Chat History Summary"},
}
# --- Helpers ---
def make_message(content, *, title=None, node=None):
msg = {"role": "assistant", "content": content}
if title or node:
msg["metadata"] = {k: v for k, v in {"title": title, "node": node}.items() if v}
return msg
def find_msg_idx(messages, node):
return next(
(i for i, m in enumerate(messages) if m.get("metadata", {}).get("node") == node),
None,
)
def parse_rewrite_json(buffer):
match = re.search(r"\{.*\}", buffer, re.DOTALL)
if not match:
return None
try:
return json.loads(match.group())
except Exception:
return None
def format_rewrite_content(buffer):
data = parse_rewrite_json(buffer)
if not data:
return "β³ Analyzing query..."
if data.get("is_document_listing"):
return "π Checking the uploaded document library."
if data.get("is_clear"):
lines = ["β
**Query is clear**"]
if data.get("questions"):
lines += ["\n**Rewritten queries:**"] + [f"- {q}" for q in data["questions"]]
else:
lines = ["β **Query is unclear**"]
clarification = data.get("clarification_needed", "")
if clarification and clarification.strip().lower() != "no":
lines.append(f"\nClarification needed: *{clarification}*")
return "\n".join(lines)
# --- End of Helpers ---
class ChatInterface:
def __init__(self, rag_system):
self.rag_system = rag_system
def _handle_system_node(self, content, node, response_messages, system_node_buffer):
"""Update (or create) the collapsible system-node message and surface any clarification."""
system_node_buffer[node] = system_node_buffer.get(node, "") + content
buffer = system_node_buffer[node]
title = SYSTEM_NODE_CONFIG[node]["title"]
content = format_rewrite_content(buffer) if node == "rewrite_query" else buffer
idx = find_msg_idx(response_messages, node)
if idx is None:
response_messages.append(make_message(content, title=title, node=node))
else:
response_messages[idx]["content"] = content
if node == "rewrite_query":
self._surface_clarification(buffer, response_messages)
def _surface_clarification(self, buffer, response_messages):
"""If the query is unclear, add/update a plain clarification message."""
data = parse_rewrite_json(buffer) or {}
if data.get("is_document_listing"):
return
clarification = data.get("clarification_needed", "")
if not data.get("is_clear") and clarification.strip().lower() not in ("", "no"):
cidx = find_msg_idx(response_messages, "clarification")
if cidx is None:
response_messages.append(make_message(clarification, node="clarification"))
else:
response_messages[cidx]["content"] = clarification
def _handle_tool_call(self, chunk, response_messages, active_tool_calls):
"""Register new tool calls as collapsible messages."""
for tc in chunk.tool_calls:
if tc.get("id") and tc["id"] not in active_tool_calls:
response_messages.append(
make_message(f"Running `{tc['name']}`...", title=f"π οΈ {tc['name']}")
)
active_tool_calls[tc["id"]] = len(response_messages) - 1
def _handle_tool_result(self, chunk, response_messages, active_tool_calls):
"""Fill in the tool result inside the matching collapsible message."""
idx = active_tool_calls.get(chunk.tool_call_id)
if idx is not None:
preview = str(chunk.content)[:300]
suffix = "\n..." if len(str(chunk.content)) > 300 else ""
response_messages[idx]["content"] = f"```\n{preview}{suffix}\n```"
def _handle_llm_token(self, chunk, node, response_messages):
"""Append streaming LLM tokens to the last plain assistant message."""
last = response_messages[-1] if response_messages else None
if not (last and last.get("role") == "assistant" and "metadata" not in last):
response_messages.append(make_message(""))
response_messages[-1]["content"] += chunk.content
def chat(self, message, history, thread_id=None):
"""Generator that streams Gradio chat message dicts."""
if not self.rag_system.agent_graph:
yield "β οΈ System not initialized!"
return
config = self.rag_system.get_config(thread_id)
current_state = self.rag_system.agent_graph.get_state(config)
log_chat_start(message.strip(), config["configurable"]["thread_id"], bool(current_state.next))
try:
turn_input = {
"messages": [HumanMessage(content=message.strip())],
"availableDocuments": self.rag_system.parent_store.list_sources(),
}
if current_state.next:
self.rag_system.agent_graph.update_state(config, turn_input)
stream_input = None
else:
stream_input = turn_input
response_messages = []
active_tool_calls = {}
system_node_buffer = {}
for chunk, metadata in self.rag_system.agent_graph.stream(stream_input, config=config, stream_mode="messages"):
node = metadata.get("langgraph_node", "")
if node in SYSTEM_NODES and isinstance(chunk, AIMessageChunk):
content = chunk.content
if node == "rewrite_query" and chunk.tool_call_chunks:
content = "".join(tc.get("args") or "" for tc in chunk.tool_call_chunks)
if not content:
continue
self._handle_system_node(content, node, response_messages, system_node_buffer)
elif hasattr(chunk, "tool_calls") and chunk.tool_calls:
self._handle_tool_call(chunk, response_messages, active_tool_calls)
elif isinstance(chunk, ToolMessage):
self._handle_tool_result(chunk, response_messages, active_tool_calls)
elif isinstance(chunk, AIMessageChunk) and chunk.content and node in FINAL_RESPONSE_NODES:
self._handle_llm_token(chunk, node, response_messages)
else:
continue
yield response_messages
final_state = self.rag_system.agent_graph.get_state(config)
final_values = getattr(final_state, "values", final_state)
if final_values.get("documentListRequested"):
response_messages.append(make_message(final_values["messages"][-1].content))
yield response_messages
log_chat_end(final_values)
except Exception as e:
log_error("chat", e)
yield f"β Error: {str(e)}"
def clear_session(self, thread_id=None):
new_thread = self.rag_system.reset_thread(thread_id)
self.rag_system.observability.flush()
return new_thread
|