news 2026/2/14 23:29:17

memos 写入记忆读取记忆的skill

作者头像

张小明

前端开发工程师

1.2k 24
文章封面图
memos 写入记忆读取记忆的skill
--- name: memos-memory-assistant description: >- 简单的记忆存储与检索助手,提供便捷的记忆功能。 适合需要存储和检索用户偏好的场景。 常见触发场景:记住用户喜好、检索用户信息 trigger_keywords: - 记住 - 记忆 - 想起 - 搜索记忆 - 用户信息 - 偏好记忆 --- ## 详细功能 - **记住信息** - **工具**: remember - **CLI调用**: `python -m skills.memos remember --content "我喜欢苹果"` - **参数**: - content (要记住的内容) - **操作**: 将信息存储到记忆库中 - **检索记忆** - **工具**: recall - **CLI调用**: `python -m skills.memos recall --query "苹果"` - **参数**: - query (搜索关键词) - **操作**: 根据关键词检索相关的记忆信息
"""Simple memory skill for storing and retrieving user memories.""" import os from typing import List, Dict, Optional # Try to import the real MemOS client, fall back to local memory if not available try: from memos.api.client import MemOSClient MEMOS_AVAILABLE = True except ImportError: MEMOS_AVAILABLE = False print("警告: MemOS客户端不可用,将使用本地记忆功能") # Import local memory functions using absolute import try: from agent_chator.memory.local_memory import add_message as local_add_message, search_memory as local_search_memory except ImportError: # Fallback if the module is not available def local_add_message(user_id, conversation_id, messages): print("警告: 本地记忆功能不可用") return {"error": "Local memory not available"} def local_search_memory(query, user_id, conversation_id): print("警告: 本地记忆功能不可用") return {"error": "Local memory not available"} class MemosSkill: """A skill for interacting with MemOS API to store and retrieve memories.""" def __init__(self): self.api_key = os.getenv("MEMOS_API_KEY") if not self.api_key or not MEMOS_AVAILABLE: print("信息: 未设置 MEMOS_API_KEY 或 MemOS不可用,将使用本地记忆功能") self.use_local = True else: self.use_local = False # Use remote if available and key is set if not self.use_local: try: self.client = MemOSClient(api_key=self.api_key) except Exception as e: print(f"警告: 无法初始化MemOS客户端 ({e}),将使用本地记忆功能") self.use_local = True else: self.client = None def add_message(self, user_id: str, conversation_id: str, messages: List[Dict[str, str]]) -> Dict: """ Store a conversation in MemOS to create memories. Args: user_id: The ID of the user conversation_id: The ID of the conversation messages: List of messages in the format {"role": "user|assistant", "content": "message content"} Returns: Response from the MemOS API """ if self.use_local: return local_add_message(user_id=user_id, conversation_id=conversation_id, messages=messages) else: return self.client.add_message(messages=messages, user_id=user_id, conversation_id=conversation_id) def search_memory(self, query: str, user_id: str, conversation_id: str) -> Dict: """ Retrieve relevant memories from MemOS based on a query. Args: query: The search query to find relevant memories user_id: The ID of the user conversation_id: The ID of the current conversation Returns: Response from the MemOS API containing relevant memories """ if self.use_local: return local_search_memory(query=query, user_id=user_id, conversation_id=conversation_id) else: return self.client.search_memory(query=query, user_id=user_id, conversation_id=conversation_id) # Global variable to hold the skill instance _memosskill = None def _get_memosskill(): """Lazy initialization of the MemosSkill instance.""" global _memosskill if _memosskill is None: _memosskill = MemosSkill() return _memosskill # Export the functions for direct use def add_message(user_id: str, conversation_id: str, messages: List[Dict[str, str]]) -> Dict: """ Store a conversation in MemOS to create memories. Args: user_id: The ID of the user conversation_id: The ID of the conversation messages: List of messages in the format {"role": "user|assistant", "content": "message content"} Returns: Response from the MemOS API """ skill = _get_memosskill() return skill.add_message(user_id, conversation_id, messages) def search_memory(query: str, user_id: str, conversation_id: str) -> Dict: """ Retrieve relevant memories from MemOS based on a query. Args: query: The search query to find relevant memories user_id: The ID of the user conversation_id: The ID of the current conversation Returns: Response from the MemOS API containing relevant memories """ skill = _get_memosskill() return skill.search_memory(query, user_id, conversation_id) # Simple convenience functions for common use cases DEFAULT_USER_ID = "default_user" def remember(content: str, user_id: str = DEFAULT_USER_ID) -> Dict: """ Remember something for a user. Args: content: The content to remember user_id: The ID of the user (optional, defaults to 'default_user') Returns: Result of the operation """ messages = [{"role": "user", "content": content}] return add_message(user_id, "default_conversation", messages) def recall(query: str, user_id: str = DEFAULT_USER_ID) -> Dict: """ Recall information for a user based on a query. Args: query: The query to search for user_id: The ID of the user (optional, defaults to 'default_user') Returns: Search results """ return search_memory(query, user_id, "default_conversation")
版权声明: 本文来自互联网用户投稿,该文观点仅代表作者本人,不代表本站立场。本站仅提供信息存储空间服务,不拥有所有权,不承担相关法律责任。如若内容造成侵权/违法违规/事实不符,请联系邮箱:809451989@qq.com进行投诉反馈,一经查实,立即删除!
网站建设 2026/2/11 1:54:34

基于memos和agentscope的ai工具和记忆调用助手

https://www.bilibili.com/video/BV1gvFLzyEEk/?spm_id_from333.1387.homepage.video_card.click memos 写入记忆读取记忆的skill-CSDN博客 请输入您的查询(输入 exit 退出):请输入您的查询: 查找一下我喜欢什么AI回复: 信息: 已启用MemOS…

作者头像 李华
网站建设 2026/2/11 4:28:19

从崩溃到重生:解决 Anaconda 环境下 Poetry 安装的一系列“大坑”

目录 从崩溃到重生:解决 Anaconda 环境下 Poetry 安装的一系列“大坑” 前言 一、 核心痛点:为什么你的 Poetry 总装不上? 二、 最终制胜方案:隔离环境 镜像强制安装 1. 另起炉灶:避开损坏的 Base 环境 2. 暴力…

作者头像 李华
网站建设 2026/2/11 13:54:14

Thinkphp和Laravel框架的校园二手书籍交易平台的设计实现

目录 ThinkPHP与Laravel框架的校园二手书籍交易平台设计实现平台架构设计核心功能实现性能优化策略部署与扩展性 开发技术源码文档获取/同行可拿货,招校园代理 :文章底部获取博主联系方式! ThinkPHP与Laravel框架的校园二手书籍交易平台设计实现 平台架…

作者头像 李华
网站建设 2026/2/13 12:07:42

Thinkphp和Laravel框架的校园共享厨房预约美食菜谱系统

目录 ThinkPHP与Laravel框架的校园共享厨房预约美食菜谱系统摘要系统概述功能模块技术实现特色与优势应用场景 开发技术源码文档获取/同行可拿货,招校园代理 :文章底部获取博主联系方式! ThinkPHP与Laravel框架的校园共享厨房预约美食菜谱系统摘要 系统…

作者头像 李华
网站建设 2026/2/14 13:26:46

Thinkphp和Laravel框架的物流车辆货车配送路线信息管理系统

目录 ThinkPHP与Laravel框架的物流车辆配送路线管理系统摘要系统目标技术框架对比核心功能模块数据库设计安全与扩展性适用场景 开发技术源码文档获取/同行可拿货,招校园代理 :文章底部获取博主联系方式! ThinkPHP与Laravel框架的物流车辆配送路线管理系…

作者头像 李华
网站建设 2026/2/13 17:10:49

Thinkphp和Laravel框架的物流运输仓储仓库采购信息系统平台的设计与实现

目录Thinkphp与Laravel框架下的物流仓储信息系统设计摘要开发技术源码文档获取/同行可拿货,招校园代理 :文章底部获取博主联系方式!Thinkphp与Laravel框架下的物流仓储信息系统设计摘要 技术架构对比与选型 Thinkphp框架以其轻量级、高效性和易用性著称…

作者头像 李华