infiniflow / ragflow

RAGFlow is an open-source RAG (Retrieval-Augmented Generation) engine based on deep document understanding.
https://ragflow.io
Apache License 2.0
21.52k stars 2.11k forks source link

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

Open wbhob opened 1 week ago

wbhob commented 1 week ago

Is there an existing issue for the same bug?

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 commented 1 week ago

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
        }
Feiue commented 1 week ago

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?

wbhob commented 6 days ago

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

Feiue commented 4 days ago

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

wbhob commented 11 hours ago

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