2020-04-15 14:04:30 +02:00
|
|
|
import logging
|
|
|
|
|
2021-10-04 11:21:00 +02:00
|
|
|
from pathlib import Path
|
2020-04-15 14:04:30 +02:00
|
|
|
import uvicorn
|
|
|
|
from fastapi import FastAPI, HTTPException
|
|
|
|
from starlette.middleware.cors import CORSMiddleware
|
|
|
|
|
2021-10-04 11:21:00 +02:00
|
|
|
from haystack import Pipeline
|
|
|
|
from rest_api.config import PIPELINE_YAML_PATH, QUERY_PIPELINE_NAME
|
2020-06-22 12:07:12 +02:00
|
|
|
from rest_api.controller.errors.http_error import http_error_handler
|
2021-04-20 11:19:28 +02:00
|
|
|
from rest_api.config import ROOT_PATH
|
2020-04-15 14:04:30 +02:00
|
|
|
|
2021-10-04 11:21:00 +02:00
|
|
|
|
2020-04-15 14:04:30 +02:00
|
|
|
logging.basicConfig(format="%(asctime)s %(message)s", datefmt="%m/%d/%Y %I:%M:%S %p")
|
|
|
|
logger = logging.getLogger(__name__)
|
|
|
|
logging.getLogger("elasticsearch").setLevel(logging.WARNING)
|
2021-04-07 17:53:32 +02:00
|
|
|
logging.getLogger("haystack").setLevel(logging.INFO)
|
2020-04-15 14:04:30 +02:00
|
|
|
|
|
|
|
|
2021-10-04 11:21:00 +02:00
|
|
|
PIPELINE = Pipeline.load_from_yaml(Path(PIPELINE_YAML_PATH), pipeline_name=QUERY_PIPELINE_NAME)
|
|
|
|
# TODO make this generic for other pipelines with different naming
|
|
|
|
RETRIEVER = PIPELINE.get_node(name="Retriever")
|
|
|
|
DOCUMENT_STORE = RETRIEVER.document_store if RETRIEVER else None
|
|
|
|
logging.info(f"Loaded pipeline nodes: {PIPELINE.graph.nodes.keys()}")
|
|
|
|
|
|
|
|
|
|
|
|
from rest_api.controller.router import router as api_router
|
|
|
|
|
|
|
|
|
2020-04-15 14:04:30 +02:00
|
|
|
def get_application() -> FastAPI:
|
2021-04-20 11:19:28 +02:00
|
|
|
application = FastAPI(title="Haystack-API", debug=True, version="0.1", root_path=ROOT_PATH)
|
2020-04-15 14:04:30 +02:00
|
|
|
|
2021-04-07 17:53:32 +02:00
|
|
|
# This middleware enables allow all cross-domain requests to the API from a browser. For production
|
|
|
|
# deployments, it could be made more restrictive.
|
2020-04-15 14:04:30 +02:00
|
|
|
application.add_middleware(
|
|
|
|
CORSMiddleware, allow_origins=["*"], allow_credentials=True, allow_methods=["*"], allow_headers=["*"],
|
|
|
|
)
|
2020-04-17 15:29:19 +02:00
|
|
|
|
2020-04-15 14:04:30 +02:00
|
|
|
application.add_exception_handler(HTTPException, http_error_handler)
|
|
|
|
|
|
|
|
application.include_router(api_router)
|
|
|
|
|
|
|
|
return application
|
|
|
|
|
|
|
|
|
|
|
|
app = get_application()
|
|
|
|
|
2021-10-04 11:21:00 +02:00
|
|
|
|
2020-04-15 14:04:30 +02:00
|
|
|
logger.info("Open http://127.0.0.1:8000/docs to see Swagger API Documentation.")
|
|
|
|
logger.info(
|
|
|
|
"""
|
2021-07-19 12:57:43 +02:00
|
|
|
Or just try it out directly: curl --request POST --url 'http://127.0.0.1:8000/query' -H "Content-Type: application/json" --data '{"query": "Did Albus Dumbledore die?"}'
|
|
|
|
"""
|
2020-04-15 14:04:30 +02:00
|
|
|
)
|
|
|
|
|
|
|
|
if __name__ == "__main__":
|
|
|
|
uvicorn.run(app, host="0.0.0.0", port=8000)
|