mirror of
https://github.com/xinnan-tech/xiaozhi-esp32-server.git
synced 2026-07-26 00:53:54 +08:00
解析json字符串提取相关文本
This commit is contained in:
@@ -37,11 +37,26 @@ class MemoryProvider(MemoryProviderBase):
|
|||||||
|
|
||||||
try:
|
try:
|
||||||
# Format the content as a message list for mem0
|
# Format the content as a message list for mem0
|
||||||
messages = [
|
messages = []
|
||||||
{"role": message.role, "content": message.content}
|
for message in msgs:
|
||||||
for message in msgs
|
if message.role == "system":
|
||||||
if message.role != "system"
|
continue
|
||||||
]
|
|
||||||
|
content = message.content
|
||||||
|
|
||||||
|
# Extract content from JSON format if present (for ASR with emotion/language tags)
|
||||||
|
# Same logic as in query_memory method
|
||||||
|
try:
|
||||||
|
if content and content.strip().startswith("{") and content.strip().endswith("}"):
|
||||||
|
data = json.loads(content)
|
||||||
|
if "content" in data:
|
||||||
|
content = data["content"]
|
||||||
|
except (json.JSONDecodeError, KeyError, TypeError):
|
||||||
|
# If parsing fails, use original content
|
||||||
|
pass
|
||||||
|
|
||||||
|
messages.append({"role": message.role, "content": content})
|
||||||
|
|
||||||
result = self.client.add(messages, user_id=self.role_id)
|
result = self.client.add(messages, user_id=self.role_id)
|
||||||
logger.bind(tag=TAG).debug(f"Save memory result: {result}")
|
logger.bind(tag=TAG).debug(f"Save memory result: {result}")
|
||||||
except Exception as e:
|
except Exception as e:
|
||||||
|
|||||||
@@ -149,10 +149,22 @@ class MemoryProvider(MemoryProviderBase):
|
|||||||
|
|
||||||
msgStr = ""
|
msgStr = ""
|
||||||
for msg in msgs:
|
for msg in msgs:
|
||||||
|
content = msg.content
|
||||||
|
|
||||||
|
# Extract content from JSON format if present (for ASR with emotion/language tags)
|
||||||
|
try:
|
||||||
|
if content and content.strip().startswith("{") and content.strip().endswith("}"):
|
||||||
|
data = json.loads(content)
|
||||||
|
if "content" in data:
|
||||||
|
content = data["content"]
|
||||||
|
except (json.JSONDecodeError, KeyError, TypeError):
|
||||||
|
# If parsing fails, use original content
|
||||||
|
pass
|
||||||
|
|
||||||
if msg.role == "user":
|
if msg.role == "user":
|
||||||
msgStr += f"User: {msg.content}\n"
|
msgStr += f"User: {content}\n"
|
||||||
elif msg.role == "assistant":
|
elif msg.role == "assistant":
|
||||||
msgStr += f"Assistant: {msg.content}\n"
|
msgStr += f"Assistant: {content}\n"
|
||||||
if self.short_memory and len(self.short_memory) > 0:
|
if self.short_memory and len(self.short_memory) > 0:
|
||||||
msgStr += "历史记忆:\n"
|
msgStr += "历史记忆:\n"
|
||||||
msgStr += self.short_memory
|
msgStr += self.short_memory
|
||||||
|
|||||||
@@ -173,11 +173,25 @@ class MemoryProvider(MemoryProviderBase):
|
|||||||
|
|
||||||
try:
|
try:
|
||||||
# Format the content as a message list for PowerMem
|
# Format the content as a message list for PowerMem
|
||||||
messages = [
|
messages = []
|
||||||
{"role": message.role, "content": message.content}
|
for message in msgs:
|
||||||
for message in msgs
|
if message.role == "system":
|
||||||
if message.role != "system"
|
continue
|
||||||
]
|
|
||||||
|
content = message.content
|
||||||
|
|
||||||
|
# Extract content from JSON format if present (for ASR with emotion/language tags)
|
||||||
|
# Same logic as in query_memory method
|
||||||
|
try:
|
||||||
|
if content and content.strip().startswith("{") and content.strip().endswith("}"):
|
||||||
|
data = json.loads(content)
|
||||||
|
if "content" in data:
|
||||||
|
content = data["content"]
|
||||||
|
except (json.JSONDecodeError, KeyError, TypeError):
|
||||||
|
# If parsing fails, use original content
|
||||||
|
pass
|
||||||
|
|
||||||
|
messages.append({"role": message.role, "content": content})
|
||||||
|
|
||||||
# Add memory using PowerMem SDK
|
# Add memory using PowerMem SDK
|
||||||
result = self.memory_client.add(
|
result = self.memory_client.add(
|
||||||
|
|||||||
Reference in New Issue
Block a user