Spaces:
Build error
Build error
Commit
·
7733c24
1
Parent(s):
e0c1af0
Test RAG with background task
Browse files- app/main.py +23 -7
app/main.py
CHANGED
|
@@ -2,6 +2,7 @@ from fastapi import FastAPI, Request, status
|
|
| 2 |
from fastapi.responses import JSONResponse
|
| 3 |
from fastapi.responses import Response
|
| 4 |
from fastapi.exceptions import HTTPException
|
|
|
|
| 5 |
from slowapi import Limiter, _rate_limit_exceeded_handler
|
| 6 |
from slowapi.errors import RateLimitExceeded
|
| 7 |
from slowapi.util import get_remote_address
|
|
@@ -107,7 +108,7 @@ class WebhookPayload(BaseModel):
|
|
| 107 |
|
| 108 |
@app.post("/webhook")
|
| 109 |
@limiter.limit("20/minute")
|
| 110 |
-
async def webhook(request: Request):
|
| 111 |
try:
|
| 112 |
payload = await request.json()
|
| 113 |
|
|
@@ -125,23 +126,38 @@ async def webhook(request: Request):
|
|
| 125 |
llm_model = request.query_params.get("cx_code")
|
| 126 |
|
| 127 |
# Return HTTP 200 immediately
|
| 128 |
-
response = JSONResponse(
|
| 129 |
-
|
| 130 |
-
|
| 131 |
-
)
|
| 132 |
|
| 133 |
print(f"payload: {payload}")
|
| 134 |
-
response = await webhook_handler.process_webhook(
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 135 |
payload=payload,
|
| 136 |
whatsapp_token=ACCESS_TOKEN,
|
| 137 |
whatsapp_url=WHATSAPP_API_URL,
|
| 138 |
gemini_api=GEMINI_API,
|
| 139 |
rag_system=rag_system,
|
| 140 |
)
|
|
|
|
| 141 |
return JSONResponse(
|
| 142 |
-
content=
|
| 143 |
status_code=status.HTTP_200_OK
|
| 144 |
)
|
|
|
|
|
|
|
|
|
|
|
|
|
| 145 |
|
| 146 |
except ValidationError as ve:
|
| 147 |
logger.error(f"Validation error: {ve}")
|
|
|
|
| 2 |
from fastapi.responses import JSONResponse
|
| 3 |
from fastapi.responses import Response
|
| 4 |
from fastapi.exceptions import HTTPException
|
| 5 |
+
from fastapi.background import BackgroundTasks
|
| 6 |
from slowapi import Limiter, _rate_limit_exceeded_handler
|
| 7 |
from slowapi.errors import RateLimitExceeded
|
| 8 |
from slowapi.util import get_remote_address
|
|
|
|
| 108 |
|
| 109 |
@app.post("/webhook")
|
| 110 |
@limiter.limit("20/minute")
|
| 111 |
+
async def webhook(request: Request, background_tasks: BackgroundTasks):
|
| 112 |
try:
|
| 113 |
payload = await request.json()
|
| 114 |
|
|
|
|
| 126 |
llm_model = request.query_params.get("cx_code")
|
| 127 |
|
| 128 |
# Return HTTP 200 immediately
|
| 129 |
+
# response = JSONResponse(
|
| 130 |
+
# content={"status": "received"},
|
| 131 |
+
# status_code=200
|
| 132 |
+
# )
|
| 133 |
|
| 134 |
print(f"payload: {payload}")
|
| 135 |
+
# response = await webhook_handler.process_webhook(
|
| 136 |
+
# payload=payload,
|
| 137 |
+
# whatsapp_token=ACCESS_TOKEN,
|
| 138 |
+
# whatsapp_url=WHATSAPP_API_URL,
|
| 139 |
+
# gemini_api=GEMINI_API,
|
| 140 |
+
# rag_system=rag_system,
|
| 141 |
+
# )
|
| 142 |
+
|
| 143 |
+
# Add the processing to background tasks
|
| 144 |
+
background_tasks.add_task(
|
| 145 |
+
webhook_handler.process_webhook,
|
| 146 |
payload=payload,
|
| 147 |
whatsapp_token=ACCESS_TOKEN,
|
| 148 |
whatsapp_url=WHATSAPP_API_URL,
|
| 149 |
gemini_api=GEMINI_API,
|
| 150 |
rag_system=rag_system,
|
| 151 |
)
|
| 152 |
+
# Return HTTP 200 immediately
|
| 153 |
return JSONResponse(
|
| 154 |
+
content={"status": "received"},
|
| 155 |
status_code=status.HTTP_200_OK
|
| 156 |
)
|
| 157 |
+
# return JSONResponse(
|
| 158 |
+
# content=response.__dict__,
|
| 159 |
+
# status_code=status.HTTP_200_OK
|
| 160 |
+
# )
|
| 161 |
|
| 162 |
except ValidationError as ve:
|
| 163 |
logger.error(f"Validation error: {ve}")
|