Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

CohereAPIError thrown when base retriever returns empty documents in ContextualCompressionRetriever using Cohere Rank #5304

Closed
2 of 14 tasks
hanguofeng opened this issue May 26, 2023 · 2 comments · Fixed by #5306

Comments

@hanguofeng
Copy link
Contributor

System Info

  • 5.19.0-42-generic # 43~22.04.1-Ubuntu SMP PREEMPT_DYNAMIC Fri Apr 21 16:51:08 UTC 2 x86_64 x86_64 x86_64 GNU/Linux
  • langchain==0.0.180
  • Python 3.10.11

Who can help?

No response

Information

  • The official example notebooks/scripts
  • My own modified scripts

Related Components

  • LLMs/Chat Models
  • Embedding Models
  • Prompts / Prompt Templates / Prompt Selectors
  • Output Parsers
  • Document Loaders
  • Vector Stores / Retrievers
  • Memory
  • Agents / Agent Executors
  • Tools / Toolkits
  • Chains
  • Callbacks/Tracing
  • Async

Reproduction

  1. Set up a retriever using any type of retriever (for example, I used Pinecone).
  2. Pass it into the ContextualCompressionRetriever.
  3. If the base retriever returns empty documents,
  4. It throws an error: cohere.error.CohereAPIError: invalid request: list of documents must not be empty

File "/workspaces/example/.venv/lib/python3.10/site-packages/langchain/retrievers/contextual_compression.py", line 37, in get_relevant_documents
compressed_docs = self.base_compressor.compress_documents(docs, query)
File "/workspaces/example/.venv/lib/python3.10/site-packages/langchain/retrievers/document_compressors/cohere_rerank.py", line 57, in compress_documents
results = self.client.rerank(
File "/workspaces/example/.venv/lib/python3.10/site-packages/cohere/client.py", line 633, in rerank
reranking = Reranking(self._request(cohere.RERANK_URL, json=json_body))
File "/workspaces/example/.venv/lib/python3.10/site-packages/cohere/client.py", line 692, in _request
self._check_response(json_response, response.headers, response.status_code)
File "/workspaces/example/.venv/lib/python3.10/site-packages/cohere/client.py", line 642, in _check_response
raise CohereAPIError(
cohere.error.CohereAPIError: invalid request: list of documents must not be empty

Code is Like

 
retriever = vectorstore.as_retriever()
compressor = CohereRerank()
compression_retriever = ContextualCompressionRetriever(
    base_compressor=compressor, base_retriever=retriever
)
return compression_retriever

Expected behavior

no error throws and return empty list

@dev2049
Copy link
Contributor

dev2049 commented May 26, 2023

good catch! happy to review if you want to open a fix, otherwise can get to it this weekend

@dev2049
Copy link
Contributor

dev2049 commented May 26, 2023

ah see you already opened!

hwchase17 pushed a commit that referenced this issue May 28, 2023
…5304) (#5306)

# Fix: Handle empty documents in ContextualCompressionRetriever (Issue
#5304)

Fixes #5304 

Prevent cohere.error.CohereAPIError caused by an empty list of documents
by adding a condition to check if the input documents list is empty in
the compress_documents method. If the list is empty, return an empty
list immediately, avoiding the error and unnecessary processing.

@dev2049

---------

Co-authored-by: Dev 2049 <[email protected]>
Undertone0809 pushed a commit to Undertone0809/langchain that referenced this issue Jun 19, 2023
…angchain-ai#5304) (langchain-ai#5306)

# Fix: Handle empty documents in ContextualCompressionRetriever (Issue
langchain-ai#5304)

Fixes langchain-ai#5304 

Prevent cohere.error.CohereAPIError caused by an empty list of documents
by adding a condition to check if the input documents list is empty in
the compress_documents method. If the list is empty, return an empty
list immediately, avoiding the error and unnecessary processing.

@dev2049

---------

Co-authored-by: Dev 2049 <[email protected]>
This was referenced Jun 25, 2023
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

Successfully merging a pull request may close this issue.

2 participants