From 5b63c2086e0dca49f09e00cdfdc079c11d096c87 Mon Sep 17 00:00:00 2001 From: Silvano Cerza <3314350+silvanocerza@users.noreply.github.com> Date: Mon, 27 Mar 2023 15:31:22 +0200 Subject: [PATCH] refactor: Deprecate BaseKnowledgeGraph, GraphDBKnowledgeGraph, InMemoryKnowledgeGraph and Text2SparqlRetriever (#4500) * Deprecate BaseKnowledgeGraph and InMemoryKnowledgeGraph * Deprecate GraphDBKnowledgeGraph * Fix mypy * Deprecate Text2SparqlRetriever --- haystack/document_stores/base.py | 8 +++++++ haystack/document_stores/graphdb.py | 5 ++++ .../document_stores/memory_knowledgegraph.py | 7 +++++- haystack/nodes/retriever/text2sparql.py | 5 ++++ test/document_stores/test_graphdb.py | 22 ++++++++++++++++++ .../test_memory_knowledgegraph.py | 22 ++++++++++++++++++ test/nodes/test_retriever.py | 23 ++++++++++++++++++- 7 files changed, 90 insertions(+), 2 deletions(-) create mode 100644 test/document_stores/test_graphdb.py create mode 100644 test/document_stores/test_memory_knowledgegraph.py diff --git a/haystack/document_stores/base.py b/haystack/document_stores/base.py index 4726e201c..24afb7ad8 100644 --- a/haystack/document_stores/base.py +++ b/haystack/document_stores/base.py @@ -2,6 +2,7 @@ from typing import Generator, Optional, Dict, List, Set, Union, Any +import warnings import logging import collections from pathlib import Path @@ -40,6 +41,13 @@ class BaseKnowledgeGraph(BaseComponent): Base class for implementing Knowledge Graphs. """ + def __init__(self): + warnings.warn( + "The BaseKnowledgeGraph component is deprecated and will be removed in future versions.", + category=DeprecationWarning, + ) + super().__init__() + outgoing_edges = 1 def run(self, sparql_query: str, index: Optional[str] = None, headers: Optional[Dict[str, str]] = None): # type: ignore diff --git a/haystack/document_stores/graphdb.py b/haystack/document_stores/graphdb.py index 3d9b75e62..48984a9b9 100644 --- a/haystack/document_stores/graphdb.py +++ b/haystack/document_stores/graphdb.py @@ -1,5 +1,6 @@ from typing import Dict, Optional, Union, Tuple +import warnings from pathlib import Path import requests @@ -39,6 +40,10 @@ class GraphDBKnowledgeGraph(BaseKnowledgeGraph): :param index: name of the index (also called repository) stored in the GraphDB instance :param prefixes: definitions of namespaces with a new line after each namespace, e.g., PREFIX hp: """ + warnings.warn( + "The GraphDBKnowledgeGraph component is deprecated and will be removed in future versions.", + category=DeprecationWarning, + ) super().__init__() self.url = f"http://{host}:{port}" diff --git a/haystack/document_stores/memory_knowledgegraph.py b/haystack/document_stores/memory_knowledgegraph.py index bc0f8049f..ae1755118 100644 --- a/haystack/document_stores/memory_knowledgegraph.py +++ b/haystack/document_stores/memory_knowledgegraph.py @@ -1,5 +1,6 @@ from typing import Dict, Optional +import warnings import logging from collections import defaultdict from pathlib import Path @@ -22,6 +23,10 @@ class InMemoryKnowledgeGraph(BaseKnowledgeGraph): :param index: name of the index """ + warnings.warn( + "The InMemoryKnowledgeGraph component is deprecated and will be removed in future versions.", + category=DeprecationWarning, + ) super().__init__() self.indexes: Dict[str, Graph] = defaultdict(dict) # type: ignore [arg-type] @@ -131,7 +136,7 @@ class InMemoryKnowledgeGraph(BaseKnowledgeGraph): items = list(b.items()) for item in items: type_ = item[0].toPython()[1:] - uri = item[1].toPython() + uri = item[1].toPython() # type: ignore [attr-defined] formatted_result[type_] = {"type": "uri", "value": uri} formatted_results.append(formatted_result) return formatted_results diff --git a/haystack/nodes/retriever/text2sparql.py b/haystack/nodes/retriever/text2sparql.py index 5bb28e4ab..c295019e6 100644 --- a/haystack/nodes/retriever/text2sparql.py +++ b/haystack/nodes/retriever/text2sparql.py @@ -1,5 +1,6 @@ from typing import Optional, List, Union +import warnings import logging from transformers import BartForConditionalGeneration, BartTokenizer @@ -38,6 +39,10 @@ class Text2SparqlRetriever(BaseGraphRetriever): Additional information can be found here https://huggingface.co/transformers/main_classes/model.html#transformers.PreTrainedModel.from_pretrained """ + warnings.warn( + "The Text2SparqlRetriever component is deprecated and will be removed in future versions.", + category=DeprecationWarning, + ) super().__init__() self.knowledge_graph = knowledge_graph diff --git a/test/document_stores/test_graphdb.py b/test/document_stores/test_graphdb.py new file mode 100644 index 000000000..e3ae98486 --- /dev/null +++ b/test/document_stores/test_graphdb.py @@ -0,0 +1,22 @@ +import pytest + +from haystack.document_stores.graphdb import GraphDBKnowledgeGraph + +from ..conftest import fail_at_version + + +@pytest.mark.unit +@fail_at_version(1, 17) +def test_graphdb_knowledge_graph_deprecation_warning(): + with pytest.warns(DeprecationWarning) as w: + GraphDBKnowledgeGraph() + + assert len(w) == 2 + assert ( + w[0].message.args[0] + == "The GraphDBKnowledgeGraph component is deprecated and will be removed in future versions." + ) + assert ( + w[1].message.args[0] + == "The BaseKnowledgeGraph component is deprecated and will be removed in future versions." + ) diff --git a/test/document_stores/test_memory_knowledgegraph.py b/test/document_stores/test_memory_knowledgegraph.py new file mode 100644 index 000000000..f871439e6 --- /dev/null +++ b/test/document_stores/test_memory_knowledgegraph.py @@ -0,0 +1,22 @@ +import pytest + +from haystack.document_stores.memory_knowledgegraph import InMemoryKnowledgeGraph + +from ..conftest import fail_at_version + + +@pytest.mark.unit +@fail_at_version(1, 17) +def test_in_memory_knowledge_graph_deprecation_warning(): + with pytest.warns(DeprecationWarning) as w: + InMemoryKnowledgeGraph() + + assert len(w) == 2 + assert ( + w[0].message.args[0] + == "The InMemoryKnowledgeGraph component is deprecated and will be removed in future versions." + ) + assert ( + w[1].message.args[0] + == "The BaseKnowledgeGraph component is deprecated and will be removed in future versions." + ) diff --git a/test/nodes/test_retriever.py b/test/nodes/test_retriever.py index 6b622a4bb..2916d49c1 100644 --- a/test/nodes/test_retriever.py +++ b/test/nodes/test_retriever.py @@ -5,6 +5,7 @@ import logging import os from math import isclose from typing import Dict, List, Optional, Union +from unittest.mock import patch, Mock, DEFAULT import pytest import numpy as np @@ -17,6 +18,7 @@ from haystack.document_stores.base import BaseDocumentStore, FilterType from haystack.document_stores.memory import InMemoryDocumentStore from haystack.document_stores import WeaviateDocumentStore from haystack.nodes.retriever.base import BaseRetriever +from haystack.nodes.retriever import Text2SparqlRetriever from haystack.pipelines import DocumentSearchPipeline from haystack.schema import Document from haystack.document_stores.elasticsearch import ElasticsearchDocumentStore @@ -24,7 +26,7 @@ from haystack.nodes.retriever.dense import DensePassageRetriever, EmbeddingRetri from haystack.nodes.retriever.sparse import BM25Retriever, FilterRetriever, TfidfRetriever from haystack.nodes.retriever.multimodal import MultiModalRetriever -from ..conftest import SAMPLES_PATH, MockRetriever +from ..conftest import SAMPLES_PATH, MockRetriever, fail_at_version # TODO check if we this works with only "memory" arg @@ -1150,3 +1152,22 @@ def test_multimodal_text_image_retrieval(text_docs: List[Document], image_docs: assert str(image_results[0].content) == str(SAMPLES_PATH / "images" / "paris.jpg") assert text_results[0].content == "My name is Christelle and I live in Paris" + + +@pytest.mark.unit +@fail_at_version(1, 17) +def test_text_2_sparql_retriever_deprecation(): + BartForConditionalGeneration = object() + BartTokenizer = object() + with patch.multiple( + "haystack.nodes.retriever.text2sparql", BartForConditionalGeneration=DEFAULT, BartTokenizer=DEFAULT + ): + knowledge_graph = Mock() + with pytest.warns(DeprecationWarning) as w: + Text2SparqlRetriever(knowledge_graph) + + assert len(w) == 1 + assert ( + w[0].message.args[0] + == "The Text2SparqlRetriever component is deprecated and will be removed in future versions." + )