File size: 2,917 Bytes
0dda2a1
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
40d15f0
 
 
0dda2a1
 
 
 
 
 
40d15f0
0dda2a1
 
 
 
 
 
40d15f0
0dda2a1
 
 
 
 
 
 
 
 
 
 
 
40d15f0
 
0dda2a1
 
 
 
40d15f0
0dda2a1
 
 
 
 
 
 
 
8f8a88e
0dda2a1
8f8a88e
0dda2a1
 
 
 
 
 
 
40d15f0
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
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
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
import io
from functions import *
from PyPDF2 import PdfReader
from fastapi import FastAPI, File, UploadFile
from fastapi.middleware.cors import CORSMiddleware


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):
    try:
        response = createUser(username = username, password = password)
        output = {
            "output": response
        }
    except Exception as e:
        output = {
            "error": e
        }
    return output


@app.post("/login")
async def login(username: str, password: str):
    try:
        response = matchPassword(username = username, password = password)
        output = {
            "output": response
        }
    except Exception as e:
        output = {
            "error": e
        }
    return output


@app.get("/newChatbot/{chatbotName}")
async def newChatbot(chatbotName: str):
    createTable(tablename = chatbotName)
    return {
        "output": "SUCCESS"
    }


@app.post("/addPDF")
async def addPDFData(vectorstore: str, pdf: UploadFile = File(...)):
    try:
        pdf = await pdf.read()
        reader = PdfReader(io.BytesIO(pdf))
        text = ""
        for page in reader.pages:
            text += page.extract_text()
        addDocuments(text = text, vectorstore = vectorstore)
        output = {
            "output": "SUCCESS"
        }
    except Exception as e:
        output = {
            "error": e
        }
    return output



@app.post("/addText")
async def addText(vectorstore: str, text: str):
    addDocuments(text = text, vectorstore = vectorstore)
    try:
        output = {
            "output": "SUCCESS"
        }
        return output
    except Exception as e:
        output = {
            "error": e
        }
    return output


@app.get("/answerQuery")
async def answerQuestion(query: str, vectorstore: str, llmModel: str = "llama3-70b-8192"):
    try:
        response = answerQuery(query=query, vectorstore=vectorstore, llmModel=llmModel)
        output = {
            "output": response
        }
    except Exception as e:
        output = {
            "error": e
        }
    return output

@app.get("/deleteChatbot/{chatbotName}")
async def delete(chatbotName: str):
    try:
        deleteTable(tableName=chatbotName)
        response = {
            "output": "SUCCESS"
        }
    except Exception as e:
        response = {
            "output": e
        }
    return response


@app.get("/listChatbots/{username}")
async def delete(username: str):
    try:
        chatbots = listTables(username=username)
        response = {
            "output": chatbots
        }
    except Exception as e:
        response = {
            "output": e
        }
    return response