Skip to content

Commit d0ae41f

Browse files
authored
feat: update merge prompt to reduce token cost (#103)
* feat: update merge zh prompt * feat: update en merge prompt
1 parent cf41d90 commit d0ae41f

File tree

5 files changed

+167
-467
lines changed

5 files changed

+167
-467
lines changed

src/server/api/memobase_server/controllers/modal/chat/merge.py

Lines changed: 26 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -155,6 +155,32 @@ async def handle_profile_merge_or_valid(
155155
"attributes": profile_attributes,
156156
}
157157
)
158+
elif update_response["action"] == "APPEND":
159+
if runtime_profile is None:
160+
session_merge_validate_results["add"].append(
161+
{
162+
"content": profile_content,
163+
"attributes": profile_attributes,
164+
}
165+
)
166+
else:
167+
if ContanstTable.update_hits not in runtime_profile.attributes:
168+
runtime_profile.attributes[ContanstTable.update_hits] = 1
169+
else:
170+
runtime_profile.attributes[ContanstTable.update_hits] += 1
171+
session_merge_validate_results["update"].append(
172+
{
173+
"profile_id": runtime_profile.id,
174+
"content": f"{runtime_profile.content};{profile_content}",
175+
"attributes": runtime_profile.attributes,
176+
}
177+
)
178+
session_merge_validate_results["update_delta"].append(
179+
{
180+
"content": profile_content,
181+
"attributes": profile_attributes,
182+
}
183+
)
158184
elif update_response["action"] == "ABORT":
159185
if runtime_profile is None:
160186
TRACE_LOG.info(

src/server/api/memobase_server/controllers/modal/roleplay/predict_new_topics.py

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -48,6 +48,7 @@ async def predict_new_topics(
4848
model=CONFIG.thinking_llm_model,
4949
**prompt.get_kwargs(),
5050
no_cache=True,
51+
thinking_enabled=True,
5152
)
5253
if not r.ok():
5354
return r

src/server/api/memobase_server/llms/doubao_cache_llm.py

Lines changed: 12 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -50,7 +50,12 @@ async def doubao_cache_create_context_and_save(
5050

5151

5252
async def doubao_cache_complete(
53-
model, prompt, system_prompt=None, history_messages=[], **kwargs
53+
model,
54+
prompt,
55+
system_prompt=None,
56+
history_messages=[],
57+
thinking_enable=False,
58+
**kwargs,
5459
) -> str:
5560
sp_args, kwargs = exclude_special_kwargs(kwargs)
5661
prompt_id = sp_args.get("prompt_id", None)
@@ -60,6 +65,12 @@ async def doubao_cache_complete(
6065
messages = []
6166
messages.extend(history_messages)
6267
messages.append({"role": "user", "content": prompt})
68+
69+
# if thinking_enable:
70+
# kwargs["thinking"] = {"type": "enabled"}
71+
# else:
72+
# kwargs["thinking"] = {"type": "disabled"}
73+
6374
if sp_args.get("no_cache", None) or system_prompt is None:
6475

6576
if system_prompt is not None:

src/server/api/memobase_server/prompts/merge_profile.py

Lines changed: 66 additions & 234 deletions
Original file line numberDiff line numberDiff line change
@@ -5,215 +5,72 @@
55
ADD_KWARGS = {
66
"prompt_id": "merge_profile",
77
}
8-
EXAMPLES = {
9-
"replace": [
10-
{
11-
"input": """## User Topic
12-
basic_info, Age
13-
## Old Memo
14-
User is 39 years old
15-
## New Memo
16-
User is 40 years old[mentioned on 2025-05-17]
17-
""",
18-
"response": """
19-
Age has one true value only, the old one is outdated, so replace it with the new one.
20-
---
21-
- UPDATE{tab}User is 40 years old[mentioned on 2025-05-17]
22-
""",
23-
},
24-
],
25-
"merge": [
26-
{
27-
"input": """## User Topic
28-
interest, Food
29-
## Old Memo
30-
User loves cheese pizza[mentioned on 2025-03]
31-
## New Memo
32-
User loves chicken pizza[mentioned on 2025-05]; User ate chicken pizza[mentioned on 2025-05]
33-
""",
34-
"response": """
35-
interest of food is not exclusive, so merge the two memos. Also, I need to keep the final memo concise.
36-
---
37-
- UPDATE{tab}Love cheese pizza[mentioned on 2025-03] and chicken pizza[mentioned on 2025-05]
38-
""",
39-
},
40-
],
41-
"keep": [
42-
{
43-
"input": """## User Topic
44-
basic_info, Birthday
45-
## Old Memo
46-
1999/04/30
47-
## New Memo
48-
User didn't provide any birthday
49-
""",
50-
"response": """
51-
birthday is a unique value and the new memo doesn't provide any valuable info, so keep the old one.
52-
---
53-
- ABORT{tab}invalid
54-
""",
55-
},
56-
],
57-
"special": [
58-
{
59-
"input": """## Update Instruction
60-
Always keep the latest goal and remove the old one.
61-
## User Topic
62-
work, goal
63-
## Old Memo
64-
Want to be a software engineer
65-
## New Memo
66-
Want to start a startup
67-
""",
68-
"response": """
69-
Goal is not exclusive, but the instruction requires to keep the latest goal and remove the old one.
70-
So replace the old one with the new one.
71-
---
72-
- UPDATE{tab}Start a startup
73-
""",
74-
},
75-
],
76-
"validate": [
77-
{
78-
"input": """### Topic Description
79-
Record the user's long-term goal of study.
80-
## User Topic
81-
study, goal
82-
## Old Memo
83-
NONE
84-
## New Memo
85-
I want to play video game in the next weekend
86-
""",
87-
"response": """Just validate the new memo.
88-
The topic is about the user's goal of study, but the value is about planning for playing games.
89-
Also, this topic is about long-term goal and the value is about short-term plan.
90-
---
91-
- ABORT{tab}invalid
92-
""",
93-
},
94-
{
95-
"input": """Today is 2025-04-05
96-
### Topic Description
97-
Record the user's current working plans, forgive the outdated plans
98-
## User Topic
99-
work, curent_plans
100-
## Old Memo
101-
User need to prepare for the interview in 2025-03-21[mentioned on 2025-03-11]
102-
## New Memo
103-
User need to develop a Memobase Playgeound App before 2025-05-01[mentioned on 2025-04-05]
104-
""",
105-
"response": """User can have multiple current working plans, I can merge the two plans.
106-
But based on the requirements, the old memo is outdated(today is 04-05, but the interview is in 03-21), so I need to discard the old memo.
107-
---
108-
- UPDATE{tab}User need to develop a Memobase Playgeound App before 2025-05-01[mentioned on 2025-04-05]
109-
""",
110-
},
111-
],
112-
}
113-
114-
MERGE_FACTS_PROMPT = """You are a smart memo manager which controls the memory/figure of a user.
115-
You job is to validate the memo and merge memos.
116-
You will be given two memos, one old and one new on the same topic/aspect of the user.
117-
You should update the memo based on the inputs.
118-
119-
There are some guidelines about how to update the memo:
120-
### replace the old one
121-
The old memo is considered outdated and should be replaced with the new memo, or the new memo is conflicting with the old memo:
122-
<example>
123-
{example_replace}
124-
</example>
125-
126-
### merge the memos
127-
Note that MERGE should be selected as long as there is information in the old memo that is not included in the new memo.
128-
The old and new memo tell different parts of the same story and should be merged together:
129-
<example>
130-
{example_merge}
131-
</example>
132-
133-
### keep the old one
134-
If the new memo has no information added, containing nothing useful or is invalid, you should keep the old memo by aborting this update(output `- ABORT{tab}invalid`)
135-
<example>
136-
{example_keep}
137-
</example>
138-
139-
### special case
140-
User may give you instructions in '## Update Instruction' section to update the memo in a certain way.
141-
You need to understand the instruction and update the memo accordingly.
142-
<example>
143-
{example_special}
144-
</example>
145-
146-
### no old memo
147-
`## Old Memo` is not always provided, if empty, you just need to validate the new memo based on the topic description.
1488

149-
## Save the final memo with valid requirements
150-
The final memo(w/wo old memo) should be saved matching the topic description.
151-
The topic description may contain some requirements for the memo:
152-
- The value should be certain type, format, in a certain range, etc.
153-
- The value should only record certain information, for example, the user's name, email, long-term goal of study, etc.
154-
You need to judge whether the topic's value matches the description.
155-
If not, you should modify the valid content in memo or decide to discard this operation(output `- ABORT{tab}invalid`).
156-
!! If there is not specific topic description, you should accept the memo as long as it's revelant, only reject/invalid the memo if it's completely inrevelant.
157-
<example>
158-
{example_validate}
159-
</example>
160-
161-
## Input formate
162-
Below is the input format:
163-
<template>
164-
Today is [YYYY-MM-DD]
165-
## Update Instruction
166-
[update_instruction]
167-
### Topic Description
168-
[topic_description]
169-
## User Topic
170-
[topic], [subtopic]
171-
## Old Memo
172-
[old_memo]
173-
## New Memo
174-
[new_memo]
175-
</template>
176-
- [update_instruction], [topic_description], [old_memo] may be empty. When empty, a `NONE` will be placed.
177-
- Pay attention to and keep the time annotation in the new and old memos (e.g., XXX[mentioned on 2025, married on 2023]).
178-
179-
## Output requirements
180-
Think step by step before memo update.
181-
Based on the above instructions, you need to think step by step and output your final result in the following format:
9+
MERGE_FACTS_PROMPT = """You are responsible for maintaining user memos.
10+
Your job is to determine how new supplementary information should be merged with the current memo.
11+
You should decide whether the new supplementary information should be directly added, updated, or merged should be abandoned.
12+
The user will provide the topic/subtopic of the memo, and may also provide topic descriptions and specific update requirements.
13+
14+
Here are your output actions:
15+
1. Direct addition: If the supplementary information brings new information, you should directly add it. If the current memo is empty, you should directly add the supplementary information.
16+
2. Update memo: If the supplementary information conflicts with the current memo or you need to modify the current memo to better reflect the current information, you should update the memo.
17+
3. Abandon merge: If the supplementary information itself has no value, or the information is already completely covered by the current memo, or does not meet the content requirements of the current memo, you should abandon the merge.
18+
19+
## Thinking
20+
Before you output an action, you need to think about the following:
21+
1. Whether the supplementary information meets the topic description of the memo
22+
1.1. If it doesn't meet the requirements, determine whether you can modify the supplementary information to get content that meets the memo requirements, then process your modified supplementary information
23+
1.2. If you can't modify the supplementary information, you should abandon the merge
24+
3. For supplementary information that meets the current memo requirements, you need to refer to the above description to determine the output action
25+
4. If you choose to update the memo, also think about whether there are other parts of the current memo that can be simplified or removed.
26+
27+
Additional situations:
28+
1. The current memo may be empty. In this case, after thinking step 1, if you can get supplementary information that meets the requirements, just add it directly
29+
2. If the update requirements are not empty, you need to refer to the user's update requirements for thinking
30+
31+
## Output Actions
32+
### Direct Addition
18233
```
183-
YOUR THOUGHT
184-
---
185-
- UPDATE{tab}MEMO
34+
- APPEND{tab}APPEND
18635
```
187-
Or
36+
When choosing direct addition, output the `APPEND` word directly, without repeating the content
37+
### Update Memo
18838
```
189-
YOUR THOUGHT
190-
---
191-
- ABORT{tab}invalid
39+
- UPDATE{tab}[UPDATED_MEMO]
19240
```
41+
When choosing to update the memo, you need to rewrite the updated memo in the `[UPDATED_MEMO]` section
42+
### Abort Merge
43+
```
44+
- ABORT{tab}ABORT
45+
```
46+
When choosing to abandon the merge, output the `ABORT` word directly, without repeating the content
19347
194-
You first need to think about the requirements and if the topic's value is suitable for this topic step by step.
195-
Then output your result on topic's value after `---` .
196-
### RESULT
197-
If the topic can be revised to match the description's requirements, output:
198-
- UPDATE{tab}MEMO
199-
the new line must start with `- UPDATE{tab}`, then output the revised value of the topic
200-
If the memo is totally invalid, just output `- ABORT{tab}invalid` after `---`
201-
If there is not specific topic description, you should accept the memo as long as it's revelant, only reject/invalid the memo if it's completely inrevelant.
202-
If the memo is not formatted correctly, you should decide whether it can be fixed by revising the memo, if yes, update it, if not, abort the update.
48+
## Output Template
49+
Based on the above instructions, your output should be in the following format:
20350
204-
Make sure you understand the topic description(In `### Topic Description` section) if it exists and update the final memo accordingly.
205-
Understand the memos wisely, you are allowed to infer the information from the new memo and old memo to decide the final memo.
206-
Follow the instruction mentioned below:
207-
- Do not return anything from the custom few shot prompts provided above.
208-
- Stick to the correct output format.
209-
- Make sure the final memo is no more than 5 sentences. Always concise and output the guts of the memo.
210-
- Do not make any explanations in MEMO, only output the final value related to the topic.
211-
- Never make up things that are not mentioned in the input.
212-
- If the input memos are not matching the topic description, you should output `- ABORT{tab}invalid` after `---`
213-
- Keep the time annotation in the new and old memos (e.g., XXX[mentioned on 2025, happend at 2023]).
214-
- If you decide to update, make sure the final memo is concise and no redundant information. (e.g. "User is sad; User's mood is sad" -> "User is sad")
51+
THOUGHT
52+
---
53+
ACTION
54+
55+
Where:
56+
- `THOUGHT` is your thinking process
57+
- `ACTION` is your output action
58+
For example:
59+
```example
60+
The supplementary information mentions that the user's current learning goal is to prepare for final exams, and the current topic description records the user's learning goals, which meets the requirements. At the same time, the current memo also has a record of preparing for midterm exams, which suggests that the midterm exams should already be over. So the supplementary information cannot simply be added, but needs to update the current memo.
61+
I need to update the corresponding area while retaining the rest of the memo
62+
---
63+
- UPDATE{tab}...Currently self-studying Japanese using Duolingo, hoping to pass the Japanese Level 2 exam [mentioned on 2025/05/05]; Preparing for final exams [mentioned on 2025/06/01];
64+
```
65+
66+
Follow these instructions:
67+
- Strictly adhere to the correct output format.
68+
- Ensure the final memo does not exceed 5 sentences. Always keep it concise and output the key points of the memo.
69+
- Never make up content not mentioned in the input.
70+
- Preserve time annotations from both old and new memos (e.g.: XXX[mentioned on 2025/05/05, occurred in 2022]).
71+
- If you decide to update, ensure the final memo is concise and has no redundant information. (e.g.: "User is sad; User's mood is sad" == "User is sad")
21572
216-
That's all, now perform your job.
73+
That's all the content, now execute your work.
21774
"""
21875

21976

@@ -222,46 +79,21 @@ def get_input(
22279
):
22380
today = datetime.now().astimezone(CONFIG.timezone).strftime("%Y-%m-%d")
22481
return f"""Today is {today}.
225-
## Update Instruction
226-
{update_instruction or "NONE"}
227-
### Topic Description
228-
{topic_description or "NONE"}
229-
## User Topic
82+
## Memo Update Instruction
83+
{update_instruction or "[empty]"}
84+
### Memo Topic Description
85+
{topic_description or "[empty]"}
86+
## Memo Topic
23087
{topic}, {subtopic}
231-
## Old Memo
232-
{old_memo or "NONE"}
233-
## New Memo
88+
## Current Memo
89+
{old_memo or "[empty]"}
90+
## Supplementary Information
23491
{new_memo}
23592
"""
23693

23794

238-
def form_example(examples: list[dict]) -> str:
239-
return "\n".join(
240-
[
241-
f"""<input>
242-
{example['input']}
243-
</input>
244-
<output>
245-
{example['response']}
246-
</output>
247-
"""
248-
for example in examples
249-
]
250-
).format(tab=CONFIG.llm_tab_separator)
251-
252-
25395
def get_prompt() -> str:
254-
example_replace = form_example(EXAMPLES["replace"])
255-
example_merge = form_example(EXAMPLES["merge"])
256-
example_keep = form_example(EXAMPLES["keep"])
257-
example_special = form_example(EXAMPLES["special"])
258-
example_validate = form_example(EXAMPLES["validate"])
25996
return MERGE_FACTS_PROMPT.format(
260-
example_replace=example_replace,
261-
example_merge=example_merge,
262-
example_keep=example_keep,
263-
example_special=example_special,
264-
example_validate=example_validate,
26597
tab=CONFIG.llm_tab_separator,
26698
)
26799

0 commit comments

Comments
 (0)