File size: 2,314 Bytes
0dda2a1
fc01c1b
0dda2a1
 
fc01c1b
0dda2a1
 
fc01c1b
0dda2a1
 
 
 
 
 
 
 
 
 
 
 
 
cfd2b5e
064943c
0dda2a1
 
 
 
cfd2b5e
064943c
0dda2a1
 
011040f
 
 
064943c
0dda2a1
 
 
40d15f0
cfd2b5e
 
 
 
 
064943c
0dda2a1
 
 
40d15f0
064943c
0dda2a1
 
fc01c1b
 
 
 
 
 
 
8cf3c06
fc01c1b
 
 
011040f
8f8a88e
681223f
cfd2b5e
40d15f0
011040f
40d15f0
064943c
40d15f0
011040f
40d15f0
064943c
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
import io
import re
from functions import *
from PyPDF2 import PdfReader
from bs4 import BeautifulSoup
from fastapi import FastAPI, File, UploadFile
from fastapi.middleware.cors import CORSMiddleware
from langchain_community.document_loaders import RecursiveUrlLoader


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):
    def bs4_extractor(html: str) -> str:
        soup = BeautifulSoup(html, "lxml")
        return re.sub(r"\n\n+", "\n\n", soup.text).strip()
    loader = RecursiveUrlLoader(websiteUrl, max_depth=2, timeout = 60, extractor=bs4_extractor)
    docs = loader.load()
    text = "\n\n".join([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)