Merge branch tiancheng_0802 into master

This commit is contained in:
jinli.yl 2024-08-06 11:04:05 +08:00
commit fb4e88e5a1
5 changed files with 1224 additions and 748 deletions

View file

@ -13,7 +13,7 @@ Equip your LLM chatbot with a powerful and flexible long term memory system.
- **[2024-07-29]** We release MemoryScope v0.1.0.2 now, which is also available in [PyPI](https://pypi.org/simple)!
----
## What is MemoryScope
## What is MemoryScope
MemoryScope is a powerful and flexible long term memory system for LLM chatbots. It consists
of a memory database and three customizable system operations, which can be flexibly combined to provide
@ -52,10 +52,15 @@ accurate relevant information when the query involves reference to time.
### Example Usages
- [Simple Usages (Quick Start)](./examples/api/simple_usages_en.ipynb)
- [CLI with a MemoryScope Chatbot](./examples/cli/dash_cli_cn1.sh)
- [Advanced Customization](./examples/api/advanced_customization_en.ipynb)
# 🚀 Installation
## (1) Docker-Compose (Recommended)
## 🚀 Installation
### (1) Docker-Compose (Recommended)
1. Clone the project and edit the config.
```
@ -72,7 +77,7 @@ accurate relevant information when the query involves reference to time.
3. Run `docker-compose up` to build and launch the memory-scope cli interface.
## (2) Docker
### (2) Docker
1. Clone the project and edit the config.
@ -100,7 +105,7 @@ accurate relevant information when the query involves reference to time.
sudo docker run -it --rm --net=host memoryscope
```
# 💡 Contribute
## 💡 Contribute
Contributions are always encouraged!
@ -114,7 +119,7 @@ pre-commit install
# 📖 Citation
## 📖 Citation
Reference to cite if you use MemoryScope in a paper:

View file

@ -13,7 +13,7 @@
- **[2024-07-26]** 我们现在发布了 MemoryScope v0.1.0.2,该版本也可以在 [PyPI](https://pypi.org/simple) 上获取!
----
## MemoryScope 是什么
## MemoryScope 是什么
MemoryScope 是一个为LLM聊天机器人服务的强大且灵活的长期记忆系统。它由一个记忆数据库和三个可定制的系统操作组成这些操作可以灵活组合
为您的LLM聊天机器人提供强大的长期记忆服务。
@ -24,7 +24,7 @@ MemoryScope 是一个为LLM聊天机器人服务的强大且灵活的长期记
🛠️ 系统操作:
- 记忆检索:当用户输入对话,此操作返回语义相关的记忆片段。如果输入对话包含对时间的指涉,则同时返回相应时间中的记忆片段。
- 记忆巩固:此操作接收一批用户的输入对话,并从对话中提取重要的用户信息,将其作为 *observation* 形式的记忆片段存储在记忆数据库中。
- 反思再巩固:每隔一段时间,此操作对新记录的 *observations* 进行反思,以形成和更新 *insight* 形式的记忆片段。然后执行记忆再巩固,
- 反思再巩固:每隔一段时间,此操作对新记录的 *observations* 进行反思,以形成和更新 *insight* 形式的记忆片段。然后执行记忆再巩固,
以确保记忆片段之间的矛盾和重复得到妥善处理。
### 主要特点
@ -42,9 +42,13 @@ MemoryScope 是一个为LLM聊天机器人服务的强大且灵活的长期记
- T系统在执行记忆检索和记忆巩固时具备时间敏感性因此在输入对话包含对时间的指涉时可以检索到准确的相关信息。
### 用法示例
- [简易用法(快速开始)](./examples/api/simple_usages_cn.ipynb)
- [在命令行与MemoryScope聊天机器人交互](./examples/cli/dash_cli_cn1.sh)
- [进阶自定义用法](./examples/api/advanced_customization_cn.ipynb)
## 🚀 安装
# 💡 代码贡献
## 💡 代码贡献
欢迎社区的代码贡献。
@ -57,7 +61,7 @@ pre-commit install
# 📖 引用
## 📖 引用
如果您在论文中有使用该项目,请添加以下引用:

View file

@ -1,738 +0,0 @@
{
"cells": [
{
"cell_type": "markdown",
"source": [
"# Example usages of **chat** and **service** interfaces\n",
"This notebook shows simple usages of MemoryScope's **chat** and **service** interfaces, along with its main features.\n",
"\n",
"To run this notebook, follow the **Installation** guidelines in Readme, and start the Docker image.\n"
],
"metadata": {
"collapsed": false
}
},
{
"cell_type": "markdown",
"source": [
"## Initiate a MemoryScope instance\n",
"First, we need to specify a configuration and initiate a MemoryScope instance."
],
"metadata": {
"collapsed": false
}
},
{
"cell_type": "code",
"execution_count": 1,
"outputs": [
{
"name": "stderr",
"output_type": "stream",
"text": [
"2024-08-02 18:28:24 INFO MainThread logger:64] logger=memoryscope_20240802_182824 is inited.\n",
"2024-08-02 18:28:24 INFO MainThread config_manager:39] init by arguments mode: {'language': 'cn', 'thread_pool_max_workers': 5, 'logger_name': 'memoryscope', 'logger_name_time_suffix': '%Y%m%d_%H%M%S', 'logger_to_screen': True, 'memory_chat_class': 'api_memory_chat', 'chat_stream': None, 'human_name': '用户', 'assistant_name': 'AI', 'consolidate_memory_interval_time': 1, 'reflect_and_reconsolidate_interval_time': 15, 'worker_params': {'get_reflection_subject': {'reflect_num_questions': 3}}, 'generation_backend': 'dashscope_generation', 'generation_model': 'qwen2-72b-instruct', 'generation_params': {}, 'embedding_backend': 'dashscope_embedding', 'embedding_model': 'text-embedding-v2', 'embedding_params': {}, 'rank_backend': 'dashscope_rank', 'rank_model': 'gte-rerank', 'rank_params': {}, 'es_index_name': 'memory_index', 'es_url': 'http://localhost:9200', 'retrieve_mode': 'dense', 'enable_ranker': False, 'enable_today_contra_repeat': True, 'enable_long_contra_repeat': False, 'output_memory_max_count': 20}\n",
"2024-08-02 18:28:24 INFO MainThread config_manager:50] \n",
"global:\n",
" enable_long_contra_repeat: false\n",
" enable_ranker: false\n",
" enable_today_contra_repeat: true\n",
" language: cn\n",
" logger_name: memoryscope\n",
" logger_name_time_suffix: '%Y%m%d_%H%M%S'\n",
" logger_to_screen: true\n",
" output_memory_max_count: 20\n",
" thread_pool_max_workers: 5\n",
"memory_chat:\n",
" cli_memory_chat:\n",
" class: core.chat.api_memory_chat\n",
" generation_model: generation_model\n",
" memory_service: memoryscope_service\n",
" stream: false\n",
"memory_service:\n",
" memoryscope_service:\n",
" assistant_name: AI\n",
" class: core.service.memory_scope_service\n",
" human_name: 用户\n",
" memory_operations:\n",
" add_memory:\n",
" class: core.operation.frontend_operation\n",
" description: add a single observation\n",
" workflow: add_memory\n",
" consolidate_memory:\n",
" class: core.operation.consolidate_memory_op\n",
" description: summary user's observation memory, run backend.\n",
" interval_time: 1\n",
" workflow: info_filter,[get_observation|get_observation_with_time|load_today_memory],contra_repeat,store_memory\n",
" delete_all:\n",
" class: core.operation.frontend_operation\n",
" description: delete all long-term memory\n",
" workflow: set_query,retrieve_all_memory,delete_all\n",
" delete_memory:\n",
" class: core.operation.frontend_operation\n",
" description: delete a single long-term memory\n",
" workflow: set_query,retrieve_all_memory,delete_memory\n",
" list_memory:\n",
" class: core.operation.frontend_operation\n",
" description: read all long-term memory of the user, use `refresh_time=5` to\n",
" refresh screen.\n",
" workflow: set_query,retrieve_top_memory,print_memory\n",
" read_message:\n",
" class: core.operation.frontend_operation\n",
" description: read short memory\n",
" workflow: read_message\n",
" reflect_and_reconsolidate:\n",
" class: core.operation.backend_operation\n",
" description: summary user's insight memory, run backend.\n",
" interval_time: 15\n",
" workflow: load_obs_and_insight,get_reflection_subject,update_insight,long_contra_repeat,store_memory\n",
" retrieve_memory:\n",
" class: core.operation.frontend_operation\n",
" description: retrieve long-term memory\n",
" workflow: set_query,[extract_time|retrieve_obs_ins,semantic_rank],fuse_rerank\n",
"memory_store:\n",
" class: core.storage.llama_index_es_memory_store\n",
" embedding_model: embedding_model\n",
" es_url: http://localhost:9200\n",
" index_name: memory_index\n",
" retrieve_mode: dense\n",
"model:\n",
" embedding_model:\n",
" class: core.models.llama_index_embedding_model\n",
" model_name: text-embedding-v2\n",
" module_name: dashscope_embedding\n",
" generation_model:\n",
" class: core.models.llama_index_generation_model\n",
" max_tokens: 2000\n",
" model_name: qwen2-72b-instruct\n",
" module_name: dashscope_generation\n",
" rank_model:\n",
" class: core.models.llama_index_rank_model\n",
" model_name: gte-rerank\n",
" module_name: dashscope_rank\n",
" top_n: 500\n",
"monitor:\n",
" class: core.storage.dummy_monitor\n",
"worker:\n",
" add_memory:\n",
" class: core.worker.backend.update_memory_worker\n",
" method: from_query\n",
" contra_repeat:\n",
" class: core.worker.backend.contra_repeat_worker\n",
" generation_model: generation_model\n",
" delete_all:\n",
" class: core.worker.backend.update_memory_worker\n",
" method: delete_all\n",
" delete_memory:\n",
" class: core.worker.backend.update_memory_worker\n",
" method: delete_memory\n",
" dummy:\n",
" class: core.worker.dummy_worker\n",
" embedding_model: embedding_model\n",
" generation_model: generation_model\n",
" rank_model: rank_model\n",
" extract_time:\n",
" class: core.worker.frontend.extract_time_worker\n",
" generation_model: generation_model\n",
" fuse_rerank:\n",
" class: core.worker.frontend.fuse_rerank_worker\n",
" fuse_ratio_dict:\n",
" conversation: 0.5\n",
" insight: 2.0\n",
" obs_customized: 1.2\n",
" observation: 1\n",
" fuse_score_threshold: 0.01\n",
" fuse_time_ratio: 2.0\n",
" get_observation:\n",
" class: core.worker.backend.get_observation_worker\n",
" generation_model: generation_model\n",
" get_observation_with_time:\n",
" class: core.worker.backend.get_observation_with_time_worker\n",
" generation_model: generation_model\n",
" get_reflection_subject:\n",
" class: core.worker.backend.get_reflection_subject_worker\n",
" generation_model: generation_model\n",
" reflect_num_questions: 3\n",
" reflect_obs_cnt_threshold: 5\n",
" info_filter:\n",
" class: core.worker.backend.info_filter_worker\n",
" generation_model: generation_model\n",
" load_obs_and_insight:\n",
" class: core.worker.backend.load_memory_worker\n",
" retrieve_insight_top_k: 100\n",
" retrieve_not_reflected_top_k: 100\n",
" retrieve_not_updated_top_k: 100\n",
" load_today_memory:\n",
" class: core.worker.backend.load_memory_worker\n",
" retrieve_today_top_k: 100\n",
" long_contra_repeat:\n",
" class: core.worker.backend.long_contra_repeat_worker\n",
" generation_model: generation_model\n",
" long_contra_repeat_threshold: 0.5\n",
" print_memory:\n",
" class: core.worker.frontend.print_memory_worker\n",
" read_message:\n",
" class: core.worker.frontend.read_message_worker\n",
" retrieve_all_memory:\n",
" class: core.worker.frontend.retrieve_memory_worker\n",
" retrieve_expired_top_k: 1000\n",
" retrieve_ins_top_k: 1000\n",
" retrieve_obs_top_k: 1000\n",
" retrieve_obs_ins:\n",
" class: core.worker.frontend.retrieve_memory_worker\n",
" retrieve_ins_top_k: 100\n",
" retrieve_obs_top_k: 100\n",
" retrieve_top_memory:\n",
" class: core.worker.frontend.retrieve_memory_worker\n",
" retrieve_expired_top_k: 100\n",
" retrieve_ins_top_k: 100\n",
" retrieve_obs_top_k: 100\n",
" semantic_rank:\n",
" class: core.worker.frontend.semantic_rank_worker\n",
" rank_model: rank_model\n",
" set_query:\n",
" class: core.worker.frontend.set_query_worker\n",
" store_memory:\n",
" class: core.worker.backend.update_memory_worker\n",
" memory_key: all\n",
" method: from_memory_key\n",
" update_insight:\n",
" class: core.worker.backend.update_insight_worker\n",
" embedding_model: embedding_model\n",
" generation_model: generation_model\n",
" rank_model: rank_model\n",
" update_insight_threshold: 0.01\n",
"\n",
"2024-08-02 18:28:24 WARNING MainThread memoryscope:32] If a semantic ranking model is not available, MemoryScope will use cosine similarity scoring as a substitute. However, the ranking effectiveness will be somewhat compromised.\n"
]
}
],
"source": [
"import sys\n",
"sys.path.append(\".\")\n",
"from memoryscope import MemoryScope, Arguments\n",
"arguments = Arguments(\n",
" language=\"cn\",\n",
" human_name=\"用户\",\n",
" assistant_name=\"AI\",\n",
" logger_to_screen=True,\n",
" memory_chat_class=\"api_memory_chat\",\n",
" generation_backend=\"dashscope_generation\",\n",
" generation_model=\"qwen2-72b-instruct\",\n",
" embedding_backend=\"dashscope_embedding\",\n",
" embedding_model=\"text-embedding-v2\",\n",
" rank_backend=\"dashscope_rank\",\n",
" rank_model=\"gte-rerank\",\n",
" enable_ranker=False,\n",
" worker_params={\"get_reflection_subject\": {\"reflect_num_questions\": 3}}\n",
")\n",
"\n",
"ms = MemoryScope(arguments=arguments)\n"
],
"metadata": {
"collapsed": false,
"ExecuteTime": {
"end_time": "2024-08-02T10:28:26.547739Z",
"start_time": "2024-08-02T10:28:24.509118Z"
}
}
},
{
"cell_type": "markdown",
"source": [
"## Chat without memory\n",
"ms comes with a default **chat** interface, so it's very easy to start chatting, just as what you'll do with any LLM chatbot."
],
"metadata": {
"collapsed": false
}
},
{
"cell_type": "code",
"execution_count": 2,
"outputs": [
{
"name": "stderr",
"output_type": "stream",
"text": [
"2024-08-02 18:28:26 INFO MainThread base_workflow:95] ----- workflow.read_message.print.begin -----\n",
"2024-08-02 18:28:26 INFO MainThread base_workflow:101] stage0: read_message\n",
"2024-08-02 18:28:26 INFO MainThread base_workflow:112] ----- workflow.read_message.print.end -----\n",
"2024-08-02 18:28:26 INFO MainThread memory_scope_service:83] service=MemoryScopeService init operation=read_message\n",
"2024-08-02 18:28:26 INFO MainThread base_workflow:95] ----- workflow.retrieve_memory.print.begin -----\n",
"2024-08-02 18:28:26 INFO MainThread base_workflow:101] stage0: set_query\n",
"2024-08-02 18:28:26 INFO MainThread base_workflow:106] stage1: extract_time | retrieve_obs_ins\n",
"2024-08-02 18:28:26 INFO MainThread base_workflow:106] stage2: - | semantic_rank\n",
"2024-08-02 18:28:26 INFO MainThread base_workflow:101] stage3: fuse_rerank\n",
"2024-08-02 18:28:26 INFO MainThread base_workflow:112] ----- workflow.retrieve_memory.print.end -----\n",
"2024-08-02 18:28:26 INFO MainThread memory_scope_service:83] service=MemoryScopeService init operation=retrieve_memory\n",
"2024-08-02 18:28:26 INFO MainThread base_workflow:95] ----- workflow.list_memory.print.begin -----\n",
"2024-08-02 18:28:26 INFO MainThread base_workflow:101] stage0: set_query\n",
"2024-08-02 18:28:26 INFO MainThread base_workflow:101] stage1: retrieve_top_memory\n",
"2024-08-02 18:28:26 INFO MainThread base_workflow:101] stage2: print_memory\n",
"2024-08-02 18:28:26 INFO MainThread base_workflow:112] ----- workflow.list_memory.print.end -----\n",
"2024-08-02 18:28:26 INFO MainThread memory_scope_service:83] service=MemoryScopeService init operation=list_memory\n",
"2024-08-02 18:28:26 INFO MainThread base_workflow:95] ----- workflow.delete_memory.print.begin -----\n",
"2024-08-02 18:28:26 INFO MainThread base_workflow:101] stage0: set_query\n",
"2024-08-02 18:28:26 INFO MainThread base_workflow:101] stage1: retrieve_all_memory\n",
"2024-08-02 18:28:26 INFO MainThread base_workflow:101] stage2: delete_memory\n",
"2024-08-02 18:28:26 INFO MainThread base_workflow:112] ----- workflow.delete_memory.print.end -----\n",
"2024-08-02 18:28:26 INFO MainThread memory_scope_service:83] service=MemoryScopeService init operation=delete_memory\n",
"2024-08-02 18:28:26 INFO MainThread base_workflow:95] ----- workflow.delete_all.print.begin -----\n",
"2024-08-02 18:28:26 INFO MainThread base_workflow:101] stage0: set_query\n",
"2024-08-02 18:28:26 INFO MainThread base_workflow:101] stage1: retrieve_all_memory\n",
"2024-08-02 18:28:26 INFO MainThread base_workflow:101] stage2: delete_all\n",
"2024-08-02 18:28:26 INFO MainThread base_workflow:112] ----- workflow.delete_all.print.end -----\n",
"2024-08-02 18:28:26 INFO MainThread memory_scope_service:83] service=MemoryScopeService init operation=delete_all\n",
"2024-08-02 18:28:26 INFO MainThread base_workflow:95] ----- workflow.add_memory.print.begin -----\n",
"2024-08-02 18:28:26 INFO MainThread base_workflow:101] stage0: add_memory\n",
"2024-08-02 18:28:26 INFO MainThread base_workflow:112] ----- workflow.add_memory.print.end -----\n",
"2024-08-02 18:28:26 INFO MainThread memory_scope_service:83] service=MemoryScopeService init operation=add_memory\n",
"2024-08-02 18:28:26 INFO MainThread base_workflow:95] ----- workflow.consolidate_memory.print.begin -----\n",
"2024-08-02 18:28:26 INFO MainThread base_workflow:101] stage0: info_filter\n",
"2024-08-02 18:28:26 INFO MainThread base_workflow:106] stage1: get_observation | get_observation_with_time | load_today_memory\n",
"2024-08-02 18:28:26 INFO MainThread base_workflow:101] stage2: contra_repeat\n",
"2024-08-02 18:28:26 INFO MainThread base_workflow:101] stage3: store_memory\n",
"2024-08-02 18:28:26 INFO MainThread base_workflow:112] ----- workflow.consolidate_memory.print.end -----\n",
"2024-08-02 18:28:26 INFO MainThread memory_scope_service:83] service=MemoryScopeService init operation=consolidate_memory\n",
"2024-08-02 18:28:26 INFO MainThread base_workflow:95] ----- workflow.reflect_and_reconsolidate.print.begin -----\n",
"2024-08-02 18:28:26 INFO MainThread base_workflow:101] stage0: load_obs_and_insight\n",
"2024-08-02 18:28:26 INFO MainThread base_workflow:101] stage1: get_reflection_subject\n",
"2024-08-02 18:28:26 INFO MainThread base_workflow:101] stage2: update_insight\n",
"2024-08-02 18:28:26 INFO MainThread base_workflow:101] stage3: long_contra_repeat\n",
"2024-08-02 18:28:26 INFO MainThread base_workflow:101] stage4: store_memory\n",
"2024-08-02 18:28:26 INFO MainThread base_workflow:112] ----- workflow.reflect_and_reconsolidate.print.end -----\n",
"2024-08-02 18:28:26 INFO MainThread memory_scope_service:83] service=MemoryScopeService init operation=reflect_and_reconsolidate\n",
"2024-08-02 18:28:26 INFO MainThread timer:75] ----- workflow.delete_all.begin -----\n",
"2024-08-02 18:28:26 INFO MainThread timer:75] ----- worker.set_query.begin -----\n",
"2024-08-02 18:28:26 INFO MainThread base_worker:145] ----- worker.set_query.end ----- cost=0.3171ms\n",
"2024-08-02 18:28:26 INFO MainThread timer:75] ----- worker.retrieve_all_memory.begin -----\n",
"2024-08-02 18:28:26 INFO MainThread retrieve_memory_worker:123] retrieve memory with query=.\n",
"2024-08-02 18:28:26 INFO ThreadPoolExecutor-0_2 timer:127] retrieve_expired_memory cost=382.0448ms query=\n",
"2024-08-02 18:28:26 INFO ThreadPoolExecutor-0_0 timer:127] retrieve_from_observation cost=383.0619ms query=\n",
"2024-08-02 18:28:26 INFO ThreadPoolExecutor-0_1 timer:127] retrieve_from_insight cost=383.0431ms query=\n",
"2024-08-02 18:28:26 INFO MainThread retrieve_memory_worker:132] memory_node_list.size=0\n",
"2024-08-02 18:28:26 INFO MainThread base_worker:145] ----- worker.retrieve_all_memory.end ----- cost=386.7860ms\n",
"2024-08-02 18:28:26 INFO MainThread timer:75] ----- worker.delete_all.begin -----\n",
"2024-08-02 18:28:26 INFO MainThread update_memory_worker:62] delete_all.size=0\n",
"2024-08-02 18:28:26 INFO MainThread base_worker:145] ----- worker.delete_all.end ----- cost=1.1411ms\n",
"2024-08-02 18:28:26 INFO MainThread base_workflow:167] ----- workflow.delete_all.end ----- cost=389.7450ms\n",
"2024-08-02 18:28:26 INFO MainThread timer:75] ----- workflow.retrieve_memory.begin -----\n",
"2024-08-02 18:28:26 INFO MainThread timer:75] ----- worker.set_query.begin -----\n",
"2024-08-02 18:28:26 INFO MainThread base_worker:145] ----- worker.set_query.end ----- cost=0.4051ms\n",
"2024-08-02 18:28:26 INFO ThreadPoolExecutor-0_2 timer:75] ----- worker.extract_time.begin -----\n",
"2024-08-02 18:28:26 INFO ThreadPoolExecutor-0_0 timer:75] ----- worker.retrieve_obs_ins.begin -----\n",
"2024-08-02 18:28:26 INFO ThreadPoolExecutor-0_2 extract_time_worker:37] contain_datetime=False\n",
"2024-08-02 18:28:26 INFO ThreadPoolExecutor-0_0 retrieve_memory_worker:123] retrieve memory with query=我的爱好是弹琴。.\n",
"2024-08-02 18:28:26 INFO ThreadPoolExecutor-0_2 base_worker:145] ----- worker.extract_time.end ----- cost=1.2507ms\n",
"2024-08-02 18:28:26 INFO ThreadPoolExecutor-0_4 timer:127] retrieve_expired_memory cost=0.0010ms query=我的爱好是弹琴。\n",
"2024-08-02 18:28:27 INFO ThreadPoolExecutor-0_1 timer:127] retrieve_from_insight cost=263.5777ms query=我的爱好是弹琴。\n",
"2024-08-02 18:28:27 INFO ThreadPoolExecutor-0_3 timer:127] retrieve_from_observation cost=317.2297ms query=我的爱好是弹琴。\n",
"2024-08-02 18:28:27 INFO ThreadPoolExecutor-0_0 retrieve_memory_worker:132] memory_node_list.size=0\n",
"2024-08-02 18:28:27 INFO ThreadPoolExecutor-0_0 base_worker:145] ----- worker.retrieve_obs_ins.end ----- cost=320.3712ms\n",
"2024-08-02 18:28:27 INFO ThreadPoolExecutor-0_0 timer:75] ----- worker.semantic_rank.begin -----\n",
"2024-08-02 18:28:27 WARNING ThreadPoolExecutor-0_0 semantic_rank_worker:38] Retrieve memory nodes is empty!\n",
"2024-08-02 18:28:27 INFO ThreadPoolExecutor-0_0 base_worker:145] ----- worker.semantic_rank.end ----- cost=0.8211ms\n",
"2024-08-02 18:28:27 INFO MainThread timer:75] ----- worker.fuse_rerank.begin -----\n",
"2024-08-02 18:28:27 WARNING MainThread fuse_rerank_worker:69] Ranked memory nodes list is empty.\n",
"2024-08-02 18:28:27 INFO MainThread base_worker:145] ----- worker.fuse_rerank.end ----- cost=1.2810ms\n",
"2024-08-02 18:28:27 INFO MainThread base_workflow:167] ----- workflow.retrieve_memory.end ----- cost=325.6671ms\n",
"2024-08-02 18:28:27 INFO MainThread timer:75] ----- workflow.read_message.begin -----\n",
"2024-08-02 18:28:27 INFO MainThread timer:75] ----- worker.read_message.begin -----\n",
"2024-08-02 18:28:27 INFO MainThread base_worker:145] ----- worker.read_message.end ----- cost=0.2921ms\n",
"2024-08-02 18:28:27 INFO MainThread base_workflow:167] ----- workflow.read_message.end ----- cost=1.1899ms\n",
"2024-08-02 18:28:27 INFO MainThread api_memory_chat:186] chat_messages=[Message(role='system', role_name='', content='你是一个名为MemoryScope的智能助理请用中文简洁地回答问题。当前时间是2024-08-02 18:28:27 周五。', time_created=1722594507, memorized=False, meta_data={}), Message(role='user', role_name='用户', content='我的爱好是弹琴。', time_created=1722594506, memorized=False, meta_data={})]\n"
]
},
{
"name": "stdout",
"output_type": "stream",
"text": [
"回答1\n",
"那真是一个优雅的爱好!弹琴不仅能够陶冶情操,还能提升音乐素养和手指灵活性。你最喜欢弹奏哪种类型的曲子呢?\n"
]
}
],
"source": [
"memory_chat = ms.default_memory_chat\n",
"memory_chat.run_service_operation(\"delete_all\")\n",
"response = memory_chat.chat_with_memory(query=\"我的爱好是弹琴。\")\n",
"print(\"回答1\\n\" + response.message.content)"
],
"metadata": {
"collapsed": false,
"ExecuteTime": {
"end_time": "2024-08-02T10:28:29.660299Z",
"start_time": "2024-08-02T10:28:26.549127Z"
}
}
},
{
"cell_type": "markdown",
"source": [
"You can choose to chat with or without multi-round conversation contexts. However, since *Memory Consolidation* has not been called, there's no memory pieces in the system yet."
],
"metadata": {
"collapsed": false
}
},
{
"cell_type": "code",
"execution_count": 3,
"outputs": [
{
"name": "stderr",
"output_type": "stream",
"text": [
"2024-08-02 18:28:29 INFO MainThread timer:75] ----- workflow.retrieve_memory.begin -----\n",
"2024-08-02 18:28:29 INFO MainThread timer:75] ----- worker.set_query.begin -----\n",
"2024-08-02 18:28:29 INFO MainThread base_worker:145] ----- worker.set_query.end ----- cost=0.6740ms\n",
"2024-08-02 18:28:30 INFO MainThread timer:75] ----- worker.fuse_rerank.begin -----\n",
"2024-08-02 18:28:30 WARNING MainThread fuse_rerank_worker:69] Ranked memory nodes list is empty.\n",
"2024-08-02 18:28:30 INFO MainThread base_worker:145] ----- worker.fuse_rerank.end ----- cost=0.8800ms\n",
"2024-08-02 18:28:30 INFO MainThread base_workflow:167] ----- workflow.retrieve_memory.end ----- cost=384.5589ms\n",
"2024-08-02 18:28:30 INFO MainThread timer:75] ----- workflow.read_message.begin -----\n",
"2024-08-02 18:28:30 INFO MainThread timer:75] ----- worker.read_message.begin -----\n",
"2024-08-02 18:28:30 INFO MainThread base_worker:145] ----- worker.read_message.end ----- cost=0.2897ms\n",
"2024-08-02 18:28:30 INFO MainThread base_workflow:167] ----- workflow.read_message.end ----- cost=1.0922ms\n",
"2024-08-02 18:28:30 INFO MainThread api_memory_chat:186] chat_messages=[Message(role='system', role_name='', content='你是一个名为MemoryScope的智能助理请用中文简洁地回答问题。当前时间是2024-08-02 18:28:30 周五。', time_created=1722594510, memorized=False, meta_data={}), Message(role='user', role_name='用户', content='我的爱好是弹琴。', time_created=1722594506, memorized=False, meta_data={}), Message(role='assistant', role_name='AI', content='那真是一个优雅的爱好!弹琴不仅能够陶冶情操,还能提升音乐素养和手指灵活性。你最喜欢弹奏哪种类型的曲子呢?', time_created=1722594509, memorized=False, meta_data={}), Message(role='user', role_name='用户', content='你知道我有什么乐器爱好吗?', time_created=1722594509, memorized=False, meta_data={})]\n",
"2024-08-02 18:28:32 INFO MainThread timer:75] ----- workflow.retrieve_memory.begin -----\n",
"2024-08-02 18:28:32 INFO MainThread timer:75] ----- worker.set_query.begin -----\n",
"2024-08-02 18:28:32 INFO MainThread base_worker:145] ----- worker.set_query.end ----- cost=0.7801ms\n"
]
},
{
"name": "stdout",
"output_type": "stream",
"text": [
"回答2\n",
"您提到过的爱好是弹琴,所以我了解到您对弹奏乐器,特别是钢琴有兴趣。是否还有其他乐器爱好呢?\n"
]
},
{
"name": "stderr",
"output_type": "stream",
"text": [
"2024-08-02 18:28:32 INFO MainThread timer:75] ----- worker.fuse_rerank.begin -----\n",
"2024-08-02 18:28:32 WARNING MainThread fuse_rerank_worker:69] Ranked memory nodes list is empty.\n",
"2024-08-02 18:28:32 INFO MainThread base_worker:145] ----- worker.fuse_rerank.end ----- cost=0.8247ms\n",
"2024-08-02 18:28:32 INFO MainThread base_workflow:167] ----- workflow.retrieve_memory.end ----- cost=303.6480ms\n",
"2024-08-02 18:28:32 INFO MainThread api_memory_chat:186] chat_messages=[Message(role='system', role_name='', content='你是一个名为MemoryScope的智能助理请用中文简洁地回答问题。当前时间是2024-08-02 18:28:32 周五。', time_created=1722594512, memorized=False, meta_data={}), Message(role='user', role_name='用户', content='你知道我有什么乐器爱好吗?', time_created=1722594512, memorized=False, meta_data={})]\n"
]
},
{
"name": "stdout",
"output_type": "stream",
"text": [
"回答3\n",
"作为MemoryScope我无法直接获取或记忆您的个人信息包括您的乐器爱好除非您之前已告知我。如果您愿意分享我可以帮助您记录或提供与乐器爱好相关的信息。\n"
]
}
],
"source": [
"response = memory_chat.chat_with_memory(query=\"你知道我有什么乐器爱好吗?\")\n",
"print(\"回答2\\n\" + response.message.content)\n",
"response = memory_chat.chat_with_memory(query=\"你知道我有什么乐器爱好吗?\",\n",
" history_message_strategy=None)\n",
"print(\"回答3\\n\" + response.message.content)"
],
"metadata": {
"collapsed": false,
"ExecuteTime": {
"end_time": "2024-08-02T10:28:35.807252Z",
"start_time": "2024-08-02T10:28:29.659682Z"
}
}
},
{
"cell_type": "markdown",
"source": [
"## Memory Consolidation\n",
"Now, we do a bit more chatting and try out **Memory Consolidation**."
],
"metadata": {
"collapsed": false
}
},
{
"cell_type": "code",
"execution_count": null,
"outputs": [
{
"name": "stderr",
"output_type": "stream",
"text": [
"2024-08-02 18:28:35 INFO MainThread timer:75] ----- workflow.retrieve_memory.begin -----\n",
"2024-08-02 18:28:35 INFO MainThread timer:75] ----- worker.set_query.begin -----\n",
"2024-08-02 18:28:35 INFO MainThread base_worker:145] ----- worker.set_query.end ----- cost=0.4082ms\n",
"2024-08-02 18:28:36 INFO MainThread timer:75] ----- worker.fuse_rerank.begin -----\n",
"2024-08-02 18:28:36 WARNING MainThread fuse_rerank_worker:69] Ranked memory nodes list is empty.\n",
"2024-08-02 18:28:36 INFO MainThread base_worker:145] ----- worker.fuse_rerank.end ----- cost=1.5481ms\n",
"2024-08-02 18:28:36 INFO MainThread base_workflow:167] ----- workflow.retrieve_memory.end ----- cost=384.7449ms\n",
"2024-08-02 18:28:36 INFO MainThread timer:75] ----- workflow.read_message.begin -----\n",
"2024-08-02 18:28:36 INFO MainThread timer:75] ----- worker.read_message.begin -----\n",
"2024-08-02 18:28:36 INFO MainThread base_worker:145] ----- worker.read_message.end ----- cost=0.9732ms\n",
"2024-08-02 18:28:36 INFO MainThread base_workflow:167] ----- workflow.read_message.end ----- cost=1.9441ms\n",
"2024-08-02 18:28:36 INFO MainThread api_memory_chat:186] chat_messages=[Message(role='system', role_name='', content='你是一个名为MemoryScope的智能助理请用中文简洁地回答问题。当前时间是2024-08-02 18:28:36 周五。', time_created=1722594516, memorized=False, meta_data={}), Message(role='user', role_name='用户', content='我的爱好是弹琴。', time_created=1722594506, memorized=False, meta_data={}), Message(role='assistant', role_name='AI', content='那真是一个优雅的爱好!弹琴不仅能够陶冶情操,还能提升音乐素养和手指灵活性。你最喜欢弹奏哪种类型的曲子呢?', time_created=1722594509, memorized=False, meta_data={}), Message(role='user', role_name='用户', content='你知道我有什么乐器爱好吗?', time_created=1722594509, memorized=False, meta_data={}), Message(role='assistant', role_name='AI', content='您提到过的爱好是弹琴,所以我了解到您对弹奏乐器,特别是钢琴有兴趣。是否还有其他乐器爱好呢?', time_created=1722594512, memorized=False, meta_data={}), Message(role='user', role_name='用户', content='你知道我有什么乐器爱好吗?', time_created=1722594512, memorized=False, meta_data={}), Message(role='assistant', role_name='AI', content='作为MemoryScope我无法直接获取或记忆您的个人信息包括您的乐器爱好除非您之前已告知我。如果您愿意分享我可以帮助您记录或提供与乐器爱好相关的信息。', time_created=1722594515, memorized=False, meta_data={}), Message(role='user', role_name='用户', content='我在阿里巴巴干活', time_created=1722594515, memorized=False, meta_data={})]\n",
"2024-08-02 18:28:40 INFO MainThread timer:75] ----- workflow.retrieve_memory.begin -----\n",
"2024-08-02 18:28:40 INFO MainThread timer:75] ----- worker.set_query.begin -----\n",
"2024-08-02 18:28:40 INFO MainThread base_worker:145] ----- worker.set_query.end ----- cost=0.6080ms\n"
]
},
{
"name": "stdout",
"output_type": "stream",
"text": [
"回答4\n",
"太好了,阿里巴巴是一家知名的国际企业,涉及电子商务、零售、互联网和技术等多个领域。在阿里巴巴工作,您可能参与到了推动数字经济发展和创新的前沿工作中。希望您在那边的工作经历丰富且充满成就感!如果有任何职业发展或相关问题,欢迎随时探讨。\n"
]
},
{
"name": "stderr",
"output_type": "stream",
"text": [
"2024-08-02 18:28:42 INFO MainThread timer:75] ----- worker.fuse_rerank.begin -----\n",
"2024-08-02 18:28:42 WARNING MainThread fuse_rerank_worker:69] Ranked memory nodes list is empty.\n",
"2024-08-02 18:28:42 INFO MainThread base_worker:145] ----- worker.fuse_rerank.end ----- cost=1.0219ms\n",
"2024-08-02 18:28:42 INFO MainThread base_workflow:167] ----- workflow.retrieve_memory.end ----- cost=1879.3590ms\n",
"2024-08-02 18:28:42 INFO MainThread timer:75] ----- workflow.read_message.begin -----\n",
"2024-08-02 18:28:42 INFO MainThread timer:75] ----- worker.read_message.begin -----\n",
"2024-08-02 18:28:42 INFO MainThread base_worker:145] ----- worker.read_message.end ----- cost=0.2019ms\n",
"2024-08-02 18:28:42 INFO MainThread base_workflow:167] ----- workflow.read_message.end ----- cost=0.8912ms\n",
"2024-08-02 18:28:42 INFO MainThread api_memory_chat:186] chat_messages=[Message(role='system', role_name='', content='你是一个名为MemoryScope的智能助理请用中文简洁地回答问题。当前时间是2024-08-02 18:28:42 周五。', time_created=1722594522, memorized=False, meta_data={}), Message(role='user', role_name='用户', content='我的爱好是弹琴。', time_created=1722594506, memorized=False, meta_data={}), Message(role='assistant', role_name='AI', content='那真是一个优雅的爱好!弹琴不仅能够陶冶情操,还能提升音乐素养和手指灵活性。你最喜欢弹奏哪种类型的曲子呢?', time_created=1722594509, memorized=False, meta_data={}), Message(role='user', role_name='用户', content='你知道我有什么乐器爱好吗?', time_created=1722594509, memorized=False, meta_data={}), Message(role='assistant', role_name='AI', content='您提到过的爱好是弹琴,所以我了解到您对弹奏乐器,特别是钢琴有兴趣。是否还有其他乐器爱好呢?', time_created=1722594512, memorized=False, meta_data={}), Message(role='user', role_name='用户', content='你知道我有什么乐器爱好吗?', time_created=1722594512, memorized=False, meta_data={}), Message(role='assistant', role_name='AI', content='作为MemoryScope我无法直接获取或记忆您的个人信息包括您的乐器爱好除非您之前已告知我。如果您愿意分享我可以帮助您记录或提供与乐器爱好相关的信息。', time_created=1722594515, memorized=False, meta_data={}), Message(role='user', role_name='用户', content='我在阿里巴巴干活', time_created=1722594515, memorized=False, meta_data={}), Message(role='assistant', role_name='AI', content='太好了,阿里巴巴是一家知名的国际企业,涉及电子商务、零售、互联网和技术等多个领域。在阿里巴巴工作,您可能参与到了推动数字经济发展和创新的前沿工作中。希望您在那边的工作经历丰富且充满成就感!如果有任何职业发展或相关问题,欢迎随时探讨。', time_created=1722594520, memorized=False, meta_data={}), Message(role='user', role_name='用户', content='今天下午吃什么水果好?', time_created=1722594520, memorized=False, meta_data={})]\n"
]
}
],
"source": [
"response = memory_chat.chat_with_memory(query=\"我在阿里巴巴干活\")\n",
"print(\"回答4\\n\" + response.message.content)\n",
"response = memory_chat.chat_with_memory(query=\"今天下午吃什么水果好?\")\n",
"print(\"回答5\\n\" + response.message.content)\n",
"response = memory_chat.chat_with_memory(query=\"我喜欢吃西瓜。\")\n",
"print(\"回答6\\n\" + response.message.content)\n",
"response = memory_chat.chat_with_memory(query=\"帮我写一句给朋友的生日祝福语,简短一点。\")\n",
"print(\"回答7\\n\" + response.message.content)"
],
"metadata": {
"collapsed": false,
"is_executing": true,
"ExecuteTime": {
"start_time": "2024-08-02T10:28:35.808409Z"
}
}
},
{
"cell_type": "code",
"execution_count": null,
"outputs": [],
"source": [
"memory_service = ms.default_memory_service\n",
"memory_service.init_service()\n",
"result = memory_service.consolidate_memory()\n",
"print(f\"consolidate_memory result={result}\")"
],
"metadata": {
"collapsed": false,
"is_executing": true
}
},
{
"cell_type": "markdown",
"source": [
"**Memory Consolidation** extracted 3 *observations* out of the 7 chat messages from the user, with the uninformative ones being filtered out.\n",
"\n",
"We try more cases to test its time awareness and the ability to filter out fictitious contents from the user."
],
"metadata": {
"collapsed": false
}
},
{
"cell_type": "code",
"execution_count": null,
"outputs": [],
"source": [
"response = memory_chat.chat_with_memory(query=\"假如我去京东工作,前景怎么样?\")\n",
"print(\"回答8\\n\" + response.message.content)\n",
"response = memory_chat.chat_with_memory(query=\"记一下,下周我准备去北京出差\")\n",
"print(\"回答9\\n\" + response.message.content)\n",
"response = memory_chat.chat_with_memory(query=\"我同学李亚平现在在亚马逊工作,他下个月回上海,我要和他吃个饭\")\n",
"print(\"回答10\\n\" + response.message.content)\n",
"response = memory_chat.chat_with_memory(query=\"小亮是我最好的朋友他决定去山西上大学。以这个为开头写一个80字的微剧本。\")\n",
"print(\"回答11\\n\" + response.message.content)\n",
"response = memory_chat.chat_with_memory(query=\"SMCI是什么公司做什么的\")\n",
"print(\"回答12\\n\" + response.message.content)"
],
"metadata": {
"collapsed": false,
"is_executing": true
}
},
{
"cell_type": "code",
"execution_count": null,
"outputs": [],
"source": [
"result = memory_service.consolidate_memory()\n",
"print(f\"consolidate_memory result={result}\")"
],
"metadata": {
"collapsed": false,
"is_executing": true
}
},
{
"cell_type": "markdown",
"source": [
"We can see **Memory Consolidation** successfully filtered out fictitious contents, and shows good time sensitivity.\n",
"\n",
"We try more cases to test its resolution of conflicting contents."
],
"metadata": {
"collapsed": false
}
},
{
"cell_type": "code",
"execution_count": null,
"outputs": [],
"source": [
"response = memory_chat.chat_with_memory(query=\"今天下午吃什么水果好?\")\n",
"print(\"回答13\\n\" + response.message.content)\n",
"response = memory_chat.chat_with_memory(query=\"西瓜确实不错,但是我也喜欢吃芒果。我今天想吃芒果。\")\n",
"print(\"回答14\\n\" + response.message.content)\n",
"response = memory_chat.chat_with_memory(query=\"我最近跳槽去了美团。\")\n",
"print(\"回答15\\n\" + response.message.content)\n",
"response = memory_chat.chat_with_memory(query=\"我还喜欢吃桃子和苹果。\")\n",
"print(\"回答16\\n\" + response.message.content)\n",
"response = memory_chat.chat_with_memory(query=\"我不喜欢吃椰子。\")\n",
"print(\"回答17\\n\" + response.message.content)\n",
"response = memory_chat.chat_with_memory(query=\"我准备下个月去海南冲浪。\")\n",
"print(\"回答18\\n\" + response.message.content)\n",
"response = memory_chat.chat_with_memory(query=\"明天是我生日。\")\n",
"print(\"回答19\\n\" + response.message.content)"
],
"metadata": {
"collapsed": false,
"is_executing": true
}
},
{
"cell_type": "code",
"execution_count": null,
"outputs": [],
"source": [
"result = memory_service.consolidate_memory()\n",
"print(f\"consolidate_memory result={result}\")"
],
"metadata": {
"collapsed": false,
"is_executing": true
}
},
{
"cell_type": "markdown",
"source": [
"## Reflection and Re-Consolidation\n",
"Now, we have accumulated enough new *observations* in the system, so we can call **Reflection and Re-Consolidation**, let's see what will it get."
],
"metadata": {
"collapsed": false
}
},
{
"cell_type": "code",
"execution_count": null,
"outputs": [],
"source": [
"result = memory_service.reflect_and_reconsolidate()\n",
"print(f\"consolidate_memory result={result}\")"
],
"metadata": {
"collapsed": false,
"is_executing": true
}
},
{
"cell_type": "markdown",
"source": [
"## Low response-time (RT) for the user\n",
"Here we test the RT of MemoryScope system for the user. Specifically, we test the difference of RT when responding with and without retrieving memory pieces from the system."
],
"metadata": {
"collapsed": false
}
},
{
"cell_type": "code",
"execution_count": null,
"outputs": [],
"source": [
"import time\n",
"\n",
"start_time = time.time()\n",
"response = memory_chat.chat_with_memory(query=\"你知道我的乐器爱好是什么吗?\",\n",
" history_message_strategy=None)\n",
"end_time = time.time()\n",
"total_time = end_time - start_time\n",
"print(\"使用记忆检索\\n回答20\\n\" + response.message.content + f\"\\n 耗时:{total_time}秒\\n\")\n",
"\n",
"start_time = time.time()\n",
"response = memory_chat.chat_with_memory(query=\"你知道我接下去的一个月内有什么计划吗?\",\n",
" history_message_strategy=None)\n",
"end_time = time.time()\n",
"total_time = end_time - start_time\n",
"print(\"使用记忆检索\\n回答21\\n\" + response.message.content + f\"\\n 耗时:{total_time}秒\\n\")\n",
"\n",
"memory_chat.run_service_operation(\"delete_all\")\n",
"start_time = time.time()\n",
"response = memory_chat.chat_with_memory(query=\"你知道我的乐器爱好是什么吗?\",\n",
" history_message_strategy=None)\n",
"end_time = time.time()\n",
"total_time = end_time - start_time\n",
"print(\"不使用记忆检索\\n回答20\\n\" + response.message.content + f\"\\n 耗时:{total_time}秒\\n\")\n",
"\n",
"start_time = time.time()\n",
"response = memory_chat.chat_with_memory(query=\"你知道我接下去的一个月内有什么计划吗?\\n\",\n",
" history_message_strategy=None)\n",
"end_time = time.time()\n",
"total_time = end_time - start_time\n",
"print(\"不使用记忆检索\\n回答21\\n\" + response.message.content + f\"\\n 耗时:{total_time}秒\")"
],
"metadata": {
"collapsed": false,
"is_executing": true
}
},
{
"cell_type": "markdown",
"source": [
"We can see responding with retrieving memory pieces from MemoryScope does not increase RT."
],
"metadata": {
"collapsed": false
}
}
],
"metadata": {
"kernelspec": {
"display_name": "Python 3",
"language": "python",
"name": "python3"
},
"language_info": {
"codemirror_mode": {
"name": "ipython",
"version": 2
},
"file_extension": ".py",
"mimetype": "text/x-python",
"name": "python",
"nbconvert_exporter": "python",
"pygments_lexer": "ipython2",
"version": "2.7.6"
}
},
"nbformat": 4,
"nbformat_minor": 0
}

View file

@ -0,0 +1,593 @@
{
"cells": [
{
"cell_type": "markdown",
"source": [
"# chat 和 service 接口的示例用法\n",
"这个笔记本展示了 MemoryScope 的 **chat** 和 **service** 接口的简单用法,以及它的主要功能。\n",
"\n",
"在运行这个笔记本之前,请先按照 Readme 中的 **Installation** 指南进行安装,并启动 Docker 镜像。\n"
],
"metadata": {
"collapsed": false
}
},
{
"cell_type": "markdown",
"source": [
"## 初始化一个 MemoryScope 实例\n",
"首先,我们需要指定一个配置并初始化一个 MemoryScope 实例。"
],
"metadata": {
"collapsed": false
}
},
{
"cell_type": "code",
"execution_count": 1,
"outputs": [],
"source": [
"import sys\n",
"sys.path.append(\".\")\n",
"from memoryscope import MemoryScope, Arguments\n",
"arguments = Arguments(\n",
" language=\"cn\",\n",
" human_name=\"用户\",\n",
" assistant_name=\"AI\",\n",
" logger_to_screen=False,\n",
" memory_chat_class=\"api_memory_chat\",\n",
" generation_backend=\"dashscope_generation\",\n",
" generation_model=\"qwen2-72b-instruct\",\n",
" embedding_backend=\"dashscope_embedding\",\n",
" embedding_model=\"text-embedding-v2\",\n",
" rank_backend=\"dashscope_rank\",\n",
" rank_model=\"gte-rerank\",\n",
" enable_ranker=True,\n",
" worker_params={\"get_reflection_subject\": {\"reflect_num_questions\": 3}}\n",
")\n",
"\n",
"ms = MemoryScope(arguments=arguments)\n"
],
"metadata": {
"collapsed": false,
"ExecuteTime": {
"end_time": "2024-08-02T14:42:19.303078Z",
"start_time": "2024-08-02T14:42:17.332785Z"
}
}
},
{
"cell_type": "markdown",
"source": [
"## 聊天(不含记忆)\n",
"MemoryScope 配有默认的 chat 接口,因此开始聊天非常容易,就像使用任何大型语言模型聊天机器人一样。"
],
"metadata": {
"collapsed": false
}
},
{
"cell_type": "code",
"execution_count": 2,
"outputs": [
{
"name": "stdout",
"output_type": "stream",
"text": [
"回答1\n",
"很高兴了解到您的爱好是弹琴,这是一种既能陶冶情操又能提升音乐技能的美妙艺术形式。无论是古典钢琴、爵士乐还是现代流行曲目,每一种风格都能带来不同的享受和挑战。希望您在弹琴的过程中能够持续发现乐趣,创造出更多动人的旋律。\n"
]
}
],
"source": [
"memory_chat = ms.default_memory_chat\n",
"memory_chat.run_service_operation(\"delete_all\")\n",
"response = memory_chat.chat_with_memory(query=\"我的爱好是弹琴。\")\n",
"print(\"回答1\\n\" + response.message.content)"
],
"metadata": {
"collapsed": false,
"ExecuteTime": {
"end_time": "2024-08-02T14:42:24.843842Z",
"start_time": "2024-08-02T14:42:19.304038Z"
}
}
},
{
"cell_type": "markdown",
"source": [
"----\n",
"你可以选择进行含有或不含有多轮对话上下文的聊天。然而,由于尚未调用**记忆巩固**功能,系统中还没有任何记忆片段。"
],
"metadata": {
"collapsed": false
}
},
{
"cell_type": "code",
"execution_count": 3,
"outputs": [
{
"name": "stdout",
"output_type": "stream",
"text": [
"回答2\n",
"是的,您提到过您的爱好是弹琴,所以我认为您对键盘乐器,特别是钢琴有一定的爱好。\n",
"回答3\n",
"作为基于当前对话的MemoryScope智能助理我没有之前关于您乐器爱好的信息。请告诉我您喜欢哪种乐器这样我就可以记住并提供相关帮助了。\n"
]
}
],
"source": [
"response = memory_chat.chat_with_memory(query=\"你知道我有什么乐器爱好吗?\")\n",
"print(\"回答2\\n\" + response.message.content)\n",
"response = memory_chat.chat_with_memory(query=\"你知道我有什么乐器爱好吗?\",\n",
" history_message_strategy=None)\n",
"print(\"回答3\\n\" + response.message.content)"
],
"metadata": {
"collapsed": false,
"ExecuteTime": {
"end_time": "2024-08-02T14:42:33.777924Z",
"start_time": "2024-08-02T14:42:24.845133Z"
}
}
},
{
"cell_type": "markdown",
"source": [
"## **记忆巩固**\n",
"现在,我们再聊多几句,然后尝试**记忆巩固**功能。"
],
"metadata": {
"collapsed": false
}
},
{
"cell_type": "code",
"execution_count": 4,
"outputs": [
{
"name": "stdout",
"output_type": "stream",
"text": [
"回答4\n",
"了解,您在阿里巴巴工作。阿里巴巴集团是一家总部位于中国杭州的全球领先的电子商务和科技公司,以其电子商务平台如淘宝、天猫闻名,同时在云计算、数字媒体及娱乐、金融科技等领域也有广泛布局。如果您有关于工作、技术或公司文化方面的问题,欢迎随时询问。\n",
"回答5\n",
"选择水果可以根据个人口味、营养需求以及季节来决定。夏天,一些清爽解暑的水果会是不错的选择,比如西瓜、哈密瓜、葡萄、桃子或者李子,它们都含有丰富的水分和维生素,有助于消暑降温。如果您想要补充纤维素,火龙果或者猕猴桃也是很好的选择。最终,选择哪种水果,还是要看您自己的喜好和身体状况。\n",
"回答6\n",
"西瓜是夏季的理想选择它不仅清凉解渴还含有大量的水分和电解质可以帮助身体补充流失的水分。西瓜还富含维生素C、A和抗氧化剂如番茄红素对皮肤健康和心血管系统都有益处。享用美味的西瓜时记得切块后冷藏一下口感会更加清爽哦\n",
"回答7\n",
"\"生日快乐,愿你的每一天都如蛋糕般甜蜜,笑容比烛光更灿烂!\"\n"
]
}
],
"source": [
"response = memory_chat.chat_with_memory(query=\"我在阿里巴巴干活\")\n",
"print(\"回答4\\n\" + response.message.content)\n",
"response = memory_chat.chat_with_memory(query=\"今天下午吃什么水果好?\")\n",
"print(\"回答5\\n\" + response.message.content)\n",
"response = memory_chat.chat_with_memory(query=\"我喜欢吃西瓜。\")\n",
"print(\"回答6\\n\" + response.message.content)\n",
"response = memory_chat.chat_with_memory(query=\"帮我写一句给朋友的生日祝福语,简短一点。\")\n",
"print(\"回答7\\n\" + response.message.content)"
],
"metadata": {
"collapsed": false,
"ExecuteTime": {
"end_time": "2024-08-02T14:42:52.517936Z",
"start_time": "2024-08-02T14:42:33.780200Z"
}
}
},
{
"cell_type": "code",
"execution_count": 5,
"outputs": [
{
"name": "stdout",
"output_type": "stream",
"text": [
"consolidate_memory result=[MEMORY ACTIONS]:\n",
"new observation: 用户爱好弹琴。 (valid)\n",
"new observation: 用户在阿里巴巴工作。 (valid)\n",
"new observation: 用户喜欢吃西瓜。 (valid)\n"
]
}
],
"source": [
"memory_service = ms.default_memory_service\n",
"memory_service.init_service()\n",
"result = memory_service.consolidate_memory()\n",
"print(f\"consolidate_memory result={result}\")"
],
"metadata": {
"collapsed": false,
"ExecuteTime": {
"end_time": "2024-08-02T14:43:14.475175Z",
"start_time": "2024-08-02T14:42:52.518868Z"
}
}
},
{
"cell_type": "markdown",
"source": [
"----\n",
"**记忆巩固**从用户的7条聊天消息中提取了3条 *observations* ,其余无效的信息被过滤掉了。\n",
"\n",
"我们尝试更多的情况,以测试其时间感知能力和过滤用户虚构的内容的能力。"
],
"metadata": {
"collapsed": false
}
},
{
"cell_type": "code",
"execution_count": 6,
"outputs": [
{
"name": "stdout",
"output_type": "stream",
"text": [
"回答8\n",
"京东作为中国领先的电商平台之一,拥有广泛的业务范围和良好的行业声誉。加入京东工作,您可能会享受到以下几方面的前景优势:\n",
"\n",
"1. **职业发展**:京东提供多元化的职业路径和晋升机会,您可以在电商、物流、科技、金融等多个领域找到适合自己的位置和发展空间。\n",
"\n",
"2. **技术与创新**:京东持续投入于技术创新,尤其是在人工智能、大数据、云计算等方面,为员工提供了接触和学习前沿技术的机会。\n",
"\n",
"3. **企业文化**:京东强调“正道成功”的企业文化,注重诚信、团队合作与社会责任,有利于塑造积极向上的工作氛围。\n",
"\n",
"4. **福利待遇**:京东通常为员工提供有竞争力的薪酬福利体系,包括但不限于健康保险、员工培训、股权激励等。\n",
"\n",
"5. **行业影响力**:作为行业巨头,京东的从业经历对您的职业生涯将是一大亮点,有助于提升个人品牌和未来的职业选择灵活性。\n",
"\n",
"当然,具体前景还取决于您的职位、个人能力、行业趋势及个人职业规划等因素。建议您深入了解目标岗位的具体要求,评估个人与岗位的匹配度,并关注行业动态,为自己的职业发展做出合理规划。\n",
"回答9\n",
"好的,已记录您计划下周去北京出差。请随时告诉我如果您需要关于北京的出行建议、天气预报或是其他相关信息。祝您出差顺利!\n",
"回答10\n",
"已记录您的安排,您将于下个月与在亚马逊工作的同学李亚平在上海共进晚餐。临近日期时,不妨提前联系李亚平确认具体时间和地点,以便愉快地进行聚会。希望你们有个美好的重聚!\n",
"回答11\n",
"【场景:傍晚,公园长椅】 \n",
"小亮是我最好的朋友,他决定去山西上大学。夕阳下,我们肩并肩坐着。 \n",
"我:“山西的面食可出名了,你这小吃货有福了!” \n",
"小亮笑:“那必须的,说好你放假就来找我,咱们一起吃遍山西!” \n",
"我点头,心中泛起不舍:“一言为定,别忘了,那里还有千年古城等你探索。” \n",
"小亮看向远方,眼里闪烁着梦想的光:“新旅程,我们一起加油!” \n",
"【画面渐暗,友情的力量温暖而坚定】\n",
"回答12\n",
"SMCI可能指代的是Super Micro Computer, Inc.超微电脑股份有限公司简称Supermicro。这是一家总部位于美国加利福尼亚州圣何塞的公司成立于1993年。Supermicro主要设计、制造和销售高性能服务器和技术解决方案包括服务器、存储系统、主板以及支持云计算、数据中心、企业IT、高性能计算HPC和嵌入式系统的其他硬件组件。它们的产品以高效率、灵活性和定制化选项著称在全球范围内服务于各种规模的企业和组织。\n"
]
}
],
"source": [
"response = memory_chat.chat_with_memory(query=\"假如我去京东工作,前景怎么样?\")\n",
"print(\"回答8\\n\" + response.message.content)\n",
"response = memory_chat.chat_with_memory(query=\"记一下,下周我准备去北京出差\")\n",
"print(\"回答9\\n\" + response.message.content)\n",
"response = memory_chat.chat_with_memory(query=\"我同学李亚平现在在亚马逊工作,他下个月回上海,我要和他吃个饭\")\n",
"print(\"回答10\\n\" + response.message.content)\n",
"response = memory_chat.chat_with_memory(query=\"小亮是我最好的朋友他决定去山西上大学。以这个为开头写一个80字的微剧本。\")\n",
"print(\"回答11\\n\" + response.message.content)\n",
"response = memory_chat.chat_with_memory(query=\"SMCI是什么公司做什么的\")\n",
"print(\"回答12\\n\" + response.message.content)"
],
"metadata": {
"collapsed": false,
"ExecuteTime": {
"end_time": "2024-08-02T14:43:57.226685Z",
"start_time": "2024-08-02T14:43:14.475977Z"
}
}
},
{
"cell_type": "code",
"execution_count": 7,
"outputs": [
{
"name": "stdout",
"output_type": "stream",
"text": [
"consolidate_memory result=[MEMORY ACTIONS]:\n",
"new observation: 用户计划2024年8月9日去北京出差。 (推断时间: 2024年8月9日) (valid)\n",
"new observation: 用户的同学李亚平下个月回上海,用户将与其见面吃饭。 (推断时间: 2024年9月) (valid)\n"
]
}
],
"source": [
"result = memory_service.consolidate_memory()\n",
"print(f\"consolidate_memory result={result}\")"
],
"metadata": {
"collapsed": false,
"ExecuteTime": {
"end_time": "2024-08-02T14:44:26.290389Z",
"start_time": "2024-08-02T14:43:57.227597Z"
}
}
},
{
"cell_type": "markdown",
"source": [
"----\n",
"我们可以看到,**记忆巩固**成功过滤掉了虚假内容,并展示了良好的时间敏感性。\n",
"\n",
"我们尝试更多的情况,以测试其解决冲突内容的能力。"
],
"metadata": {
"collapsed": false
}
},
{
"cell_type": "code",
"execution_count": 8,
"outputs": [
{
"name": "stdout",
"output_type": "stream",
"text": [
"回答13\n",
"既然您喜欢吃西瓜,今天下午吃西瓜是个不错的选择。\n",
"回答14\n",
"那太好了,如果您今天想换换口味,吃芒果也是很好的选择,享受它的香甜吧!\n",
"回答15\n",
"恭喜您加入美团!希望您的新工作一切顺利,有新的挑战和机遇。\n",
"回答16\n",
"了解了,桃子和苹果都是既营养又美味的选择,多吃水果对身体有益,您可以根据季节和个人喜好来挑选。\n",
"回答17\n",
"好的,知道您不喜欢椰子,以后在推荐水果时会留意这一点。\n",
"回答18\n",
"听起来很有趣!去海南冲浪是个很棒的计划,下个月那边的天气应该很适合水上活动,祝您玩得开心!别忘了做好防晒哦。\n",
"回答19\n",
"生日快乐!希望您明天能度过一个特别且难忘的一天,满满的祝福给您!有任何庆祝计划吗?\n"
]
}
],
"source": [
"response = memory_chat.chat_with_memory(query=\"今天下午吃什么水果好?\")\n",
"print(\"回答13\\n\" + response.message.content)\n",
"response = memory_chat.chat_with_memory(query=\"西瓜确实不错,但是我也喜欢吃芒果。我今天想吃芒果。\")\n",
"print(\"回答14\\n\" + response.message.content)\n",
"response = memory_chat.chat_with_memory(query=\"我最近跳槽去了美团。\")\n",
"print(\"回答15\\n\" + response.message.content)\n",
"response = memory_chat.chat_with_memory(query=\"我还喜欢吃桃子和苹果。\")\n",
"print(\"回答16\\n\" + response.message.content)\n",
"response = memory_chat.chat_with_memory(query=\"我不喜欢吃椰子。\")\n",
"print(\"回答17\\n\" + response.message.content)\n",
"response = memory_chat.chat_with_memory(query=\"我准备下个月去海南冲浪。\")\n",
"print(\"回答18\\n\" + response.message.content)\n",
"response = memory_chat.chat_with_memory(query=\"明天是我生日。\")\n",
"print(\"回答19\\n\" + response.message.content)"
],
"metadata": {
"collapsed": false,
"ExecuteTime": {
"end_time": "2024-08-02T14:44:57.748723Z",
"start_time": "2024-08-02T14:44:26.292543Z"
}
}
},
{
"cell_type": "code",
"execution_count": 9,
"outputs": [
{
"name": "stdout",
"output_type": "stream",
"text": [
"consolidate_memory result=[MEMORY ACTIONS]:\n",
"new observation: 用户喜欢吃桃子和苹果。 (valid)\n",
"new observation: 用户不喜欢吃椰子。 (valid)\n",
"new observation: 用户喜欢吃芒果。 (valid)\n",
"new observation: 用户计划2024年9月去海南冲浪。 (推断时间: 2024年9月) (valid)\n",
"new observation: 用户的生日是每年8月3日。 (推断时间: 每年8月3日) (valid)\n",
"modified observation: 用户在阿里巴巴工作。 (expired)\n",
"modified observation: 用户最近跳槽至美团。 (expired)\n"
]
}
],
"source": [
"result = memory_service.consolidate_memory()\n",
"print(f\"consolidate_memory result={result}\")"
],
"metadata": {
"collapsed": false,
"ExecuteTime": {
"end_time": "2024-08-02T14:45:38.454853Z",
"start_time": "2024-08-02T14:44:57.750558Z"
}
}
},
{
"cell_type": "markdown",
"source": [
"## **反思与再巩固**\n",
"现在,我们在系统中已经积累了足够多的新的 *observations* ,因此我们可以调用**反思与再巩固**功能,让我们看看会得到什么。"
],
"metadata": {
"collapsed": false
}
},
{
"cell_type": "code",
"execution_count": 10,
"outputs": [
{
"name": "stdout",
"output_type": "stream",
"text": [
"consolidate_memory result=[MEMORY ACTIONS]:\n",
"new insight: 用户的出差计划: 2024年8月9日去北京出差 (valid)\n",
"new insight: 用户的生日: 每年8月3日 (valid)\n",
"new insight: 用户的水果偏好: 喜欢桃子、苹果、西瓜、芒果,不喜欢吃椰子 (valid)\n",
"modified observation: 用户计划2024年8月9日去北京出差。 (推断时间: 2024年8月9日) (valid)\n",
"modified observation: 用户的生日是每年8月3日。 (推断时间: 每年8月3日) (valid)\n",
"modified observation: 用户计划2024年9月去海南冲浪。 (推断时间: 2024年9月) (valid)\n",
"modified observation: 用户喜欢吃芒果。 (valid)\n",
"modified observation: 用户喜欢吃桃子和苹果。 (valid)\n",
"modified observation: 用户爱好弹琴。 (valid)\n",
"modified observation: 用户喜欢吃西瓜。 (valid)\n",
"modified observation: 用户不喜欢吃椰子。 (valid)\n",
"modified observation: 用户的同学李亚平下个月回上海,用户将与其见面吃饭。 (推断时间: 2024年9月) (valid)\n"
]
}
],
"source": [
"result = memory_service.reflect_and_reconsolidate()\n",
"print(f\"consolidate_memory result={result}\")"
],
"metadata": {
"collapsed": false,
"ExecuteTime": {
"end_time": "2024-08-02T14:45:52.599528Z",
"start_time": "2024-08-02T14:45:38.455556Z"
}
}
},
{
"cell_type": "markdown",
"source": [
"## 低用户时延RT\n",
"\n",
"最后,我们测试 MemoryScope 系统对用户的响应时间 (RT)。具体来说,我们测试在有和没有从系统中检索记忆片段时聊天的响应时间的差异。"
],
"metadata": {
"collapsed": false
}
},
{
"cell_type": "code",
"execution_count": 0,
"outputs": [],
"source": [
"import time\n",
"\n",
"start_time = time.time()\n",
"response = memory_chat.chat_with_memory(query=\"你知道我的乐器爱好是什么吗?\",\n",
" history_message_strategy=None)\n",
"end_time = time.time()\n",
"total_time = end_time - start_time\n",
"print(\"使用记忆检索\\n回答20\\n\" + response.message.content + f\"\\n 耗时:{total_time}秒\\n\")\n",
"\n",
"start_time = time.time()\n",
"response = memory_chat.chat_with_memory(query=\"你知道我接下去的一个月内有什么计划吗?\",\n",
" history_message_strategy=None)\n",
"end_time = time.time()\n",
"total_time = end_time - start_time\n",
"print(\"使用记忆检索\\n回答21\\n\" + response.message.content + f\"\\n 耗时:{total_time}秒\\n\")"
],
"metadata": {
"collapsed": false
}
},
{
"cell_type": "code",
"execution_count": 11,
"outputs": [
{
"name": "stdout",
"output_type": "stream",
"text": [
"使用记忆检索\n",
"回答20\n",
"您喜欢弹琴。\n",
" 耗时1.3783161640167236秒\n",
"\n",
"使用记忆检索\n",
"回答21\n",
"您接下来一个月内的计划包括:\n",
"- 2024年8月9日去北京出差。\n",
"- 计划在2024年9月去海南冲浪。\n",
"- 2024年9月您的同学李亚平回上海您将与他见面吃饭。\n",
" 耗时6.538439035415649秒\n",
"\n",
"不使用记忆检索\n",
"回答20\n",
"对不起,我没有记录您的个人信息,包括您的乐器爱好。如果您告诉我,我可以帮您记住。\n",
" 耗时2.597784996032715秒\n",
"\n",
"不使用记忆检索\n",
"回答21\n",
"对不起作为基于当前会话的MemoryScope智能助理我无法获取或存储您的个人日程信息。如果您需要查询自己的计划建议您查看自己的日历或者备忘录。\n",
" 耗时5.246160984039307秒\n"
]
}
],
"source": [
"memory_chat.run_service_operation(\"delete_all\")\n",
"start_time = time.time()\n",
"response = memory_chat.chat_with_memory(query=\"你知道我的乐器爱好是什么吗?\",\n",
" history_message_strategy=None)\n",
"end_time = time.time()\n",
"total_time = end_time - start_time\n",
"print(\"不使用记忆检索\\n回答20\\n\" + response.message.content + f\"\\n 耗时:{total_time}秒\\n\")\n",
"\n",
"start_time = time.time()\n",
"response = memory_chat.chat_with_memory(query=\"你知道我接下去的一个月内有什么计划吗?\\n\",\n",
" history_message_strategy=None)\n",
"end_time = time.time()\n",
"total_time = end_time - start_time\n",
"print(\"不使用记忆检索\\n回答21\\n\" + response.message.content + f\"\\n 耗时:{total_time}秒\")"
],
"metadata": {
"collapsed": false,
"ExecuteTime": {
"end_time": "2024-08-02T14:46:08.814486Z",
"start_time": "2024-08-02T14:45:52.601688Z"
}
}
},
{
"cell_type": "markdown",
"source": [
"----\n",
"我们可以看到,从 MemoryScope 检索记忆片段不会增加聊天的响应时间。"
],
"metadata": {
"collapsed": false
}
},
{
"cell_type": "markdown",
"source": [
"## 更多用法\n",
"我们建议读者参考[进阶自定义用法](./examples/api/advanced_customization_cn.ipynb)来对MemoryScope系统进行各种自定义设置。您还可以通过自定义**workflow**和对应的**worker**来创建或定制满足您特定需求的**operation**。\n",
"\n",
"此外,您还可以尝试使用[在命令行与MemoryScope聊天机器人交互](./examples/cli/dash_cli_cn1.sh)。我们在这里实现了始终在后台异步运行**记忆巩固**和**反思与再巩固**这两个操作,从而使得它们不会增加聊天的响应时间。"
],
"metadata": {
"collapsed": false
}
},
{
"cell_type": "code",
"execution_count": null,
"outputs": [],
"source": [],
"metadata": {
"collapsed": false
}
}
],
"metadata": {
"kernelspec": {
"display_name": "Python 3",
"language": "python",
"name": "python3"
},
"language_info": {
"codemirror_mode": {
"name": "ipython",
"version": 2
},
"file_extension": ".py",
"mimetype": "text/x-python",
"name": "python",
"nbconvert_exporter": "python",
"pygments_lexer": "ipython2",
"version": "2.7.6"
}
},
"nbformat": 4,
"nbformat_minor": 0
}

View file

@ -0,0 +1,612 @@
{
"cells": [
{
"cell_type": "markdown",
"source": [
"# Example usages of **chat** and **service** interfaces\n",
"This notebook shows simple usages of MemoryScope's **chat** and **service** interfaces, along with its main features.\n",
"\n",
"Before running this notebook, follow the **Installation** guidelines in Readme, and start the Docker image first."
],
"metadata": {
"collapsed": false
}
},
{
"cell_type": "markdown",
"source": [
"## Initiate a MemoryScope instance\n",
"First, we need to specify a configuration and initiate a MemoryScope instance."
],
"metadata": {
"collapsed": false
}
},
{
"cell_type": "code",
"execution_count": 1,
"outputs": [],
"source": [
"import sys\n",
"sys.path.append(\".\")\n",
"from memoryscope import MemoryScope, Arguments\n",
"arguments = Arguments(\n",
" language=\"en\",\n",
" human_name=\"User\",\n",
" assistant_name=\"AI\",\n",
" logger_to_screen=False,\n",
" memory_chat_class=\"api_memory_chat\",\n",
" generation_backend=\"dashscope_generation\",\n",
" generation_model=\"qwen2-72b-instruct\",\n",
" embedding_backend=\"dashscope_embedding\",\n",
" embedding_model=\"text-embedding-v2\",\n",
" rank_backend=\"dashscope_rank\",\n",
" rank_model=\"gte-rerank\",\n",
" enable_ranker=True,\n",
" worker_params={\"get_reflection_subject\": {\"reflect_num_questions\": 3}}\n",
")\n",
"\n",
"ms = MemoryScope(arguments=arguments)\n"
],
"metadata": {
"collapsed": false,
"ExecuteTime": {
"end_time": "2024-08-02T14:34:30.387354Z",
"start_time": "2024-08-02T14:34:28.512658Z"
}
}
},
{
"cell_type": "markdown",
"source": [
"## Chat without memory\n",
"MemoryScope comes with a default **chat** interface, so it's very easy to start chatting, just as what you'll do with any LLM chatbot."
],
"metadata": {
"collapsed": false
}
},
{
"cell_type": "code",
"execution_count": 2,
"outputs": [
{
"name": "stdout",
"output_type": "stream",
"text": [
"Response 1: \n",
"That's wonderful! Playing the piano is a beautiful and expressive hobby that can bring joy, relaxation, and a sense of achievement. It engages both the creative and technical aspects of your mind, enhancing cognitive skills and fostering emotional expression. Whether you enjoy classical pieces, modern compositions, or improvisation, the piano offers a vast repertoire to explore. Keep practicing and enjoying your musical journey!\n"
]
}
],
"source": [
"memory_chat = ms.default_memory_chat\n",
"memory_chat.run_service_operation(\"delete_all\")\n",
"response = memory_chat.chat_with_memory(query=\"My hobby is to play piano.\")\n",
"print(\"Response 1: \\n\" + response.message.content)"
],
"metadata": {
"collapsed": false,
"ExecuteTime": {
"end_time": "2024-08-02T14:34:35.313590Z",
"start_time": "2024-08-02T14:34:30.388733Z"
}
}
},
{
"cell_type": "markdown",
"source": [
"----\n",
"You can choose to chat with or without multi-round conversation contexts. However, since **Memory Consolidation** has not been called, there's no memory pieces in the system yet."
],
"metadata": {
"collapsed": false
}
},
{
"cell_type": "code",
"execution_count": 3,
"outputs": [
{
"name": "stdout",
"output_type": "stream",
"text": [
"Response 2: \n",
"Based on our conversation, you have mentioned that your hobby is to play the piano. Therefore, yes, you play a musical instrument the piano.\n",
"Response 3: \n",
"I'm sorry, but as MemoryScope, I don't have access to personal information about individuals unless it has been shared with me during our conversation. Therefore, I cannot determine if you play any musical instruments. If you do play an instrument, feel free to share that information with me!\n"
]
}
],
"source": [
"response = memory_chat.chat_with_memory(query=\"Do you know if I play any musical instruments?\")\n",
"print(\"Response 2: \\n\" + response.message.content)\n",
"response = memory_chat.chat_with_memory(query=\"Do you know if I play any musical instruments?\",\n",
" history_message_strategy=None)\n",
"print(\"Response 3: \\n\" + response.message.content)"
],
"metadata": {
"collapsed": false,
"ExecuteTime": {
"end_time": "2024-08-02T14:34:42.621918Z",
"start_time": "2024-08-02T14:34:35.315441Z"
}
}
},
{
"cell_type": "markdown",
"source": [
"## Memory Consolidation\n",
"Now, we do a bit more chatting and then try out **Memory Consolidation**."
],
"metadata": {
"collapsed": false
}
},
{
"cell_type": "code",
"execution_count": 4,
"outputs": [
{
"name": "stdout",
"output_type": "stream",
"text": [
"Response 4: \n",
"Thank you for letting me know! Working for Meta (formerly known as Facebook) implies that you're part of a company at the forefront of technology, social media, and innovation. Meta is known for its influential platforms like Facebook, Instagram, and WhatsApp, as well as its work in virtual reality with Oculus and efforts towards building the metaverse. It's exciting to be part of a company shaping the future of digital communication and interaction.\n",
"Response 5: \n",
"Eating fruit is a great way to nourish your body and satisfy your taste buds. For this afternoon, consider having a refreshing and nutritious option like a juicy orange, a sweet apple, a handful of berries (such as strawberries, blueberries, or raspberries), or a slice of refreshing watermelon. These fruits are not only delicious but also packed with vitamins, antioxidants, and fiber to keep you energized throughout the day. Choose the one that appeals to you most or mix a few for a colorful fruit salad!\n",
"Response 6: \n",
"Watermelon is an excellent choice! It's hydrating, low in calories, and rich in nutrients like vitamin C, vitamin A, and lycopene. Its high water content makes it perfect for a refreshing snack on a warm day. Enjoy your watermelon; it's a tasty and healthy way to treat yourself this afternoon.\n",
"Response 7: \n",
"\"Happy Birthday, dear [friend's name]! May your special day be filled with love, laughter, and unforgettable moments. May the coming year bring you joy, success, and adventures that make your heart sing. Cheers to another year of friendship and wonderful memories. Enjoy your day to the fullest!\"\n"
]
}
],
"source": [
"response = memory_chat.chat_with_memory(query=\"I work for Meta\")\n",
"print(\"Response 4: \\n\" + response.message.content)\n",
"response = memory_chat.chat_with_memory(query=\"What fruit should I eat this afternoon?\")\n",
"print(\"Response 5: \\n\" + response.message.content)\n",
"response = memory_chat.chat_with_memory(query=\"I like watermelon。\")\n",
"print(\"Response 6: \\n\" + response.message.content)\n",
"response = memory_chat.chat_with_memory(query=\"Help me write a short birthday wish for a friend.\")\n",
"print(\"Response 7: \\n\" + response.message.content)"
],
"metadata": {
"collapsed": false,
"ExecuteTime": {
"end_time": "2024-08-02T14:35:07.399702Z",
"start_time": "2024-08-02T14:34:42.622804Z"
}
}
},
{
"cell_type": "code",
"execution_count": 5,
"outputs": [
{
"name": "stdout",
"output_type": "stream",
"text": [
"consolidate_memory result=[MEMORY ACTIONS]:\n",
"new observation: user's hobby is playing the piano (valid)\n",
"new observation: user works for meta (valid)\n",
"new observation: user likes watermelon (valid)\n"
]
}
],
"source": [
"memory_service = ms.default_memory_service\n",
"memory_service.init_service()\n",
"result = memory_service.consolidate_memory()\n",
"print(f\"consolidate_memory result={result}\")"
],
"metadata": {
"collapsed": false,
"ExecuteTime": {
"end_time": "2024-08-02T14:35:42.560772Z",
"start_time": "2024-08-02T14:35:07.401224Z"
}
}
},
{
"cell_type": "markdown",
"source": [
"----\n",
"**Memory Consolidation** extracted 3 *observations* out of the 7 chat messages from the user, with the uninformative ones being filtered out.\n",
"\n",
"We try more cases to test its time awareness and the ability to filter out fictitious contents from the user."
],
"metadata": {
"collapsed": false
}
},
{
"cell_type": "code",
"execution_count": 6,
"outputs": [
{
"name": "stdout",
"output_type": "stream",
"text": [
"Response 8: \n",
"Prospects at Amazon can be quite promising, given its status as one of the world's leading tech companies. You can expect competitive compensation, career growth opportunities, a dynamic work environment, and the chance to work on innovative projects. However, actual experiences may vary based on role, team, and individual performance. It's always a good idea to research specific positions, company culture, and employee reviews to get a more comprehensive understanding.\n",
"Response 9: \n",
"Great, planning a business trip to Seattle can be exciting. Some key points to consider for your trip:\n",
"\n",
"1. **Weather**: Check the weather forecast for Seattle to pack appropriately. Seattle is known for its mild, marine climate with possible rainfall throughout the year.\n",
"\n",
"2. **Transportation**: Familiarize yourself with transportation options like Sea-Tac Airport, light rail, buses, and ride-sharing services for getting around.\n",
"\n",
"3. **Accommodation**: Book a hotel near your meeting location or in a convenient area like Downtown Seattle for easy access to amenities.\n",
"\n",
"4. **Meetings**: Confirm all meeting schedules and locations in advance. Consider any time differences if coming from outside the Pacific Time Zone.\n",
"\n",
"5. **Business Etiquette**: Brush up on general business etiquette, especially if you're meeting with new clients or partners.\n",
"\n",
"6. **Networking**: Look out for industry events or networking opportunities during your stay.\n",
"\n",
"7. **Dining**: Research dining options for client meetings or personal meals, Seattle offers a diverse culinary scene.\n",
"\n",
"8. **Leisure**: If time permits, plan to explore local attractions like Pike Place Market, Space Needle, or take a stroll along Puget Sound.\n",
"\n",
"Safe travels and have a productive trip!\n",
"Response 10: \n",
"That sounds like a nice plan! Catching up with Liam over a meal after his return from Google in New York will likely be an opportunity to exchange experiences and stories. Here are a few tips for your meet-up:\n",
"\n",
"1. **Choose a Venue**: Pick a restaurant that suits both your tastes and preferences, perhaps somewhere central or with a special significance to your friendship.\n",
"\n",
"2. **Schedule**: Coordinate a date and time that works well for both of your schedules, considering Liam might need time to adjust after his return.\n",
"\n",
"3. **Conversation Topics**: Prepare some conversation starters about his experience at Google, life in New York, and any shared interests or memories from your time in class together.\n",
"\n",
"4. **Professional Insights**: Liam might have valuable insights from his work at Google that could be beneficial for your own career growth, especially considering your interest in the tech industry.\n",
"\n",
"5. **Personal Updates**: Share your own updates too your work at Meta, your love for watermelon, and your piano hobby could spark interesting discussions.\n",
"\n",
"Enjoy your reunion and the chance to learn from each other's experiences!\n",
"Response 11: \n",
"As Cynthia hugged her tightly, User felt a bittersweet pang. \"Promise we'll stay close,\" User whispered, staring at the luggage adorned with University of Iowa stickers. Cynthia smiled, eyes glistening, \"Distance means nothing when hearts are intertwined.\" They laughed, remembering late-night study sessions fueled by watermelon slices, and the echoes of piano melodies that filled User's apartment. As the cab honked, their hands reluctantly parted, but their dreams intertwined, ready for chapters anew.\n",
"Response 12: \n",
"I'm unable to provide real-time information or look up specific companies without prior data. If SMCI is a hypothetical or fictional company within our conversation context, I don't have details on it. For actual companies, it would be best to search online or refer to official sources for the most accurate and up-to-date information on their nature and activities.\n"
]
}
],
"source": [
"response = memory_chat.chat_with_memory(query=\"What are the prospects like if I go to work at Amazon?\")\n",
"print(\"Response 8: \\n\" + response.message.content)\n",
"response = memory_chat.chat_with_memory(query=\"Note: I am planning a business trip to Seattle next week.\")\n",
"print(\"Response 9: \\n\" + response.message.content)\n",
"response = memory_chat.chat_with_memory(query=\"My classmate Liam is currently working at Google. He will return to New York next month, and I plan to have a meal with him.\")\n",
"print(\"Response 10: \\n\" + response.message.content)\n",
"response = memory_chat.chat_with_memory(query=\"Cynthia is my best friend, and she has decided to go to university in Iowa. Write an 80-word micro drama starting with this.\")\n",
"print(\"Response 11: \\n\" + response.message.content)\n",
"response = memory_chat.chat_with_memory(query=\"What kind of company is SMCI, and what do they do?\")\n",
"print(\"Response 12: \\n\" + response.message.content)"
],
"metadata": {
"collapsed": false,
"ExecuteTime": {
"end_time": "2024-08-02T14:36:30.548435Z",
"start_time": "2024-08-02T14:35:42.559854Z"
}
}
},
{
"cell_type": "code",
"execution_count": 7,
"outputs": [
{
"name": "stdout",
"output_type": "stream",
"text": [
"consolidate_memory result=[MEMORY ACTIONS]:\n",
"new observation: user is interested in career prospects at amazon (valid)\n",
"new observation: user is inquiring about smci's nature and business (valid)\n",
"new observation: user is planning a business trip to seattle next week (Inference time: next week after august 2, 2024) (valid)\n",
"new observation: user plans to meet classmate liam in new york next month (Inference time: next month after august 2, 2024) (valid)\n"
]
}
],
"source": [
"result = memory_service.consolidate_memory()\n",
"print(f\"consolidate_memory result={result}\")"
],
"metadata": {
"collapsed": false,
"ExecuteTime": {
"end_time": "2024-08-02T14:37:04.220392Z",
"start_time": "2024-08-02T14:36:30.549719Z"
}
}
},
{
"cell_type": "markdown",
"source": [
"----\n",
"We can see **Memory Consolidation** successfully filtered out fictitious contents, and shows good time sensitivity.\n",
"\n",
"We try more cases to test its resolution of conflicting contents."
],
"metadata": {
"collapsed": false
}
},
{
"cell_type": "code",
"execution_count": 8,
"outputs": [
{
"name": "stdout",
"output_type": "stream",
"text": [
"Response 13: \n",
"You might enjoy eating watermelon this afternoon since you like it.\n",
"Response 14: \n",
"Great choice! Enjoy your mangoes today.\n",
"Response 15: \n",
"Congratulations on your new job at Apple! How are you finding the experience so far?\n",
"Response 16: \n",
"That's wonderful! Peaches and apples are both delicious and nutritious choices. Enjoy snacking on them whenever you like.\n",
"Response 17: \n",
"No problem at all, everyone has their preferences. If you don't like coconuts, there are plenty of other fruits to enjoy.\n",
"Response 18: \n",
"Sounds like an exciting plan! Florida offers great surfing spots with its warm waters and waves. Have a fantastic time surfing next month.\n",
"Response 19\n",
"Happy Birthday for tomorrow! May your day be filled with joy, laughter, and memorable moments.\n"
]
}
],
"source": [
"response = memory_chat.chat_with_memory(query=\"What fruit should I eat this afternoon?\")\n",
"print(\"Response 13: \\n\" + response.message.content)\n",
"response = memory_chat.chat_with_memory(query=\"Watermelon is really good, but I also like mangoes. Today, I want to eat mangoes.\")\n",
"print(\"Response 14: \\n\" + response.message.content)\n",
"response = memory_chat.chat_with_memory(query=\"I recently switched jobs and joined Apple.\")\n",
"print(\"Response 15: \\n\" + response.message.content)\n",
"response = memory_chat.chat_with_memory(query=\"I also like eating peaches and apples.\")\n",
"print(\"Response 16: \\n\" + response.message.content)\n",
"response = memory_chat.chat_with_memory(query=\"I don't like eating coconuts.\")\n",
"print(\"Response 17: \\n\" + response.message.content)\n",
"response = memory_chat.chat_with_memory(query=\"I am planning to go surfing in Florida next month.\")\n",
"print(\"Response 18: \\n\" + response.message.content)\n",
"response = memory_chat.chat_with_memory(query=\"Tomorrow is my birthday\")\n",
"print(\"Response 19\\n\" + response.message.content)"
],
"metadata": {
"collapsed": false,
"ExecuteTime": {
"end_time": "2024-08-02T14:37:25.239829Z",
"start_time": "2024-08-02T14:37:04.221032Z"
}
}
},
{
"cell_type": "code",
"execution_count": 9,
"outputs": [
{
"name": "stdout",
"output_type": "stream",
"text": [
"consolidate_memory result=[MEMORY ACTIONS]:\n",
"new observation: user likes mangoes and wants to eat them today (valid)\n",
"new observation: user is planning to go surfing in florida next month (valid)\n",
"new observation: user's birthday is tomorrow (valid)\n",
"new observation: user recently switched jobs and joined apple (valid)\n",
"new observation: user likes eating peaches and apples (valid)\n",
"new observation: user doesn't like eating coconuts (valid)\n"
]
}
],
"source": [
"result = memory_service.consolidate_memory()\n",
"print(f\"consolidate_memory result={result}\")"
],
"metadata": {
"collapsed": false,
"ExecuteTime": {
"end_time": "2024-08-02T14:38:05.369583Z",
"start_time": "2024-08-02T14:37:25.241674Z"
}
}
},
{
"cell_type": "markdown",
"source": [
"## Reflection and Re-Consolidation\n",
"Now, we have accumulated enough new *observations* in the system, so we can call **Reflection and Re-Consolidation**, let's see what will it get."
],
"metadata": {
"collapsed": false
}
},
{
"cell_type": "code",
"execution_count": 10,
"outputs": [
{
"name": "stdout",
"output_type": "stream",
"text": [
"consolidate_memory result=[MEMORY ACTIONS]:\n",
"new insight: The Travel plans of User: travel plans include a business trip to seattle next week after august 2, 2024, meeting a classmate in new york next month after august 2, 2024, and going surfing in florida next month (valid)\n",
"new insight: The Fruit preferences of User: fruit preferences: watermelon, peaches, apples, mangoes; dislikes coconuts (valid)\n",
"new insight: The Career interest of User: amazon (valid)\n",
"modified observation: user is planning a business trip to seattle next week (Inference time: next week after august 2, 2024) (valid)\n",
"modified observation: user likes mangoes and wants to eat them today (valid)\n",
"modified observation: user is interested in career prospects at amazon (valid)\n",
"modified observation: user is inquiring about smci's nature and business (valid)\n",
"modified observation: user recently switched jobs and joined apple (valid)\n",
"modified observation: user's hobby is playing the piano (valid)\n",
"modified observation: user is planning to go surfing in florida next month (valid)\n",
"modified observation: user's birthday is tomorrow (valid)\n",
"modified observation: user likes watermelon (valid)\n",
"modified observation: user works for meta (valid)\n",
"modified observation: user plans to meet classmate liam in new york next month (Inference time: next month after august 2, 2024) (valid)\n",
"modified observation: user likes eating peaches and apples (valid)\n",
"modified observation: user doesn't like eating coconuts (valid)\n"
]
}
],
"source": [
"result = memory_service.reflect_and_reconsolidate()\n",
"print(f\"consolidate_memory result={result}\")"
],
"metadata": {
"collapsed": false,
"ExecuteTime": {
"end_time": "2024-08-02T14:38:27.126860Z",
"start_time": "2024-08-02T14:38:05.370326Z"
}
}
},
{
"cell_type": "markdown",
"source": [
"## Low response-time (RT) for the user\n",
"Finally, we test the RT of MemoryScope system for the user. Specifically, we test the difference of RT when responding with and without retrieving memory pieces from the system."
],
"metadata": {
"collapsed": false
}
},
{
"cell_type": "code",
"execution_count": 0,
"outputs": [],
"source": [
"import time\n",
"\n",
"start_time = time.time()\n",
"response = memory_chat.chat_with_memory(query=\"Do you know if I play any musical instruments?\",\n",
" history_message_strategy=None)\n",
"end_time = time.time()\n",
"total_time = end_time - start_time\n",
"print(\"With memory retrieval\\nResponse 20: \\n\" + response.message.content + f\"\\n RT: {total_time} seconds\\n\")\n",
"\n",
"start_time = time.time()\n",
"response = memory_chat.chat_with_memory(query=\"Do you know if I have any plans in the next month?\",\n",
" history_message_strategy=None)\n",
"end_time = time.time()\n",
"total_time = end_time - start_time\n",
"print(\"With memory retrieval\\nResponse 21: \\n\" + response.message.content + f\"\\n RT: {total_time} seconds\\n\")"
],
"metadata": {
"collapsed": false
}
},
{
"cell_type": "code",
"execution_count": 11,
"outputs": [
{
"name": "stdout",
"output_type": "stream",
"text": [
"With memory retrieval\n",
"Response 20: \n",
"Yes, you play the piano.\n",
" RT: 1.494797706604004 seconds\n",
"\n",
"With memory retrieval\n",
"Response 21: \n",
"Yes, you have the following plans in the next month:\n",
"\n",
"1. A business trip to Seattle sometime after August 2, 2024.\n",
"2. Meeting a classmate in New York next month, also after August 2, 2024.\n",
"3. Going surfing in Florida next month.\n",
" RT: 6.400442123413086 seconds\n",
"\n",
"Without memory retrieval\n",
"Response 22: \n",
"I'm sorry, but as MemoryScope, I don't have access to personal information about individuals unless it has been shared with me during our conversation. Therefore, I cannot determine if you play any musical instruments without you providing that information. Have you mentioned anything about your musical abilities before?\n",
" RT: 3.8412117958068848 seconds\n",
"\n",
"Without memory retrieval\n",
"Response 23: \n",
"I'm sorry, but as an AI, I don't have access to personal schedules or information unless it has been shared with me during our conversation, which we haven't had. I cannot access external databases or personal calendars. I'm here to provide general information and assistance based on the knowledge I've been trained on.\n",
" RT: 6.7565529346466064 seconds\n",
"\n"
]
}
],
"source": [
"memory_chat.run_service_operation(\"delete_all\")\n",
"start_time = time.time()\n",
"response = memory_chat.chat_with_memory(query=\"Do you know if I play any musical instruments?\",\n",
" history_message_strategy=None)\n",
"end_time = time.time()\n",
"total_time = end_time - start_time\n",
"print(\"Without memory retrieval\\nResponse 22: \\n\" + response.message.content + f\"\\n RT: {total_time} seconds\\n\")\n",
"\n",
"start_time = time.time()\n",
"response = memory_chat.chat_with_memory(query=\"Do you know if I have any plans in the next month?\\n\",\n",
" history_message_strategy=None)\n",
"end_time = time.time()\n",
"total_time = end_time - start_time\n",
"print(\"Without memory retrieval\\nResponse 23: \\n\" + response.message.content + f\"\\n RT: {total_time} seconds\\n\")"
],
"metadata": {
"collapsed": false,
"ExecuteTime": {
"end_time": "2024-08-02T14:38:46.158169Z",
"start_time": "2024-08-02T14:38:27.128634Z"
}
}
},
{
"cell_type": "markdown",
"source": [
"----\n",
"We can see responding with retrieving memory pieces from MemoryScope does not increase RT."
],
"metadata": {
"collapsed": false
}
},
{
"cell_type": "markdown",
"source": [
"## More Examples\n",
"We direct the reader to [Advanced Customization](./examples/api/advanced_customization_en.ipynb) for guidance on customizing the various settings of the MemoryScope system. It is also possible to create or customize your own MemoryScope **operations** by specifying a **workflow** and the corresponding **workers** that best meet your specific needs.\n",
"\n",
"Additionally, you can also try out the [CLI with a MemoryScope Chatbot](./examples/cli/dash_cli_cn1.sh). We have implemented the chatbot so that the **Memory Consolidation** and **Reflection and Re-Consolidation** operations are always run asynchronously in the backend, ensuring that they do not incur any response time for the user.\n",
"\n"
],
"metadata": {
"collapsed": false
}
},
{
"cell_type": "code",
"execution_count": null,
"outputs": [],
"source": [],
"metadata": {
"collapsed": false
}
}
],
"metadata": {
"kernelspec": {
"display_name": "Python 3",
"language": "python",
"name": "python3"
},
"language_info": {
"codemirror_mode": {
"name": "ipython",
"version": 2
},
"file_extension": ".py",
"mimetype": "text/x-python",
"name": "python",
"nbconvert_exporter": "python",
"pygments_lexer": "ipython2",
"version": "2.7.6"
}
},
"nbformat": 4,
"nbformat_minor": 0
}