|
from fastapi import FastAPI, Depends, HTTPException, status, Request |
|
from fastapi.responses import RedirectResponse |
|
from langserve import add_routes |
|
from rag_chroma import chain as rag_chroma_chain |
|
|
|
from dotenv import load_dotenv |
|
load_dotenv() |
|
|
|
import os |
|
|
|
|
|
app = FastAPI() |
|
|
|
API_KEY = os.getenv("BACK_API_KEY") |
|
|
|
async def get_api_key(request: Request): |
|
api_key = request.headers.get('x-api-key') |
|
if api_key != API_KEY: |
|
raise HTTPException( |
|
status_code=status.HTTP_401_UNAUTHORIZED, |
|
detail="Invalid API Key" |
|
) |
|
|
|
@app.get("/") |
|
async def redirect_root_to_docs(): |
|
return RedirectResponse("/docs") |
|
|
|
|
|
|
|
add_routes(app, rag_chroma_chain, |
|
path="/rag-chroma", |
|
|
|
dependencies=[Depends(get_api_key)] |
|
) |
|
|
|
if __name__ == "__main__": |
|
import uvicorn |
|
|
|
uvicorn.run(app, host="0.0.0.0", port=8000) |
|
|