Implement `rank_fields` compatibility with `langchain-cohere` and `BedrockClientV2.rerank`
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
- 1 on this, would be great if we can have it, I also face the same issue today.
Hey, taking a look into this, thanks
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.
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
Closing as stale