Skip to content

Commit 7c4c10e

Browse files
committed
fix: fix the short memory profile eval
1 parent e2f5991 commit 7c4c10e

File tree

3 files changed

+122
-18
lines changed

3 files changed

+122
-18
lines changed

veadk/memory/short_term_memory.py

Lines changed: 65 additions & 18 deletions
Original file line numberDiff line numberDiff line change
@@ -188,39 +188,86 @@ async def generate_profile(
188188
import json
189189

190190
from veadk import Agent, Runner
191-
from veadk.memory.types import MemoryProfile
191+
from veadk.memory.types import MemoryProfileListV2
192192
from veadk.utils.misc import write_string_to_file
193193

194194
event_text = ""
195195
for event in events:
196196
event_text += f"- Event id: {event.id}\nEvent content: {event.content}\n"
197+
print("the events is:\n ", event_text)
198+
print("-------------------")
199+
# instructions_v1 = """Summarize the memory events into at least two groups according to the event content. An event can belong to multiple groups. You must output the summary in JSON format (Each group should have a simple name (only a-z and _ is allowed), and a list of event ids):
200+
# [
201+
# {
202+
# "name": "",
203+
# "describe":"",
204+
# "event_ids": ["Event id here"]
205+
# },
206+
# {
207+
# "name": "",
208+
# "describe":"",
209+
# "event_ids": ["Event id here"]
210+
# }
211+
# ]
212+
# The event id must be derived from the provided events data.
213+
# """
214+
instructions_v2 = """You are tasked with grouping memory events based on their content. Follow these requirements strictly:
215+
216+
1. **Core Task**: Summarize the provided memory events into at least 2 groups. An event can belong to multiple groups.
217+
2. **Input Specification**: The input is structured memory events data containing unique event IDs (to be used directly in outputs) and event details (characters, time, location, event type, topic, emotion, etc.).
218+
3. **Group Requirements**:
219+
- **Name**: A concise name for the group, using only lowercase letters and underscores (e.g., "family_dinner_events").
220+
- **Tags**: A list of entity tags extracted from the events in the group, covering characters, time, location, event type, topic, and emotion (e.g., ["Alice", "2023-10-01", "home", "reunion", "holiday_plans", "joyful"]).
221+
- **Event IDs**: A list of event IDs from the input data that belong to the group (must match the IDs provided in the input).
222+
4. **Output Format**: Strictly use JSON format with no extra content. The JSON structure must be:
223+
{
224+
"groups": [
225+
{
226+
"name": "group_name_1",
227+
"tags": ["tag1", "tag2", ...],
228+
"event_ids": ["event_id_1", "event_id_2", ...]
229+
},
230+
{
231+
"name": "group_name_2",
232+
"tags": ["tag3", "tag4", ...],
233+
"event_ids": ["event_id_3", "event_id_4", ...]
234+
}
235+
]
236+
}
237+
5. **Prohibitions**: Do not fabricate event IDs, tags, or group names; all content must be derived from the input events data. Do not output any text outside the JSON structure.
238+
239+
"""
197240

198241
agent = Agent(
199242
name="memory_summarizer",
200243
description="A summarizer that summarizes the memory events.",
201-
instruction="""Summarize the memory events into different groups according to the event content. An event can belong to multiple groups. You must output the summary in JSON format (Each group should have a simple name (only a-z and _ is allowed), and a list of event ids):
202-
[
203-
{
204-
"name": "",
205-
"event_ids": ["Event id here"]
206-
},
207-
{
208-
"name": "",
209-
"event_ids": ["Event id here"]
210-
}
211-
]""",
244+
instruction=instructions_v2,
212245
model_name="deepseek-v3-2-251201",
213-
output_schema=MemoryProfile,
246+
output_schema=MemoryProfileListV2,
214247
)
215248
runner = Runner(agent=agent)
249+
print("event_text =", event_text)
216250

217251
response = await runner.run(messages="Events are: \n" + event_text)
252+
print("response =", response)
218253

219254
# profile path: ./profiles/memory/<app_name>/user_id/session_id/profile_name.json
220-
groups = json.loads(response)
221-
group_names = [group["name"] for group in groups]
222-
223-
for group in groups:
255+
try:
256+
groups = json.loads(response)
257+
print("groups =", groups)
258+
except Exception as e:
259+
# 捕获所有可能的异常(如JSON解析错误、response未定义、打印异常等)
260+
print(f"解析JSON时出错: {e}") # 可选:打印错误信息,便于排查
261+
groups = {
262+
"profiles": [],
263+
} # 报错时生成空列表
264+
265+
group_names = [
266+
{"name": group["name"], "tags": group["tags"]}
267+
for group in groups["profiles"]
268+
]
269+
270+
for group in groups["profiles"]:
224271
group["event_list"] = []
225272
for event_id in group["event_ids"]:
226273
for event in events:
@@ -232,7 +279,7 @@ async def generate_profile(
232279
file_path=f"./profiles/memory/{app_name}/{user_id}/{session_id}/profile_list.json",
233280
)
234281

235-
for group in groups:
282+
for group in groups["profiles"]:
236283
write_string_to_file(
237284
content=json.dumps(group, ensure_ascii=False),
238285
file_path=f"./profiles/memory/{app_name}/{user_id}/{session_id}/{group['name']}.json",

veadk/memory/types.py

Lines changed: 15 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -17,4 +17,19 @@
1717

1818
class MemoryProfile(BaseModel):
1919
name: str
20+
describe: str
2021
event_ids: list[str]
22+
23+
24+
class MemoryProfileList(BaseModel):
25+
profiles: list[MemoryProfile] # 核心:list[MemoryProfile]类型字段
26+
27+
28+
class MemoryProfileV2(BaseModel):
29+
name: str
30+
tags: list[str]
31+
event_ids: list[str]
32+
33+
34+
class MemoryProfileListV2(BaseModel):
35+
profiles: list[MemoryProfileV2] # 核心:list[MemoryProfile]类型字段

veadk/tools/demo_tools.py

Lines changed: 42 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -56,3 +56,45 @@ def get_location_weather(city: str) -> dict[str, str]:
5656
)
5757
temperature = random.randint(-10, 40)
5858
return {"result": f"{condition}, {temperature}°C"}
59+
60+
61+
def search_agent_tool(query: str) -> dict[str, str]:
62+
"""Search for available agents based on the user query."""
63+
# Mock agent data
64+
agents = {
65+
"travel_agent": "Helps with booking flights and hotels.",
66+
"coding_agent": "Assists with writing and debugging code.",
67+
"research_agent": "Summarizes academic papers and articles.",
68+
}
69+
70+
result = {}
71+
query = query.lower()
72+
for name, card in agents.items():
73+
if query in name or query in card.lower():
74+
result[name] = card
75+
76+
return result
77+
78+
79+
def agent_invoke(prompt: str, url: str) -> dict:
80+
"""Invokes an agent service using the A2A protocol."""
81+
import requests
82+
83+
response = requests.post(
84+
url,
85+
headers={"Content-Type": "application/json"},
86+
json={
87+
"jsonrpc": "2.0",
88+
"id": "req-1",
89+
"method": "message/send",
90+
"params": {
91+
"message": {
92+
"id": "123456",
93+
"messageId": "123456",
94+
"role": "user",
95+
"parts": [{"text": prompt}],
96+
}
97+
},
98+
},
99+
)
100+
return response.json()

0 commit comments

Comments
 (0)