zilliztech / GPTCache

Semantic cache for LLMs. Fully integrated with LangChain and llama_index.
https://gptcache.readthedocs.io
MIT License
7.18k stars 502 forks source link

NumpyNormEvaluation numpy error fix #502

Closed keenborder786 closed 1 year ago

keenborder786 commented 1 year ago

When I ran the following code:

## Embedding Function
embeddings = OpenAIEmbeddings(model="text-embedding-ada-002")
encoder = LangChain(embeddings=embeddings)

## Data Manager
redis_cache_storage = CacheBase(name="redis", redis_host="localhost", redis_port=6379)
redis_vector_storage = VectorBase(
    name="redis", host="localhost", port=6379, dimension=1536
)
data_manager = get_data_manager(redis_cache_storage, redis_vector_storage)

init_similar_cache(
    embedding=encoder,
    data_manager=data_manager,
    evaluation=NumpyNormEvaluation(),
    data_dir=f"similar_cache",
    config=Config(similarity_threshold=0.2),
)

put("apple", "boy")
print(get("appl"))

I was getting the following error:


Traceback (most recent call last):
  File "/home/mohtashimkhan/GPTCache/test.py", line 36, in <module>
    print(get("appl"))
  File "/home/mohtashimkhan/GPTCache/gptcache/adapter/api.py", line 124, in get
    res = adapt(
  File "/home/mohtashimkhan/GPTCache/gptcache/adapter/adapter.py", line 158, in adapt
    rank = time_cal(
  File "/home/mohtashimkhan/GPTCache/gptcache/utils/time.py", line 9, in inner
    res = func(*args, **kwargs)
  File "/home/mohtashimkhan/GPTCache/gptcache/similarity_evaluation/np.py", line 71, in evaluation
    if 'embedding' not in src_dict or 'embedding' not in cache_dict or not src_dict['embedding'] or not cache_dict['embedding']:
ValueError: The truth value of an array with more than one element is ambiguous. Use a.any() or a.all()

Numpy was raising a ValueError because the not operator with a numpy array was not reducing to a single boolean value. Therefore, I decided to explicitly check if the array is None, which resolved the issue.

sre-ci-robot commented 1 year ago

[APPROVALNOTIFIER] This PR is NOT APPROVED

This pull-request has been approved by: keenborder786 To complete the pull request process, please assign xiaofan-luan after the PR has been reviewed. You can assign the PR to them by writing /assign @xiaofan-luan in a comment when ready.

The full list of commands accepted by this bot can be found here.

Needs approval from an approver in each of these files: - **[OWNERS](https://github.com/zilliztech/GPTCache/blob/main/OWNERS)** Approvers can indicate their approval by writing `/approve` in a comment Approvers can cancel approval by writing `/approve cancel` in a comment
sre-ci-robot commented 1 year ago

Welcome @keenborder786! It looks like this is your first PR to zilliztech/GPTCache 🎉

keenborder786 commented 1 year ago

/assign @xiaofan-luan

SimFG commented 1 year ago

@keenborder786 thanks for your pr. please make the dev branch as the target branch

keenborder786 commented 1 year ago

@SimFG ok