-
Notifications
You must be signed in to change notification settings - Fork 16k
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
langchain/fix-cohere-reranker-rerank-method #19486
langchain/fix-cohere-reranker-rerank-method #19486
Conversation
The latest updates on your projects. Learn more about Vercel for Git ↗︎ 1 Ignored Deployment
|
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
This issue seems to be due to the changes made to Cohere API since v5:
cohere.Client.rerank
now expects named parameters- The return value is now a
RerankResponse
class, which doesn't implement__iter__
.
I recommend updating cohere
's version in the requirements though (from v4 to v5).
@mspronesti What do you recommend to be the next steps for this PR? The changes I made handle the changes in the Cohere API. |
This is a blocking issue for using Rerank. And it was working previously: #19461 Can we get this merged or another solution? |
@jjovalle99 Only to update Cohere's version in the |
@mspronesti @baskaryan Cohere version updated from @^4 to @^5. Let me know if something else is needed. |
the cohere team is actually working on splitting all cohere integrations into their own partner package, and in the process updating them to have cohere v5 support! see #19049. the classes in langchain-community will be deprecated in favor of the ones in langchain-cohere |
Ok, thanks for the update, @baskaryan! Will switching to langchain-cohere now fix this issue? |
@baskaryan the https://github.com/langchain-ai/langchain/blob/master/libs/langchain/pyproject.toml#L39 cohere version here still needs to be updated from 4 to 5... as referenced above by @mspronesti. |
This fix looks good to me, will copy it into our partner repo, thanks @jjovalle99 and folks |
Awesome! willing to help if something comes up @billytrend-cohere |
Created the pr, we can probably merge this PR as well but recommend you use our partner package now |
Sounds great! |
libs/langchain/langchain/retrievers/document_compressors/cohere_rerank.py
Outdated
Show resolved
Hide resolved
Fix cohere rerank inspired by #19486
@baskaryan Done! |
how should i change if i'm using CohereRerank() in ipynb |
@AndreaEr I would say that you should now use everything from the Cohere partner library So using |
Fix cohere rerank inspired by langchain-ai#19486
…angchain-ai#19486) #### Description Fixed the following error with `rerank` method from `CohereRerank`: ``` ---> [79](https://vscode-remote+wsl-002bubuntu.vscode-resource.vscode-cdn.net/home/jjmov99/legal-colombia/~/legal-colombia/.venv/lib/python3.11/site-packages/langchain/retrievers/document_compressors/cohere_rerank.py:79) results = self.client.rerank( [80](https://vscode-remote+wsl-002bubuntu.vscode-resource.vscode-cdn.net/home/jjmov99/legal-colombia/~/legal-colombia/.venv/lib/python3.11/site-packages/langchain/retrievers/document_compressors/cohere_rerank.py:80) query, docs, model, top_n=top_n, max_chunks_per_doc=max_chunks_per_doc [81](https://vscode-remote+wsl-002bubuntu.vscode-resource.vscode-cdn.net/home/jjmov99/legal-colombia/~/legal-colombia/.venv/lib/python3.11/site-packages/langchain/retrievers/document_compressors/cohere_rerank.py:81) ) [82](https://vscode-remote+wsl-002bubuntu.vscode-resource.vscode-cdn.net/home/jjmov99/legal-colombia/~/legal-colombia/.venv/lib/python3.11/site-packages/langchain/retrievers/document_compressors/cohere_rerank.py:82) result_dicts = [] [83](https://vscode-remote+wsl-002bubuntu.vscode-resource.vscode-cdn.net/home/jjmov99/legal-colombia/~/legal-colombia/.venv/lib/python3.11/site-packages/langchain/retrievers/document_compressors/cohere_rerank.py:83) for res in results.results: TypeError: BaseCohere.rerank() takes 1 positional argument but 4 positional arguments (and 2 keyword-only arguments) were given ``` This was easily fixed going from this: ``` def rerank( self, documents: Sequence[Union[str, Document, dict]], query: str, *, model: Optional[str] = None, top_n: Optional[int] = -1, max_chunks_per_doc: Optional[int] = None, ) -> List[Dict[str, Any]]: ... if len(documents) == 0: # to avoid empty api call return [] docs = [ doc.page_content if isinstance(doc, Document) else doc for doc in documents ] model = model or self.model top_n = top_n if (top_n is None or top_n > 0) else self.top_n results = self.client.rerank( query, docs, model, top_n=top_n, max_chunks_per_doc=max_chunks_per_doc ) result_dicts = [] for res in results: result_dicts.append( {"index": res.index, "relevance_score": res.relevance_score} ) return result_dicts ``` to this: ``` def rerank( self, documents: Sequence[Union[str, Document, dict]], query: str, *, model: Optional[str] = None, top_n: Optional[int] = -1, max_chunks_per_doc: Optional[int] = None, ) -> List[Dict[str, Any]]: ... if len(documents) == 0: # to avoid empty api call return [] docs = [ doc.page_content if isinstance(doc, Document) else doc for doc in documents ] model = model or self.model top_n = top_n if (top_n is None or top_n > 0) else self.top_n results = self.client.rerank( query=query, documents=docs, model=model, top_n=top_n, max_chunks_per_doc=max_chunks_per_doc <------------- ) result_dicts = [] for res in results.results: <------------- result_dicts.append( {"index": res.index, "relevance_score": res.relevance_score} ) return result_dicts ``` #### Unit & Integration tests I added a unit test to check the behaviour of `rerank`. Also fixed the original integration test which was failing. #### Format & Linting Everything worked properly with `make lint_diff`, `make format_diff` and `make format`. However I noticed an error coming from other part of the library when doing `make lint`: ``` (langchain-py3.9) ➜ langchain git:(master) make format [ "." = "" ] || poetry run ruff format . 1636 files left unchanged [ "." = "" ] || poetry run ruff --select I --fix . (langchain-py3.9) ➜ langchain git:(master) make lint ./scripts/check_pydantic.sh . ./scripts/lint_imports.sh poetry run ruff . [ "." = "" ] || poetry run ruff format . --diff 1636 files already formatted [ "." = "" ] || poetry run ruff --select I . [ "." = "" ] || mkdir -p .mypy_cache && poetry run mypy . --cache-dir .mypy_cache langchain/agents/openai_assistant/base.py:252: error: Argument "file_ids" to "create" of "Assistants" has incompatible type "Optional[Any]"; expected "Union[list[str], NotGiven]" [arg-type] langchain/agents/openai_assistant/base.py:374: error: Argument "file_ids" to "create" of "AsyncAssistants" has incompatible type "Optional[Any]"; expected "Union[list[str], NotGiven]" [arg-type] Found 2 errors in 1 file (checked 1634 source files) make: *** [Makefile:65: lint] Error 1 ``` --------- Co-authored-by: Bagatur <[email protected]> Co-authored-by: Bagatur <[email protected]>
Fix cohere rerank inspired by langchain-ai/langchain#19486
Fix cohere rerank inspired by #19486
…19486) #### Description Fixed the following error with `rerank` method from `CohereRerank`: ``` ---> [79](https://vscode-remote+wsl-002bubuntu.vscode-resource.vscode-cdn.net/home/jjmov99/legal-colombia/~/legal-colombia/.venv/lib/python3.11/site-packages/langchain/retrievers/document_compressors/cohere_rerank.py:79) results = self.client.rerank( [80](https://vscode-remote+wsl-002bubuntu.vscode-resource.vscode-cdn.net/home/jjmov99/legal-colombia/~/legal-colombia/.venv/lib/python3.11/site-packages/langchain/retrievers/document_compressors/cohere_rerank.py:80) query, docs, model, top_n=top_n, max_chunks_per_doc=max_chunks_per_doc [81](https://vscode-remote+wsl-002bubuntu.vscode-resource.vscode-cdn.net/home/jjmov99/legal-colombia/~/legal-colombia/.venv/lib/python3.11/site-packages/langchain/retrievers/document_compressors/cohere_rerank.py:81) ) [82](https://vscode-remote+wsl-002bubuntu.vscode-resource.vscode-cdn.net/home/jjmov99/legal-colombia/~/legal-colombia/.venv/lib/python3.11/site-packages/langchain/retrievers/document_compressors/cohere_rerank.py:82) result_dicts = [] [83](https://vscode-remote+wsl-002bubuntu.vscode-resource.vscode-cdn.net/home/jjmov99/legal-colombia/~/legal-colombia/.venv/lib/python3.11/site-packages/langchain/retrievers/document_compressors/cohere_rerank.py:83) for res in results.results: TypeError: BaseCohere.rerank() takes 1 positional argument but 4 positional arguments (and 2 keyword-only arguments) were given ``` This was easily fixed going from this: ``` def rerank( self, documents: Sequence[Union[str, Document, dict]], query: str, *, model: Optional[str] = None, top_n: Optional[int] = -1, max_chunks_per_doc: Optional[int] = None, ) -> List[Dict[str, Any]]: ... if len(documents) == 0: # to avoid empty api call return [] docs = [ doc.page_content if isinstance(doc, Document) else doc for doc in documents ] model = model or self.model top_n = top_n if (top_n is None or top_n > 0) else self.top_n results = self.client.rerank( query, docs, model, top_n=top_n, max_chunks_per_doc=max_chunks_per_doc ) result_dicts = [] for res in results: result_dicts.append( {"index": res.index, "relevance_score": res.relevance_score} ) return result_dicts ``` to this: ``` def rerank( self, documents: Sequence[Union[str, Document, dict]], query: str, *, model: Optional[str] = None, top_n: Optional[int] = -1, max_chunks_per_doc: Optional[int] = None, ) -> List[Dict[str, Any]]: ... if len(documents) == 0: # to avoid empty api call return [] docs = [ doc.page_content if isinstance(doc, Document) else doc for doc in documents ] model = model or self.model top_n = top_n if (top_n is None or top_n > 0) else self.top_n results = self.client.rerank( query=query, documents=docs, model=model, top_n=top_n, max_chunks_per_doc=max_chunks_per_doc <------------- ) result_dicts = [] for res in results.results: <------------- result_dicts.append( {"index": res.index, "relevance_score": res.relevance_score} ) return result_dicts ``` #### Unit & Integration tests I added a unit test to check the behaviour of `rerank`. Also fixed the original integration test which was failing. #### Format & Linting Everything worked properly with `make lint_diff`, `make format_diff` and `make format`. However I noticed an error coming from other part of the library when doing `make lint`: ``` (langchain-py3.9) ➜ langchain git:(master) make format [ "." = "" ] || poetry run ruff format . 1636 files left unchanged [ "." = "" ] || poetry run ruff --select I --fix . (langchain-py3.9) ➜ langchain git:(master) make lint ./scripts/check_pydantic.sh . ./scripts/lint_imports.sh poetry run ruff . [ "." = "" ] || poetry run ruff format . --diff 1636 files already formatted [ "." = "" ] || poetry run ruff --select I . [ "." = "" ] || mkdir -p .mypy_cache && poetry run mypy . --cache-dir .mypy_cache langchain/agents/openai_assistant/base.py:252: error: Argument "file_ids" to "create" of "Assistants" has incompatible type "Optional[Any]"; expected "Union[list[str], NotGiven]" [arg-type] langchain/agents/openai_assistant/base.py:374: error: Argument "file_ids" to "create" of "AsyncAssistants" has incompatible type "Optional[Any]"; expected "Union[list[str], NotGiven]" [arg-type] Found 2 errors in 1 file (checked 1634 source files) make: *** [Makefile:65: lint] Error 1 ``` --------- Co-authored-by: Bagatur <[email protected]> Co-authored-by: Bagatur <[email protected]>
thx, thats help me a lot |
Description
Fixed the following error with
rerank
method fromCohereRerank
:This was easily fixed going from this:
to this:
Unit & Integration tests
I added a unit test to check the behaviour of
rerank
. Also fixed the original integration test which was failing.Format & Linting
Everything worked properly with
make lint_diff
,make format_diff
andmake format
. However I noticed an error coming from other part of the library when doingmake lint
: