mirror of
https://github.com/deepset-ai/haystack.git
synced 2025-10-26 23:38:58 +00:00
refactor: Deprecate BaseKnowledgeGraph, GraphDBKnowledgeGraph, InMemoryKnowledgeGraph and Text2SparqlRetriever (#4500)
* Deprecate BaseKnowledgeGraph and InMemoryKnowledgeGraph * Deprecate GraphDBKnowledgeGraph * Fix mypy * Deprecate Text2SparqlRetriever
This commit is contained in:
parent
f8bb270d62
commit
5b63c2086e
@ -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
|
||||
|
||||
@ -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: <https://deepset.ai/harry_potter/>
|
||||
"""
|
||||
warnings.warn(
|
||||
"The GraphDBKnowledgeGraph component is deprecated and will be removed in future versions.",
|
||||
category=DeprecationWarning,
|
||||
)
|
||||
super().__init__()
|
||||
|
||||
self.url = f"http://{host}:{port}"
|
||||
|
||||
@ -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
|
||||
|
||||
@ -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
|
||||
|
||||
22
test/document_stores/test_graphdb.py
Normal file
22
test/document_stores/test_graphdb.py
Normal 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."
|
||||
)
|
||||
22
test/document_stores/test_memory_knowledgegraph.py
Normal file
22
test/document_stores/test_memory_knowledgegraph.py
Normal 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."
|
||||
)
|
||||
@ -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."
|
||||
)
|
||||
|
||||
Loading…
x
Reference in New Issue
Block a user