Rauhan commited on
Commit
064943c
1 Parent(s): 02f4796

UPDATE: QDRANT

Browse files
Files changed (2) hide show
  1. app.py +8 -42
  2. functions.py +105 -55
app.py CHANGED
@@ -17,28 +17,18 @@ app.add_middleware(
17
  @app.post("/signup")
18
  async def signup(username: str, password: str):
19
  response = createUser(username = username, password = password)
20
- output = {
21
- "output": response
22
- }
23
-
24
- return output
25
 
26
 
27
  @app.post("/login")
28
  async def login(username: str, password: str):
29
  response = matchPassword(username = username, password = password)
30
- output = {
31
- "output": response
32
- }
33
- return output
34
 
35
 
36
  @app.get("/newChatbot/{chatbotName}")
37
  async def newChatbot(chatbotName: str):
38
- createTable(tablename = chatbotName)
39
- return {
40
- "output": "SUCCESS"
41
- }
42
 
43
 
44
  @app.post("/addPDF")
@@ -48,47 +38,23 @@ async def addPDFData(vectorstore: str, pdf: UploadFile = File(...)):
48
  text = ""
49
  for page in reader.pages:
50
  text += page.extract_text()
51
- addDocuments(text = text, vectorstore = vectorstore)
52
- output = {
53
- "output": "SUCCESS"
54
- }
55
-
56
- return output
57
-
58
 
59
 
60
  @app.post("/addText")
61
  async def addText(vectorstore: str, text: str):
62
- addDocuments(text = text, vectorstore = vectorstore)
63
- output = {
64
- "output": "SUCCESS"
65
- }
66
- return output
67
 
68
 
69
  @app.get("/answerQuery")
70
  async def answerQuestion(query: str, vectorstore: str, llmModel: str = "llama3-70b-8192"):
71
- response = answerQuery(query=query, vectorstore=vectorstore, llmModel=llmModel)
72
- output = {
73
- "output": response
74
- }
75
 
76
- return output
77
 
78
  @app.get("/deleteChatbot/{chatbotName}")
79
  async def delete(chatbotName: str):
80
- deleteTable(tableName=chatbotName)
81
- response = {
82
- "output": "SUCCESS"
83
- }
84
- return response
85
-
86
 
87
  @app.get("/listChatbots/{username}")
88
  async def delete(username: str):
89
- chatbots = listTables(username=username)
90
- response = {
91
- "output": chatbots
92
- }
93
-
94
- return response
 
17
  @app.post("/signup")
18
  async def signup(username: str, password: str):
19
  response = createUser(username = username, password = password)
20
+ return response
 
 
 
 
21
 
22
 
23
  @app.post("/login")
24
  async def login(username: str, password: str):
25
  response = matchPassword(username = username, password = password)
26
+ return response
 
 
 
27
 
28
 
29
  @app.get("/newChatbot/{chatbotName}")
30
  async def newChatbot(chatbotName: str):
31
+ return createTable(tablename = chatbotName)
 
 
 
32
 
33
 
34
  @app.post("/addPDF")
 
38
  text = ""
39
  for page in reader.pages:
40
  text += page.extract_text()
41
+ return addDocuments(text = text, vectorstore = vectorstore)
 
 
 
 
 
 
42
 
43
 
44
  @app.post("/addText")
45
  async def addText(vectorstore: str, text: str):
46
+ return addDocuments(text = text, vectorstore = vectorstore)
 
 
 
 
47
 
48
 
49
  @app.get("/answerQuery")
50
  async def answerQuestion(query: str, vectorstore: str, llmModel: str = "llama3-70b-8192"):
51
+ return answerQuery(query=query, vectorstore=vectorstore, llmModel=llmModel)
 
 
 
52
 
 
53
 
54
  @app.get("/deleteChatbot/{chatbotName}")
55
  async def delete(chatbotName: str):
56
+ return deleteTable(tableName=chatbotName)
 
 
 
 
 
57
 
58
  @app.get("/listChatbots/{username}")
59
  async def delete(username: str):
60
+ return listTables(username=username)
 
 
 
 
 
functions.py CHANGED
@@ -14,7 +14,7 @@ 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(
@@ -43,16 +43,25 @@ prompt = ChatPromptTemplate.from_template(prompt)
43
 
44
 
45
  def createUser(username: str, password: str) -> None:
46
- userData = client.table("ConversAI_UserInfo").select("*").execute().data
47
- if username not in [userData[x]["username"] for x in range(len(userData))]:
48
- response = (
49
- client.table("ConversAI_UserInfo")
50
- .insert({"username": username, "password": password})
51
- .execute()
52
- )
53
- return "done"
54
- else:
55
- return "already exists"
 
 
 
 
 
 
 
 
 
56
 
57
 
58
  def matchPassword(username: str, password: str) -> str:
@@ -62,34 +71,54 @@ def matchPassword(username: str, password: str) -> str:
62
  .eq("username", username)
63
  .execute()
64
  )
65
- try: return password == response.data[0]["password"]
66
- except: return "user doesn't exist"
 
 
 
 
67
 
68
 
69
  def createTable(tablename: str):
70
- qdrant = QdrantVectorStore.from_documents(
71
- [],
72
- embeddings,
73
- url=os.environ["QDRANT_URL"],
74
- prefer_grpc=True,
75
- api_key=os.environ["QDRANT_API_KEY"],
76
- collection_name=tablename
77
- )
 
 
 
 
 
 
 
 
78
 
79
  def addDocuments(text: str, vectorstore: str):
80
- global embeddings
81
- text_splitter = RecursiveCharacterTextSplitter(
82
- chunk_size = 1024,
83
- chunk_overlap = 200,
84
- add_start_index = True
85
- )
86
- texts = text_splitter.create_documents([text])
87
- vectorstore = QdrantVectorStore.from_existing_collection(
88
- embedding = embeddings,
89
- collection_name=vectorstore,
90
- url=os.environ["QDRANT_URL"],
91
- )
92
- vectorstore.add_documents(documents = texts)
 
 
 
 
 
 
 
 
93
 
94
 
95
  def format_docs(docs: str):
@@ -99,31 +128,52 @@ def format_docs(docs: str):
99
  else: pass
100
  return context
101
 
102
-
103
  def answerQuery(query: str, vectorstore: str, llmModel: str = "llama3-70b-8192") -> str:
104
- global prompt
105
- global client
106
- global embeddings
107
- vectorstore = QdrantVectorStore.from_existing_collection(
108
- embedding = embeddings,
109
- collection_name=vectorstore,
110
- url=os.environ["QDRANT_URL"],
111
- )
112
- retriever = vectorstore.as_retriever()
113
- chain = (
114
- {"context": retriever | RunnableLambda(format_docs), "question": RunnablePassthrough(query)}
115
- | prompt
116
- | ChatGroq(model = llmModel, temperature = 0.3, max_tokens = 512)
117
- | StrOutputParser()
118
  )
119
- return chain.invoke(query)
 
 
 
 
 
 
 
 
 
 
 
 
 
120
 
121
 
122
  def deleteTable(tableName: str):
123
- global qdrantClient
124
- qdrantClient.delete_collection(collection_name=tableName)
 
 
 
 
 
 
 
 
125
 
126
  def listTables(username: str):
127
- global qdrantClient
128
- qdrantCollections = qdrantClient.get_collections()
129
- return [qdrantCollections.collections[x].name for x in qdrantCollections if qdrantCollections.collections[x].name.split("-")[1] == username]
 
 
 
 
 
 
 
 
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"], api_key=os.environ["QDRANT_API_KEY"])
18
  model_kwargs = {"device": "cuda"}
19
  encode_kwargs = {"normalize_embeddings": True}
20
  embeddings = HuggingFaceEmbeddings(
 
43
 
44
 
45
  def createUser(username: str, password: str) -> None:
46
+ try:
47
+ userData = client.table("ConversAI_UserInfo").select("*").execute().data
48
+ if username not in [userData[x]["username"] for x in range(len(userData))]:
49
+ response = (
50
+ client.table("ConversAI_UserInfo")
51
+ .insert({"username": username, "password": password})
52
+ .execute()
53
+ )
54
+ return {
55
+ "output": "SUCCESS"
56
+ }
57
+ else:
58
+ return {
59
+ "output": "USER ALREADY EXISTS"
60
+ }
61
+ except Exception as e:
62
+ return {
63
+ "error": e
64
+ }
65
 
66
 
67
  def matchPassword(username: str, password: str) -> str:
 
71
  .eq("username", username)
72
  .execute()
73
  )
74
+ try: return {
75
+ "output": password == response.data[0]["password"]
76
+ }
77
+ except: return {
78
+ "output": "USER DOESN'T EXIST"
79
+ }
80
 
81
 
82
  def createTable(tablename: str):
83
+ try:
84
+ qdrant = QdrantVectorStore.from_documents(
85
+ [],
86
+ embeddings,
87
+ url=os.environ["QDRANT_URL"],
88
+ prefer_grpc=True,
89
+ api_key=os.environ["QDRANT_API_KEY"],
90
+ collection_name=tablename
91
+ )
92
+ return {
93
+ "output": "SUCCESS"
94
+ }
95
+ except Exception as e:
96
+ return {
97
+ "error": e
98
+ }
99
 
100
  def addDocuments(text: str, vectorstore: str):
101
+ try:
102
+ global embeddings
103
+ text_splitter = RecursiveCharacterTextSplitter(
104
+ chunk_size = 1024,
105
+ chunk_overlap = 200,
106
+ add_start_index = True
107
+ )
108
+ texts = text_splitter.create_documents([text])
109
+ vectorstore = QdrantVectorStore.from_existing_collection(
110
+ embedding = embeddings,
111
+ collection_name=vectorstore,
112
+ url=os.environ["QDRANT_URL"],
113
+ )
114
+ vectorstore.add_documents(documents = texts)
115
+ return {
116
+ "output": "SUCCESS"
117
+ }
118
+ except Exception as e:
119
+ return {
120
+ "error": e
121
+ }
122
 
123
 
124
  def format_docs(docs: str):
 
128
  else: pass
129
  return context
130
 
 
131
  def answerQuery(query: str, vectorstore: str, llmModel: str = "llama3-70b-8192") -> str:
132
+ try:
133
+ global prompt
134
+ global client
135
+ global embeddings
136
+ vectorstore = QdrantVectorStore.from_existing_collection(
137
+ embedding = embeddings,
138
+ collection_name=vectorstore,
139
+ url=os.environ["QDRANT_URL"],
 
 
 
 
 
 
140
  )
141
+ retriever = vectorstore.as_retriever()
142
+ chain = (
143
+ {"context": retriever | RunnableLambda(format_docs), "question": RunnablePassthrough(query)}
144
+ | prompt
145
+ | ChatGroq(model = llmModel, temperature = 0.3, max_tokens = 512)
146
+ | StrOutputParser()
147
+ )
148
+ return {
149
+ "output": chain.invoke(query)
150
+ }
151
+ except Exception as e:
152
+ return {
153
+ "error": e
154
+ }
155
 
156
 
157
  def deleteTable(tableName: str):
158
+ try:
159
+ global qdrantClient
160
+ qdrantClient.delete_collection(collection_name=tableName)
161
+ return {
162
+ "output": "SUCCESS"
163
+ }
164
+ except Exception as e:
165
+ return {
166
+ "error": e
167
+ }
168
 
169
  def listTables(username: str):
170
+ try:
171
+ global qdrantClient
172
+ qdrantCollections = qdrantClient.get_collections()
173
+ return {
174
+ "output": [qdrantCollections.collections[x].name for x in qdrantCollections if qdrantCollections.collections[x].name.split("-")[1] == username]
175
+ }
176
+ except Exception as e:
177
+ return {
178
+ "error": e
179
+ }