MemoryScope工程部署测试笔记
1.elastic search本地安装配置
1.1.工程项目下载
从elastic下载地址选择一个版本下载:
# 1.工程下载
>> wget https://artifacts.elastic.co/downloads/elasticsearch/elasticsearch-8.16.1-linux-x86_64.tar.gz
# 2.解压
>> tar -zxvf elasticsearch-8.16.1-linux-x86_64.tar.gz
1.2.修改配置
文件路径:config/elasticsearch.yml
:
cluster.name: elasticsearch
node.name: es-node0
# 数据存放路径和日志存放路径
path.data: /home/elasticsearch/data
path.logs: /home/elasticsearch/logs
# 允许外网访问
network.host: 0.0.0.0
# 绑定端口设置
http.port: 9301
1.3.启动服务
- (1) 添加新用户
elasticsearch
:elasticsearch不允许root启动
# 1.添加用户
>> useradd elasticsearch
# 2.给一下权限
## 2.1 数据及日志保存路径权限
>> chown -R elasticsearch:elasticsearch /home/elasticsearch
## 2.2 软件本身所在路径权限
>> chown -R elasticsearch:elasticsearch /home/software/elasticsearch
- (2) 启动
elasticsearch
服务:
>> su - elasticsearch -c "/home/software/elasticsearch/bin/elasticsearch -E xpack.security.enabled=false -E discovery.type=single-node -E xpack.license.self_generated.type=trial -d"
CompileCommand: dontinline java/lang/invoke/MethodHandle.setAsTypeCache bool dontinline = true
CompileCommand: dontinline java/lang/invoke/MethodHandle.asTypeUncached bool dontinline = true
[2025-04-09T17:08:17,167][INFO ][o.e.n.NativeAccess ] [es-node0] Using native vector library; to disable start with -Dorg.elasticsearch.nativeaccess.enableVectorLibrary=false
[2025-04-09T17:08:17,201][INFO ][o.e.n.NativeAccess ] [es-node0] Using [jdk] native provider and native methods for [Linux]
...
[2025-04-09T17:08:27,784][INFO ][o.e.n.Node ] [es-node0] started {es-node0}{Mf-3LlKHTtu0DektDb3UsA}{78CspDtpSLmZSdnsbvKxFA}{es-node0}{192.168.65.3}{192.168.65.3:9300}{cdfhilmrstw}{8.16.1}{7000099-8518000}{ml.config_version=12.0.0, xpack.installed=true, transform.config_version=10.0.0, ml.machine_memory=33661394944, ml.allocated_processors=8, ml.allocated_processors_double=8.0, ml.max_jvm_size=16835936256}
1.4 docker安装
这个挺简单,elasticsearch提供了一键式安装脚本,具体安装版本,可以去https://github.com/elastic/start-local/releases查看:
# 1.直接安装最新版本
>> curl -fsSL https://github.com/elastic/start-local/releases/download/0.8.0/start-local.sh | sh
# 2.安装指定版本
>> curl -fsSL https://github.com/elastic/start-local/releases/download/0.8.0/start-local.sh -o start-local.sh
>> chmod +x start-local.sh
>> ./start-local.sh -v 8.18.1
这里安装的es会跟你默认生成账号密码:
🎉 Congrats, Elasticsearch and Kibana are installed and running in Docker!
🌐 Open your browser at http://localhost:5601
Username: elastic
Password: *******
🔌 Elasticsearch API endpoint: http://localhost:9200
🔑 API key: *************************************************
Learn more at https://github.com/elastic/start-local
2.MemoryScope简易环境配置
2.1 下载源码
>> git clone git@github.com:modelscope/MemoryScope.git
2.2 依赖包安装
>> cd MemoryScope
>> pip install -r requirements.txt
2.3 修改配置
- (1) 直接安装的情况,需要做如下修改:
memoryscope/core/config/demo_config_zh.yaml
,修改如下内容中 es_url
配置:
memory_store:
class: core.storage.llama_index_es_memory_store
embedding_model: embedding_model
index_name: memory_index
es_url: http://localhost:9301
retrieve_mode: dense
- (2) docker安装的情况,需要做如下修改:
memoryscope/core/config/demo_config_zh.yaml
,增加 es_user
和 es_password字段:
memory_store:
class: core.storage.llama_index_es_memory_store
embedding_model: embedding_model
index_name: memory_index
es_url: http://localhost:9200
es_user: elastic
es_password: <步骤一生成的密码>
retrieve_mode: dense
memoryscope/core/storage/llama_index_es_memory_store.py
文件的 __init__
函数改为如下内容:
def __init__(self,
embedding_model: BaseModel,
index_name: str,
es_url: str,
es_user: str,
es_password: str,
retrieve_mode: str = "dense",
hybrid_alpha: float = None,
**kwargs):
self.emb_dims = None
self.index_name = index_name
self.embedding_model: BaseModel = embedding_model
retrieval_strategy = ESCombinedRetrieveStrategy(retrieve_mode=retrieve_mode, hybrid_alpha=hybrid_alpha)
self.es_store = SyncElasticsearchStore(index_name=index_name,
es_url=es_url,
es_user=es_user,
es_password=es_password,
retrieval_strategy=retrieval_strategy,
**kwargs)
2.4.测试效果
>> python quick-start-demo.py --config_path=memoryscope/core/config/demo_config_zh.yaml
Registering loggers at: /home/workspace/MemoryScope/log/20250409_172859. System logs can be found in this directory.
/home/software/miniconda3/envs/agent/lib/python3.10/site-packages/llama_index/core/bridge/langchain.py:23: LangChainDeprecationWarning: Importing ChatMessageHistory from langchain.memory is deprecated. Please replace deprecated imports:
>> from langchain.memory import ChatMessageHistory
with new imports of:
>> from langchain_community.chat_message_histories import ChatMessageHistory
You can use the langchain cli to **automatically** upgrade many imports. Please see documentation here <https://python.langchain.com/docs/versions/v0_2/>
from langchain.memory import (
__ __ ____
| \/ | ___ _ __ ___ ___ _ __ _ _/ ___| ___ ___ _ __ ___
| |\/| |/ _ \ '_ ` _ \ / _ \| '__| | | \___ \ / __/ _ \| '_ \ / _ \
| | | | __/ | | | | | (_) | | | |_| |___) | (_| (_) | |_) | __/
|_| |_|\___|_| |_| |_|\___/|_| \__, |____/ \___\___/| .__/ \___|
|___/ |_|
> 用户: 我爱吃肉
> AI: 好的,了解您喜欢吃肉。有什么肉类菜品推荐或者想了解的烹饪方法吗?
> 用户: 我喜欢喝茶
> AI: 了解了,您喜欢喝茶。有没有特别喜欢的茶类或者想了解的茶叶知识呢?
> 用户: 我喜欢熬夜
> AI: 了解了,您有熬夜的习惯。不过长期熬夜对健康不利,建议适当调整作息时间,保证充足的睡眠。如果需要提神,可以适量喝茶。
> 用户: 人物画像
> AI: 根据您提供的信息,您的画像如下:
- 工作地点:厦门
- 饮食偏好:喜欢吃肉、喜欢喝茶
- 生活习惯:有熬夜的习惯
如果您需要更详细的画像或有其他问题,请告诉我。
3.查看记忆
# 1.用上面相同的命令再起一个窗口
>> python quick-start-demo.py --config_path=memoryscope/core/config/demo_config_zh.yaml
# 2.查看帮助
>> /help
/exit:
Exit the CLI.
/clear:
Clear the command history.
/help:
Display available CLI commands and their descriptions.
/stream:
Toggle between getting streamed responses from the model.
/read_message:
read short memory
/retrieve_memory:
retrieve long-term memory
/list_memory:
read all long-term memory of the user, use `refresh_time=5` to refresh screen every 5 seconds.
/delete_memory:
delete a single long-term memory
/delete_all:
delete all long-term memory
/add_memory:
add a single observation
/consolidate_memory:
summary user's observation memory, run backend.
/reflect_and_reconsolidate:
summary user's insight memory, run backend.
# 3.查看记忆
>> /list_memory refresh_time=5
__ __ ____
| \/ | ___ _ __ ___ ___ _ __ _ _/ ___| ___ ___ _ __ ___
| |\/| |/ _ \ '_ ` _ \ / _ \| '__| | | \___ \ / __/ _ \| '_ \ / _ \
| | | | __/ | | | | | (_) | | | |_| |___) | (_| (_) | |_) | __/
|_| |_|\___|_| |_| |_|\___/|_| \__, |____/ \___\___/| .__/ \___|
|___/ |_|
========== AI关于用户的长期记忆 ==========
----- 观察记忆 -----
20250409 17:55:10] 1. 用户不喜欢吃肉。 [status(0,0)
20250409 17:29:32] 2. 用户有熬夜的习惯。 [status(0,0)
20250409 17:29:23] 3. 用户喜欢喝茶。 [status(0,0)
20250409 17:15:02] 4. 用户在厦门工作。 [status(0,0)
----- 洞察记忆 -----
----- 过期记忆 -----
20250409 17:29:15] 1. 用户喜欢吃肉。
4.python代码测试
from memoryscope import MemoryScope
ms = MemoryScope(config_path='./config/demo_config_zh.yaml')
if __name__ == '__main__':
memory_chat = ms.default_memory_chat
memory_chat.run()
运行效果:
>> python main.py
Registering loggers at: /home/workspace/memory/log/20250410_101342. System logs can be found in this
directory.
/home/software/miniconda3/envs/agent/lib/python3.10/site-packages/llama_index/core/bridge/langchain.py:23: LangChainDeprecationWarning: Importing ChatMessageHistory from langchain.memory is deprecated. Please replace deprecated imports:
>> from langchain.memory import ChatMessageHistory
with new imports of:
>> from langchain_community.chat_message_histories import ChatMessageHistory
You can use the langchain cli to **automatically** upgrade many imports. Please see documentation here <https://python.langchain.com/docs/versions/v0_2/>
from langchain.memory import (
__ __ ____
| \/ | ___ _ __ ___ ___ _ __ _ _/ ___| ___ ___ _ __ ___
| |\/| |/ _ \ '_ ` _ \ / _ \| '__| | | \___ \ / __/ _ \| '_ \ / _ \
| | | | __/ | | | | | (_) | | | |_| |___) | (_| (_) | |_) | __/
|_| |_|\___|_| |_| |_|\___/|_| \__, |____/ \___\___/| .__/ \___|
|___/ |_|
> 用户: 你好
> AI: 你好!有什么可以帮助你的?
> 用户: 我的人物画像
> AI: 您在厦门工作,喜欢喝茶,不喜欢吃肉,并且有熬夜的习惯。
> 用户: /list_memory
> AI: ========== AI关于用户的长期记忆 ==========
----- 观察记忆 -----
20250409 17:55:10] 1. 用户不喜欢吃肉。 [status(0,0)
20250409 17:29:32] 2. 用户有熬夜的习惯。 [status(0,0)
20250409 17:29:23] 3. 用户喜欢喝茶。 [status(0,0)
20250409 17:15:02] 4. 用户在厦门工作。 [status(0,0)
----- 洞察记忆 -----
----- 过期记忆 -----
20250409 17:29:15] 1. 用户喜欢吃肉。
> 用户:
5.参考资料
- [1] elastic
- [2] MemoryScope