Skip to content

Commit

Permalink
Correct the wrong search return value in the Redis vector store. (#452)
Browse files Browse the repository at this point in the history
Signed-off-by: SimFG <[email protected]>
  • Loading branch information
SimFG authored Jun 28, 2023
1 parent 66a3a1b commit e43160f
Show file tree
Hide file tree
Showing 3 changed files with 72 additions and 5 deletions.
2 changes: 1 addition & 1 deletion gptcache/manager/vector_data/redis_vectorstore.py
Original file line number Diff line number Diff line change
Expand Up @@ -126,7 +126,7 @@ def search(self, data: np.ndarray, top_k: int = -1):
.search(query, query_params=query_params)
.docs
)
return [(result.score, result.id) for result in results]
return [(float(result.score), int(result.id[len(self.doc_prefix):])) for result in results]

def rebuild(self, ids=None) -> bool:
pass
Expand Down
4 changes: 3 additions & 1 deletion gptcache/utils/dependency_control.py
Original file line number Diff line number Diff line change
Expand Up @@ -8,11 +8,13 @@ def prompt_install(package: str, warn: bool = False): # pragma: no cover
"""
Function used to prompt user to install a package.
"""
cmd = f"pip install {package}"
cmd = f"pip install -q {package}"
try:
if warn and input(f"Install {package}? Y/n: ") != "Y":
raise ModuleNotFoundError(f"No module named {package}")
print(f"start to install package: {package}")
subprocess.check_call(cmd, shell=True)
print(f"successfully installed package: {package}")
gptcache_log.info("%s installed successfully!", package)
except subprocess.CalledProcessError as e:
raise PipInstallError(package) from e
71 changes: 68 additions & 3 deletions tests/unit_tests/manager/test_redis.py
Original file line number Diff line number Diff line change
@@ -1,11 +1,21 @@
import random
from unittest.mock import patch

import numpy as np

from gptcache.manager import VectorBase
from gptcache import Cache
from gptcache.adapter import openai
from gptcache.adapter.api import init_similar_cache
from gptcache.embedding import Onnx
from gptcache.manager import VectorBase, manager_factory
from gptcache.manager.vector_data.base import VectorData
from gptcache.processor.pre import last_content
from gptcache.utils.response import get_message_from_openai_answer


def test_redis_vector_store():
dim = 10
encoder = Onnx()
dim = encoder.dimension
vector_base = VectorBase("redis", dimension=dim)
vector_base.mul_add([VectorData(id=i, data=np.random.rand(dim)) for i in range(10)])

Expand All @@ -21,4 +31,59 @@ def test_redis_vector_store():

search_res = vector_base.search(np.random.rand(dim), top_k=10)
print(search_res)
assert len(search_res) == 5
assert len(search_res) == 5


def test_redis_sqlite():
redis_cache = Cache()
encoder = Onnx()
redis_data_manager = manager_factory(
"sqlite,redis",
data_dir=str(random.random()),
vector_params={"dimension": encoder.dimension},
)
init_similar_cache(
cache_obj=redis_cache,
pre_func=last_content,
embedding=encoder,
data_manager=redis_data_manager,
)
question = "what's github"
expect_answer = "GitHub is an online platform used primarily for version control and coding collaborations."
with patch("openai.ChatCompletion.create") as mock_create:
datas = {
"choices": [
{
"message": {"content": expect_answer, "role": "assistant"},
"finish_reason": "stop",
"index": 0,
}
],
"created": 1677825464,
"id": "chatcmpl-6ptKyqKOGXZT6iQnqiXAH8adNLUzD",
"model": "gpt-3.5-turbo-0301",
"object": "chat.completion.chunk",
}
mock_create.return_value = datas

response = openai.ChatCompletion.create(
model="gpt-3.5-turbo",
messages=[
{"role": "system", "content": "You are a helpful assistant."},
{"role": "user", "content": question},
],
cache_obj=redis_cache,
)

assert get_message_from_openai_answer(response) == expect_answer, response

response = openai.ChatCompletion.create(
model="gpt-3.5-turbo",
messages=[
{"role": "system", "content": "You are a helpful assistant."},
{"role": "user", "content": "can you explain what GitHub is"},
],
cache_obj=redis_cache,
)
answer_text = get_message_from_openai_answer(response)
assert answer_text == expect_answer, answer_text

0 comments on commit e43160f

Please sign in to comment.