-
Notifications
You must be signed in to change notification settings - Fork 512
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Abstract scalar/vector store and vector index
Signed-off-by: SimFG <bang.fu@zilliz.com>
- Loading branch information
Showing
21 changed files
with
520 additions
and
87 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
|
@@ -132,3 +132,5 @@ dmypy.json | |
**/data_map.txt | ||
**/faiss.index | ||
**/sqlite.db | ||
**/example.py | ||
**/example.db |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,48 @@ | ||
from gpt_cache.view import openai | ||
from gpt_cache.core import cache, Config | ||
from gpt_cache.cache.factory import get_ss_data_manager | ||
from gpt_cache.similarity_evaluation.simple import pair_evaluation | ||
import numpy as np | ||
|
||
|
||
d = 8 | ||
|
||
|
||
def mock_embeddings(data, **kwargs): | ||
return np.random.random((1, d)).astype('float32') | ||
|
||
|
||
def run(): | ||
# milvus | ||
data_manager = get_ss_data_manager("sqlite", "milvus", dimension=d, max_size=8, clean_size=2) | ||
# milvus cloud | ||
# data_manager = get_ss_data_manager("sqlite", "milvus", dimension=d, max_size=8, clean_size=2, | ||
# host="xxx.zillizcloud.com", | ||
# port=19530, | ||
# user="xxx", password="xxx", is_https=True, | ||
# ) | ||
cache.init(embedding_func=mock_embeddings, | ||
data_manager=data_manager, | ||
evaluation_func=pair_evaluation, | ||
similarity_threshold=10000, | ||
similarity_positive=False, | ||
config=Config(), | ||
) | ||
|
||
mock_messages = [ | ||
{"role": "system", "content": "You are a helpful assistant."}, | ||
{"role": "user", "content": "foo"} | ||
] | ||
# you should OPEN it if you FIRST run it | ||
for i in range(10): | ||
cache.data_manager.save(f"receiver the foo {i}", cache.embedding_func("foo")) | ||
|
||
answer = openai.ChatCompletion.create( | ||
model="gpt-3.5-turbo", | ||
messages=mock_messages, | ||
) | ||
print(answer) | ||
|
||
|
||
if __name__ == '__main__': | ||
run() |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1,25 +1,90 @@ | ||
from gpt_cache.cache.data_manager import DataManager | ||
from .data_manager import DataManager, SIDataManager, SSDataManager | ||
from .scalar_data.sqllite3 import SQLite | ||
from .vector_data.faiss import Faiss | ||
from .vector_data.milvus import Milvus | ||
|
||
|
||
def get_data_manager(name: str, **kwargs) -> DataManager: | ||
if name == "map": | ||
from gpt_cache.cache.data_manager import MapDataManager | ||
def get_data_manager(data_manager_name: str, **kwargs) -> DataManager: | ||
if data_manager_name == "map": | ||
from .data_manager import MapDataManager | ||
|
||
return MapDataManager(kwargs.get("data_path", "data_map.txt"), | ||
kwargs.get("max_size", 100)) | ||
elif name == "sqlite_faiss": | ||
from gpt_cache.cache.data_manager import SFDataManager | ||
|
||
dimension = kwargs.get("dimension", 0) | ||
if dimension <= 0: | ||
raise ValueError(f"the sqlite_faiss data manager should set the 'dimension' parameter greater than zero, " | ||
f"current: {dimension}") | ||
top_k = kwargs.get("top_k", 1) | ||
sqlite_path = kwargs.get("sqlite_path", "sqlite.db") | ||
index_path = kwargs.get("index_path", "faiss.index") | ||
elif data_manager_name == "scalar_vector": | ||
scalar_store = kwargs.get("scalar_store", None) | ||
vector_store = kwargs.get("vector_store", None) | ||
max_size = kwargs.get("max_size", 1000) | ||
clean_size = kwargs.get("clean_size", int(max_size * 0.2)) | ||
if scalar_store is None or vector_store is None: | ||
raise ValueError(f"Missing scalar_store or vector_store parameter for scalar_vector") | ||
return SSDataManager(max_size, clean_size, scalar_store, vector_store) | ||
elif data_manager_name == "scalar_vector_index": | ||
scalar_store = kwargs.get("scalar_store", None) | ||
vector_index = kwargs.get("vector_index", None) | ||
max_size = kwargs.get("max_size", 1000) | ||
clean_size = kwargs.get("clean_size", int(max_size * 0.2)) | ||
if scalar_store is None or vector_index is None: | ||
raise ValueError(f"Missing scalar_store or vector_index parameter for scalar_vector_index") | ||
return SIDataManager(max_size, clean_size, scalar_store, vector_index) | ||
# elif data_manager_name == "sqlite_faiss": | ||
# from .data_manager import SFDataManager | ||
# | ||
# dimension = kwargs.get("dimension", 0) | ||
# if dimension <= 0: | ||
# raise ValueError(f"the sqlite_faiss data manager should set the 'dimension' parameter greater than zero, " | ||
# f"current: {dimension}") | ||
# top_k = kwargs.get("top_k", 1) | ||
# sqlite_path = kwargs.get("sqlite_path", "sqlite.db") | ||
# index_path = kwargs.get("index_path", "faiss.index") | ||
# max_size = kwargs.get("max_size", 1000) | ||
# clean_size = kwargs.get("clean_size", int(max_size * 0.2)) | ||
# clean_cache_strategy = kwargs.get("clean_cache_strategy", "least_accessed_data") | ||
# return SFDataManager(sqlite_path, index_path, dimension, top_k, max_size, clean_size, clean_cache_strategy) | ||
else: | ||
raise ValueError(f"Unsupported data manager: {data_manager_name}") | ||
|
||
|
||
def _get_scalar_store(scalar_store: str, **kwargs): | ||
if scalar_store == "sqlite": | ||
sqlite_path = kwargs.get("sqlite_path", "sqlite.db") | ||
clean_cache_strategy = kwargs.get("clean_cache_strategy", "least_accessed_data") | ||
return SFDataManager(sqlite_path, index_path, dimension, top_k, max_size, clean_size, clean_cache_strategy) | ||
store = SQLite(sqlite_path, clean_cache_strategy) | ||
else: | ||
raise ValueError(f"Unsupported data manager: {name}") | ||
raise ValueError(f"Unsupported scalar store: {scalar_store}") | ||
return store | ||
|
||
|
||
def _get_common_params(**kwargs): | ||
max_size = kwargs.get("max_size", 1000) | ||
clean_size = kwargs.get("clean_size", int(max_size * 0.2)) | ||
top_k = kwargs.get("top_k", 1) | ||
dimension = kwargs.get("dimension", 0) | ||
if dimension <= 0: | ||
raise ValueError(f"the data manager should set the 'dimension' parameter greater than zero, " | ||
f"current: {dimension}") | ||
return max_size, clean_size, dimension, top_k | ||
|
||
|
||
# scalar_store + vector_store | ||
def get_ss_data_manager(scalar_store: str, vector_store: str, **kwargs): | ||
max_size, clean_size, dimension, top_k = _get_common_params(**kwargs) | ||
scalar = _get_scalar_store(scalar_store, **kwargs) | ||
if vector_store == "milvus": | ||
vector = Milvus(collection_name="gpt_cache", dim=dimension, top_k=top_k, **kwargs) | ||
else: | ||
raise ValueError(f"Unsupported vector store: {vector_store}") | ||
return SSDataManager(max_size, clean_size, scalar, vector) | ||
|
||
|
||
# scalar_store + vector_index | ||
def get_si_data_manager(scalar_store: str, vector_index: str, **kwargs): | ||
max_size, clean_size, dimension, top_k = _get_common_params(**kwargs) | ||
store = _get_scalar_store(scalar_store, **kwargs) | ||
|
||
if vector_index == "faiss": | ||
index_path = kwargs.get("index_path", "faiss.index") | ||
index = Faiss(index_path, dimension, top_k) | ||
else: | ||
raise ValueError(f"Unsupported vector index: {vector_index}") | ||
|
||
return SIDataManager(max_size, clean_size, store, index) |
Oops, something went wrong.