diff --git a/app/__init__.py b/app/__init__.py index 8c4f4e7..acd8b3c 100644 --- a/app/__init__.py +++ b/app/__init__.py @@ -2,9 +2,11 @@ from .main import app from .routers.upload import router as upload_router +from .routers.questionGenerate import router as questionGenerate_router app.include_router(upload_router, prefix="/api/v1") +app.include_router(questionGenerate_router, prefix="/api/v1") # app/routers/upload.py diff --git a/app/routers/questionGenerate.py b/app/routers/questionGenerate.py new file mode 100644 index 0000000..0a2dc9d --- /dev/null +++ b/app/routers/questionGenerate.py @@ -0,0 +1,14 @@ +from fastapi import APIRouter, Query, HTTPException +from typing import List + +from ..services.prompt import prompt + + +router = APIRouter() + +@router.get("/generate-question/", response_model=str) +async def generate_question(text: str = Query(..., description="The text to generate a question for"), + examid: str = Query(..., description="The ID of the exam related to the text")) -> str: + """Endpoint to generate a question for a given text using OpenAI's model.""" + + return prompt(text, examid) \ No newline at end of file diff --git a/app/routers/upload.py b/app/routers/upload.py index 6d159cf..c2a1bb8 100644 --- a/app/routers/upload.py +++ b/app/routers/upload.py @@ -1,4 +1,4 @@ -from fastapi import APIRouter, File, UploadFile, HTTPException, Response +from fastapi import APIRouter, File, UploadFile, HTTPException, Response, Query import os from ..services.pinecone_upsert import upsert @@ -6,13 +6,15 @@ router = APIRouter() @router.post("/upload-pdf/", status_code=201) -async def upload_pdf(file: UploadFile = File(...)) -> dict: +async def upload_pdf(file: UploadFile = File(...), examid:str = Query(..., description="The ID of the exam related to the uploaded PDF") ) -> dict: """Endpoint to upload a PDF and upsert its contents into a Pinecone vector store.""" if file.content_type != 'application/pdf': raise HTTPException(status_code=415, detail="Unsupported file type. Please upload a PDF.") # Call the upsert function from the imported service - upsert(file, examid='examid') + upsert(file, examid) - return {"filename": file.filename} + # return {"filename": file.filename} + Response(status_code=201) + return {"message": "PDF uploaded successfully."} diff --git a/app/services/prompt.py b/app/services/prompt.py new file mode 100644 index 0000000..0d5c2a6 --- /dev/null +++ b/app/services/prompt.py @@ -0,0 +1,64 @@ +from typing import Any, BinaryIO +import os +import dotenv +import pdfplumber +from langchain_openai import OpenAIEmbeddings +from langchain_pinecone import PineconeVectorStore +from pinecone import Pinecone, ServerlessSpec + +from langchain.chat_models import ChatOpenAI +from langchain.chains import RetrievalQA + + + +dotenv.load_dotenv() + +pinecone = Pinecone(api_key=os.getenv('PINECONE_API_KEY')) + + + + +def prompt(text: str, examid: str) -> str: + """Upserts PDF text into a Pinecone vector store and returns the extracted text.""" + + embed = OpenAIEmbeddings( + model="text-embedding-3-large", + api_key=os.getenv('OPENAI_API_KEY'), + dimensions=3072 + ) + + vectorstore = PineconeVectorStore( + namespace=examid, + index_name="abc", + embedding=embed + + ) + + vectorstore.similarity_search( + text, + # top_k=5 + ) + + llm = ChatOpenAI( + model="gpt-3.5-turbo", + api_key=os.getenv('OPENAI_API_KEY') + ) + + qa = RetrievalQA.from_chain_type( + llm=llm, + chain_type="stuff", + retriever=vectorstore.as_retriever() + ) + + print(qa.invoke(text)) + return "Question generated successfully." + + + + + + + + + +