import io from functions import * from PyPDF2 import PdfReader from fastapi import FastAPI, File, UploadFile from fastapi.middleware.cors import CORSMiddleware from langchain_community.document_loaders import UnstructuredURLLoader app = FastAPI(title = "ConversAI", root_path = "/api/v1") app.add_middleware( CORSMiddleware, allow_origins=["*"], allow_credentials=True, allow_methods=["*"], allow_headers=["*"], ) @app.post("/signup") async def signup(username: str, password: str): response = createUser(username = username, password = password) return response @app.post("/login") async def login(username: str, password: str): response = matchPassword(username = username, password = password) return response @app.post("/newChatbot") async def newChatbot(chatbotName: str, username: str): chatbotName = f"convai-{username}-{chatbotName}" return createTable(tablename = chatbotName) @app.post("/addPDF") async def addPDFData(vectorstore: str, pdf: UploadFile = File(...)): pdf = await pdf.read() reader = PdfReader(io.BytesIO(pdf)) text = "" for page in reader.pages: text += page.extract_text() return addDocuments(text = text, vectorstore = vectorstore) @app.post("/addText") async def addText(vectorstore: str, text: str): return addDocuments(text = text, vectorstore = vectorstore) @app.post("/addWebsite") async def addWebsite(vectorstore: str, websiteUrl: str): urls = getLinks(websiteUrl) loader = UnstructuredURLLoader(urls=urls) docs = loader.load() text = "\n\n\n\n".join([f"Metadata:\n{docs[doc].metadata} \nPage Content:\n {docs[doc].page_content}" for doc in range(len(docs))]) return addDocuments(text = text, vectorstore = vectorstore) @app.post("/answerQuery") async def answerQuestion(query: str, vectorstore: str, llmModel: str = "llama3-70b-8192"): return answerQuery(query=query, vectorstore=vectorstore, llmModel=llmModel) @app.post("/deleteChatbot") async def delete(chatbotName: str): return deleteTable(tableName=chatbotName) @app.post("/listChatbots") async def delete(username: str): return listTables(username=username)