add backend operation lock

This commit is contained in:
fuqingxu.fqx 2024-09-05 11:44:12 +08:00 committed by jinli.yl
parent 66ddfc7bc0
commit 993c48cdca
6 changed files with 31 additions and 7 deletions

View file

@ -43,7 +43,7 @@
3. Run `docker-compose up` to build and launch the memory-scope cli interface.
## III. Install from PyPI [Linux]
## III. Install from PyPI
1. Install from PyPI
```bash
@ -90,7 +90,7 @@ The docker method is recommended:
--enable_ranker=False
```
## IV. Install from source [Linux only]
## IV. Install from source
1. Clone the repository and edit settings
```bash

View file

@ -41,7 +41,7 @@
3. 运行 `docker-compose up` 命令来构建并启动 MemoryScope CLI 界面。
## 三、通过 PYPI 安装 [仅限 Linux]
## 三、通过 PYPI 安装
1. 从 PyPI 安装:
```bash
@ -89,7 +89,7 @@
```
## 四、从源码安装 [仅限 Linux]
## 四、从源码安装
1. 克隆仓库并编辑设置
```bash

View file

@ -7,7 +7,7 @@
"# chat 和 service 接口的示例用法\n",
"这个笔记本展示了 MemoryScope 的 **chat** 和 **service** 接口的简单用法,以及它的主要功能。\n",
"\n",
"在运行这个笔记本之前,请先按照 Readme 中的 **Installation** 指南进行安装,并启动 Docker 镜像。\n"
"在运行这个笔记本之前,请先按照 Readme 中的 [**Installation**](../../docs/installation_zh.md#三通过-pypi-安装) 指南进行安装,并启动 Docker 镜像。\n"
]
},
{

View file

@ -7,7 +7,7 @@
"# 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."
"Before running this notebook, follow the [**Installation**](../../docs/installation.md#iii-install-from-pypi) guidelines in Readme, and start the Docker image first."
]
},
{

View file

@ -1,4 +1,5 @@
import time
import threading
from memoryscope.core.operation.base_operation import OPERATION_TYPE
from memoryscope.core.operation.frontend_operation import FrontendOperation
@ -10,6 +11,7 @@ class BackendOperation(FrontendOperation):
It manages operation status, loop control, and integrates with a global context for thread management.
"""
operation_type: OPERATION_TYPE = "backend"
operation_lock: threading.Lock = threading.Lock()
def __init__(self, interval_time: int, **kwargs):
super().__init__(**kwargs)
@ -52,7 +54,8 @@ class BackendOperation(FrontendOperation):
for target_name in self.target_names:
try:
self.run_operation(target_name=target_name, **kwargs)
with BackendOperation.operation_lock:
self.run_operation(target_name=target_name, **kwargs)
except Exception as e:
self.logger.exception(f"op_name={self.name} target_name={target_name} encounter exception. "
f"args={e.args}")

View file

@ -197,3 +197,24 @@ def cosine_similarity(query: List[float], documents: List[List[float]]):
cosine_similarities = dot_product / (query_norm * documents_norm)
return cosine_similarities.tolist()
def cosine_similarity_matrix(query: List[List[float]]):
query = np.array(query)
documents_norm = np.linalg.norm(query, axis=1)
if np.any(documents_norm == 0):
raise ValueError("One of the document vectors has zero norm, which will result in a division by zero")
n_query = query.shape[0]
query_expanded = np.expand_dims(query, axis=0)
query_triplicated = np.repeat(query_expanded, repeats=n_query, axis=0)
query_transpose = query_triplicated.swapaxes(0, 1)
q = np.expand_dims(documents_norm, axis=0)
norm_dot = q.transpose() * q
dot_product = (query_triplicated*query_transpose).sum(-1) / norm_dot
return dot_product