Skip to content

Commit

Permalink
Don't rerank empty docs
Browse files Browse the repository at this point in the history
  • Loading branch information
collindutter committed Sep 6, 2024
1 parent dc569b3 commit 6480784
Show file tree
Hide file tree
Showing 3 changed files with 17 additions and 1 deletion.
1 change: 1 addition & 0 deletions CHANGELOG.md
Original file line number Diff line number Diff line change
Expand Up @@ -30,6 +30,7 @@ and this project adheres to [Semantic Versioning](https://semver.org/spec/v2.0.0
- Missing `maxTokens` inference parameter in `AmazonBedrockPromptDriver`.
- Incorrect model in `OpenAiDriverConfig`'s `text_to_speech_driver`.
- Crash when using `CohereRerankDriver` with `CsvRowArtifact`s.
- Crash when passing "empty" Artifacts to `CohereRerankDriver`.


## [0.30.2] - 2024-08-26
Expand Down
4 changes: 3 additions & 1 deletion griptape/drivers/rerank/cohere_rerank_driver.py
Original file line number Diff line number Diff line change
Expand Up @@ -24,7 +24,9 @@ class CohereRerankDriver(BaseRerankDriver):
)

def run(self, query: str, artifacts: list[TextArtifact]) -> list[TextArtifact]:
artifacts_dict = {str(hash(a.to_text())): a for a in artifacts}
# Cohere errors out if passed "empty" documents
artifacts_dict = {str(hash(a.to_text())): a for a in artifacts if a}

response = self.client.rerank(
model=self.model,
query=query,
Expand Down
13 changes: 13 additions & 0 deletions tests/unit/drivers/rerank/test_cohere_rerank_driver.py
Original file line number Diff line number Diff line change
Expand Up @@ -20,8 +20,21 @@ def mock_client(self, mocker):

return mock_client

@pytest.fixture()
def mock_empty_client(self, mocker):
mock_client = mocker.patch("cohere.Client").return_value
mock_client.rerank.return_value.results = []

return mock_client

def test_run(self, mock_client):
driver = CohereRerankDriver(api_key="api-key")
result = driver.run("hello", artifacts=[TextArtifact("foo"), TextArtifact("bar")])

assert len(result) == 2

def test_run_empty_artifacts(self, mock_empty_client):
driver = CohereRerankDriver(api_key="api-key")
result = driver.run("hello", artifacts=[TextArtifact(""), TextArtifact(" ")])

assert len(result) == 0

0 comments on commit 6480784

Please sign in to comment.