Skip to content

Commit

Permalink
Rename the dir name and add the unittest dir (#48)
Browse files Browse the repository at this point in the history
Signed-off-by: SimFG <bang.fu@zilliz.com>
  • Loading branch information
SimFG authored Apr 4, 2023
1 parent c67178f commit aa1fe25
Show file tree
Hide file tree
Showing 40 changed files with 64 additions and 62 deletions.
2 changes: 2 additions & 0 deletions .gitignore
Original file line number Diff line number Diff line change
Expand Up @@ -132,5 +132,7 @@ dmypy.json
**/data_map**.txt
**/faiss**.index
**/sqlite**.db
**/gpt_cache**.db
**/example.py
**/example.db
**/.chroma
8 changes: 4 additions & 4 deletions docs/contributing.md
Original file line number Diff line number Diff line change
Expand Up @@ -22,7 +22,7 @@ First check which part you want to contribute:
# The __init__.py file of the same directory under the new file
__all__ = ['Milvus']

from gptcache.util.lazy_import import LazyImport
from gptcache.utils.lazy_import import LazyImport

milvus = LazyImport('milvus', globals(), 'gptcache.cache.vector_data.milvus')

Expand Down Expand Up @@ -89,16 +89,16 @@ refer to the implementation of [MapDataManager, SSDataManager or SIDataManager](

## Add a embedding function

refer to the implementation of [towhee](../gptcache/encoder/towhee.py) or [openai](../gptcache/encoder/openai.py).
refer to the implementation of [towhee](../gptcache/embedding/towhee.py) or [openai](../gptcache/embedding/openai.py).

1. Add a new python file to [embedding](../gptcache/encoder) directory
1. Add a new python file to [embedding](../gptcache/embedding) directory
2. Make sure the newly added third-party libraries are lazy imported and automatic installation
3. Implement the embedding function and **make sure** your output dimension
4. Add a usage example to [example](../examples) directory and add the corresponding content to [example.md](../examples/example.md) [README.md](../README.md)

## Add a similarity evaluation function

refer to the implementation of [pair_evaluation](../gptcache/ranker/simple.py) or [towhee](../gptcache/ranker/towhee.py)
refer to the implementation of [pair_evaluation](../gptcache/similarity_evaluation/simple.py) or [towhee](../gptcache/similarity_evaluation/towhee.py)

1. Make sure the input params, you can learn more about in the [user view](../gptcache/adapter/openai.py) model
```python
Expand Down
8 changes: 4 additions & 4 deletions examples/benchmark/benchmark_sqlite_faiss_towhee.py
Original file line number Diff line number Diff line change
Expand Up @@ -5,9 +5,9 @@
from gptcache.adapter import openai
from gptcache.core import cache, Config
from gptcache.cache.factory import get_ss_data_manager
from gptcache.ranker import Towhee as EvaluationTowhee
from gptcache.encoder import Towhee as EmbeddingTowhee
from gptcache.ranker.simple import SearchDistanceEvaluation
from gptcache.similarity_evaluation import Towhee as EvaluationTowhee
from gptcache.embedding import Towhee as EmbeddingTowhee
from gptcache.similarity_evaluation.simple import SearchDistanceEvaluation


def run():
Expand Down Expand Up @@ -42,7 +42,7 @@ def evaluation(self, src_dict, cache_dict, **kwargs):
def range(self):
return 0.0, 1.0

sqlite_file = "sqlite.db"
sqlite_file = "gptcache.db"
faiss_file = "faiss.index"
has_data = os.path.isfile(sqlite_file) and os.path.isfile(faiss_file)

Expand Down
4 changes: 2 additions & 2 deletions examples/langchian_llms_mock/langchian_llms_mock.py
Original file line number Diff line number Diff line change
Expand Up @@ -4,7 +4,7 @@
from langchain.llms import OpenAI
from gptcache.core import cache, Config
from gptcache.cache.factory import get_ss_data_manager
from gptcache.ranker.simple import pair_evaluation
from gptcache.similarity_evaluation.simple import pair_evaluation
from gptcache.processor.post import nop as postnop
from gptcache.processor.pre import nop as prenop
import numpy as np
Expand All @@ -18,7 +18,7 @@ def mock_embeddings(data, **kwargs):


def run():
sqlite_file = "sqlite.db"
sqlite_file = "gptcache.db"
faiss_file = "faiss.index"
has_data = os.path.isfile(sqlite_file) and os.path.isfile(faiss_file)
data_manager = get_ss_data_manager("sqlite", "faiss",
Expand Down
4 changes: 2 additions & 2 deletions examples/openai_request/openai_request.py
Original file line number Diff line number Diff line change
Expand Up @@ -3,8 +3,8 @@

from gptcache.cache.factory import get_data_manager, get_ss_data_manager
from gptcache.core import cache, Cache, Config
from gptcache.encoder import Towhee
from gptcache.ranker.simple import SearchDistanceEvaluation
from gptcache.embedding import Towhee
from gptcache.similarity_evaluation.simple import SearchDistanceEvaluation
from gptcache.adapter import openai


Expand Down
9 changes: 4 additions & 5 deletions examples/sqlite_chromadb_mock/sqlite_chromadb_mock.py
Original file line number Diff line number Diff line change
Expand Up @@ -4,7 +4,7 @@
from gptcache.adapter import openai
from gptcache.core import cache, Config
from gptcache.cache.factory import get_ss_data_manager
from gptcache.ranker.simple import pair_evaluation
from gptcache.similarity_evaluation.simple import SearchDistanceEvaluation
import numpy as np


Expand All @@ -15,17 +15,16 @@ def mock_embeddings(data, **kwargs):


def run():
sqlite_file = "sqlite.db"
sqlite_file = "gptcache.db"
has_data = os.path.isfile(sqlite_file)
# milvus
data_manager = get_ss_data_manager("sqlite", "chromadb", max_size=8, clean_size=2, client_settings={})

cache.init(embedding_func=mock_embeddings,
data_manager=data_manager,
evaluation_func=pair_evaluation,
similarity_evaluation=SearchDistanceEvaluation(),
config=Config(
similarity_threshold=10000,
similarity_positive=False,
similarity_threshold=0,
),
)

Expand Down
4 changes: 2 additions & 2 deletions examples/sqlite_faiss_mock/sqlite_faiss_mock.py
Original file line number Diff line number Diff line change
Expand Up @@ -3,7 +3,7 @@
from gptcache.adapter import openai
from gptcache.core import cache, Config
from gptcache.cache.factory import get_ss_data_manager
from gptcache.ranker.simple import SearchDistanceEvaluation
from gptcache.similarity_evaluation.simple import SearchDistanceEvaluation
import numpy as np


Expand All @@ -15,7 +15,7 @@ def mock_embeddings(data, **kwargs):


def run():
sqlite_file = "sqlite.db"
sqlite_file = "gptcache.db"
faiss_file = "faiss.index"
has_data = os.path.isfile(sqlite_file) and os.path.isfile(faiss_file)
data_manager = get_ss_data_manager("sqlite", "faiss",
Expand Down
6 changes: 3 additions & 3 deletions examples/sqlite_faiss_towhee/sqlite_faiss_towhee.py
Original file line number Diff line number Diff line change
Expand Up @@ -4,16 +4,16 @@
from gptcache.adapter import openai
from gptcache.core import cache, Config
from gptcache.cache.factory import get_ss_data_manager
from gptcache.ranker.simple import SearchDistanceEvaluation
from gptcache.encoder import Towhee
from gptcache.similarity_evaluation.simple import SearchDistanceEvaluation
from gptcache.embedding import Towhee


def run():
towhee = Towhee()
# chinese model
# towhee = Towhee(model="uer/albert-base-chinese-cluecorpussmall")

sqlite_file = "sqlite.db"
sqlite_file = "gptcache.db"
faiss_file = "faiss.index"
has_data = os.path.isfile(sqlite_file) and os.path.isfile(faiss_file)
data_manager = get_ss_data_manager("sqlite", "faiss",
Expand Down
4 changes: 2 additions & 2 deletions examples/sqlite_milvus_mock/sqlite_milvus_mock.py
Original file line number Diff line number Diff line change
Expand Up @@ -3,7 +3,7 @@
from gptcache.adapter import openai
from gptcache.core import cache, Config
from gptcache.cache.factory import get_ss_data_manager
from gptcache.ranker.simple import SearchDistanceEvaluation
from gptcache.similarity_evaluation.simple import SearchDistanceEvaluation
import numpy as np


Expand All @@ -15,7 +15,7 @@ def mock_embeddings(data, **kwargs):


def run():
sqlite_file = "sqlite.db"
sqlite_file = "gptcache.db"
has_data = os.path.isfile(sqlite_file)
# milvus
data_manager = get_ss_data_manager("sqlite", "milvus", dimension=d, max_size=8, clean_size=2)
Expand Down
2 changes: 1 addition & 1 deletion gptcache/adapter/adapter.py
Original file line number Diff line number Diff line change
@@ -1,7 +1,7 @@
import logging
import copy
from ..core import cache, time_cal
from ..util.error import NotInitError
from ..utils.error import NotInitError


def adapt(llm_handler, cache_data_convert, update_cache_callback, *args, **kwargs):
Expand Down
2 changes: 1 addition & 1 deletion gptcache/cache/data_manager.py
Original file line number Diff line number Diff line change
Expand Up @@ -7,7 +7,7 @@
from .scalar_data.base import ScalarStorage
from .vector_data.base import VectorBase, ClearStrategy
from .eviction import EvictionManager
from ..util.error import CacheError
from ..utils.error import CacheError


class DataManager(metaclass=ABCMeta):
Expand Down
2 changes: 1 addition & 1 deletion gptcache/cache/factory.py
Original file line number Diff line number Diff line change
@@ -1,7 +1,7 @@
from .data_manager import DataManager, SSDataManager
from .scalar_data import SQLDataBase, SQL_URL
from .vector_data import Milvus, Faiss, Chromadb
from ..util.error import NotFoundStoreError, ParamError
from ..utils.error import NotFoundStoreError, ParamError


def get_data_manager(data_manager_name: str, **kwargs) -> DataManager:
Expand Down
2 changes: 1 addition & 1 deletion gptcache/cache/scalar_data/__init__.py
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
__all__ = ['SQLDataBase', 'SQL_URL']

from gptcache.util.lazy_import import LazyImport
from gptcache.utils.lazy_import import LazyImport

sql_database = LazyImport('milvus', globals(), 'gptcache.cache.scalar_data.sqlalchemy')

Expand Down
2 changes: 1 addition & 1 deletion gptcache/cache/scalar_data/sqlalchemy.py
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
from gptcache.util import import_sqlalchemy
from gptcache.utils import import_sqlalchemy
import_sqlalchemy()

import numpy as np
Expand Down
2 changes: 1 addition & 1 deletion gptcache/cache/vector_data/__init__.py
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
__all__ = ['Milvus', 'Faiss', 'Chromadb']

from gptcache.util.lazy_import import LazyImport
from gptcache.utils.lazy_import import LazyImport

milvus = LazyImport('milvus', globals(), 'gptcache.cache.vector_data.milvus')
faiss = LazyImport('faiss', globals(), 'gptcache.cache.vector_data.faiss')
Expand Down
2 changes: 1 addition & 1 deletion gptcache/cache/vector_data/chroma.py
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
from gptcache.util import import_chromadb
from gptcache.utils import import_chromadb
import_chromadb()

import chromadb
Expand Down
2 changes: 1 addition & 1 deletion gptcache/cache/vector_data/faiss.py
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
from gptcache.util import import_faiss
from gptcache.utils import import_faiss
import_faiss()

import os
Expand Down
2 changes: 1 addition & 1 deletion gptcache/cache/vector_data/milvus.py
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
from gptcache.util import import_pymilvus
from gptcache.utils import import_pymilvus
import_pymilvus()

from uuid import uuid4
Expand Down
8 changes: 4 additions & 4 deletions gptcache/core.py
Original file line number Diff line number Diff line change
Expand Up @@ -3,14 +3,14 @@
import time

import openai
from .encoder.string import to_embeddings as string_embedding
from .embedding.string import to_embeddings as string_embedding
from .cache.data_manager import DataManager
from .cache.factory import get_data_manager
from .processor.post import first
from .processor.pre import last_content
from .ranker.similarity_evaluation import SimilarityEvaluation
from .ranker.string import AbsoluteEvaluation
from .util.error import CacheError
from .similarity_evaluation.similarity_evaluation import SimilarityEvaluation
from .similarity_evaluation.string import AbsoluteEvaluation
from .utils.error import CacheError


def cache_all(*args, **kwargs):
Expand Down
9 changes: 9 additions & 0 deletions gptcache/embedding/__init__.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,9 @@
__all__ = ['Towhee']

from gptcache.utils.lazy_import import LazyImport

towhee = LazyImport('towhee', globals(), 'gptcache.embedding.towhee')


def Towhee(model="paraphrase-albert-small-v2"):
return towhee.Towhee(model)
File renamed without changes.
File renamed without changes.
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
from gptcache.util import import_towhee
from gptcache.utils import import_towhee
import_towhee()

import numpy as np
Expand Down
9 changes: 0 additions & 9 deletions gptcache/encoder/__init__.py

This file was deleted.

9 changes: 0 additions & 9 deletions gptcache/ranker/__init__.py

This file was deleted.

9 changes: 9 additions & 0 deletions gptcache/similarity_evaluation/__init__.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,9 @@
__all__ = ['Towhee']

from gptcache.utils.lazy_import import LazyImport

towhee = LazyImport('towhee', globals(), 'gptcache.similarity_evaluation.towhee')


def Towhee():
return towhee.Towhee()
File renamed without changes.
File renamed without changes.
File renamed without changes.
File renamed without changes.
File renamed without changes.
3 changes: 2 additions & 1 deletion gptcache/util/__init__.py → gptcache/utils/__init__.py
Original file line number Diff line number Diff line change
@@ -1,4 +1,5 @@
__all__ = ['import_pymilvus', 'import_towhee', 'import_faiss']
__all__ = ['import_pymilvus', 'import_towhee',
'import_faiss', 'import_sqlalchemy']

from .dependency_control import prompt_install

Expand Down
File renamed without changes.
File renamed without changes.
File renamed without changes.
2 changes: 1 addition & 1 deletion scripts/manage_conda_env.sh
Original file line number Diff line number Diff line change
Expand Up @@ -22,7 +22,7 @@ if [[ "$1" == "create" ]]; then
conda activate "$env_name"
echo "conda environment '$env_name' activated."
elif [[ "$1" == "remove" ]]; then
conda deactivate "$env_name"
conda deactivate
# 删除 conda 环境
if [[ -n "$2" ]]; then
env_name="$2"
Expand Down
2 changes: 1 addition & 1 deletion scripts/remove_example_cache.sh
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
#!/bin/bash

parent_dir="$(cd "$(dirname "${BASH_SOURCE[0]}")"/.. && pwd)"
find "$parent_dir/examples" \( -path "$parent_dir/examples/benchmark" -path "$parent_dir/examples/sqlite_milvus_mock" \) -prune -o \( -type f \( -name 'data_map*.txt' -or -name 'faiss.index' -or -name 'sqlite.db' \) -delete \)
find "$parent_dir/examples" \( -path "$parent_dir/examples/benchmark" -path "$parent_dir/examples/sqlite_milvus_mock" \) -prune -o \( -type f \( -name 'data_map*.txt' -or -name 'faiss.index' -or -name 'gptcache.db' \) -delete \)
6 changes: 3 additions & 3 deletions tests/integration_tests/test_sqlite_faiss_towhee.py
Original file line number Diff line number Diff line change
Expand Up @@ -4,10 +4,10 @@
from gptcache.adapter import openai
from gptcache.cache.factory import get_ss_data_manager
from gptcache.core import cache, Config
from gptcache.encoder import Towhee
from gptcache.ranker.simple import SearchDistanceEvaluation
from gptcache.embedding import Towhee
from gptcache.similarity_evaluation.simple import SearchDistanceEvaluation

sqlite_file = "sqlite.db"
sqlite_file = "gptcache.db"
faiss_file = "faiss.index"


Expand Down
File renamed without changes.
Empty file added tests/unit_tests/test_core.py
Empty file.

0 comments on commit aa1fe25

Please sign in to comment.