MemReader-4B / chat_template.jinja
Nyakult's picture
Upload 12 files
13223f1 verified
{%- if extract_chat_memory %}
{{- '<|im_start|>user\nYou are a memory extraction expert.\n\nYour task is to extract memories from the perspective of user, based on a conversation between user and assistant. This means identifying what user would plausibly remember — including their own experiences, thoughts, plans, or relevant statements and actions made by others (such as assistant) that impacted or were acknowledged by user.\n\nPlease perform:\n1. Identify information that reflects user\'s experiences, beliefs, concerns, decisions, plans, or reactions — including meaningful input from assistant that user acknowledged or responded to.\n2. Resolve all time, person, and event references clearly:\n - Convert relative time expressions (e.g., “yesterday,” “next Friday”) into absolute dates using the message timestamp if possible.\n - Clearly distinguish between event time and message time.\n - If uncertainty exists, state it explicitly (e.g., “around June 2025,” “exact date unclear”).\n - Include specific locations if mentioned.\n - Resolve all pronouns, aliases, and ambiguous references into full names or identities.\n - Disambiguate people with the same name if applicable.\n3. Always write from a third-person perspective, referring to user as\n"The user" or by name if name mentioned, rather than using first-person ("I", "me", "my").\nFor example, write "The user felt exhausted..." instead of "I felt exhausted...".\n4. Do not omit any information that user is likely to remember.\n - Include all key experiences, thoughts, emotional responses, and plans — even if they seem minor.\n - Prioritize completeness and fidelity over conciseness.\n - Do not generalize or skip details that could be personally meaningful to user.\n\nReturn a single valid JSON object with the following structure:\n\n{\n "memory list": [\n {\n "key": <string, a unique, concise memory title>,\n "memory_type": <string, Either "LongTermMemory" or "UserMemory">,\n "value": <A detailed, self-contained, and unambiguous memory statement — written in English if the input conversation is in English, or in Chinese if the conversation is in Chinese>,\n "tags": <A list of relevant thematic keywords (e.g., ["deadline", "team", "planning"])>\n },\n ...\n ],\n "summary": <a natural paragraph summarizing the above memories from user\'s perspective, 120–200 words, same language as the input>\n}\n\nLanguage rules:\n- The `key`, `value`, `tags`, `summary` fields must match the language of the input conversation.\n- Keep `memory_type` in English.\n\nConversation:\n' }}
{%- for message in messages %}
{%- if message.role == 'user' %}
{%- if 'chat_time' in message %}
{{- 'user: [' + message.chat_time + ']: ' + message.content + '\n' }}
{%- else %}
{{- 'user: ' + message.content + '\n' }}
{%- endif %}
{%- elif message.role == 'assistant' %}
{%- if 'chat_time' in message %}
{{- 'assistant: [' + message.chat_time + ']: ' + message.content + '\n' }}
{%- else %}
{{- 'assistant: ' + message.content + '\n'}}
{%- endif %}
{%- endif %}
{%- endfor %}
{{- '\n\nYour Output:' }}
{%- elif extract_doc_memory %}
{{- '<|im_start|>\nYou are an expert text analyst for a search and retrieval system. Your task is to process a document chunk and generate a single, structured JSON object.\nThe input is a single piece of text: `[DOCUMENT_CHUNK]`.\nYou must generate a single JSON object with two top-level keys: `summary` and `tags`.\n1. `summary`:\n - A dense, searchable summary of the ENTIRE `[DOCUMENT_CHUNK]`.\n - The purpose is for semantic search embedding.\n - A clear and accurate sentence that comprehensively summarizes the main points, arguments, and information within the `[DOCUMENT_CHUNK]`.\n - The goal is to create a standalone overview that allows a reader to fully understand the essence of the chunk without reading the original text.\n - The summary should be **no more than 50 words**.\n2. `tags`:\n - A concise list of **3 to 5 high-level, summative tags**.\n - **Each tag itself should be a short phrase, ideally 2 to 4 words long.**\n - These tags must represent the core abstract themes of the text, suitable for broad categorization.\n - **Crucially, prioritize abstract concepts** over specific entities or phrases mentioned in the text. For example, prefer "Supply Chain Resilience" over "Reshoring Strategies".\n\nHere is the document chunk to process:\n`[DOCUMENT_CHUNK]`\n' }}
{{- messages}}
{{- '\n\nProduce ONLY the JSON object as your response.\n' }}
{%- else %}
{%- if tools %}
{{- '<|im_start|>system\n' }}
{%- if messages[0].role == 'system' %}
{{- messages[0].content + '\n\n' }}
{%- endif %}
{{- "# Tools\n\nYou may call one or more functions to assist with the user query.\n\nYou are provided with function signatures within <tools></tools> XML tags:\n<tools>" }}
{%- for tool in tools %}
{{- "\n" }}
{{- tool | tojson }}
{%- endfor %}
{{- "\n</tools>\n\nFor each function call, return a json object with function name and arguments within <tool_call></tool_call> XML tags:\n<tool_call>\n{\"name\": <function-name>, \"arguments\": <args-json-object>}\n</tool_call><|im_end|>\n" }}
{%- else %}
{%- if messages[0].role == 'system' %}
{{- '<|im_start|>system\n' + messages[0].content + '<|im_end|>\n' }}
{%- endif %}
{%- endif %}
{%- set ns = namespace(multi_step_tool=true, last_query_index=messages|length - 1) %}
{%- for message in messages[::-1] %}
{%- set index = (messages|length - 1) - loop.index0 %}
{%- if ns.multi_step_tool and message.role == "user" and message.content is string and not(message.content.startswith('<tool_response>') and message.content.endswith('</tool_response>')) %}
{%- set ns.multi_step_tool = false %}
{%- set ns.last_query_index = index %}
{%- endif %}
{%- endfor %}
{%- for message in messages %}
{%- if message.content is string %}
{%- set content = message.content %}
{%- else %}
{%- set content = '' %}
{%- endif %}
{%- if (message.role == "user") or (message.role == "system" and not loop.first) %}
{{- '<|im_start|>' + message.role + '\n' + content + '<|im_end|>' + '\n' }}
{%- elif message.role == "assistant" %}
{%- set reasoning_content = '' %}
{%- if message.reasoning_content is string %}
{%- set reasoning_content = message.reasoning_content %}
{%- else %}
{%- if '</think>' in content %}
{%- set reasoning_content = content.split('</think>')[0].rstrip('\n').split('<think>')[-1].lstrip('\n') %}
{%- set content = content.split('</think>')[-1].lstrip('\n') %}
{%- endif %}
{%- endif %}
{%- if loop.index0 > ns.last_query_index %}
{%- if loop.last or (not loop.last and reasoning_content) %}
{{- '<|im_start|>' + message.role + '\n<think>\n' + reasoning_content.strip('\n') + '\n</think>\n\n' + content.lstrip('\n') }}
{%- else %}
{{- '<|im_start|>' + message.role + '\n' + content }}
{%- endif %}
{%- else %}
{{- '<|im_start|>' + message.role + '\n' + content }}
{%- endif %}
{%- if message.tool_calls %}
{%- for tool_call in message.tool_calls %}
{%- if (loop.first and content) or (not loop.first) %}
{{- '\n' }}
{%- endif %}
{%- if tool_call.function %}
{%- set tool_call = tool_call.function %}
{%- endif %}
{{- '<tool_call>\n{"name": "' }}
{{- tool_call.name }}
{{- '", "arguments": ' }}
{%- if tool_call.arguments is string %}
{{- tool_call.arguments }}
{%- else %}
{{- tool_call.arguments | tojson }}
{%- endif %}
{{- '}\n</tool_call>' }}
{%- endfor %}
{%- endif %}
{{- '<|im_end|>\n' }}
{%- elif message.role == "tool" %}
{%- if loop.first or (messages[loop.index0 - 1].role != "tool") %}
{{- '<|im_start|>user' }}
{%- endif %}
{{- '\n<tool_response>\n' }}
{{- content }}
{{- '\n</tool_response>' }}
{%- if loop.last or (messages[loop.index0 + 1].role != "tool") %}
{{- '<|im_end|>\n' }}
{%- endif %}
{%- endif %}
{%- endfor %}
{%- endif %}
{%- if add_generation_prompt %}
{{- '<|im_start|>assistant\n' }}
{%- if enable_thinking is defined and enable_thinking is false %}
{{- '<think>\n\n</think>\n\n' }}
{%- endif %}
{%- endif %}