2021-11-15 12:16:27 +01:00
|
|
|
from pathlib import Path
|
2022-02-10 16:58:40 +01:00
|
|
|
from collections import defaultdict
|
2022-05-04 17:39:06 +02:00
|
|
|
from unittest.mock import Mock
|
2021-11-15 12:16:27 +01:00
|
|
|
|
|
|
|
import os
|
|
|
|
import math
|
|
|
|
import pytest
|
|
|
|
|
|
|
|
from haystack.document_stores.elasticsearch import ElasticsearchDocumentStore
|
2022-03-07 19:25:33 +01:00
|
|
|
from haystack.pipelines import Pipeline, FAQPipeline, DocumentSearchPipeline, RootNode, MostSimilarDocumentsPipeline
|
2022-02-03 13:43:18 +01:00
|
|
|
from haystack.nodes import (
|
|
|
|
DensePassageRetriever,
|
2022-04-26 16:09:39 +02:00
|
|
|
BM25Retriever,
|
2022-02-03 13:43:18 +01:00
|
|
|
SklearnQueryClassifier,
|
|
|
|
TransformersQueryClassifier,
|
2022-05-04 17:39:06 +02:00
|
|
|
EmbeddingRetriever,
|
2022-02-03 13:43:18 +01:00
|
|
|
JoinDocuments,
|
|
|
|
)
|
2021-11-15 12:16:27 +01:00
|
|
|
from haystack.schema import Document
|
|
|
|
|
2022-05-17 10:55:53 +02:00
|
|
|
from ..conftest import SAMPLES_PATH
|
2022-01-26 18:12:55 +01:00
|
|
|
|
2021-11-15 12:16:27 +01:00
|
|
|
|
|
|
|
@pytest.mark.parametrize(
|
|
|
|
"retriever,document_store",
|
2022-03-07 19:25:33 +01:00
|
|
|
[("embedding", "memory"), ("embedding", "faiss"), ("embedding", "milvus1"), ("embedding", "elasticsearch")],
|
2021-11-15 12:16:27 +01:00
|
|
|
indirect=True,
|
|
|
|
)
|
|
|
|
def test_faq_pipeline(retriever, document_store):
|
|
|
|
documents = [
|
2022-03-07 19:25:33 +01:00
|
|
|
{"content": "How to test module-1?", "meta": {"source": "wiki1", "answer": "Using tests for module-1"}},
|
|
|
|
{"content": "How to test module-2?", "meta": {"source": "wiki2", "answer": "Using tests for module-2"}},
|
|
|
|
{"content": "How to test module-3?", "meta": {"source": "wiki3", "answer": "Using tests for module-3"}},
|
|
|
|
{"content": "How to test module-4?", "meta": {"source": "wiki4", "answer": "Using tests for module-4"}},
|
|
|
|
{"content": "How to test module-5?", "meta": {"source": "wiki5", "answer": "Using tests for module-5"}},
|
2021-11-15 12:16:27 +01:00
|
|
|
]
|
|
|
|
|
|
|
|
document_store.write_documents(documents)
|
|
|
|
document_store.update_embeddings(retriever)
|
|
|
|
|
|
|
|
pipeline = FAQPipeline(retriever=retriever)
|
|
|
|
|
|
|
|
output = pipeline.run(query="How to test this?", params={"Retriever": {"top_k": 3}})
|
|
|
|
assert len(output["answers"]) == 3
|
|
|
|
assert output["query"].startswith("How to")
|
|
|
|
assert output["answers"][0].answer.startswith("Using tests")
|
|
|
|
|
|
|
|
if isinstance(document_store, ElasticsearchDocumentStore):
|
2022-02-03 13:43:18 +01:00
|
|
|
output = pipeline.run(
|
|
|
|
query="How to test this?", params={"Retriever": {"filters": {"source": ["wiki2"]}, "top_k": 5}}
|
|
|
|
)
|
2021-11-15 12:16:27 +01:00
|
|
|
assert len(output["answers"]) == 1
|
|
|
|
|
|
|
|
|
|
|
|
@pytest.mark.parametrize("retriever", ["embedding"], indirect=True)
|
2022-03-03 15:19:27 +01:00
|
|
|
@pytest.mark.parametrize(
|
2022-03-21 22:24:09 +07:00
|
|
|
"document_store", ["elasticsearch", "faiss", "memory", "milvus1", "milvus", "weaviate", "pinecone"], indirect=True
|
2022-03-03 15:19:27 +01:00
|
|
|
)
|
2021-11-15 12:16:27 +01:00
|
|
|
def test_document_search_pipeline(retriever, document_store):
|
|
|
|
documents = [
|
|
|
|
{"content": "Sample text for document-1", "meta": {"source": "wiki1"}},
|
|
|
|
{"content": "Sample text for document-2", "meta": {"source": "wiki2"}},
|
|
|
|
{"content": "Sample text for document-3", "meta": {"source": "wiki3"}},
|
|
|
|
{"content": "Sample text for document-4", "meta": {"source": "wiki4"}},
|
|
|
|
{"content": "Sample text for document-5", "meta": {"source": "wiki5"}},
|
|
|
|
]
|
|
|
|
|
|
|
|
document_store.write_documents(documents)
|
|
|
|
document_store.update_embeddings(retriever)
|
|
|
|
|
|
|
|
pipeline = DocumentSearchPipeline(retriever=retriever)
|
|
|
|
output = pipeline.run(query="How to test this?", params={"top_k": 4})
|
|
|
|
assert len(output.get("documents", [])) == 4
|
|
|
|
|
|
|
|
if isinstance(document_store, ElasticsearchDocumentStore):
|
|
|
|
output = pipeline.run(query="How to test this?", params={"filters": {"source": ["wiki2"]}, "top_k": 5})
|
|
|
|
assert len(output["documents"]) == 1
|
|
|
|
|
|
|
|
|
2022-05-04 17:39:06 +02:00
|
|
|
@pytest.mark.integration
|
|
|
|
@pytest.mark.parametrize("retriever_with_docs", ["elasticsearch", "dpr", "embedding"], indirect=True)
|
|
|
|
@pytest.mark.parametrize("document_store_with_docs", ["elasticsearch"], indirect=True)
|
|
|
|
def test_documentsearch_es_authentication(retriever_with_docs, document_store_with_docs: ElasticsearchDocumentStore):
|
|
|
|
if isinstance(retriever_with_docs, (DensePassageRetriever, EmbeddingRetriever)):
|
|
|
|
document_store_with_docs.update_embeddings(retriever=retriever_with_docs)
|
|
|
|
mock_client = Mock(wraps=document_store_with_docs.client)
|
|
|
|
document_store_with_docs.client = mock_client
|
|
|
|
auth_headers = {"Authorization": "Basic YWRtaW46cm9vdA=="}
|
|
|
|
pipeline = DocumentSearchPipeline(retriever=retriever_with_docs)
|
|
|
|
prediction = pipeline.run(
|
|
|
|
query="Who lives in Berlin?", params={"Retriever": {"top_k": 10, "headers": auth_headers}}
|
|
|
|
)
|
|
|
|
assert prediction is not None
|
|
|
|
assert len(prediction["documents"]) == 5
|
|
|
|
mock_client.search.assert_called_once()
|
|
|
|
args, kwargs = mock_client.search.call_args
|
|
|
|
assert "headers" in kwargs
|
|
|
|
assert kwargs["headers"] == auth_headers
|
|
|
|
|
|
|
|
|
|
|
|
@pytest.mark.integration
|
|
|
|
@pytest.mark.parametrize("retriever_with_docs", ["tfidf"], indirect=True)
|
|
|
|
def test_documentsearch_document_store_authentication(retriever_with_docs, document_store_with_docs):
|
|
|
|
mock_client = None
|
|
|
|
if isinstance(document_store_with_docs, ElasticsearchDocumentStore):
|
|
|
|
es_document_store: ElasticsearchDocumentStore = document_store_with_docs
|
|
|
|
mock_client = Mock(wraps=es_document_store.client)
|
|
|
|
es_document_store.client = mock_client
|
|
|
|
auth_headers = {"Authorization": "Basic YWRtaW46cm9vdA=="}
|
|
|
|
pipeline = DocumentSearchPipeline(retriever=retriever_with_docs)
|
|
|
|
if not mock_client:
|
|
|
|
with pytest.raises(Exception):
|
|
|
|
prediction = pipeline.run(
|
|
|
|
query="Who lives in Berlin?", params={"Retriever": {"top_k": 10, "headers": auth_headers}}
|
|
|
|
)
|
|
|
|
else:
|
|
|
|
prediction = pipeline.run(
|
|
|
|
query="Who lives in Berlin?", params={"Retriever": {"top_k": 10, "headers": auth_headers}}
|
|
|
|
)
|
|
|
|
assert prediction is not None
|
|
|
|
assert len(prediction["documents"]) == 5
|
|
|
|
mock_client.count.assert_called_once()
|
|
|
|
args, kwargs = mock_client.count.call_args
|
|
|
|
assert "headers" in kwargs
|
|
|
|
assert kwargs["headers"] == auth_headers
|
|
|
|
|
|
|
|
|
2021-11-15 12:16:27 +01:00
|
|
|
@pytest.mark.parametrize(
|
2022-02-03 13:43:18 +01:00
|
|
|
"retriever,document_store",
|
2022-03-07 19:25:33 +01:00
|
|
|
[("embedding", "faiss"), ("embedding", "milvus1"), ("embedding", "elasticsearch")],
|
2022-02-03 13:43:18 +01:00
|
|
|
indirect=True,
|
2021-11-15 12:16:27 +01:00
|
|
|
)
|
|
|
|
def test_most_similar_documents_pipeline(retriever, document_store):
|
|
|
|
documents = [
|
|
|
|
{"id": "a", "content": "Sample text for document-1", "meta": {"source": "wiki1"}},
|
|
|
|
{"id": "b", "content": "Sample text for document-2", "meta": {"source": "wiki2"}},
|
|
|
|
{"content": "Sample text for document-3", "meta": {"source": "wiki3"}},
|
|
|
|
{"content": "Sample text for document-4", "meta": {"source": "wiki4"}},
|
|
|
|
{"content": "Sample text for document-5", "meta": {"source": "wiki5"}},
|
|
|
|
]
|
|
|
|
|
|
|
|
document_store.write_documents(documents)
|
|
|
|
document_store.update_embeddings(retriever)
|
|
|
|
|
|
|
|
docs_id: list = ["a", "b"]
|
|
|
|
pipeline = MostSimilarDocumentsPipeline(document_store=document_store)
|
|
|
|
list_of_documents = pipeline.run(document_ids=docs_id)
|
|
|
|
|
|
|
|
assert len(list_of_documents[0]) > 1
|
|
|
|
assert isinstance(list_of_documents, list)
|
|
|
|
assert len(list_of_documents) == len(docs_id)
|
|
|
|
|
|
|
|
for another_list in list_of_documents:
|
|
|
|
assert isinstance(another_list, list)
|
|
|
|
for document in another_list:
|
|
|
|
assert isinstance(document, Document)
|
|
|
|
assert isinstance(document.id, str)
|
|
|
|
assert isinstance(document.content, str)
|
|
|
|
|
|
|
|
|
|
|
|
@pytest.mark.elasticsearch
|
2022-01-12 19:28:20 +01:00
|
|
|
@pytest.mark.parametrize("document_store_dot_product_with_docs", ["elasticsearch"], indirect=True)
|
2022-02-10 16:58:40 +01:00
|
|
|
def test_join_merge_no_weights(document_store_dot_product_with_docs):
|
2022-04-26 16:09:39 +02:00
|
|
|
es = BM25Retriever(document_store=document_store_dot_product_with_docs)
|
2021-11-15 12:16:27 +01:00
|
|
|
dpr = DensePassageRetriever(
|
2022-01-12 19:28:20 +01:00
|
|
|
document_store=document_store_dot_product_with_docs,
|
2021-11-15 12:16:27 +01:00
|
|
|
query_embedding_model="facebook/dpr-question_encoder-single-nq-base",
|
|
|
|
passage_embedding_model="facebook/dpr-ctx_encoder-single-nq-base",
|
|
|
|
use_gpu=False,
|
|
|
|
)
|
2022-01-12 19:28:20 +01:00
|
|
|
document_store_dot_product_with_docs.update_embeddings(dpr)
|
2021-11-15 12:16:27 +01:00
|
|
|
|
|
|
|
query = "Where does Carla live?"
|
|
|
|
|
|
|
|
join_node = JoinDocuments(join_mode="merge")
|
|
|
|
p = Pipeline()
|
|
|
|
p.add_node(component=es, name="R1", inputs=["Query"])
|
|
|
|
p.add_node(component=dpr, name="R2", inputs=["Query"])
|
|
|
|
p.add_node(component=join_node, name="Join", inputs=["R1", "R2"])
|
|
|
|
results = p.run(query=query)
|
2022-02-04 13:43:12 +01:00
|
|
|
assert len(results["documents"]) == 5
|
2021-11-15 12:16:27 +01:00
|
|
|
|
2022-02-10 16:58:40 +01:00
|
|
|
|
|
|
|
@pytest.mark.elasticsearch
|
|
|
|
@pytest.mark.parametrize("document_store_dot_product_with_docs", ["elasticsearch"], indirect=True)
|
|
|
|
def test_join_merge_with_weights(document_store_dot_product_with_docs):
|
2022-04-26 16:09:39 +02:00
|
|
|
es = BM25Retriever(document_store=document_store_dot_product_with_docs)
|
2022-02-10 16:58:40 +01:00
|
|
|
dpr = DensePassageRetriever(
|
|
|
|
document_store=document_store_dot_product_with_docs,
|
|
|
|
query_embedding_model="facebook/dpr-question_encoder-single-nq-base",
|
|
|
|
passage_embedding_model="facebook/dpr-ctx_encoder-single-nq-base",
|
|
|
|
use_gpu=False,
|
|
|
|
)
|
|
|
|
document_store_dot_product_with_docs.update_embeddings(dpr)
|
|
|
|
|
|
|
|
query = "Where does Carla live?"
|
|
|
|
|
2021-11-15 12:16:27 +01:00
|
|
|
join_node = JoinDocuments(join_mode="merge", weights=[1000, 1], top_k_join=2)
|
|
|
|
p = Pipeline()
|
|
|
|
p.add_node(component=es, name="R1", inputs=["Query"])
|
|
|
|
p.add_node(component=dpr, name="R2", inputs=["Query"])
|
|
|
|
p.add_node(component=join_node, name="Join", inputs=["R1", "R2"])
|
|
|
|
results = p.run(query=query)
|
2022-02-04 13:43:12 +01:00
|
|
|
assert math.isclose(results["documents"][0].score, 0.5481393431183286, rel_tol=0.0001)
|
2021-11-15 12:16:27 +01:00
|
|
|
assert len(results["documents"]) == 2
|
|
|
|
|
2022-02-10 16:58:40 +01:00
|
|
|
|
|
|
|
@pytest.mark.elasticsearch
|
|
|
|
@pytest.mark.parametrize("document_store_dot_product_with_docs", ["elasticsearch"], indirect=True)
|
|
|
|
def test_join_concatenate(document_store_dot_product_with_docs):
|
2022-04-26 16:09:39 +02:00
|
|
|
es = BM25Retriever(document_store=document_store_dot_product_with_docs)
|
2022-02-10 16:58:40 +01:00
|
|
|
dpr = DensePassageRetriever(
|
|
|
|
document_store=document_store_dot_product_with_docs,
|
|
|
|
query_embedding_model="facebook/dpr-question_encoder-single-nq-base",
|
|
|
|
passage_embedding_model="facebook/dpr-ctx_encoder-single-nq-base",
|
|
|
|
use_gpu=False,
|
|
|
|
)
|
|
|
|
document_store_dot_product_with_docs.update_embeddings(dpr)
|
|
|
|
|
|
|
|
query = "Where does Carla live?"
|
|
|
|
|
2021-11-15 12:16:27 +01:00
|
|
|
join_node = JoinDocuments(join_mode="concatenate")
|
|
|
|
p = Pipeline()
|
|
|
|
p.add_node(component=es, name="R1", inputs=["Query"])
|
|
|
|
p.add_node(component=dpr, name="R2", inputs=["Query"])
|
|
|
|
p.add_node(component=join_node, name="Join", inputs=["R1", "R2"])
|
|
|
|
results = p.run(query=query)
|
2022-02-04 13:43:12 +01:00
|
|
|
assert len(results["documents"]) == 5
|
2021-11-15 12:16:27 +01:00
|
|
|
|
2022-02-10 16:58:40 +01:00
|
|
|
|
|
|
|
@pytest.mark.elasticsearch
|
|
|
|
@pytest.mark.parametrize("document_store_dot_product_with_docs", ["elasticsearch"], indirect=True)
|
|
|
|
def test_join_concatenate_with_topk(document_store_dot_product_with_docs):
|
2022-04-26 16:09:39 +02:00
|
|
|
es = BM25Retriever(document_store=document_store_dot_product_with_docs)
|
2022-02-10 16:58:40 +01:00
|
|
|
dpr = DensePassageRetriever(
|
|
|
|
document_store=document_store_dot_product_with_docs,
|
|
|
|
query_embedding_model="facebook/dpr-question_encoder-single-nq-base",
|
|
|
|
passage_embedding_model="facebook/dpr-ctx_encoder-single-nq-base",
|
|
|
|
use_gpu=False,
|
|
|
|
)
|
|
|
|
document_store_dot_product_with_docs.update_embeddings(dpr)
|
|
|
|
|
|
|
|
query = "Where does Carla live?"
|
|
|
|
|
2022-01-26 16:30:16 +00:00
|
|
|
join_node = JoinDocuments(join_mode="concatenate")
|
|
|
|
p = Pipeline()
|
|
|
|
p.add_node(component=es, name="R1", inputs=["Query"])
|
|
|
|
p.add_node(component=dpr, name="R2", inputs=["Query"])
|
|
|
|
p.add_node(component=join_node, name="Join", inputs=["R1", "R2"])
|
2022-02-03 13:43:18 +01:00
|
|
|
one_result = p.run(query=query, params={"Join": {"top_k_join": 1}})
|
|
|
|
two_results = p.run(query=query, params={"Join": {"top_k_join": 2}})
|
2022-01-26 16:30:16 +00:00
|
|
|
assert len(one_result["documents"]) == 1
|
|
|
|
assert len(two_results["documents"]) == 2
|
|
|
|
|
2022-02-10 16:58:40 +01:00
|
|
|
|
|
|
|
@pytest.mark.elasticsearch
|
|
|
|
@pytest.mark.parametrize("document_store_dot_product_with_docs", ["elasticsearch"], indirect=True)
|
|
|
|
@pytest.mark.parametrize("reader", ["farm"], indirect=True)
|
|
|
|
def test_join_with_reader(document_store_dot_product_with_docs, reader):
|
2022-04-26 16:09:39 +02:00
|
|
|
es = BM25Retriever(document_store=document_store_dot_product_with_docs)
|
2022-02-10 16:58:40 +01:00
|
|
|
dpr = DensePassageRetriever(
|
|
|
|
document_store=document_store_dot_product_with_docs,
|
|
|
|
query_embedding_model="facebook/dpr-question_encoder-single-nq-base",
|
|
|
|
passage_embedding_model="facebook/dpr-ctx_encoder-single-nq-base",
|
|
|
|
use_gpu=False,
|
|
|
|
)
|
|
|
|
document_store_dot_product_with_docs.update_embeddings(dpr)
|
|
|
|
|
|
|
|
query = "Where does Carla live?"
|
|
|
|
|
2021-11-15 12:16:27 +01:00
|
|
|
join_node = JoinDocuments()
|
|
|
|
p = Pipeline()
|
|
|
|
p.add_node(component=es, name="R1", inputs=["Query"])
|
|
|
|
p.add_node(component=dpr, name="R2", inputs=["Query"])
|
|
|
|
p.add_node(component=join_node, name="Join", inputs=["R1", "R2"])
|
|
|
|
p.add_node(component=reader, name="Reader", inputs=["Join"])
|
|
|
|
results = p.run(query=query)
|
2022-02-03 13:43:18 +01:00
|
|
|
# check whether correct answer is within top 2 predictions
|
2021-11-15 12:16:27 +01:00
|
|
|
assert results["answers"][0].answer == "Berlin" or results["answers"][1].answer == "Berlin"
|
|
|
|
|
|
|
|
|
2022-02-10 16:58:40 +01:00
|
|
|
@pytest.mark.elasticsearch
|
|
|
|
@pytest.mark.parametrize("document_store_dot_product_with_docs", ["elasticsearch"], indirect=True)
|
|
|
|
def test_join_with_rrf(document_store_dot_product_with_docs):
|
2022-04-26 16:09:39 +02:00
|
|
|
es = BM25Retriever(document_store=document_store_dot_product_with_docs)
|
2022-02-10 16:58:40 +01:00
|
|
|
dpr = DensePassageRetriever(
|
|
|
|
document_store=document_store_dot_product_with_docs,
|
|
|
|
query_embedding_model="facebook/dpr-question_encoder-single-nq-base",
|
|
|
|
passage_embedding_model="facebook/dpr-ctx_encoder-single-nq-base",
|
|
|
|
use_gpu=False,
|
|
|
|
)
|
|
|
|
document_store_dot_product_with_docs.update_embeddings(dpr)
|
|
|
|
|
|
|
|
query = "Where does Carla live?"
|
|
|
|
|
|
|
|
join_node = JoinDocuments(join_mode="reciprocal_rank_fusion")
|
|
|
|
p = Pipeline()
|
|
|
|
p.add_node(component=es, name="R1", inputs=["Query"])
|
|
|
|
p.add_node(component=dpr, name="R2", inputs=["Query"])
|
|
|
|
p.add_node(component=join_node, name="Join", inputs=["R1", "R2"])
|
|
|
|
results = p.run(query=query)
|
|
|
|
|
|
|
|
# list of precalculated expected results
|
|
|
|
expected_scores = [
|
|
|
|
0.03278688524590164,
|
|
|
|
0.03200204813108039,
|
|
|
|
0.03200204813108039,
|
|
|
|
0.031009615384615385,
|
|
|
|
0.031009615384615385,
|
|
|
|
]
|
|
|
|
|
|
|
|
assert all([doc.score == expected_scores[idx] for idx, doc in enumerate(results["documents"])])
|
|
|
|
|
|
|
|
|
2021-11-15 12:16:27 +01:00
|
|
|
def test_query_keyword_statement_classifier():
|
|
|
|
class KeywordOutput(RootNode):
|
|
|
|
outgoing_edges = 2
|
|
|
|
|
|
|
|
def run(self, **kwargs):
|
|
|
|
kwargs["output"] = "keyword"
|
|
|
|
return kwargs, "output_1"
|
|
|
|
|
|
|
|
class QuestionOutput(RootNode):
|
|
|
|
outgoing_edges = 2
|
|
|
|
|
|
|
|
def run(self, **kwargs):
|
|
|
|
kwargs["output"] = "question"
|
|
|
|
return kwargs, "output_2"
|
|
|
|
|
|
|
|
pipeline = Pipeline()
|
2022-03-07 19:25:33 +01:00
|
|
|
pipeline.add_node(name="SkQueryKeywordQuestionClassifier", component=SklearnQueryClassifier(), inputs=["Query"])
|
2021-11-15 12:16:27 +01:00
|
|
|
pipeline.add_node(
|
2022-03-07 19:25:33 +01:00
|
|
|
name="KeywordNode", component=KeywordOutput(), inputs=["SkQueryKeywordQuestionClassifier.output_2"]
|
2021-11-15 12:16:27 +01:00
|
|
|
)
|
|
|
|
pipeline.add_node(
|
2022-03-07 19:25:33 +01:00
|
|
|
name="QuestionNode", component=QuestionOutput(), inputs=["SkQueryKeywordQuestionClassifier.output_1"]
|
2021-11-15 12:16:27 +01:00
|
|
|
)
|
|
|
|
output = pipeline.run(query="morse code")
|
|
|
|
assert output["output"] == "keyword"
|
|
|
|
|
|
|
|
output = pipeline.run(query="How old is John?")
|
|
|
|
assert output["output"] == "question"
|
|
|
|
|
|
|
|
pipeline = Pipeline()
|
|
|
|
pipeline.add_node(
|
2022-03-07 19:25:33 +01:00
|
|
|
name="TfQueryKeywordQuestionClassifier", component=TransformersQueryClassifier(), inputs=["Query"]
|
2021-11-15 12:16:27 +01:00
|
|
|
)
|
|
|
|
pipeline.add_node(
|
2022-03-07 19:25:33 +01:00
|
|
|
name="KeywordNode", component=KeywordOutput(), inputs=["TfQueryKeywordQuestionClassifier.output_2"]
|
2021-11-15 12:16:27 +01:00
|
|
|
)
|
|
|
|
pipeline.add_node(
|
2022-03-07 19:25:33 +01:00
|
|
|
name="QuestionNode", component=QuestionOutput(), inputs=["TfQueryKeywordQuestionClassifier.output_1"]
|
2021-11-15 12:16:27 +01:00
|
|
|
)
|
|
|
|
output = pipeline.run(query="morse code")
|
|
|
|
assert output["output"] == "keyword"
|
|
|
|
|
|
|
|
output = pipeline.run(query="How old is John?")
|
|
|
|
assert output["output"] == "question"
|
|
|
|
|
|
|
|
|
|
|
|
@pytest.mark.elasticsearch
|
|
|
|
@pytest.mark.parametrize("document_store", ["elasticsearch"], indirect=True)
|
|
|
|
def test_indexing_pipeline_with_classifier(document_store):
|
|
|
|
# test correct load of indexing pipeline from yaml
|
|
|
|
pipeline = Pipeline.load_from_yaml(
|
2022-05-04 17:39:06 +02:00
|
|
|
SAMPLES_PATH / "pipeline" / "test.haystack-pipeline.yml", pipeline_name="indexing_pipeline_with_classifier"
|
2021-11-15 12:16:27 +01:00
|
|
|
)
|
2022-02-03 13:43:18 +01:00
|
|
|
pipeline.run(file_paths=SAMPLES_PATH / "pdf" / "sample_pdf_1.pdf")
|
2021-11-15 12:16:27 +01:00
|
|
|
# test correct load of query pipeline from yaml
|
2022-05-04 17:39:06 +02:00
|
|
|
pipeline = Pipeline.load_from_yaml(
|
|
|
|
SAMPLES_PATH / "pipeline" / "test.haystack-pipeline.yml", pipeline_name="query_pipeline"
|
|
|
|
)
|
2021-11-15 12:16:27 +01:00
|
|
|
prediction = pipeline.run(
|
|
|
|
query="Who made the PDF specification?", params={"ESRetriever": {"top_k": 10}, "Reader": {"top_k": 3}}
|
|
|
|
)
|
|
|
|
assert prediction["query"] == "Who made the PDF specification?"
|
|
|
|
assert prediction["answers"][0].answer == "Adobe Systems"
|
|
|
|
assert prediction["answers"][0].meta["classification"]["label"] == "joy"
|
|
|
|
assert "_debug" not in prediction.keys()
|
|
|
|
|
|
|
|
|
|
|
|
@pytest.mark.elasticsearch
|
|
|
|
@pytest.mark.parametrize("document_store", ["elasticsearch"], indirect=True)
|
|
|
|
def test_query_pipeline_with_document_classifier(document_store):
|
|
|
|
# test correct load of indexing pipeline from yaml
|
|
|
|
pipeline = Pipeline.load_from_yaml(
|
2022-05-04 17:39:06 +02:00
|
|
|
SAMPLES_PATH / "pipeline" / "test.haystack-pipeline.yml", pipeline_name="indexing_pipeline"
|
2021-11-15 12:16:27 +01:00
|
|
|
)
|
2022-02-03 13:43:18 +01:00
|
|
|
pipeline.run(file_paths=SAMPLES_PATH / "pdf" / "sample_pdf_1.pdf")
|
2021-11-15 12:16:27 +01:00
|
|
|
# test correct load of query pipeline from yaml
|
|
|
|
pipeline = Pipeline.load_from_yaml(
|
2022-05-04 17:39:06 +02:00
|
|
|
SAMPLES_PATH / "pipeline" / "test.haystack-pipeline.yml",
|
|
|
|
pipeline_name="query_pipeline_with_document_classifier",
|
2021-11-15 12:16:27 +01:00
|
|
|
)
|
|
|
|
prediction = pipeline.run(
|
|
|
|
query="Who made the PDF specification?", params={"ESRetriever": {"top_k": 10}, "Reader": {"top_k": 3}}
|
|
|
|
)
|
|
|
|
assert prediction["query"] == "Who made the PDF specification?"
|
|
|
|
assert prediction["answers"][0].answer == "Adobe Systems"
|
|
|
|
assert prediction["answers"][0].meta["classification"]["label"] == "joy"
|
|
|
|
assert "_debug" not in prediction.keys()
|