Skip to content

Commit a8678b7

Browse files
pavanjavapavanmantha
andauthored
Fastapi impl (#25)
* -product pitch presentation * -llama-deploy implementation, -implemented fastapi for all templates, -partial implementation of docker for templates * implemented the llama-deploy and workflows with simplemq and rabbitmq * - modified the docs * - implemented kafaka queue mechanism * - modified the docs and version * - implemented fastapi for simple rag * - refactoring the code * -implemented fastapi for simple rag * -implemented fastapi for self correcting rag * -modified docs for fastapi impl --------- Co-authored-by: pavanmantha <pavan.mantha@thevaslabs.io>
1 parent 675805f commit a8678b7

File tree

34 files changed

+558
-3
lines changed

34 files changed

+558
-3
lines changed

bootstraprag/templates/llamaindex/llama_deploy_with_kafka/readme.md

Lines changed: 0 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -11,4 +11,3 @@
1111
- `python deploy_code.py`
1212
- `python deploy_workflow.py`
1313
- `python main.py`
14-

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

Whitespace-only changes.
Lines changed: 5 additions & 0 deletions
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_self_correction/api_routes/__init__.py

Whitespace-only changes.
Lines changed: 14 additions & 0 deletions
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 self_correction_core import SelfCorrectingRAG
4+
5+
6+
self_correcting_rag = SelfCorrectingRAG(input_dir='data', show_progress=True, no_of_retries=3)
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 = self_correcting_rag.query_with_source_query_engine(query=payload.query)
14+
return response
Lines changed: 110 additions & 0 deletions
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_self_correction/models/__init__.py

Whitespace-only changes.
Lines changed: 5 additions & 0 deletions
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_self_correction/readme.md

Lines changed: 6 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -13,3 +13,9 @@
1313
- query_with_retry_query_engine(query=user_query)
1414
- query_with_source_query_engine(query=user_query)
1515
- query_with_guideline_query_engine(query=user_query)
16+
17+
### How to expose RAG as API
18+
- run `python apis.py`
19+
- verify the swagger redoc and documentation as below
20+
- open browser and hit `http://localhost:8000/redoc`
21+
- open browser and hit `http://localhost:8000/documentation`

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

Whitespace-only changes.

0 commit comments

Comments
 (0)