ReMe/memory_scope/node/memory_node.py
2024-06-17 20:12:58 +08:00

71 lines
2.5 KiB
Python
Raw Blame History

This file contains ambiguous Unicode characters

This file contains Unicode characters that might be confused with other characters. If you think that this is intentional, you can safely ignore this warning. Use the Escape button to reveal them.

import re
from typing import Dict, List
from pydantic import Field, BaseModel
class MemoryNode(BaseModel):
"""
除了 content_modified其他均和数据库字段保持统一
根据code判断如果code是空则为新增的memoryNode如果有值则为更新
if content_modified is true则需要调用embedding服务
"""
id: str = Field("", description="唯一主键 uuid64")
code: str = Field("", description="和id保持一致为空则是新增")
# 0520新增
timeCreated: str = Field("", description="Memory创建时间算法不关注")
# 0520新增
timeModified: str = Field("", description="Memory更新时间算法不关注")
content: str = Field("", description="记忆内容")
memoryId: str = Field("", description="记忆 id检索区分字段")
# 0520新增
scene: str = Field("", description="source: TONGYI_MAIN_CHAT/TONGYI_CHAR_CHAT/BAILIAN/ASSISTANT")
# 0520新增
# NOTE 百炼服务端只召回observation, insight, profile, obs_customized, profile_customized
memoryType: str = Field("", description="conversation, observation, insight, "
"profile, obs_customized, profile_customized")
# 0520新增但不是数据库字段
content_modified: bool = Field(False, description="content是否被更新if true则需要调用embedding服务")
# reflected: 1 is reflected before, 0 has not reflected, 如果是用户自定义,写入空值"".
metaData: Dict[str, str] = Field({}, description="元信息: infoScore, algoVersion, datetime, reflected")
status: str = Field("active", description="active or expired")
tenantId: str = Field("", description="request id")
vector: List[float] = Field([], description="content embedding result, return empty")
def get_time_info(self, time_format: str):
pattern = re.compile(r'\{([^}]*)}')
keys = pattern.findall(time_format)
match_flag = True
kv_dict = {}
for k in keys:
if k not in self.metaData:
match_flag = False
break
v = self.metaData[k]
if not v:
match_flag = False
break
kv_dict[k] = v
if match_flag:
return time_format.format(**kv_dict)
return ""
def to_dict(self):
res = {"content": self.content, "memoryId": self.memoryId, "memoryType": self.memoryType,
"status": self.status, "metaData": self.metaData}
return res