def test_load_yaml(document_store_with_docs): # # test correct load from yaml pipeline = Pipeline.load_from_yaml(Path("samples/pipeline/test_pipeline.yaml", pipeline_name="my_query")) prediction = pipeline.run(query="Who lives in Berlin?", top_k_retriever=10, top_k_reader=3) assert prediction["query"] == "Who lives in Berlin?" assert prediction["answers"][0]["answer"] == "Carla" # test invalid pipeline name with pytest.raises(Exception): Pipeline.load_from_yaml(path=Path("samples/pipeline/test_pipeline.yaml"), pipeline_name="invalid")
def test_load_yaml(document_store_with_docs): # test correct load of indexing pipeline from yaml pipeline = Pipeline.load_from_yaml(Path("samples/pipeline/test_pipeline.yaml"), pipeline_name="test_indexing_pipeline") pipeline.run(file_path=Path("samples/pdf/sample_pdf_1.pdf"), top_k_retriever=10, top_k_reader=3) # test correct load of query pipeline from yaml pipeline = Pipeline.load_from_yaml(Path("samples/pipeline/test_pipeline.yaml"), pipeline_name="test_query_pipeline") prediction = pipeline.run(query="Who made the PDF specification?", top_k_retriever=10, top_k_reader=3) assert prediction["query"] == "Who made the PDF specification?" assert prediction["answers"][0]["answer"] == "Adobe Systems" # test invalid pipeline name with pytest.raises(Exception): Pipeline.load_from_yaml(path=Path("samples/pipeline/test_pipeline.yaml"), pipeline_name="invalid")
def test_load_and_save_yaml(document_store_with_docs, tmp_path): # test correct load of indexing pipeline from yaml pipeline = Pipeline.load_from_yaml(Path("samples/pipeline/test_pipeline.yaml"), pipeline_name="indexing_pipeline") pipeline.run(file_path=Path("samples/pdf/sample_pdf_1.pdf"), top_k_retriever=10, top_k_reader=3) # test correct load of query pipeline from yaml pipeline = Pipeline.load_from_yaml(Path("samples/pipeline/test_pipeline.yaml"), pipeline_name="query_pipeline") prediction = pipeline.run(query="Who made the PDF specification?", top_k_retriever=10, top_k_reader=3) assert prediction["query"] == "Who made the PDF specification?" assert prediction["answers"][0]["answer"] == "Adobe Systems" # test invalid pipeline name with pytest.raises(Exception): Pipeline.load_from_yaml(path=Path("samples/pipeline/test_pipeline.yaml"), pipeline_name="invalid") # test config export pipeline.save_to_yaml(tmp_path / "test.yaml") with open(tmp_path/"test.yaml", "r", encoding='utf-8') as stream: saved_yaml = stream.read() expected_yaml = ''' components: - name: ESRetriever params: document_store: ElasticsearchDocumentStore type: ElasticsearchRetriever - name: ElasticsearchDocumentStore params: index: haystack_test_document label_index: haystack_test_label type: ElasticsearchDocumentStore - name: Reader params: model_name_or_path: deepset/roberta-base-squad2 no_ans_boost: -10 type: FARMReader pipelines: - name: query nodes: - inputs: - Query name: ESRetriever - inputs: - ESRetriever name: Reader type: Query version: '0.8' ''' assert saved_yaml.replace(" ", "").replace("\n", "") == expected_yaml.replace(" ", "").replace("\n", "")
import os import shutil import uuid from pathlib import Path from typing import Optional, List from fastapi import APIRouter, UploadFile, File, Form, HTTPException from haystack.pipeline import Pipeline from rest_api.config import PIPELINE_YAML_PATH, FILE_UPLOAD_PATH, INDEXING_PIPELINE_NAME logger = logging.getLogger(__name__) router = APIRouter() try: INDEXING_PIPELINE = Pipeline.load_from_yaml( Path(PIPELINE_YAML_PATH), pipeline_name=INDEXING_PIPELINE_NAME) except KeyError: INDEXING_PIPELINE = None logger.info( "Indexing Pipeline not found in the YAML configuration. File Upload API will not be available." ) os.makedirs(FILE_UPLOAD_PATH, exist_ok=True) # create directory for uploading files @router.post("/file-upload") def file_upload( files: List[UploadFile] = File(...), meta: Optional[str] = Form("null"), # JSON serialized string remove_numeric_tables: Optional[bool] = Form(None),