-
Notifications
You must be signed in to change notification settings - Fork 2.1k
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
EvaluationSetClient for deepset cloud to fetch evaluation sets and la… #2345
Merged
Merged
Changes from 8 commits
Commits
Show all changes
37 commits
Select commit
Hold shift + click to select a range
d0106db
EvaluationSetClient for deepset cloud to fetch evaluation sets and la…
FHardow 382c2fb
make DeepsetCloudDocumentStore able to fetch uploaded evaluation set …
FHardow 45fcf05
fix missing renaming of get_evaluation_set_names in DeepsetCloudDocum…
FHardow 1534666
update documentation for evaluation set functionality in deepset clou…
FHardow 3f559f0
DeepsetCloudDocumentStore tests for evaluation set functionality
FHardow 21de609
rename index to evaluation_set_name for DeepsetCloudDocumentStore eva…
FHardow 7168807
raise DeepsetCloudError when no labels were found for evaluation set
FHardow 8e68bff
make use of .get_with_auto_paging in EvaluationSetClient
FHardow dc16792
Return result of get_with_auto_paging() as it parses the response alr…
FHardow 2d5219b
Make schema import source more specific
FHardow 9229110
fetch all evaluation sets for a workspace in deepset Cloud
FHardow 164b8ec
Rename evaluation_set_name to label_index
FHardow ab22631
make use of generator functionality for fetching labels
FHardow 68d7fbb
Update Documentation & Code Style
github-actions[bot] fdefaa5
Adjust function input for DeepsetCloudDocumentStore.get_all_labels, a…
FHardow 4487b53
Merge branch 'feature/fetch-evaluation-set-from-dc' of github.com:dee…
FHardow 56bdb6c
Match error message with pytest.raises
FHardow 23bee8d
Update Documentation & Code Style
github-actions[bot] e2154b2
DeepsetCloudDocumentStore.get_labels_count raises DeepsetCloudError w…
FHardow ab138a3
Merge branch 'feature/fetch-evaluation-set-from-dc' of github.com:dee…
FHardow 6b6b8bf
remove unneeded import in tests
FHardow 9604991
DeepsetCloudDocumentStore tests, make reponse bodies a string through…
FHardow 86ee3f2
Merge branch 'master' of github.com:deepset-ai/haystack into feature/…
FHardow f3b03bc
DeepsetcloudDocumentStore.get_label_count - move raise to return
FHardow 2e7059c
stringify uuid before json.dump as uuid is not serilizable
FHardow c75d5e6
DeepsetcloudDocumentStore - adjust response mocking in tests
FHardow 5f3e4bb
DeepsetcloudDocumentStore - json dump response body in test
FHardow 32b901a
DeepsetCloudDocumentStore introduce label_index, EvaluationSetClient …
FHardow 2553129
Update Documentation & Code Style
github-actions[bot] eb1054d
DeepsetCloudDocumentStore rename evaluation_set to evaluation_set_res…
FHardow 805b9ca
Merge branch 'feature/fetch-evaluation-set-from-dc' of github.com:dee…
FHardow f539c7c
DeepsetCloudDocumentStore - rename missed variable in test
FHardow a8b33aa
DeepsetCloudDocumentStore - rename missed label_index to index in doc…
FHardow a93e7f3
Update Documentation & Code Style
github-actions[bot] 10eff9c
DeepsetCloudDocumentStore - update docstrings for EvaluationSetClient
FHardow 6638585
Merge branch 'feature/fetch-evaluation-set-from-dc' of github.com:dee…
FHardow 709ac21
DeepsetCloudDocumentStore - fix typo in doc string
FHardow File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
|
@@ -5,6 +5,8 @@ | |
import time | ||
from typing import Any, Dict, Generator, List, Optional, Tuple, Union | ||
|
||
from haystack import Label, Document, Answer | ||
|
||
try: | ||
from typing import Literal | ||
except ImportError: | ||
|
@@ -635,6 +637,115 @@ def _build_workspace_url(self, workspace: Optional[str] = None): | |
return self.client.build_workspace_url(workspace) | ||
|
||
|
||
class EvaluationSetClient: | ||
def __init__( | ||
self, client: DeepsetCloudClient, workspace: Optional[str] = None, evaluation_set_name: Optional[str] = None | ||
): | ||
""" | ||
A client to communicate with Deepset Cloud evaluation sets and labels. | ||
|
||
:param client: Deepset Cloud client | ||
:param workspace: workspace in Deepset Cloud | ||
:param evaluation_set_name: name of the evaluation set | ||
|
||
""" | ||
self.client = client | ||
self.workspace = workspace | ||
self.evaluation_set_name = evaluation_set_name | ||
|
||
def get_labels(self, evaluation_set_name: str, workspace: Optional[str] = None) -> List[Label]: | ||
""" | ||
Searches for labels for a given evaluation set in deepset cloud. Returns a list of all found labels. | ||
If no labels were found, raises DeepsetCloudError. | ||
|
||
:param evaluation_set_name: name of the evaluation set for which labels should be fetched | ||
:param workspace: Optional workspace in Deepset Cloud | ||
If None, the EvaluationSetClient's default workspace (self.workspace) will be used. | ||
|
||
:return: list of Label | ||
""" | ||
try: | ||
evaluation_set = self._get_evaluation_set(evaluation_set_name=evaluation_set_name, workspace=workspace)[0] | ||
except IndexError: | ||
raise DeepsetCloudError(f"No evaluation set found with the name {evaluation_set_name}") | ||
|
||
labels = self._get_labels_from_evaluation_set( | ||
workspace=workspace, evaluation_set_id=evaluation_set["evaluation_set_id"] | ||
) | ||
|
||
return [ | ||
Label( | ||
query=label_dict["query"], | ||
document=Document(content=label_dict["context"]), | ||
is_correct_answer=True, | ||
is_correct_document=True, | ||
origin="user-feedback", | ||
answer=Answer(label_dict["answer"]), | ||
id=label_dict["label_id"], | ||
no_answer=False if label_dict.get("answer", None) else True, | ||
pipeline_id=None, | ||
created_at=None, | ||
updated_at=None, | ||
meta=label_dict["meta"], | ||
filters={}, | ||
) | ||
for label_dict in labels | ||
] | ||
|
||
def get_labels_count(self, evaluation_set_name: Optional[str] = None, workspace: Optional[str] = None) -> int: | ||
""" | ||
Counts labels for a given evaluation set in deepset cloud. | ||
|
||
:param evaluation_set_name: Optional index in Deepset Cloud | ||
If None, the EvaluationSetClient's default index (self.index) will be used. | ||
:param workspace: Optional workspace in Deepset Cloud | ||
If None, the EvaluationSetClient's default workspace (self.workspace) will be used. | ||
|
||
:return: Number of labels for the given (or defaulting) index | ||
""" | ||
evaluation_set = self._get_evaluation_set(evaluation_set_name=evaluation_set_name, workspace=workspace) | ||
return evaluation_set[0]["total_labels"] | ||
|
||
def get_evaluation_set_names(self, workspace: Optional[str] = None): | ||
""" | ||
Searches for all evaluation set names in the given workspace in Deepset Cloud. | ||
|
||
:param workspace: Optional workspace in Deepset Cloud | ||
If None, the EvaluationSetClient's default workspace (self.workspace) will be used. | ||
|
||
:return: list of Label | ||
""" | ||
evaluation_sets_response = self._get_evaluation_set(evaluation_set_name=None, workspace=workspace) | ||
|
||
return [eval_set["name"] for eval_set in evaluation_sets_response] | ||
|
||
def _get_evaluation_set(self, evaluation_set_name: Optional[str], workspace: Optional[str] = None) -> List[dict]: | ||
if not evaluation_set_name: | ||
evaluation_set_name = self.evaluation_set_name | ||
|
||
url = self._build_workspace_url(workspace=workspace) | ||
evaluation_set_url = f"{url}/evaluation_sets" | ||
|
||
for response in self.client.get_with_auto_paging( | ||
url=evaluation_set_url, query_params={"name": evaluation_set_name} | ||
): | ||
return response.json().get("data", []) | ||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. That doesn't seem to fit together. There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Ahh true, let me change that :) |
||
|
||
def _get_labels_from_evaluation_set( | ||
self, workspace: Optional[str] = None, evaluation_set_id: Optional[str] = None | ||
) -> Generator[dict]: | ||
url = f"{self._build_workspace_url(workspace=workspace)}/evaluation_sets/{evaluation_set_id}" | ||
labels = self.client.get(url=url).json() | ||
|
||
for label in labels: | ||
yield label | ||
|
||
def _build_workspace_url(self, workspace: Optional[str] = None): | ||
if workspace is None: | ||
workspace = self.workspace | ||
return self.client.build_workspace_url(workspace) | ||
FHardow marked this conversation as resolved.
Show resolved
Hide resolved
|
||
|
||
|
||
class DeepsetCloud: | ||
""" | ||
A facade to communicate with Deepset Cloud. | ||
|
@@ -683,3 +794,25 @@ def get_pipeline_client( | |
""" | ||
client = DeepsetCloudClient(api_key=api_key, api_endpoint=api_endpoint) | ||
return PipelineClient(client=client, workspace=workspace, pipeline_config_name=pipeline_config_name) | ||
|
||
@classmethod | ||
def get_evaluation_set_client( | ||
cls, | ||
api_key: Optional[str] = None, | ||
api_endpoint: Optional[str] = None, | ||
workspace: str = "default", | ||
evaluation_set_name: str = "default", | ||
) -> EvaluationSetClient: | ||
""" | ||
Creates a client to communicate with Deepset Cloud labels. | ||
|
||
:param api_key: Secret value of the API key. | ||
If not specified, will be read from DEEPSET_CLOUD_API_KEY environment variable. | ||
:param api_endpoint: The URL of the Deepset Cloud API. | ||
If not specified, will be read from DEEPSET_CLOUD_API_ENDPOINT environment variable. | ||
:param workspace: workspace in Deepset Cloud | ||
:param evaluation_set_name: name of the evaluation set in Deepset Cloud | ||
|
||
""" | ||
client = DeepsetCloudClient(api_key=api_key, api_endpoint=api_endpoint) | ||
return EvaluationSetClient(client=client, workspace=workspace, evaluation_set_name=evaluation_set_name) |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
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.
I think this import introduces a cyclic dependency causing all tests to fail. Could you please try
from haystack.schema import ...
?