Skip to content

Commit dddc836

Browse files
pavanjavapavanmantha
and
pavanmantha
authored
Fast api impl to all templates (#29)
* -created the required configs for pypi * -modified the configs accordingly * -implemented fastapi support for all the RAG templates --------- Co-authored-by: pavanmantha <[email protected]>
1 parent bdd3354 commit dddc836

File tree

35 files changed

+670
-0
lines changed

35 files changed

+670
-0
lines changed

bootstraprag/templates/llamaindex/rag_with_flare/api_core/__init__.py

Whitespace-only changes.
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,5 @@
1+
class Settings:
2+
PROJECT_NAME: str = "Simple RAG as FastAPI Application"
3+
4+
5+
settings = Settings()

bootstraprag/templates/llamaindex/rag_with_flare/api_routes/__init__.py

Whitespace-only changes.
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,14 @@
1+
from fastapi import APIRouter, Depends
2+
from models.payload import Payload
3+
from base_rag import BaseRAG
4+
5+
6+
base_rag = BaseRAG(show_progress=True, data_path='data')
7+
8+
router = APIRouter(prefix="/api/v1/rag", tags=["rag"])
9+
10+
11+
@router.post(path='/query')
12+
def fetch_response(payload: Payload):
13+
response = base_rag.query(query_string=payload.query)
14+
return response
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,110 @@
1+
from fastapi import FastAPI, Request
2+
from fastapi.openapi.utils import get_openapi
3+
from api_routes.apis import router
4+
from fastapi.middleware.cors import CORSMiddleware
5+
import uvicorn
6+
import logging
7+
import time
8+
9+
logging.basicConfig(level=logging.DEBUG)
10+
logging.basicConfig(
11+
level=logging.INFO,
12+
format="%(asctime)s [%(levelname)s] %(name)s: %(message)s",
13+
)
14+
logger = logging.getLogger(__name__)
15+
allowed_origins = [
16+
"*"
17+
]
18+
19+
app = FastAPI(
20+
title="My FastAPI Application",
21+
description="This is a FastAPI implementation for RAG application with Swagger UI configurations.",
22+
version="1.0.0",
23+
docs_url="/documentation",
24+
redoc_url="/redoc",
25+
openapi_url="/openapi.json",
26+
contact={
27+
"name": "M K Pavan Kumar",
28+
"linkedin": "https://www.linkedin.com",
29+
},
30+
license_info={
31+
"name": "MIT License",
32+
"url": "https://opensource.org/licenses/MIT",
33+
},
34+
terms_of_service="https://www.yourwebsite.com/terms/",
35+
)
36+
app.add_middleware(
37+
CORSMiddleware,
38+
allow_origins=allowed_origins,
39+
allow_credentials=True,
40+
allow_methods=["*"],
41+
allow_headers=["*"],
42+
)
43+
app.include_router(router)
44+
45+
46+
# Custom OpenAPI schema generation (optional)
47+
def custom_openapi():
48+
if app.openapi_schema:
49+
return app.openapi_schema
50+
openapi_schema = get_openapi(
51+
title="RAG APIs",
52+
version="1.0.0",
53+
description="This is a custom OpenAPI schema with additional metadata.",
54+
routes=app.routes,
55+
tags=[
56+
{
57+
"name": "rag",
58+
"description": "Operations for RAG query.",
59+
}
60+
],
61+
)
62+
# Modify openapi_schema as needed
63+
app.openapi_schema = openapi_schema
64+
return app.openapi_schema
65+
66+
67+
app.openapi = custom_openapi
68+
69+
70+
@app.middleware("http")
71+
async def log_requests(request: Request, call_next):
72+
try:
73+
logger.info(f"Incoming request: {request.method} {request.url}")
74+
response = await call_next(request)
75+
logger.info(f"Response status: {response.status_code}")
76+
return response
77+
except Exception as e:
78+
logger.exception(f"Error processing request: {e}")
79+
raise e
80+
81+
82+
# Request Timing Middleware
83+
@app.middleware("http")
84+
async def add_process_time_header(request: Request, call_next):
85+
start_time = time.time()
86+
response = await call_next(request)
87+
process_time = time.time() - start_time
88+
response.headers["X-Process-Time"] = str(process_time)
89+
logger.info(f"Processed in {process_time:.4f} seconds")
90+
return response
91+
92+
93+
# Logging Middleware
94+
@app.middleware("http")
95+
async def log_requests(request: Request, call_next):
96+
logger.info(f"Incoming request: {request.method} {request.url}")
97+
response = await call_next(request)
98+
logger.info(f"Response status: {response.status_code}")
99+
return response
100+
101+
102+
if __name__ == "__main__":
103+
uvicorn.run(
104+
"apis:app",
105+
host="127.0.0.1",
106+
port=8000,
107+
reload=True,
108+
log_level="info",
109+
workers=1,
110+
)

bootstraprag/templates/llamaindex/rag_with_flare/models/__init__.py

Whitespace-only changes.
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,5 @@
1+
from pydantic import BaseModel
2+
3+
4+
class Payload(BaseModel):
5+
query: str

bootstraprag/templates/llamaindex/rag_with_hyde/api_core/__init__.py

Whitespace-only changes.
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,5 @@
1+
class Settings:
2+
PROJECT_NAME: str = "Simple RAG as FastAPI Application"
3+
4+
5+
settings = Settings()

bootstraprag/templates/llamaindex/rag_with_hyde/api_routes/__init__.py

Whitespace-only changes.
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,14 @@
1+
from fastapi import APIRouter, Depends
2+
from models.payload import Payload
3+
from base_rag import BaseRAG
4+
5+
6+
base_rag = BaseRAG(show_progress=True, data_path='data')
7+
8+
router = APIRouter(prefix="/api/v1/rag", tags=["rag"])
9+
10+
11+
@router.post(path='/query')
12+
def fetch_response(payload: Payload):
13+
response = base_rag.query(query_string=payload.query)
14+
return response
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,110 @@
1+
from fastapi import FastAPI, Request
2+
from fastapi.openapi.utils import get_openapi
3+
from api_routes.apis import router
4+
from fastapi.middleware.cors import CORSMiddleware
5+
import uvicorn
6+
import logging
7+
import time
8+
9+
logging.basicConfig(level=logging.DEBUG)
10+
logging.basicConfig(
11+
level=logging.INFO,
12+
format="%(asctime)s [%(levelname)s] %(name)s: %(message)s",
13+
)
14+
logger = logging.getLogger(__name__)
15+
allowed_origins = [
16+
"*"
17+
]
18+
19+
app = FastAPI(
20+
title="My FastAPI Application",
21+
description="This is a FastAPI implementation for RAG application with Swagger UI configurations.",
22+
version="1.0.0",
23+
docs_url="/documentation",
24+
redoc_url="/redoc",
25+
openapi_url="/openapi.json",
26+
contact={
27+
"name": "M K Pavan Kumar",
28+
"linkedin": "https://www.linkedin.com",
29+
},
30+
license_info={
31+
"name": "MIT License",
32+
"url": "https://opensource.org/licenses/MIT",
33+
},
34+
terms_of_service="https://www.yourwebsite.com/terms/",
35+
)
36+
app.add_middleware(
37+
CORSMiddleware,
38+
allow_origins=allowed_origins,
39+
allow_credentials=True,
40+
allow_methods=["*"],
41+
allow_headers=["*"],
42+
)
43+
app.include_router(router)
44+
45+
46+
# Custom OpenAPI schema generation (optional)
47+
def custom_openapi():
48+
if app.openapi_schema:
49+
return app.openapi_schema
50+
openapi_schema = get_openapi(
51+
title="RAG APIs",
52+
version="1.0.0",
53+
description="This is a custom OpenAPI schema with additional metadata.",
54+
routes=app.routes,
55+
tags=[
56+
{
57+
"name": "rag",
58+
"description": "Operations for RAG query.",
59+
}
60+
],
61+
)
62+
# Modify openapi_schema as needed
63+
app.openapi_schema = openapi_schema
64+
return app.openapi_schema
65+
66+
67+
app.openapi = custom_openapi
68+
69+
70+
@app.middleware("http")
71+
async def log_requests(request: Request, call_next):
72+
try:
73+
logger.info(f"Incoming request: {request.method} {request.url}")
74+
response = await call_next(request)
75+
logger.info(f"Response status: {response.status_code}")
76+
return response
77+
except Exception as e:
78+
logger.exception(f"Error processing request: {e}")
79+
raise e
80+
81+
82+
# Request Timing Middleware
83+
@app.middleware("http")
84+
async def add_process_time_header(request: Request, call_next):
85+
start_time = time.time()
86+
response = await call_next(request)
87+
process_time = time.time() - start_time
88+
response.headers["X-Process-Time"] = str(process_time)
89+
logger.info(f"Processed in {process_time:.4f} seconds")
90+
return response
91+
92+
93+
# Logging Middleware
94+
@app.middleware("http")
95+
async def log_requests(request: Request, call_next):
96+
logger.info(f"Incoming request: {request.method} {request.url}")
97+
response = await call_next(request)
98+
logger.info(f"Response status: {response.status_code}")
99+
return response
100+
101+
102+
if __name__ == "__main__":
103+
uvicorn.run(
104+
"apis:app",
105+
host="127.0.0.1",
106+
port=8000,
107+
reload=True,
108+
log_level="info",
109+
workers=1,
110+
)

bootstraprag/templates/llamaindex/rag_with_hyde/models/__init__.py

Whitespace-only changes.
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,5 @@
1+
from pydantic import BaseModel
2+
3+
4+
class Payload(BaseModel):
5+
query: str

bootstraprag/templates/llamaindex/rag_with_hyde_with_observability/api_core/__init__.py

Whitespace-only changes.
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,5 @@
1+
class Settings:
2+
PROJECT_NAME: str = "Simple RAG as FastAPI Application"
3+
4+
5+
settings = Settings()

bootstraprag/templates/llamaindex/rag_with_hyde_with_observability/api_routes/__init__.py

Whitespace-only changes.
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,14 @@
1+
from fastapi import APIRouter, Depends
2+
from models.payload import Payload
3+
from base_rag import BaseRAG
4+
5+
6+
base_rag = BaseRAG(show_progress=True, data_path='data')
7+
8+
router = APIRouter(prefix="/api/v1/rag", tags=["rag"])
9+
10+
11+
@router.post(path='/query')
12+
def fetch_response(payload: Payload):
13+
response = base_rag.query(query_string=payload.query)
14+
return response

0 commit comments

Comments
 (0)