diff --git a/docs/release_note.md b/docs/release_note.md index 911dc170..8d628f0f 100644 --- a/docs/release_note.md +++ b/docs/release_note.md @@ -5,6 +5,20 @@ To read the following content, you need to understand the basic use of GPTCache, - [Readme doc](https://github.com/zilliztech/GPTCache) - [Usage doc](https://github.com/zilliztech/GPTCache/blob/main/docs/usage.md) +## v0.1.29 (2023.6.2) + +1. Improve the GPTCache server by using FASTAPI + +**NOTE**: The api struct has been optimized, details: [Use GPTCache server](https://github.com/zilliztech/GPTCache/blob/dev/docs/usage.md#use-gptcache-server) + +2. Add the usearch vector store + +```python +from gptcache.manager import manager_factory + +data_manager = manager_factory("sqlite,usearch", vector_params={"dimension": 10}) +``` + ## v0.1.28 (2023.5.29) To handle a large prompt, there are currently two options available: diff --git a/gptcache/__init__.py b/gptcache/__init__.py index d855df6b..7e9eaf4f 100644 --- a/gptcache/__init__.py +++ b/gptcache/__init__.py @@ -1,5 +1,5 @@ """gptcache version""" -__version__ = "0.1.28" +__version__ = "0.1.29" from gptcache.config import Config from gptcache.core import Cache diff --git a/gptcache/manager/vector_data/manager.py b/gptcache/manager/vector_data/manager.py index 8ef64ab8..17dba46d 100644 --- a/gptcache/manager/vector_data/manager.py +++ b/gptcache/manager/vector_data/manager.py @@ -16,11 +16,8 @@ "params": {"M": 8, "efConstruction": 64}, } -PGVECTOR_URL="postgresql://postgres:postgres@localhost:5432/postgres" -PGVECTOR_INDEX_PARAMS = { - "index_type": "L2", - "params": {"lists": 100, "probes": 10} -} +PGVECTOR_URL = "postgresql://postgres:postgres@localhost:5432/postgres" +PGVECTOR_INDEX_PARAMS = {"index_type": "L2", "params": {"lists": 100, "probes": 10}} COLLECTION_NAME = "gptcache" @@ -169,6 +166,7 @@ def get(name, **kwargs): ) elif name == "pgvector": from gptcache.manager.vector_data.pgvector import PGVector + dimension = kwargs.get("dimension", DIMENSION) url = kwargs.get("url", PGVECTOR_URL) collection_name = kwargs.get("collection_name", COLLECTION_NAME) @@ -178,13 +176,27 @@ def get(name, **kwargs): top_k=top_k, url=url, collection_name=collection_name, - index_params=index_params + index_params=index_params, ) elif name == "docarray": from gptcache.manager.vector_data.docarray_index import DocArrayIndex index_path = kwargs.pop("index_path", "./docarray_index.bin") vector_base = DocArrayIndex(index_file_path=index_path, top_k=top_k) + elif name == "usearch": + from gptcache.manager.vector_data.usearch import USearch + + dimension = kwargs.get("dimension", DIMENSION) + index_path = kwargs.pop("index_path", "./index.usearch") + metric = kwargs.get("metric", "cos") + dtype = kwargs.get("dtype", "f32") + vector_base = USearch( + index_file_path=index_path, + dimension=dimension, + top_k=top_k, + metric=metric, + dtype=dtype, + ) else: raise NotFoundError("vector store", name) return vector_base diff --git a/gptcache/manager/vector_data/usearch.py b/gptcache/manager/vector_data/usearch.py index e7eafafb..f461c080 100644 --- a/gptcache/manager/vector_data/usearch.py +++ b/gptcache/manager/vector_data/usearch.py @@ -7,6 +7,7 @@ from gptcache.utils import import_usearch import_usearch() + from usearch.index import Index # pylint: disable=C0413 @@ -33,11 +34,11 @@ class USearch(VectorBase): def __init__( self, - index_file_path: str = 'index.usearch', + index_file_path: str = "index.usearch", dimension: int = 64, top_k: int = 1, - metric: str = 'cos', - dtype: str = 'f32', + metric: str = "cos", + dtype: str = "f32", connectivity: int = 16, expansion_add: int = 128, expansion_search: int = 64, @@ -57,16 +58,15 @@ def __init__( self._index.load(self._index_file_path) def mul_add(self, datas: List[VectorData]): - data_array, id_array = map( - list, zip(*((data.data, data.id) for data in datas))) - np_data = np.array(data_array).astype('float32') + data_array, id_array = map(list, zip(*((data.data, data.id) for data in datas))) + np_data = np.array(data_array).astype("float32") ids = np.array(id_array, dtype=np.longlong) self._index.add(ids, np_data) def search(self, data: np.ndarray, top_k: int = -1): if top_k == -1: top_k = self._top_k - np_data = np.array(data).astype('float32').reshape(1, -1) + np_data = np.array(data).astype("float32").reshape(1, -1) ids, dist, _ = self._index.search(np_data, top_k) return list(zip(dist[0], ids[0])) diff --git a/tests/unit_tests/manager/test_usearch.py b/tests/unit_tests/manager/test_usearch.py index 283e14e4..684220f1 100644 --- a/tests/unit_tests/manager/test_usearch.py +++ b/tests/unit_tests/manager/test_usearch.py @@ -1,7 +1,8 @@ import unittest + import numpy as np -from gptcache.manager.vector_data.usearch import USearch +from gptcache.manager.vector_data import VectorBase from gptcache.manager.vector_data.base import VectorData @@ -11,7 +12,8 @@ def test_normal(self): dim = 512 top_k = 10 - db = USearch( + db = VectorBase( + "usearch", index_file_path='./index.usearch', dimension=dim, top_k=top_k,