This commit is contained in:
@@ -9,10 +9,10 @@ from langchain_core.language_models import BaseChatModel
|
||||
from langchain_core.messages import AIMessage
|
||||
|
||||
# 本地模块
|
||||
from app.main_graph.state import MainGraphState
|
||||
from app.agent.prompts import create_system_prompt
|
||||
from app.utils.logging import log_state_change
|
||||
from app.logger import debug, info, error
|
||||
from ...main_graph.state import MainGraphState
|
||||
from ...agent.prompts import create_system_prompt
|
||||
from ...utils.logging import log_state_change
|
||||
from ...logger import debug, info, error
|
||||
|
||||
|
||||
def create_dynamic_llm_call_node(chat_services: Dict[str, BaseChatModel], tools: list):
|
||||
@@ -82,6 +82,12 @@ def create_dynamic_llm_call_node(chat_services: Dict[str, BaseChatModel], tools:
|
||||
try:
|
||||
# 添加上下文到消息
|
||||
messages_with_context = list(state.messages)
|
||||
info(f"[llm_call] 原始消息数量: {len(messages_with_context)}")
|
||||
for i, msg in enumerate(messages_with_context):
|
||||
msg_type = getattr(msg, 'type', 'unknown')
|
||||
msg_content = getattr(msg, 'content', '')[:100] if hasattr(msg, 'content') else str(msg)[:100]
|
||||
info(f"[llm_call] msg[{i}] type={msg_type}, content={repr(msg_content)}")
|
||||
|
||||
if state.rag_context:
|
||||
from langchain_core.messages import SystemMessage
|
||||
rag_system_msg = SystemMessage(content=f"以下是检索到的相关信息:\n{state.rag_context}")
|
||||
@@ -93,11 +99,13 @@ def create_dynamic_llm_call_node(chat_services: Dict[str, BaseChatModel], tools:
|
||||
break
|
||||
if not inserted:
|
||||
messages_with_context.insert(0, rag_system_msg)
|
||||
|
||||
info(f"[llm_call] RAG上下文已添加,长度: {len(state.rag_context)}")
|
||||
|
||||
# 恢复为:手动进行 astream,并将所有的 chunk 拼接成最终的 response 返回。
|
||||
# LangGraph 会自动监听这期间产生的所有 token。
|
||||
chain = prompt | llm_with_tools
|
||||
chunks = []
|
||||
info(f"[llm_call] 开始调用 LLM astream...")
|
||||
async for chunk in chain.astream(
|
||||
{
|
||||
"messages": messages_with_context,
|
||||
@@ -106,7 +114,26 @@ def create_dynamic_llm_call_node(chat_services: Dict[str, BaseChatModel], tools:
|
||||
config=config
|
||||
):
|
||||
chunks.append(chunk)
|
||||
|
||||
|
||||
info(f"[llm_call] LLM astream 完成,共收到 {len(chunks)} 个 chunks")
|
||||
for i, chunk in enumerate(chunks[:10]): # 只打印前10个避免日志过多
|
||||
chunk_type = type(chunk).__name__
|
||||
chunk_content = getattr(chunk, 'content', '') if hasattr(chunk, 'content') else str(chunk)
|
||||
# 打印更多属性
|
||||
additional_kwargs = getattr(chunk, 'additional_kwargs', {}) or {}
|
||||
response_metadata = getattr(chunk, 'response_metadata', {}) or {}
|
||||
# 打印所有属性
|
||||
info(f"[llm_call] chunk[{i}] type={chunk_type}")
|
||||
info(f"[llm_call] chunk[{i}] content长度={len(chunk_content) if chunk_content else 0}, content={repr(chunk_content[:200] if chunk_content else '')}")
|
||||
info(f"[llm_call] chunk[{i}] additional_kwargs={additional_kwargs}")
|
||||
info(f"[llm_call] chunk[{i}] response_metadata keys={list(response_metadata.keys()) if response_metadata else []}")
|
||||
info(f"[llm_call] chunk[{i}] response_metadata={response_metadata}")
|
||||
# 检查是否有其他可能存储内容的属性
|
||||
if hasattr(chunk, 'tool_call_chunks'):
|
||||
info(f"[llm_call] chunk[{i}] tool_call_chunks={chunk.tool_call_chunks}")
|
||||
if hasattr(chunk, 'usage_metadata'):
|
||||
info(f"[llm_call] chunk[{i}] usage_metadata={chunk.usage_metadata}")
|
||||
|
||||
# 将所有 chunk 合并成最终的 AIMessage
|
||||
if chunks:
|
||||
response = chunks[0]
|
||||
@@ -114,6 +141,7 @@ def create_dynamic_llm_call_node(chat_services: Dict[str, BaseChatModel], tools:
|
||||
response = response + chunk
|
||||
else:
|
||||
response = AIMessage(content="")
|
||||
info(f"[llm_call] ⚠️ 警告: 没有收到任何 chunks!")
|
||||
|
||||
elapsed_time = time.time() - start_time
|
||||
|
||||
|
||||
Reference in New Issue
Block a user