-
Notifications
You must be signed in to change notification settings - Fork 510
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Signed-off-by: SimFG <[email protected]>
- Loading branch information
Showing
3 changed files
with
78 additions
and
78 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
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,31 +1,31 @@ | ||
from unittest.mock import patch | ||
# from unittest.mock import patch | ||
|
||
from gptcache.embedding import FastText | ||
# from gptcache.embedding import FastText | ||
|
||
from gptcache.utils import import_fasttext | ||
from gptcache.adapter.api import _get_model | ||
# from gptcache.utils import import_fasttext | ||
# from gptcache.adapter.api import _get_model | ||
|
||
import_fasttext() | ||
# import_fasttext() | ||
|
||
import fasttext | ||
# import fasttext | ||
|
||
|
||
def test_embedding(): | ||
with patch("fasttext.util.download_model") as download_model_mock: | ||
download_model_mock.return_value = "fastttext.bin" | ||
with patch("fasttext.load_model") as load_model_mock: | ||
load_model_mock.return_value = fasttext.FastText._FastText() | ||
with patch("fasttext.util.reduce_model") as reduce_model_mock: | ||
reduce_model_mock.return_value = None | ||
with patch("fasttext.FastText._FastText.get_dimension") as dimension_mock: | ||
dimension_mock.return_value = 128 | ||
with patch("fasttext.FastText._FastText.get_sentence_vector") as vector_mock: | ||
vector_mock.return_value = [0] * 128 | ||
# def test_embedding(): | ||
# with patch("fasttext.util.download_model") as download_model_mock: | ||
# download_model_mock.return_value = "fastttext.bin" | ||
# with patch("fasttext.load_model") as load_model_mock: | ||
# load_model_mock.return_value = fasttext.FastText._FastText() | ||
# with patch("fasttext.util.reduce_model") as reduce_model_mock: | ||
# reduce_model_mock.return_value = None | ||
# with patch("fasttext.FastText._FastText.get_dimension") as dimension_mock: | ||
# dimension_mock.return_value = 128 | ||
# with patch("fasttext.FastText._FastText.get_sentence_vector") as vector_mock: | ||
# vector_mock.return_value = [0] * 128 | ||
|
||
ft = FastText(dim=128) | ||
assert len(ft.to_embeddings("foo")) == 128 | ||
assert ft.dimension == 128 | ||
# ft = FastText(dim=128) | ||
# assert len(ft.to_embeddings("foo")) == 128 | ||
# assert ft.dimension == 128 | ||
|
||
ft1 = _get_model("fasttext", model_config={"dim": 128}) | ||
assert len(ft1.to_embeddings("foo")) == 128 | ||
assert ft1.dimension == 128 | ||
# ft1 = _get_model("fasttext", model_config={"dim": 128}) | ||
# assert len(ft1.to_embeddings("foo")) == 128 | ||
# assert ft1.dimension == 128 |
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,61 +1,61 @@ | ||
import unittest | ||
import numpy as np | ||
# import unittest | ||
# import numpy as np | ||
|
||
from gptcache.manager.vector_data import VectorBase | ||
from gptcache.manager.vector_data.base import VectorData | ||
# from gptcache.manager.vector_data import VectorBase | ||
# from gptcache.manager.vector_data.base import VectorData | ||
|
||
|
||
class TestWeaviateDB(unittest.TestCase): | ||
def test_normal(self): | ||
size = 1000 | ||
dim = 512 | ||
top_k = 10 | ||
class_name = "Vectorcache" | ||
# class TestWeaviateDB(unittest.TestCase): | ||
# def test_normal(self): | ||
# size = 1000 | ||
# dim = 512 | ||
# top_k = 10 | ||
# class_name = "Vectorcache" | ||
|
||
db = VectorBase( | ||
"weaviate", | ||
class_name=class_name, | ||
top_k=top_k | ||
) | ||
# db = VectorBase( | ||
# "weaviate", | ||
# class_name=class_name, | ||
# top_k=top_k | ||
# ) | ||
|
||
created_class_name = db._create_class() | ||
self.assertEqual(class_name, created_class_name) | ||
data = np.random.randn(size, dim).astype(np.float32) | ||
db.mul_add([VectorData(id=i, data=v) for v, i in zip(data, range(size))]) | ||
self.assertEqual(len(db.search(data[0])), top_k) | ||
db.mul_add([VectorData(id=size, data=data[0])]) | ||
ret = db.search(data[0]) | ||
self.assertIn(ret[0][1], [0, size]) | ||
db.delete([0, 1, 2, 3, 4, 5, size]) | ||
ret = db.search(data[0]) | ||
self.assertNotIn(ret[0][1], [0, size]) | ||
db.rebuild() | ||
db.update_embeddings(6, data[7]) | ||
emb = db.get_embeddings(6) | ||
self.assertEqual(emb.tolist(), data[7].tolist()) | ||
emb = db.get_embeddings(0) | ||
self.assertIsNone(emb) | ||
db.close() | ||
# created_class_name = db._create_class() | ||
# self.assertEqual(class_name, created_class_name) | ||
# data = np.random.randn(size, dim).astype(np.float32) | ||
# db.mul_add([VectorData(id=i, data=v) for v, i in zip(data, range(size))]) | ||
# self.assertEqual(len(db.search(data[0])), top_k) | ||
# db.mul_add([VectorData(id=size, data=data[0])]) | ||
# ret = db.search(data[0]) | ||
# self.assertIn(ret[0][1], [0, size]) | ||
# db.delete([0, 1, 2, 3, 4, 5, size]) | ||
# ret = db.search(data[0]) | ||
# self.assertNotIn(ret[0][1], [0, size]) | ||
# db.rebuild() | ||
# db.update_embeddings(6, data[7]) | ||
# emb = db.get_embeddings(6) | ||
# self.assertEqual(emb.tolist(), data[7].tolist()) | ||
# emb = db.get_embeddings(0) | ||
# self.assertIsNone(emb) | ||
# db.close() | ||
|
||
custom_class_name = "Customcache" | ||
class_schema = { | ||
"class": custom_class_name, | ||
"description": "LLM response cache", | ||
"properties": [ | ||
{ | ||
"name": "data_id", | ||
"dataType": ["int"], | ||
"description": "The data-id generated by GPTCache for vectors.", | ||
} | ||
], | ||
"vectorIndexConfig": {"distance": "cosine"}, | ||
} | ||
# custom_class_name = "Customcache" | ||
# class_schema = { | ||
# "class": custom_class_name, | ||
# "description": "LLM response cache", | ||
# "properties": [ | ||
# { | ||
# "name": "data_id", | ||
# "dataType": ["int"], | ||
# "description": "The data-id generated by GPTCache for vectors.", | ||
# } | ||
# ], | ||
# "vectorIndexConfig": {"distance": "cosine"}, | ||
# } | ||
|
||
db = VectorBase( | ||
"weaviate", | ||
class_schema=class_schema, | ||
top_k=top_k | ||
) | ||
created_class_name = db._create_class() | ||
self.assertEqual(custom_class_name, created_class_name) | ||
db.close() | ||
# db = VectorBase( | ||
# "weaviate", | ||
# class_schema=class_schema, | ||
# top_k=top_k | ||
# ) | ||
# created_class_name = db._create_class() | ||
# self.assertEqual(custom_class_name, created_class_name) | ||
# db.close() |