cohere-python icon indicating copy to clipboard operation
cohere-python copied to clipboard

Implement `rank_fields` compatibility with `langchain-cohere` and `BedrockClientV2.rerank`

Open btiernay opened this issue 1 year ago • 4 comments

Describe the improvement

Support compatibility with langchain-cohere for rerank with AWS Bedrock by adding rank_fields parameter support to cohere.BedrockClientV2.rerank . Currently I have to do the following hack to get this to work, which removes anything that isn't in the formal method signature:

    # HACK: Override the `rerank` method to remove any unsupported parameters in this version of LangChain.
    cohere.BedrockClientV2.rerank = (lambda original:
                                     lambda self, *args, **kwargs: original(
                                         self, *args,
                                         **{k: v for k, v in kwargs.items() if
                                            k in inspect.signature(original).parameters}
                                     )
                                     )(cohere.BedrockClientV2.rerank)

Without it, I get:

...
  File ./langchain_cohere/rerank.py", line 123, in compress_documents
    for res in self.rerank(documents, query):
               ~~~~~~~~~~~^^^^^^^^^^^^^^^^^^
  File "./langchain_cohere/rerank.py", line 90, in rerank
    results = self.client.rerank(
        query=query,
    ...<4 lines>...
        max_chunks_per_doc=max_chunks_per_doc,
    )
TypeError: V2Client.rerank() got an unexpected keyword argument 'rank_fields'

cohere.BedrockClientV2 is required in my case because its the only client that supports rerank. See https://github.com/langchain-ai/langchain-cohere/pull/12 for additional context.

Code snippet of expected outcome

Simply use cohere.BedrockClientV2

btiernay avatar Jan 06 '25 16:01 btiernay

  • 1 on this, would be great if we can have it, I also face the same issue today.

Gungz avatar Jan 13 '25 11:01 Gungz

Hey, taking a look into this, thanks

billytrend-cohere avatar Jan 17 '25 15:01 billytrend-cohere

Hi, adding my voice to request this. Supporting it would be best, gracefully handling the field (even if it doesn't get used) would be a minimum.

ldorigo avatar Feb 12 '25 10:02 ldorigo

Hey @ldorigo and @Gungz can you please elaborate: a) what model you're hitting on bedrock b) what langchain code snippet results in an error?

The langchain implementation of Rerank does have support for rank_fields https://github.com/langchain-ai/langchain-cohere/blob/8d78a17ba365cefa67996962316d4c13d39567d5/libs/cohere/langchain_cohere/rerank.py#L99

mkozakov avatar May 06 '25 01:05 mkozakov

Closing as stale

mkozakov avatar Jun 16 '25 22:06 mkozakov