RAGFlow is an agent-native database: a Retrieval-Augmented Generation (RAG) engine that grew into context infrastructure.
Go agent-harnessagent-native-databaseagentic-aiagentic-retrievalagentic-searchaiai-agentscontext-enginecontext-engineeringcontext-managementharness-engineeringknowledge-compilationragretrieval-augmented-generationretrieval-harnesssearch-harness
Add {date} replacement to chat (#17430)
Wang Qi619b58faef36
diff --git a/api/apps/restful_apis/chat_api.py b/api/apps/restful_apis/chat_api.pyindex cf04953602..456df204ea 100644--- a/api/apps/restful_apis/chat_api.py+++ b/api/apps/restful_apis/chat_api.py@@ -94,7 +94,7 @@ _DEFAULT_PROMPT_CONFIG = {" The above is the knowledge base."),"prologue": "Hi! I'm your assistant. What can I do for you?",- "parameters": [{"key": "knowledge", "optional": False}],+ "parameters": [{"key": "knowledge", "optional": False}, {"key": "date", "optional": True}],"empty_response": "Sorry! No relevant content was found in the knowledge base!","quote": True,"tts": False,@@ -353,6 +353,8 @@ def _apply_prompt_defaults(req):if req.get("kb_ids") and not prompt_config.get("parameters") and "{knowledge}" in prompt_config.get("system", ""):prompt_config["parameters"] = [{"key": "knowledge", "optional": False}]+ if not any(p.get("key") == "date" for p in prompt_config.get("parameters", [])):+ prompt_config.setdefault("parameters", []).append({"key": "date", "optional": True})@manager.route("/chats", methods=["POST"]) # noqa: F821diff --git a/api/db/services/dialog_service.py b/api/db/services/dialog_service.pyindex b99207e073..2412c05d4f 100644--- a/api/db/services/dialog_service.py+++ b/api/db/services/dialog_service.py@@ -22,7 +22,7 @@ from copy import deepcopyfrom rag.advanced_rag.agentic_rag import RAGToolslogger = logging.getLogger(__name__)-from datetime import datetime+from datetime import datetime, timezonefrom functools import partialfrom timeit import default_timer as timerfrom langfuse import Langfuse, propagate_attributes@@ -335,11 +335,13 @@ async def async_chat_solo(dialog, messages, stream=True, session_id=None):msg[-1]["content"] += attachmentsif model_config["model_type"] == "chat" and image_attachments:convert_last_user_msg_to_multimodal(msg, image_attachments, factory)+ sys_date = datetime.now(timezone.utc).strftime("%Y-%m-%d %H:%M:%S")+ system_prompt = prompt_config.get("system", "").replace("{date}", sys_date)if stream:if model_config["model_type"] == "chat":- stream_iter = chat_mdl.async_chat_streamly_delta(prompt_config.get("system", ""), msg, dialog.llm_setting)+ stream_iter = chat_mdl.async_chat_streamly_delta(system_prompt, msg, dialog.llm_setting)else:- stream_iter = chat_mdl.async_chat_streamly_delta(prompt_config.get("system", ""), msg, dialog.llm_setting, images=image_files)+ stream_iter = chat_mdl.async_chat_streamly_delta(system_prompt, msg, dialog.llm_setting, images=image_files)async for kind, value, state in _stream_with_think_delta(stream_iter):if kind == "marker":flags = {"start_to_think": True} if value == "<think>" else {"end_to_think": True}@@ -348,9 +350,9 @@ async def async_chat_solo(dialog, messages, stream=True, session_id=None):yield {"answer": value, "reference": {}, "audio_binary": tts(tts_mdl, value), "prompt": "", "created_at": time.time(), "final": False}else:if model_config["model_type"] == "chat":- answer = await chat_mdl.async_chat(prompt_config.get("system", ""), msg, dialog.llm_setting)+ answer = await chat_mdl.async_chat(system_prompt, msg, dialog.llm_setting)else:- answer = await chat_mdl.async_chat(prompt_config.get("system", ""), msg, dialog.llm_setting, images=image_files)+ answer = await chat_mdl.async_chat(system_prompt, msg, dialog.llm_setting, images=image_files)user_content = msg[-1].get("content", "[content not available]")logging.debug("User: {}|Assistant: {}".format(user_content, answer))yield {"answer": answer, "reference": {}, "audio_binary": tts(tts_mdl, answer), "prompt": "", "created_at": time.time()}@@ -677,6 +679,8 @@ async def async_chat(dialog, messages, stream=True, **kwargs):param_keys.append("knowledge")logging.debug(f"attachments={attachments}, param_keys={param_keys}, embd_mdl={embd_mdl}")+ sys_date = datetime.now(timezone.utc).strftime("%Y-%m-%d %H:%M:%S")+ kwargs["date"] = sys_datefor p in prompt_config.get("parameters", []):if p["key"] == "knowledge":continuediff --git a/test/testcases/restful_api/test_chats.py b/test/testcases/restful_api/test_chats.pyindex 16b8c938e6..4b90675489 100644--- a/test/testcases/restful_api/test_chats.py+++ b/test/testcases/restful_api/test_chats.py@@ -1192,7 +1192,7 @@ def test_chat_create_allows_default_knowledge_placeholder_without_sources_unit(massert res["code"] == 0assert saved["kb_ids"] == []assert saved["prompt_config"]["system"].find("{knowledge}") >= 0- assert saved["prompt_config"]["parameters"] == [{"key": "knowledge", "optional": False}]+ assert saved["prompt_config"]["parameters"] == [{"key": "knowledge", "optional": False}, {"key": "date", "optional": True}]@pytest.mark.p2diff --git a/web/src/pages/next-chats/hooks/use-create-chat.ts b/web/src/pages/next-chats/hooks/use-create-chat.tsindex b9ebe2f9bd..3492c6bc08 100644--- a/web/src/pages/next-chats/hooks/use-create-chat.ts+++ b/web/src/pages/next-chats/hooks/use-create-chat.ts@@ -32,7 +32,10 @@ export const useCreateChatDialog = () => {refine_multiturn: false,use_kg: false,reasoning: false,- parameters: [{ key: 'knowledge', optional: false }],+ parameters: [+ { key: 'knowledge', optional: false },+ { key: 'date', optional: true },+ ],toc_enhance: false,},llm_id: defaultModelDictionary?.llm_id,