feat: quiz endpoint second varient
Browse files- .gitignore +3 -1
- Backend/app/api/v1/endpoints/quiz.py +67 -3
- Backend/app/schema/__init__.py +2 -2
- Backend/app/schema/models.py +6 -1
- Backend/app/vector_db.py +0 -4
- RUN.md +4 -4
.gitignore
CHANGED
|
@@ -222,7 +222,8 @@ test/1000-data-science-questions-answers.json
|
|
| 222 |
test/test.ipynb
|
| 223 |
vector_db
|
| 224 |
.gitignore
|
| 225 |
-
|
|
|
|
| 226 |
test.ipynb
|
| 227 |
.gitignore
|
| 228 |
test.json
|
|
@@ -230,3 +231,4 @@ test.json
|
|
| 230 |
#db files
|
| 231 |
.sqlite3
|
| 232 |
.bin
|
|
|
|
|
|
| 222 |
test/test.ipynb
|
| 223 |
vector_db
|
| 224 |
.gitignore
|
| 225 |
+
chroma_store
|
| 226 |
+
Backend/chroma_store
|
| 227 |
test.ipynb
|
| 228 |
.gitignore
|
| 229 |
test.json
|
|
|
|
| 231 |
#db files
|
| 232 |
.sqlite3
|
| 233 |
.bin
|
| 234 |
+
chroma_store/chroma.sqlite3
|
Backend/app/api/v1/endpoints/quiz.py
CHANGED
|
@@ -2,12 +2,15 @@ from fastapi import APIRouter, Depends, HTTPException, status
|
|
| 2 |
from chromadb import AsyncHttpClient
|
| 3 |
from app.models import User
|
| 4 |
from app.api.deps import get_db, get_current_user, get_chroma_client
|
| 5 |
-
from app.schema import Quiz_input, QuizOutput
|
| 6 |
from .prompts import SYSTEM_PROMPT
|
| 7 |
from fastapi import APIRouter, Depends, HTTPException
|
| 8 |
from chromadb.api.models.Collection import Collection # Import Collection type
|
| 9 |
from app.api.deps import get_chroma_collection
|
| 10 |
from app.llm import call_llm
|
|
|
|
|
|
|
|
|
|
| 11 |
router = APIRouter(prefix="/quiz")
|
| 12 |
|
| 13 |
async def search_logic(query: str, collection: Collection):
|
|
@@ -28,8 +31,8 @@ async def search_documents(
|
|
| 28 |
raise HTTPException(500, f"ChromaDB Query Error: {e}")
|
| 29 |
|
| 30 |
|
| 31 |
-
@router.post("/", response_model=QuizOutput, status_code=status.HTTP_201_CREATED)
|
| 32 |
-
async def
|
| 33 |
Input_model: Quiz_input,
|
| 34 |
collection: Collection = Depends(get_chroma_collection),
|
| 35 |
current_user: User = Depends(get_current_user)
|
|
@@ -53,6 +56,67 @@ async def generate_quiz(
|
|
| 53 |
detail=f'Invalid Input: {str(e)}'
|
| 54 |
)
|
| 55 |
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 56 |
# #--------Helper Functions--------#
|
| 57 |
|
| 58 |
|
|
|
|
| 2 |
from chromadb import AsyncHttpClient
|
| 3 |
from app.models import User
|
| 4 |
from app.api.deps import get_db, get_current_user, get_chroma_client
|
| 5 |
+
from app.schema import Quiz_input, QuizOutput, IngestRequest
|
| 6 |
from .prompts import SYSTEM_PROMPT
|
| 7 |
from fastapi import APIRouter, Depends, HTTPException
|
| 8 |
from chromadb.api.models.Collection import Collection # Import Collection type
|
| 9 |
from app.api.deps import get_chroma_collection
|
| 10 |
from app.llm import call_llm
|
| 11 |
+
import uuid
|
| 12 |
+
|
| 13 |
+
|
| 14 |
router = APIRouter(prefix="/quiz")
|
| 15 |
|
| 16 |
async def search_logic(query: str, collection: Collection):
|
|
|
|
| 31 |
raise HTTPException(500, f"ChromaDB Query Error: {e}")
|
| 32 |
|
| 33 |
|
| 34 |
+
@router.post("/resume", response_model=QuizOutput, status_code=status.HTTP_201_CREATED)
|
| 35 |
+
async def generate_quiz_resume(
|
| 36 |
Input_model: Quiz_input,
|
| 37 |
collection: Collection = Depends(get_chroma_collection),
|
| 38 |
current_user: User = Depends(get_current_user)
|
|
|
|
| 56 |
detail=f'Invalid Input: {str(e)}'
|
| 57 |
)
|
| 58 |
|
| 59 |
+
|
| 60 |
+
async def ingest_logic(input_data:IngestRequest , collection: Collection):
|
| 61 |
+
doc_id = input_data.id if input_data.id else str(uuid.uuid4())
|
| 62 |
+
|
| 63 |
+
await collection.add(
|
| 64 |
+
ids = [doc_id],
|
| 65 |
+
documents=[input_data.parsed_doc],
|
| 66 |
+
metadatas=[{"user_prompt": input_data.user_prompt}]
|
| 67 |
+
)
|
| 68 |
+
|
| 69 |
+
return {
|
| 70 |
+
"status": "success",
|
| 71 |
+
"id": doc_id,
|
| 72 |
+
"stored_prompt": input_data.user_prompt
|
| 73 |
+
}
|
| 74 |
+
|
| 75 |
+
@router.post("/ingest", status_code=status.HTTP_201_CREATED)
|
| 76 |
+
async def ingest_data(
|
| 77 |
+
input_data: IngestRequest,
|
| 78 |
+
collection: Collection = Depends(get_chroma_collection),
|
| 79 |
+
current_user: User = Depends(get_current_user)
|
| 80 |
+
):
|
| 81 |
+
try:
|
| 82 |
+
return await ingest_logic(input_data, collection)
|
| 83 |
+
except Exception as e:
|
| 84 |
+
raise HTTPException(
|
| 85 |
+
status_code=status.HTTP_500_INTERNAL_SERVER_ERROR,
|
| 86 |
+
detail= f"Ingestion failed: {str(e)}"
|
| 87 |
+
)
|
| 88 |
+
|
| 89 |
+
|
| 90 |
+
|
| 91 |
+
@router.post("/notes", response_model=QuizOutput, status_code=status.HTTP_201_CREATED)
|
| 92 |
+
async def generate_quiz_notes(
|
| 93 |
+
Input_model: IngestRequest,
|
| 94 |
+
collection: Collection = Depends(get_chroma_collection),
|
| 95 |
+
current_user: User = Depends(get_current_user)
|
| 96 |
+
):
|
| 97 |
+
try:
|
| 98 |
+
notes = Input_model
|
| 99 |
+
await ingest_logic(notes, collection)
|
| 100 |
+
|
| 101 |
+
query = Input_model.user_prompt
|
| 102 |
+
retrieved_context = await search_logic(query, collection)
|
| 103 |
+
|
| 104 |
+
|
| 105 |
+
if not retrieved_context:
|
| 106 |
+
raise ValueError("No context available to generate quiz.")
|
| 107 |
+
prompt = await prompt_builder(Input_model.parsed_doc, Input_model.user_prompt, retrieved_context)
|
| 108 |
+
|
| 109 |
+
quiz_data_obj = await call_llm(prompt)
|
| 110 |
+
|
| 111 |
+
return quiz_data_obj
|
| 112 |
+
|
| 113 |
+
except Exception as e:
|
| 114 |
+
raise HTTPException(
|
| 115 |
+
status_code=status.HTTP_400_BAD_REQUEST,
|
| 116 |
+
detail=f'Invalid Input: {str(e)}'
|
| 117 |
+
)
|
| 118 |
+
|
| 119 |
+
|
| 120 |
# #--------Helper Functions--------#
|
| 121 |
|
| 122 |
|
Backend/app/schema/__init__.py
CHANGED
|
@@ -1,3 +1,3 @@
|
|
| 1 |
-
from app.schema.models import StudentCreate, StudentUpdate, StudentResponse, UserCreate, Token, LoginRequest, Quiz_input, QuizOutput
|
| 2 |
|
| 3 |
-
__all__ = ["StudentCreate", "StudentUpdate", "StudentResponse", "UserCreate", "Token", "LoginRequest", "Quiz_input", "QuizOutput"]
|
|
|
|
| 1 |
+
from app.schema.models import StudentCreate, StudentUpdate, StudentResponse, UserCreate, Token, LoginRequest, Quiz_input, QuizOutput, IngestRequest
|
| 2 |
|
| 3 |
+
__all__ = ["StudentCreate", "StudentUpdate", "StudentResponse", "UserCreate", "Token", "LoginRequest", "Quiz_input", "QuizOutput", "IngestRequest"]
|
Backend/app/schema/models.py
CHANGED
|
@@ -61,4 +61,9 @@ class QuizQuestion(BaseModel):
|
|
| 61 |
User_response: str = Field("", alias="User_response")
|
| 62 |
|
| 63 |
class QuizOutput(BaseModel):
|
| 64 |
-
quiz: List[QuizQuestion] = Field(..., description="A list of 10 generated MCQ questions.")
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 61 |
User_response: str = Field("", alias="User_response")
|
| 62 |
|
| 63 |
class QuizOutput(BaseModel):
|
| 64 |
+
quiz: List[QuizQuestion] = Field(..., description="A list of 10 generated MCQ questions.")
|
| 65 |
+
|
| 66 |
+
class IngestRequest(BaseModel):
|
| 67 |
+
parsed_doc: str = Field(..., description="The main document content to embed")
|
| 68 |
+
user_prompt: str = Field(..., description="The user prompt associated with this document")
|
| 69 |
+
id: Optional[str] = None
|
Backend/app/vector_db.py
DELETED
|
@@ -1,4 +0,0 @@
|
|
| 1 |
-
import chromadb
|
| 2 |
-
from chromadb.config import Settings
|
| 3 |
-
|
| 4 |
-
chroma_client = None
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
RUN.md
CHANGED
|
@@ -1,7 +1,7 @@
|
|
| 1 |
-
npm run dev (frontend)
|
| 2 |
-
|
| 3 |
-
python run.py (Backend)
|
| 4 |
-
|
| 5 |
sudo service postgresql start (POstgresql)
|
| 6 |
|
| 7 |
chroma run --host 0.0.0.0 --port 8080 --path ./chroma_store (chroma_db) (project root folder)
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 1 |
sudo service postgresql start (POstgresql)
|
| 2 |
|
| 3 |
chroma run --host 0.0.0.0 --port 8080 --path ./chroma_store (chroma_db) (project root folder)
|
| 4 |
+
|
| 5 |
+
npm run dev (frontend)
|
| 6 |
+
|
| 7 |
+
python run.py (Backend)
|