Add debug info display
This commit is contained in:
parent
d3449bb50d
commit
c1665f3788
|
|
@ -232,7 +232,7 @@ def chat_stream():
|
||||||
cache_folder = data.get("cache_folder")
|
cache_folder = data.get("cache_folder")
|
||||||
temperature = data.get("temperature", DEFAULT_TEMPERATURE)
|
temperature = data.get("temperature", DEFAULT_TEMPERATURE)
|
||||||
max_tokens = data.get("max_tokens")
|
max_tokens = data.get("max_tokens")
|
||||||
agency_mode = data.get("agency_mode", False) # НОВОЕ: Перехватываем режим тумблера "Агент"
|
agency_mode = data.get("agency_mode", False)
|
||||||
# Необязательный параметр для существующего узла ассистента при регенерации
|
# Необязательный параметр для существующего узла ассистента при регенерации
|
||||||
existing_assistant_node_id = data.get("existing_assistant_node_id")
|
existing_assistant_node_id = data.get("existing_assistant_node_id")
|
||||||
|
|
||||||
|
|
|
||||||
|
|
@ -19,7 +19,7 @@ from typing import Any, get_type_hints
|
||||||
from langchain_core.tools import StructuredTool
|
from langchain_core.tools import StructuredTool
|
||||||
from pydantic import BaseModel, Field, create_model
|
from pydantic import BaseModel, Field, create_model
|
||||||
|
|
||||||
def build_react_agent(model_name: str, temperature: float, max_tokens: int, agency_mode: bool, obsidian_settings: dict):
|
def build_react_agent(model_name: str, temperature: float, max_tokens: int, agency_mode: bool, obsidian_settings: dict, debug_callback=None):
|
||||||
"""Инициализация ядра на базе LangGraph."""
|
"""Инициализация ядра на базе LangGraph."""
|
||||||
m_cfg = MODELS.get(model_name)
|
m_cfg = MODELS.get(model_name)
|
||||||
llm = ChatOpenAI(
|
llm = ChatOpenAI(
|
||||||
|
|
@ -30,7 +30,7 @@ def build_react_agent(model_name: str, temperature: float, max_tokens: int, agen
|
||||||
max_tokens=max_tokens if max_tokens else None,
|
max_tokens=max_tokens if max_tokens else None,
|
||||||
streaming=True
|
streaming=True
|
||||||
)
|
)
|
||||||
tools = get_dynamic_tools(obsidian_settings) if agency_mode else []
|
tools = get_dynamic_tools(obsidian_settings, debug_callback) if agency_mode else []
|
||||||
return create_react_agent(llm, tools=tools)
|
return create_react_agent(llm, tools=tools)
|
||||||
|
|
||||||
# Специфическая Pydantic-модель для инструментов без скрипта
|
# Специфическая Pydantic-модель для инструментов без скрипта
|
||||||
|
|
@ -41,7 +41,7 @@ class SystemHelpSchema(BaseModel):
|
||||||
dummy: str = Field(default="", description="Необязательный параметр, оставь пустым.")
|
dummy: str = Field(default="", description="Необязательный параметр, оставь пустым.")
|
||||||
|
|
||||||
def create_subprocess_tool(script_path: str, method_name: str,
|
def create_subprocess_tool(script_path: str, method_name: str,
|
||||||
hidden_prompt: str):
|
hidden_prompt: str, debug_callback=None):
|
||||||
"""Вид 2: Инструмент со скриптом + пост-процессинг."""
|
"""Вид 2: Инструмент со скриптом + пост-процессинг."""
|
||||||
|
|
||||||
script_dir = os.path.dirname(os.path.abspath(script_path))
|
script_dir = os.path.dirname(os.path.abspath(script_path))
|
||||||
|
|
@ -69,6 +69,7 @@ def create_subprocess_tool(script_path: str, method_name: str,
|
||||||
|
|
||||||
@functools.wraps(func)
|
@functools.wraps(func)
|
||||||
def wrapper(**kwargs: Any) -> str:
|
def wrapper(**kwargs: Any) -> str:
|
||||||
|
input_json = json.dumps(kwargs, indent=2, ensure_ascii=False)
|
||||||
runner_code = f"""
|
runner_code = f"""
|
||||||
import json
|
import json
|
||||||
import sys
|
import sys
|
||||||
|
|
@ -106,6 +107,10 @@ finally:
|
||||||
)
|
)
|
||||||
raw_output = process.stdout.strip()
|
raw_output = process.stdout.strip()
|
||||||
|
|
||||||
|
# ВЫЗЫВАЕМ КОЛБЭК, если он передан
|
||||||
|
if debug_callback:
|
||||||
|
debug_callback(input_json, raw_output)
|
||||||
|
|
||||||
raw_stripped = raw_output.strip() if raw_output else ""
|
raw_stripped = raw_output.strip() if raw_output else ""
|
||||||
prompt_stripped = hidden_prompt.strip() if hidden_prompt else ""
|
prompt_stripped = hidden_prompt.strip() if hidden_prompt else ""
|
||||||
|
|
||||||
|
|
@ -158,7 +163,7 @@ finally:
|
||||||
args_schema=args_schema, # ← вместо infer_schema=True
|
args_schema=args_schema, # ← вместо infer_schema=True
|
||||||
)
|
)
|
||||||
|
|
||||||
def get_dynamic_tools(obsidian_settings: dict):
|
def get_dynamic_tools(obsidian_settings: dict, debug_callback=None):
|
||||||
"""Собирает инструменты двух видов на основе настроек Obsidian."""
|
"""Собирает инструменты двух видов на основе настроек Obsidian."""
|
||||||
tools = []
|
tools = []
|
||||||
custom_tools = obsidian_settings.get("customTools", [])
|
custom_tools = obsidian_settings.get("customTools", [])
|
||||||
|
|
@ -221,7 +226,7 @@ def get_dynamic_tools(obsidian_settings: dict):
|
||||||
# ВИД 2: Скрипт + Прилизывание
|
# ВИД 2: Скрипт + Прилизывание
|
||||||
# ----------------------------------------------------
|
# ----------------------------------------------------
|
||||||
try:
|
try:
|
||||||
tool = create_subprocess_tool(script_path, method_name, hidden_prompt)
|
tool = create_subprocess_tool(script_path, method_name, hidden_prompt, debug_callback)
|
||||||
tool.name = name # Приоритет имени из UI Obsidian
|
tool.name = name # Приоритет имени из UI Obsidian
|
||||||
if desc:
|
if desc:
|
||||||
tool.description = desc
|
tool.description = desc
|
||||||
|
|
|
||||||
|
|
@ -87,13 +87,20 @@ def run_agent_streaming(graph_id: str,
|
||||||
elif role == "assistant":
|
elif role == "assistant":
|
||||||
messages_for_llm.append(AIMessage(content=msg.get("content", "")))
|
messages_for_llm.append(AIMessage(content=msg.get("content", "")))
|
||||||
|
|
||||||
|
# Локальное хранилище для отладки текущего запуска
|
||||||
|
current_run_debug = []
|
||||||
|
|
||||||
|
def capture_debug(req, res):
|
||||||
|
current_run_debug.append({"req": req, "res": res})
|
||||||
|
|
||||||
# 3. Создаем агента. Если agency_mode == False, он соберет 0 тулов и отработает как обычная LLM.
|
# 3. Создаем агента. Если agency_mode == False, он соберет 0 тулов и отработает как обычная LLM.
|
||||||
agent_executor = build_react_agent(
|
agent_executor = build_react_agent(
|
||||||
model_name=model,
|
model_name=model,
|
||||||
temperature=temperature,
|
temperature=temperature,
|
||||||
max_tokens=max_tokens,
|
max_tokens=max_tokens,
|
||||||
agency_mode=agency_mode,
|
agency_mode=agency_mode,
|
||||||
obsidian_settings=obsidian_settings or {}
|
obsidian_settings=obsidian_settings or {},
|
||||||
|
debug_callback=capture_debug
|
||||||
)
|
)
|
||||||
|
|
||||||
# 4. Выполняем цикл с потоковой передачей событий
|
# 4. Выполняем цикл с потоковой передачей событий
|
||||||
|
|
@ -132,6 +139,18 @@ def run_agent_streaming(graph_id: str,
|
||||||
"node_id": assistant_node_id
|
"node_id": assistant_node_id
|
||||||
}
|
}
|
||||||
|
|
||||||
|
# В САМОМ КОНЦЕ, после того как агент закончил работу (вышел из цикла)
|
||||||
|
if current_run_debug and obsidian_settings.get('showDebugInfo'):
|
||||||
|
debug_section = "\n\n---\n" + "\n\n---\n".join([
|
||||||
|
f"**JSON Request:**\n```json\n{d['req']}\n```\n**Result:**\n```text\n{d['res']}\n```"
|
||||||
|
for d in current_run_debug
|
||||||
|
])
|
||||||
|
yield {
|
||||||
|
"type": "chunk",
|
||||||
|
"content": debug_section,
|
||||||
|
"node_id": assistant_node_id
|
||||||
|
}
|
||||||
|
|
||||||
except Exception as e:
|
except Exception as e:
|
||||||
print(f"Ошибка при стриминге: {e}")
|
print(f"Ошибка при стриминге: {e}")
|
||||||
import traceback
|
import traceback
|
||||||
|
|
|
||||||
Loading…
Reference in New Issue
Block a user