Spaces:
Sleeping
Sleeping
| from fastapi import APIRouter, BackgroundTasks, HTTPException | |
| from app.ingestion.models import ChatbotIngest | |
| from app.ingestion.utils import extract_tally_payload | |
| from app.ingestion.workers import build_rag_for_chatbot | |
| from app.ingestion.demo_form_fetch_store import store_demo_chatbot | |
| router = APIRouter() | |
| FRONTEND_BASE_URL = "http://localhost:8000" | |
| async def ingest_chatbot_webhook(payload: dict,background_tasks: BackgroundTasks): | |
| """ | |
| Adapter endpoint for Tally webhook. | |
| Extracts form fields from Tally's nested payload and maps them to ChatbotIngest. | |
| """ | |
| try: | |
| #extact + validate payload | |
| ingest_dict = extract_tally_payload(payload) | |
| ingest_data = ChatbotIngest(**ingest_dict) | |
| print(ingest_data) | |
| #store in Mongodb | |
| chatbot_id = store_demo_chatbot(ingest_data) | |
| #trigger RAG build in background | |
| background_tasks.add_task(build_rag_for_chatbot, chatbot_id=chatbot_id) | |
| print(f"Background RAG build task added for chatbot {chatbot_id}") | |
| return { | |
| "chatbot_id": chatbot_id, | |
| "demo_url": f"{FRONTEND_BASE_URL}/chatbot/demo/{chatbot_id}", | |
| "message": "Demo chatbot ingestion succesfull" | |
| } | |
| except Exception as e: | |
| print(f"Error processing ingestion webhook: {e}") | |
| raise HTTPException(status_code=500, detail="Internal Server Error - Failed to ingest chatbot demo") | |