diff --git a/docs/installation.md b/docs/installation.md index 7c4cf0e8..be872cb5 100644 --- a/docs/installation.md +++ b/docs/installation.md @@ -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 diff --git a/docs/installation_zh.md b/docs/installation_zh.md index eedb1018..c58c67a7 100644 --- a/docs/installation_zh.md +++ b/docs/installation_zh.md @@ -41,7 +41,7 @@ 3. 运行 `docker-compose up` 命令来构建并启动 MemoryScope CLI 界面。 -## 三、通过 PYPI 安装 [仅限 Linux] +## 三、通过 PYPI 安装 1. 从 PyPI 安装: ```bash @@ -89,7 +89,7 @@ ``` -## 四、从源码安装 [仅限 Linux] +## 四、从源码安装 1. 克隆仓库并编辑设置 ```bash diff --git a/examples/api/simple_usages_cn.ipynb b/examples/api/simple_usages_cn.ipynb index 79ef7580..f4c0db3a 100644 --- a/examples/api/simple_usages_cn.ipynb +++ b/examples/api/simple_usages_cn.ipynb @@ -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" ] }, { diff --git a/examples/api/simple_usages_en.ipynb b/examples/api/simple_usages_en.ipynb index 0f7b0292..7e878ca1 100644 --- a/examples/api/simple_usages_en.ipynb +++ b/examples/api/simple_usages_en.ipynb @@ -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." ] }, { diff --git a/memoryscope/core/operation/backend_operation.py b/memoryscope/core/operation/backend_operation.py index 94f72b58..3d7f8514 100644 --- a/memoryscope/core/operation/backend_operation.py +++ b/memoryscope/core/operation/backend_operation.py @@ -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}") diff --git a/memoryscope/core/utils/tool_functions.py b/memoryscope/core/utils/tool_functions.py index d0d02d7a..9e4694ba 100644 --- a/memoryscope/core/utils/tool_functions.py +++ b/memoryscope/core/utils/tool_functions.py @@ -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 + +