Spaces:
Sleeping
Sleeping
UPDATE: QDRANT
Browse files- app.py +38 -11
- functions.py +12 -5
app.py
CHANGED
@@ -14,7 +14,6 @@ app.add_middleware(
|
|
14 |
allow_headers=["*"],
|
15 |
)
|
16 |
|
17 |
-
|
18 |
@app.post("/signup")
|
19 |
async def signup(username: str, password: str):
|
20 |
try:
|
@@ -43,23 +42,23 @@ async def login(username: str, password: str):
|
|
43 |
return output
|
44 |
|
45 |
|
46 |
-
@app.get("/
|
47 |
-
async def
|
48 |
-
|
49 |
return {
|
50 |
"output": "SUCCESS"
|
51 |
}
|
52 |
|
53 |
|
54 |
@app.post("/addPDF")
|
55 |
-
async def addPDFData(
|
56 |
try:
|
57 |
pdf = await pdf.read()
|
58 |
reader = PdfReader(io.BytesIO(pdf))
|
59 |
text = ""
|
60 |
for page in reader.pages:
|
61 |
text += page.extract_text()
|
62 |
-
addDocuments(text = text,
|
63 |
output = {
|
64 |
"output": "SUCCESS"
|
65 |
}
|
@@ -72,12 +71,13 @@ async def addPDFData(vectorstorename: str, pdf: UploadFile = File(...)):
|
|
72 |
|
73 |
|
74 |
@app.post("/addText")
|
75 |
-
async def addText(
|
|
|
76 |
try:
|
77 |
-
addDocuments(text = text, storename = vectorstorename)
|
78 |
output = {
|
79 |
"output": "SUCCESS"
|
80 |
}
|
|
|
81 |
except Exception as e:
|
82 |
output = {
|
83 |
"error": e
|
@@ -86,9 +86,9 @@ async def addText(vectorstorename: str, text: str):
|
|
86 |
|
87 |
|
88 |
@app.get("/answerQuery")
|
89 |
-
async def
|
90 |
try:
|
91 |
-
response =
|
92 |
output = {
|
93 |
"output": response
|
94 |
}
|
@@ -96,4 +96,31 @@ async def answerQuery(query: str, vectorstorename: str, llmModel: str = "llama3-
|
|
96 |
output = {
|
97 |
"error": e
|
98 |
}
|
99 |
-
return output
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
14 |
allow_headers=["*"],
|
15 |
)
|
16 |
|
|
|
17 |
@app.post("/signup")
|
18 |
async def signup(username: str, password: str):
|
19 |
try:
|
|
|
42 |
return output
|
43 |
|
44 |
|
45 |
+
@app.get("/newChatbot/{chatbotName}")
|
46 |
+
async def newChatbot(chatbotName: str):
|
47 |
+
createTable(tablename = chatbotName)
|
48 |
return {
|
49 |
"output": "SUCCESS"
|
50 |
}
|
51 |
|
52 |
|
53 |
@app.post("/addPDF")
|
54 |
+
async def addPDFData(vectorstore: str, pdf: UploadFile = File(...)):
|
55 |
try:
|
56 |
pdf = await pdf.read()
|
57 |
reader = PdfReader(io.BytesIO(pdf))
|
58 |
text = ""
|
59 |
for page in reader.pages:
|
60 |
text += page.extract_text()
|
61 |
+
addDocuments(text = text, vectorstore = vectorstore)
|
62 |
output = {
|
63 |
"output": "SUCCESS"
|
64 |
}
|
|
|
71 |
|
72 |
|
73 |
@app.post("/addText")
|
74 |
+
async def addText(vectorstore: str, text: str):
|
75 |
+
addDocuments(text = text, vectorstore = vectorstore)
|
76 |
try:
|
|
|
77 |
output = {
|
78 |
"output": "SUCCESS"
|
79 |
}
|
80 |
+
return output
|
81 |
except Exception as e:
|
82 |
output = {
|
83 |
"error": e
|
|
|
86 |
|
87 |
|
88 |
@app.get("/answerQuery")
|
89 |
+
async def answerQuestion(query: str, vectorstore: str, llmModel: str = "llama3-70b-8192"):
|
90 |
try:
|
91 |
+
response = answerQuery(query=query, vectorstore=vectorstore, llmModel=llmModel)
|
92 |
output = {
|
93 |
"output": response
|
94 |
}
|
|
|
96 |
output = {
|
97 |
"error": e
|
98 |
}
|
99 |
+
return output
|
100 |
+
|
101 |
+
@app.get("/deleteChatbot/{chatbotName}")
|
102 |
+
async def delete(chatbotName: str):
|
103 |
+
try:
|
104 |
+
deleteTable(tableName=chatbotName)
|
105 |
+
response = {
|
106 |
+
"output": "SUCCESS"
|
107 |
+
}
|
108 |
+
except Exception as e:
|
109 |
+
response = {
|
110 |
+
"output": e
|
111 |
+
}
|
112 |
+
return response
|
113 |
+
|
114 |
+
|
115 |
+
@app.get("/listChatbots/{username}")
|
116 |
+
async def delete(username: str):
|
117 |
+
try:
|
118 |
+
chatbots = listTables(username=username)
|
119 |
+
response = {
|
120 |
+
"output": chatbots
|
121 |
+
}
|
122 |
+
except Exception as e:
|
123 |
+
response = {
|
124 |
+
"output": e
|
125 |
+
}
|
126 |
+
return response
|
functions.py
CHANGED
@@ -5,15 +5,16 @@ from langchain_core.prompts.chat import ChatPromptTemplate
|
|
5 |
from langchain_core.output_parsers import StrOutputParser
|
6 |
from langchain_huggingface import HuggingFaceEmbeddings
|
7 |
from supabase.client import create_client
|
8 |
-
from
|
9 |
from langchain_groq import ChatGroq
|
|
|
10 |
from dotenv import load_dotenv
|
11 |
import pandas as pd
|
12 |
import os
|
13 |
|
14 |
-
|
15 |
load_dotenv("secrets.env")
|
16 |
client = create_client(os.environ["SUPABASE_URL"], os.environ["SUPABASE_KEY"])
|
|
|
17 |
model_kwargs = {"device": "cuda"}
|
18 |
encode_kwargs = {"normalize_embeddings": True}
|
19 |
embeddings = HuggingFaceEmbeddings(
|
@@ -76,7 +77,7 @@ def createTable(tablename: str):
|
|
76 |
collection_name=tablename
|
77 |
)
|
78 |
|
79 |
-
def addDocuments(text: str,
|
80 |
global embeddings
|
81 |
text_splitter = RecursiveCharacterTextSplitter(
|
82 |
chunk_size = 1024,
|
@@ -86,7 +87,7 @@ def addDocuments(text: str, vectorstorename: str):
|
|
86 |
texts = text_splitter.create_documents([text])
|
87 |
vectorstore = QdrantVectorStore.from_existing_collection(
|
88 |
embedding = embeddings,
|
89 |
-
collection_name=
|
90 |
url=os.environ["QDRANT_URL"],
|
91 |
)
|
92 |
vectorstore.add_documents(documents = texts)
|
@@ -120,4 +121,10 @@ def answerQuery(query: str, vectorstore: str, llmModel: str = "llama3-70b-8192")
|
|
120 |
|
121 |
|
122 |
def deleteTable(tableName: str):
|
123 |
-
|
|
|
|
|
|
|
|
|
|
|
|
|
|
5 |
from langchain_core.output_parsers import StrOutputParser
|
6 |
from langchain_huggingface import HuggingFaceEmbeddings
|
7 |
from supabase.client import create_client
|
8 |
+
from qdrant_client import QdrantClient
|
9 |
from langchain_groq import ChatGroq
|
10 |
+
from supabase import create_client
|
11 |
from dotenv import load_dotenv
|
12 |
import pandas as pd
|
13 |
import os
|
14 |
|
|
|
15 |
load_dotenv("secrets.env")
|
16 |
client = create_client(os.environ["SUPABASE_URL"], os.environ["SUPABASE_KEY"])
|
17 |
+
qdrantClient = QdrantClient(url=os.environ["QDRANT_URL"])
|
18 |
model_kwargs = {"device": "cuda"}
|
19 |
encode_kwargs = {"normalize_embeddings": True}
|
20 |
embeddings = HuggingFaceEmbeddings(
|
|
|
77 |
collection_name=tablename
|
78 |
)
|
79 |
|
80 |
+
def addDocuments(text: str, vectorstore: str):
|
81 |
global embeddings
|
82 |
text_splitter = RecursiveCharacterTextSplitter(
|
83 |
chunk_size = 1024,
|
|
|
87 |
texts = text_splitter.create_documents([text])
|
88 |
vectorstore = QdrantVectorStore.from_existing_collection(
|
89 |
embedding = embeddings,
|
90 |
+
collection_name=vectorstore,
|
91 |
url=os.environ["QDRANT_URL"],
|
92 |
)
|
93 |
vectorstore.add_documents(documents = texts)
|
|
|
121 |
|
122 |
|
123 |
def deleteTable(tableName: str):
|
124 |
+
global qdrantClient
|
125 |
+
qdrantClient.delete_collection(collection_name=tableName)
|
126 |
+
|
127 |
+
def listTables(username: str):
|
128 |
+
global qdrantClient
|
129 |
+
qdrantCollections = qdrantClient.get_collections()
|
130 |
+
return [qdrantCollections.collections[x].name for x in qdrantCollections if qdrantCollections.collections[x].name.split("-")[1] == username]
|