ashutoshneemval commited on
Commit
12e73e5
·
1 Parent(s): d84f05e

feat: Replace template with the user's actual CLAT guide website and add public chat endpoint

Browse files
Files changed (2) hide show
  1. backend/main.py +31 -0
  2. backend/templates/index.html +0 -0
backend/main.py CHANGED
@@ -4,11 +4,14 @@ import uvicorn
4
  from fastapi import FastAPI
5
  from fastapi.middleware.cors import CORSMiddleware
6
  from fastapi.responses import HTMLResponse
 
7
  from config import settings
8
  from database.connection import engine, Base
9
  from auth.routes import router as auth_router
10
  from rag.routes import router as rag_router
11
  from ai.routes import router as ai_router
 
 
12
 
13
  # Configure logging
14
  logging.basicConfig(
@@ -48,6 +51,9 @@ app.include_router(auth_router)
48
  app.include_router(rag_router)
49
  app.include_router(ai_router)
50
 
 
 
 
51
  @app.get("/", response_class=HTMLResponse)
52
  def read_root():
53
  # Serve index.html dynamically
@@ -58,5 +64,30 @@ def read_root():
58
  with open(filepath, "r", encoding="utf-8") as f:
59
  return HTMLResponse(content=f.read())
60
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
61
  if __name__ == "__main__":
62
  uvicorn.run("main:app", host="0.0.0.0", port=8000, reload=settings.DEBUG)
 
4
  from fastapi import FastAPI
5
  from fastapi.middleware.cors import CORSMiddleware
6
  from fastapi.responses import HTMLResponse
7
+ from pydantic import BaseModel
8
  from config import settings
9
  from database.connection import engine, Base
10
  from auth.routes import router as auth_router
11
  from rag.routes import router as rag_router
12
  from ai.routes import router as ai_router
13
+ from ai.nemotron import nemotron_client
14
+ from rag.service import rag_service
15
 
16
  # Configure logging
17
  logging.basicConfig(
 
51
  app.include_router(rag_router)
52
  app.include_router(ai_router)
53
 
54
+ class MessageRequest(BaseModel):
55
+ message: str
56
+
57
  @app.get("/", response_class=HTMLResponse)
58
  def read_root():
59
  # Serve index.html dynamically
 
64
  with open(filepath, "r", encoding="utf-8") as f:
65
  return HTMLResponse(content=f.read())
66
 
67
+ @app.post("/chat/message")
68
+ def chat_message_public(request: MessageRequest):
69
+ """Public chatbot endpoint utilized by the frontend study guide website."""
70
+ try:
71
+ # Retrieve context from vector db (default public query)
72
+ retrieved_chunks = rag_service.retrieve_context(
73
+ query=request.message,
74
+ user_id=0, # 0 indicates public/general database
75
+ top_k=3
76
+ )
77
+
78
+ context_text = ""
79
+ if retrieved_chunks:
80
+ context_text = "\n---\n".join([chunk["text"] for chunk in retrieved_chunks])
81
+
82
+ # Call NVIDIA Nemotron doubt solver
83
+ ai_response = nemotron_client.generate_response(
84
+ prompt=request.message,
85
+ context=context_text
86
+ )
87
+ return {"response": ai_response}
88
+ except Exception as e:
89
+ logger.error(f"Public /chat/message failed: {e}")
90
+ return {"response": f"Sorry, I encountered an internal error: {str(e)}"}
91
+
92
  if __name__ == "__main__":
93
  uvicorn.run("main:app", host="0.0.0.0", port=8000, reload=settings.DEBUG)
backend/templates/index.html CHANGED
The diff for this file is too large to render. See raw diff