2020-04-15 14:04:30 +02:00 
										
									 
								 
							 
							
								
							 
							
								 
							
							
								import  logging  
						 
					
						
							
								
									
										
										
										
											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 
										
									 
								 
							 
							
								
							 
							
								 
							
							
								
							 
						 
					
						
							
								
									
										
										
										
											2022-02-09 17:35:18 +01:00 
										
									 
								 
							 
							
								
									
										 
								
							 
							
								 
							
							
								import  uvicorn  
						 
					
						
							
								
							 
							
								
							 
							
								 
							
							
								from  fastapi  import  FastAPI ,  HTTPException  
						 
					
						
							
								
							 
							
								
							 
							
								 
							
							
								from  fastapi . routing  import  APIRoute  
						 
					
						
							
								
							 
							
								
							 
							
								 
							
							
								from  fastapi . openapi . utils  import  get_openapi  
						 
					
						
							
								
							 
							
								
							 
							
								 
							
							
								from  starlette . middleware . cors  import  CORSMiddleware  
						 
					
						
							
								
									
										
										
										
											2022-01-26 18:12:55 +01:00 
										
									 
								 
							 
							
								
									
										 
								
							 
							
								 
							
							
								
							 
						 
					
						
							
								
									
										
										
										
											2022-02-09 17:35:18 +01:00 
										
									 
								 
							 
							
								
									
										 
								
							 
							
								 
							
							
								from  rest_api . controller . errors . http_error  import  http_error_handler  
						 
					
						
							
								
							 
							
								
							 
							
								 
							
							
								from  rest_api . config  import  ROOT_PATH  
						 
					
						
							
								
							 
							
								
							 
							
								 
							
							
								from  rest_api . controller . router  import  router  as  api_router  
						 
					
						
							
								
									
										
										
										
											2022-02-11 14:17:26 +01:00 
										
									 
								 
							 
							
								
									
										 
								
							 
							
								 
							
							
								from  haystack  import  __version__  as  haystack_version  
						 
					
						
							
								
									
										
										
										
											2021-10-04 11:21:00 +02:00 
										
									 
								 
							 
							
								
									
										 
								
							 
							
								 
							
							
								
							 
						 
					
						
							
								
							 
							
								
							 
							
								 
							
							
								
							 
						 
					
						
							
								
									
										
										
										
											2020-04-15 14:04:30 +02:00 
										
									 
								 
							 
							
								
							 
							
								 
							
							
								def  get_application ( )  - >  FastAPI :  
						 
					
						
							
								
									
										
										
										
											2022-02-11 14:17:26 +01:00 
										
									 
								 
							 
							
								
									
										 
								
							 
							
								 
							
							
								    application  =  FastAPI ( title = " Haystack REST API " ,  debug = True ,  version = haystack_version ,  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 ( 
							 
						 
					
						
							
								
									
										
										
										
											2022-03-07 19:25:33 +01:00 
										
									 
								 
							 
							
								
									
										 
								
							 
							
								 
							
							
								        CORSMiddleware ,  allow_origins = [ " * " ] ,  allow_credentials = True ,  allow_methods = [ " * " ] ,  allow_headers = [ " * " ] 
							 
						 
					
						
							
								
									
										
										
										
											2020-04-15 14:04:30 +02:00 
										
									 
								 
							 
							
								
							 
							
								 
							
							
								    ) 
							 
						 
					
						
							
								
							 
							
								
							 
							
								 
							
							
								    application . add_exception_handler ( HTTPException ,  http_error_handler ) 
							 
						 
					
						
							
								
							 
							
								
							 
							
								 
							
							
								    application . include_router ( api_router ) 
							 
						 
					
						
							
								
							 
							
								
							 
							
								 
							
							
								
							 
						 
					
						
							
								
							 
							
								
							 
							
								 
							
							
								    return  application 
							 
						 
					
						
							
								
							 
							
								
							 
							
								 
							
							
								
							 
						 
					
						
							
								
									
										
										
										
											2022-01-27 13:06:01 +01:00 
										
									 
								 
							 
							
								
									
										 
								
							 
							
								 
							
							
								
							 
						 
					
						
							
								
							 
							
								
							 
							
								 
							
							
								def  get_openapi_specs ( )  - >  dict :  
						 
					
						
							
								
							 
							
								
							 
							
								 
							
							
								    """ 
 
							 
						 
					
						
							
								
							 
							
								
							 
							
								 
							
							
								    Used  to  autogenerate  OpenAPI  specs  file  to  use  in  the  documentation . 
							 
						 
					
						
							
								
									
										
										
										
											2022-02-03 13:43:18 +01:00 
										
									 
								 
							 
							
								
									
										 
								
							 
							
								 
							
							
								
							 
						 
					
						
							
								
									
										
										
										
											2022-01-27 13:06:01 +01:00 
										
									 
								 
							 
							
								
									
										 
								
							 
							
								 
							
							
								    See  ` docs / _src / api / openapi / generate_openapi_specs . py ` 
							 
						 
					
						
							
								
							 
							
								
							 
							
								 
							
							
								    """ 
 
							 
						 
					
						
							
								
							 
							
								
							 
							
								 
							
							
								    app  =  get_application ( ) 
							 
						 
					
						
							
								
							 
							
								
							 
							
								 
							
							
								    return  get_openapi ( 
							 
						 
					
						
							
								
									
										
										
										
											2022-02-09 18:27:12 +01:00 
										
									 
								 
							 
							
								
									
										 
								
							 
							
								 
							
							
								        title = app . title , 
							 
						 
					
						
							
								
							 
							
								
							 
							
								 
							
							
								        version = app . version , 
							 
						 
					
						
							
								
							 
							
								
							 
							
								 
							
							
								        openapi_version = app . openapi_version , 
							 
						 
					
						
							
								
							 
							
								
							 
							
								 
							
							
								        description = app . description , 
							 
						 
					
						
							
								
							 
							
								
							 
							
								 
							
							
								        routes = app . routes , 
							 
						 
					
						
							
								
									
										
										
										
											2022-01-27 13:06:01 +01:00 
										
									 
								 
							 
							
								
									
										 
								
							 
							
								 
							
							
								    ) 
							 
						 
					
						
							
								
							 
							
								
							 
							
								 
							
							
								
							 
						 
					
						
							
								
							 
							
								
							 
							
								 
							
							
								
							 
						 
					
						
							
								
									
										
										
										
											2021-11-11 09:40:58 +01:00 
										
									 
								 
							 
							
								
									
										 
								
							 
							
								 
							
							
								def  use_route_names_as_operation_ids ( app :  FastAPI )  - >  None :  
						 
					
						
							
								
							 
							
								
							 
							
								 
							
							
								    """ 
 
							 
						 
					
						
							
								
							 
							
								
							 
							
								 
							
							
								    Simplify  operation  IDs  so  that  generated  API  clients  have  simpler  function 
							 
						 
					
						
							
								
							 
							
								
							 
							
								 
							
							
								    names  ( see  https : / / fastapi . tiangolo . com / advanced / path - operation - advanced - configuration / #using-the-path-operation-function-name-as-the-operationid). 
							 
						 
					
						
							
								
							 
							
								
							 
							
								 
							
							
								    The  operation  IDs  will  be  the  same  as  the  route  names  ( i . e .  the  python  method  names  of  the  endpoints ) 
							 
						 
					
						
							
								
							 
							
								
							 
							
								 
							
							
								    Should  be  called  only  after  all  routes  have  been  added . 
							 
						 
					
						
							
								
							 
							
								
							 
							
								 
							
							
								    """ 
 
							 
						 
					
						
							
								
							 
							
								
							 
							
								 
							
							
								    for  route  in  app . routes : 
							 
						 
					
						
							
								
							 
							
								
							 
							
								 
							
							
								        if  isinstance ( route ,  APIRoute ) : 
							 
						 
					
						
							
								
							 
							
								
							 
							
								 
							
							
								            route . operation_id  =  route . name 
							 
						 
					
						
							
								
									
										
										
										
											2020-04-15 14:04:30 +02:00 
										
									 
								 
							 
							
								
							 
							
								 
							
							
								
							 
						 
					
						
							
								
									
										
										
										
											2022-02-03 13:43:18 +01:00 
										
									 
								 
							 
							
								
									
										 
								
							 
							
								 
							
							
								
							 
						 
					
						
							
								
									
										
										
										
											2020-04-15 14:04:30 +02:00 
										
									 
								 
							 
							
								
							 
							
								 
							
							
								app  =  get_application ( )  
						 
					
						
							
								
									
										
										
										
											2021-11-11 09:40:58 +01:00 
										
									 
								 
							 
							
								
									
										 
								
							 
							
								 
							
							
								use_route_names_as_operation_ids ( app )  
						 
					
						
							
								
									
										
										
										
											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-11-18 18:13:03 +01: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 " :  " Who is the father of Arya Stark? " } ' 
							 
						 
					
						
							
								
									
										
										
										
											2021-07-19 12:57:43 +02:00 
										
									 
								 
							 
							
								
									
										 
								
							 
							
								 
							
							
								    """ 
 
							 
						 
					
						
							
								
									
										
										
										
											2020-04-15 14:04:30 +02:00 
										
									 
								 
							 
							
								
							 
							
								 
							
							
								)  
						 
					
						
							
								
							 
							
								
							 
							
								 
							
							
								
							 
						 
					
						
							
								
							 
							
								
							 
							
								 
							
							
								if  __name__  ==  " __main__ " :  
						 
					
						
							
								
							 
							
								
							 
							
								 
							
							
								    uvicorn . run ( app ,  host = " 0.0.0.0 " ,  port = 8000 )