refactor: Deprecate BaseKnowledgeGraph, GraphDBKnowledgeGraph, InMemoryKnowledgeGraph and Text2SparqlRetriever (#4500)

* Deprecate BaseKnowledgeGraph and InMemoryKnowledgeGraph

* Deprecate GraphDBKnowledgeGraph

* Fix mypy

* Deprecate Text2SparqlRetriever
This commit is contained in:
Silvano Cerza 2023-03-27 15:31:22 +02:00 committed by GitHub
parent f8bb270d62
commit 5b63c2086e
No known key found for this signature in database
GPG Key ID: 4AEE18F83AFDEB23
7 changed files with 90 additions and 2 deletions

View File

@ -2,6 +2,7 @@
from typing import Generator, Optional, Dict, List, Set, Union, Any from typing import Generator, Optional, Dict, List, Set, Union, Any
import warnings
import logging import logging
import collections import collections
from pathlib import Path from pathlib import Path
@ -40,6 +41,13 @@ class BaseKnowledgeGraph(BaseComponent):
Base class for implementing Knowledge Graphs. 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 outgoing_edges = 1
def run(self, sparql_query: str, index: Optional[str] = None, headers: Optional[Dict[str, str]] = None): # type: ignore def run(self, sparql_query: str, index: Optional[str] = None, headers: Optional[Dict[str, str]] = None): # type: ignore

View File

@ -1,5 +1,6 @@
from typing import Dict, Optional, Union, Tuple from typing import Dict, Optional, Union, Tuple
import warnings
from pathlib import Path from pathlib import Path
import requests import requests
@ -39,6 +40,10 @@ class GraphDBKnowledgeGraph(BaseKnowledgeGraph):
:param index: name of the index (also called repository) stored in the GraphDB instance :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: <https://deepset.ai/harry_potter/> :param prefixes: definitions of namespaces with a new line after each namespace, e.g., PREFIX hp: <https://deepset.ai/harry_potter/>
""" """
warnings.warn(
"The GraphDBKnowledgeGraph component is deprecated and will be removed in future versions.",
category=DeprecationWarning,
)
super().__init__() super().__init__()
self.url = f"http://{host}:{port}" self.url = f"http://{host}:{port}"

View File

@ -1,5 +1,6 @@
from typing import Dict, Optional from typing import Dict, Optional
import warnings
import logging import logging
from collections import defaultdict from collections import defaultdict
from pathlib import Path from pathlib import Path
@ -22,6 +23,10 @@ class InMemoryKnowledgeGraph(BaseKnowledgeGraph):
:param index: name of the index :param index: name of the index
""" """
warnings.warn(
"The InMemoryKnowledgeGraph component is deprecated and will be removed in future versions.",
category=DeprecationWarning,
)
super().__init__() super().__init__()
self.indexes: Dict[str, Graph] = defaultdict(dict) # type: ignore [arg-type] self.indexes: Dict[str, Graph] = defaultdict(dict) # type: ignore [arg-type]
@ -131,7 +136,7 @@ class InMemoryKnowledgeGraph(BaseKnowledgeGraph):
items = list(b.items()) items = list(b.items())
for item in items: for item in items:
type_ = item[0].toPython()[1:] 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_result[type_] = {"type": "uri", "value": uri}
formatted_results.append(formatted_result) formatted_results.append(formatted_result)
return formatted_results return formatted_results

View File

@ -1,5 +1,6 @@
from typing import Optional, List, Union from typing import Optional, List, Union
import warnings
import logging import logging
from transformers import BartForConditionalGeneration, BartTokenizer from transformers import BartForConditionalGeneration, BartTokenizer
@ -38,6 +39,10 @@ class Text2SparqlRetriever(BaseGraphRetriever):
Additional information can be found here Additional information can be found here
https://huggingface.co/transformers/main_classes/model.html#transformers.PreTrainedModel.from_pretrained 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__() super().__init__()
self.knowledge_graph = knowledge_graph self.knowledge_graph = knowledge_graph

View File

@ -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."
)

View File

@ -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."
)

View File

@ -5,6 +5,7 @@ import logging
import os import os
from math import isclose from math import isclose
from typing import Dict, List, Optional, Union from typing import Dict, List, Optional, Union
from unittest.mock import patch, Mock, DEFAULT
import pytest import pytest
import numpy as np 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.memory import InMemoryDocumentStore
from haystack.document_stores import WeaviateDocumentStore from haystack.document_stores import WeaviateDocumentStore
from haystack.nodes.retriever.base import BaseRetriever from haystack.nodes.retriever.base import BaseRetriever
from haystack.nodes.retriever import Text2SparqlRetriever
from haystack.pipelines import DocumentSearchPipeline from haystack.pipelines import DocumentSearchPipeline
from haystack.schema import Document from haystack.schema import Document
from haystack.document_stores.elasticsearch import ElasticsearchDocumentStore 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.sparse import BM25Retriever, FilterRetriever, TfidfRetriever
from haystack.nodes.retriever.multimodal import MultiModalRetriever 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 # 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 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" 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."
)