Append attachments content to last message (#17993)

This commit is contained in:
Wang Qi
2026-08-07 16:04:19 +08:00
committed by GitHub
parent 3f78c156d4
commit 993b41b7b1
5 changed files with 9 additions and 25 deletions

View File

@@ -828,7 +828,7 @@ async def async_chat(dialog, messages, stream=True, **kwargs):
kwargs.setdefault("knowledge", "")
gen_conf = dialog.llm_setting
system_content = prompt_config["system"].format(**kwargs) + text_attachments_content
system_content = prompt_config["system"].format(**kwargs)
# If knowledge was retrieved but the template has no {knowledge}
# placeholder, auto-append it so the LLM still sees the context.
if knowledges and "{knowledge}" not in prompt_config.get("system", ""):
@@ -838,6 +838,8 @@ async def async_chat(dialog, messages, stream=True, **kwargs):
if knowledges and (prompt_config.get("quote", True) and kwargs.get("quote", True)):
prompt4citation = citation_prompt()
msg.extend([{"role": m["role"], "content": re.sub(r"##\d+\$\$", "", m["content"])} for m in messages if m["role"] != "system"])
if text_attachments_content and msg:
msg[-1]["content"] += text_attachments_content
used_token_count, msg = message_fit_in(msg, int(max_tokens * 0.95))
if llm_model_config["model_type"] == "chat" and image_attachments:
convert_last_user_msg_to_multimodal(msg, image_attachments, factory)
@@ -1882,6 +1884,8 @@ async def rag_agent(dialog, messages, stream=True, **kwargs):
factory = chat_mdl.model_config.get("llm_factory", "") if chat_mdl.model_config else ""
text_attachments_content, image_attachments, image_files = get_files_content(messages[-1], model_type)
agent_messages = deepcopy(messages)
if text_attachments_content and agent_messages:
agent_messages[-1]["content"] += text_attachments_content
if model_type == "chat" and image_attachments:
convert_last_user_msg_to_multimodal(agent_messages, image_attachments, factory)
use_web_search = _should_use_web_search(prompt_config, kwargs.get("internet"))

View File

@@ -574,20 +574,9 @@ class RAGTools:
if self.tool_started_sink is not None:
self.tool_started_sink()
if self.text_attachments_content:
self.kbinfos = {
"chunks": [
{
"id": "chat_attachment",
"chunk_id": "chat_attachment",
"doc_id": "chat_attachment",
"docnm_kwd": "Chat attachment",
"content_with_weight": self.text_attachments_content,
}
],
"doc_aggs": [{"doc_id": "chat_attachment", "doc_name": "Chat attachment", "count": 1}],
}
messages = [{"role": "user", "content": question}] if question else []
if self.text_attachments_content and messages:
messages[-1]["content"] += self.text_attachments_content
final = ""
async for kind, delta in _split_think_stream(run_agentic_rag(self, messages)):
if kind == "answer":

View File

@@ -143,8 +143,6 @@ async def agentic_research(state: dict, tools) -> dict:
if action == "ANSWER_PARTIAL":
return _finalize(ctx, tools, partial=True)
if action == "ABSTAIN":
if getattr(tools, "text_attachments_content", ""):
return {"verdict": verdict.__dict__, "kbinfos": tools.kbinfos}
tools.kbinfos["chunks"] = []
return {"verdict": verdict.__dict__, "abstain": True}
if action == "REPLAN":

View File

@@ -186,8 +186,6 @@ async def decompose_and_search(state: dict, tools) -> dict:
if action in ("ANSWER", "ANSWER_PARTIAL"):
return _finalize(ctx, tools, partial=action == "ANSWER_PARTIAL", loop=completed_cycles)
if action == "ABSTAIN":
if getattr(tools, "text_attachments_content", ""):
return {"verdict": verdict.__dict__, "kbinfos": tools.kbinfos}
tools.kbinfos["chunks"] = []
return {"verdict": verdict.__dict__, "abstain": True, "loop": completed_cycles}
if action == "FALLBACK_LLM":

View File

@@ -1,5 +1,3 @@
from copy import deepcopy
import pytest
from rag.advanced_rag.agentic_rag import RAGTools
@@ -13,11 +11,10 @@ class FakeChatModel:
@pytest.mark.asyncio
async def test_rag_tool_adds_text_attachment_as_evidence(monkeypatch):
async def test_rag_tool_adds_text_attachment_to_user_question(monkeypatch):
captured = {}
async def fake_run_agentic_rag(tools, messages):
captured["kbinfos"] = deepcopy(tools.kbinfos)
captured["messages"] = messages
yield "answer"
@@ -26,6 +23,4 @@ async def test_rag_tool_adds_text_attachment_as_evidence(monkeypatch):
tools = RAGTools([], FakeChatModel(), text_attachments_content="attached facts")
assert await tools.rag("What is attached?") == "answer"
assert captured["messages"] == [{"role": "user", "content": "What is attached?"}]
assert captured["kbinfos"]["chunks"][0]["docnm_kwd"] == "Chat attachment"
assert captured["kbinfos"]["chunks"][0]["content_with_weight"] == "attached facts"
assert captured["messages"] == [{"role": "user", "content": "What is attached?attached facts"}]