Skip to content
This repository has been archived by the owner on Nov 13, 2024. It is now read-only.

Add Cohere Reranker #269

Merged
merged 7 commits into from
Jan 31, 2024
Merged

Conversation

izellevy
Copy link
Collaborator

@izellevy izellevy commented Jan 30, 2024

Problem

We currently do not rerank results. Reranking the results can result in better quality responses.

Solution

Added Cohere reranker

Type of Change

  • Bug fix (non-breaking change which fixes an issue)
  • New feature (non-breaking change which adds functionality)
  • Breaking change (fix or feature that would cause existing functionality to not work as expected)
  • This change requires a documentation update
  • Infrastructure change (CI configs, etc)
  • Non-code change (docs, etc)
  • None of the above: (explain here)

Test Plan

Added the relevant tests.

Copy link
Contributor

@acatav acatav left a comment

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

LGTM, none of my comments are critical

reranked_query_results: List[KBQueryResult] = []
for result in results:
texts = [doc.text for doc in result.documents]
response = self._client.rerank(query=result.query,
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Also needs to be wrapped in try clause.
Transient errors like rate limits etc should be retried (if the Cohere client itself doesn't do that for us already).

Errors that are caused by wrong configuration (like wrong model name or bad API key) need to be re-raised with an actionable error message

Copy link
Collaborator Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Cohere retries internally. Since Cohere does not return different error types it is hard to understand what the message is. For now I am raising a RuntimeError from the actual error.


def test_bad_api_key(should_run_test, query_result):
from cohere import CohereAPIError
with pytest.raises(CohereAPIError, match="invalid api token"):
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

We try to eliminate underlying service's errors like CohereAPIError or OpenAIError, and replace them with actionable error message (like something the user needs to change in the Canopy config, or the explicit env var to set).

In the future we will have our own error types like EncoderError, AuthenticationError etc. In the meantime simply re-raise RuntimeError for all of these cases (the CLI catches RuntimeError and prints them nicely)

Copy link
Collaborator Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I checked the client, client does not return a specific error for different errors we always get a CohereAPIError. For now I am raising RuntimeError from that error, if they improve the client we can write actionable messages.

from cohere import CohereAPIError
with pytest.raises(CohereAPIError, match="invalid api token"):
CohereReranker(api_key="bad key").rerank([query_result])

Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Missing more negative tests - wrong model name, bad input (e.g. not strings) etc.

Copy link
Collaborator Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Added wrong model name, bad input is not possible since we validate our data with pydantic.

Copy link
Contributor

@acatav acatav left a comment

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

LGTM. see the small error

src/canopy/knowledge_base/knowledge_base.py Show resolved Hide resolved
@izellevy izellevy added this pull request to the merge queue Jan 31, 2024
@github-merge-queue github-merge-queue bot removed this pull request from the merge queue due to failed status checks Jan 31, 2024
) for r in results
for d in rr.documents
],
debug_info={"db_result": QueryResult(
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I just relised that we want debug info to be only dicts with literals like str or int. This allows easier serialisation of this object

@izellevy izellevy added this pull request to the merge queue Jan 31, 2024
Merged via the queue into pinecone-io:main with commit 95c7b24 Jan 31, 2024
7 checks passed
@izellevy izellevy deleted the feature/cohere_reranker branch January 31, 2024 16:45
Sign up for free to subscribe to this conversation on GitHub. Already have an account? Sign in.
Labels
None yet
Projects
None yet
Development

Successfully merging this pull request may close these issues.

3 participants