from langgraph.store.memory import InMemoryStore
def embed(texts: list[str]) -> list[list[float]]:
# 替换为实际的嵌入函数或 LangChain 嵌入对象
return [[1.0, 2.0] * len(texts)]
# InMemoryStore 将数据保存在内存字典中。生产环境中请使用数据库支持的存储。
store = InMemoryStore(index={"embed": embed, "dims": 2})
user_id = "my-user"
application_context = "chitchat"
namespace = (user_id, application_context)
store.put(
namespace,
"a-memory",
{
"rules": [
"用户喜欢简短直接的语言",
"用户只会说英语和 Python",
],
"my-key": "my-value",
},
)
# 通过 ID 获取“记忆”
item = store.get(namespace, "a-memory")
# 在此命名空间内搜索“记忆”,按内容相等性过滤,按向量相似度排序
items = store.search(
namespace, filter={"my-key": "my-value"}, query="语言偏好"
)