ragflow icon indicating copy to clipboard operation
ragflow copied to clipboard

[Bug]: intractable error: TypeError: only length-1 arrays can be converted to Python scalars /retrieval

Open wbhob opened this issue 1 year ago • 7 comments

Is there an existing issue for the same bug?

  • [X] I have checked the existing issues.

Branch name

main

Commit ID

2a614e0

Other environment information

MacOS 15.0.1 Sequoia
M3 Max, 128 GB RAM, 2TB disk

Actual behavior

Attempting to use /retrieval results in TypeError: only length-1 arrays can be converted to Python scalars. I am providing only question, datasets, and documents. I added debug logs and got the following stack trace below.

Expected behavior

Expected results of query. Everything is showing as ingested successfully.

Steps to reproduce

1. Install the docker container
2. Upload documents to one dataset
3. Start parse via API and wait to complete
4. Call /api/v1/retrieval with a question, that dataset id, and the list of document ids
5. Unknown error

Additional information

2024-10-23 16:48:57 [ERROR] [2024-10-23 16:48:57,071] [doc.retrieval_test] [line:651]: Error during retrieval: only length-1 arrays can be converted to Python scalars
2024-10-23 16:48:57 Traceback (most recent call last):
2024-10-23 16:48:57   File "/ragflow/api/apps/sdk/doc.py", line 623, in retrieval_test
2024-10-23 16:48:57     ranks = retr.retrieval(question, embd_mdl, kb.tenant_id, kb_ids, page, size,
2024-10-23 16:48:57             ^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^
2024-10-23 16:48:57   File "/ragflow/rag/nlp/search.py", line 374, in retrieval
2024-10-23 16:48:57     sres = self.search(req, index_name(tenant_id), embd_mdl, highlight)
2024-10-23 16:48:57            ^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^
2024-10-23 16:48:57   File "/ragflow/rag/nlp/search.py", line 129, in search
2024-10-23 16:48:57     s["knn"] = self._vector(
2024-10-23 16:48:57                ^^^^^^^^^^^^^
2024-10-23 16:48:57   File "/ragflow/rag/nlp/search.py", line 64, in _vector
2024-10-23 16:48:57     "query_vector": [float(v) for v in qv]
2024-10-23 16:48:57                      ^^^^^^^^
2024-10-23 16:48:57 TypeError: only length-1 arrays can be converted to Python scalars
2024-10-23 16:48:57 [INFO] [2024-10-23 16:48:57,073] [_internal._log] [line:97]: 172.19.0.2 - - [23/Oct/2024 16:48:57] "POST /api/v1/retrieval HTTP/1.1" 200 -         

wbhob avatar Oct 23 '24 23:10 wbhob

I was able to patch this on my local version by flattening the array in rag/nlp/search.py:57-67

    def _vector(self, txt, emb_mdl, sim=0.8, topk=10):
        qv, c = emb_mdl.encode_queries(txt)
        # Ensure qv is a 1-dimensional array
        qv_flat = np.array(qv).flatten()
        return {
            "field": f"q_{len(qv_flat)}_vec",
            "k": topk,
            "similarity": sim,
            "num_candidates": topk * 2,
            "query_vector": qv_flat.tolist()  # Convert numpy array to list
        }
        ```

wbhob avatar Oct 24 '24 00:10 wbhob

image I could run it correctly. The Docker code is behind the code on GitHub, which may be the reason for this error. Or can I take a look at your request operation?

Feiue avatar Oct 24 '24 05:10 Feiue

I am sending my request the exact same way. Are you using the Cohere reranker? Not sure if that matters. But yes I was using the docker container, but even after trying to build it from main branch I was having issues

wbhob avatar Oct 25 '24 20:10 wbhob

I don't use any rerank model, maybe it's the reason for the problem. I will test it.

Feiue avatar Oct 28 '24 03:10 Feiue

Just tried with the latest dev container and v0.13, still having this problem. How do you remove the rerank model? I am using OpenAI for inference but want to turn off reranking for the time being if it is causing problems

wbhob avatar Oct 31 '24 19:10 wbhob

I'm running against the same problem, v0.13 :dev running Docker. Is it related to a rerank model?

wardnz avatar Nov 05 '24 13:11 wardnz

Appears to be. In my fresh install I didn't set a reranker and it seems to work better.

wbhob avatar Nov 05 '24 20:11 wbhob