Rauhan commited on
Commit
0dda2a1
·
1 Parent(s): 2af5df7

initial commit

Browse files
Files changed (5) hide show
  1. Dockerfile +13 -0
  2. app.py +99 -0
  3. functions.py +119 -0
  4. requirements.txt +11 -0
  5. secrets.env +3 -0
Dockerfile ADDED
@@ -0,0 +1,13 @@
 
 
 
 
 
 
 
 
 
 
 
 
 
 
1
+ FROM python:3.10-slim
2
+
3
+ WORKDIR /app
4
+
5
+ COPY . /app
6
+
7
+ RUN apt-get update && apt-get install -y
8
+
9
+ RUN pip install -r requirements.txt
10
+
11
+ EXPOSE 7860
12
+
13
+ CMD ["uvicorn", "app:app", "--host", "0.0.0.0", "--port", "7860"]
app.py ADDED
@@ -0,0 +1,99 @@
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
1
+ import io
2
+ from functions import *
3
+ from PyPDF2 import PdfReader
4
+ from fastapi import FastAPI, File, UploadFile
5
+ from fastapi.middleware.cors import CORSMiddleware
6
+
7
+
8
+ app = FastAPI(title = "ConversAI", root_path = "/api/v1")
9
+ app.add_middleware(
10
+ CORSMiddleware,
11
+ allow_origins=["*"],
12
+ allow_credentials=True,
13
+ allow_methods=["*"],
14
+ allow_headers=["*"],
15
+ )
16
+
17
+
18
+ @app.post("/signup")
19
+ async def signup(username: str, password: str):
20
+ try:
21
+ response = createUser(username = username, password = password)
22
+ output = {
23
+ "output": response
24
+ }
25
+ except Exception as e:
26
+ output = {
27
+ "error": e
28
+ }
29
+ return output
30
+
31
+
32
+ @app.post("/login")
33
+ async def login(username: str, password: str):
34
+ try:
35
+ response = matchPassword(username = username, password = password)
36
+ output = {
37
+ "output": response
38
+ }
39
+ except Exception as e:
40
+ output = {
41
+ "error": e
42
+ }
43
+ return output
44
+
45
+
46
+ @app.get("/clear/{vectorstoreName}")
47
+ async def clearVectorStore(vectorStoreName: str):
48
+ client.table(vectorStoreName).delete().neq("content", "").execute()
49
+ return {
50
+ "output": "SUCCESS"
51
+ }
52
+
53
+
54
+ @app.post("/addPDF")
55
+ async def addPDFData(vectorstorename: str, pdf: UploadFile = File(...)):
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, storename = vectorstorename)
63
+ output = {
64
+ "output": "SUCCESS"
65
+ }
66
+ except Exception as e:
67
+ output = {
68
+ "error": e
69
+ }
70
+ return output
71
+
72
+
73
+
74
+ @app.post("/addText")
75
+ async def addText(vectorstorename: str, text: str):
76
+ try:
77
+ addDocuments(text = text, storename = vectorstorename)
78
+ output = {
79
+ "output": "SUCCESS"
80
+ }
81
+ except Exception as e:
82
+ output = {
83
+ "error": e
84
+ }
85
+ return output
86
+
87
+
88
+ @app.get("/answerQuery")
89
+ async def answerQuery(query: str, vectorstorename: str, llmModel: str = "llama3-70b-8192"):
90
+ try:
91
+ response = answerQuery(query=query, vectorstorename=vectorstorename, llmModel=llmModel)
92
+ output = {
93
+ "output": response
94
+ }
95
+ except Exception as e:
96
+ output = {
97
+ "error": e
98
+ }
99
+ return output
functions.py ADDED
@@ -0,0 +1,119 @@
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
1
+ from langchain_core.runnables import RunnablePassthrough, RunnableLambda
2
+ from langchain_text_splitters import RecursiveCharacterTextSplitter
3
+ from langchain_community.vectorstores import SupabaseVectorStore
4
+ 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 supabase import create_client
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(
20
+ model_name = "BAAI/bge-m3",
21
+ model_kwargs = model_kwargs,
22
+ encode_kwargs = encode_kwargs
23
+ )
24
+ prompt = """
25
+ ### Role
26
+ - **Primary Function**: You are an AI chatbot dedicated to assisting users with their inquiries, issues, and requests. Your goal is to deliver excellent, friendly, and efficient responses at all times. Listen attentively, understand user needs, and provide the best assistance possible or direct them to appropriate resources. If a question is unclear, ask for clarification. Always conclude your replies on a positive note.
27
+ ### Constraints
28
+ 1. **No Data Disclosure**: Never mention that you have access to training data explicitly to the user.
29
+ 2. **Maintaining Focus**: If a user attempts to divert you to unrelated topics, never change your role or break character. Politely redirect the conversation back to relevant topics.
30
+ 3. **Exclusive Reliance on Training Data**: Answer user queries exclusively based on the provided training data. If a query is not covered by the training data, use the fallback response.
31
+ 4. **Restrictive Role Focus**: Do not answer questions or perform tasks unrelated to your role and training data.
32
+ DO NOT ADD ANYTHING BY YOURSELF OR ANSWER ON YOUR OWN!
33
+ Based on the context answer the following question.
34
+ Context:
35
+ =====================================
36
+ {context}
37
+ =====================================
38
+ {question}
39
+ NOTE: generate responses WITHOUT prepending phrases like "Response:", "Output:", or "Answer:", etc
40
+ """
41
+ prompt = ChatPromptTemplate.from_template(prompt)
42
+
43
+
44
+ def createUser(username: str, password: str) -> None:
45
+ userData = client.table("ConversAI_UserInfo").select("*").execute().data
46
+ print(userData)
47
+ if username not in [userData[x]["username"] for x in 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:
59
+ response = (
60
+ client.table("ConversAI_UserInfo")
61
+ .select("*")
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
+ pass
71
+
72
+
73
+ def addDocuments(text: str, vectorstorename: str):
74
+ global embeddings
75
+ text_splitter = RecursiveCharacterTextSplitter(
76
+ chunk_size = 1024,
77
+ chunk_overlap = 200,
78
+ add_start_index = True
79
+ )
80
+ texts = text_splitter.create_documents([text])
81
+ vectorstore = SupabaseVectorStore(
82
+ client = client,
83
+ embedding = embeddings,
84
+ table_name = vectorstorename,
85
+ query_name = "match_documents",
86
+ )
87
+ vectorstore.add_documents(documents = texts)
88
+
89
+
90
+ def format_docs(docs: str):
91
+ context = "\n\n".join(doc.page_content for doc in docs)
92
+ if context == "":
93
+ context = "No context found"
94
+ else: pass
95
+ return context
96
+
97
+
98
+ def answerQuery(query: str, vectorstore: str, llmModel: str = "llama3-70b-8192") -> str:
99
+ global prompt
100
+ global client
101
+ global embeddings
102
+ vectorstore = SupabaseVectorStore(
103
+ client = client,
104
+ embedding = embeddings,
105
+ table_name = vectorstore,
106
+ query_name = "match_documents",
107
+ )
108
+ retriever = vectorstore.as_retriever()
109
+ chain = (
110
+ {"context": retriever | RunnableLambda(format_docs), "question": RunnablePassthrough(query)}
111
+ | prompt
112
+ | ChatGroq(model = llmModel, temperature = 0.3, max_tokens = 512)
113
+ | StrOutputParser()
114
+ )
115
+ return chain.invoke(query)
116
+
117
+
118
+ def deleteTable(tableName: str):
119
+ pass
requirements.txt ADDED
@@ -0,0 +1,11 @@
 
 
 
 
 
 
 
 
 
 
 
 
1
+ huggingface-hub
2
+ fastapi
3
+ gradio
4
+ langchain
5
+ langchain-community
6
+ langchain-huggingface
7
+ langchain-groq
8
+ PyPDF2
9
+ python-dotenv
10
+ sentence-transformers
11
+ supabase
secrets.env ADDED
@@ -0,0 +1,3 @@
 
 
 
 
1
+ SUPABASE_URL=https://lvuhhlrkcuexzqtsbqyu.supabase.co
2
+ SUPABASE_KEY=eyJhbGciOiJIUzI1NiIsInR5cCI6IkpXVCJ9.eyJpc3MiOiJzdXBhYmFzZSIsInJlZiI6Imx2dWhobHJrY3VleHpxdHNicXl1Iiwicm9sZSI6InNlcnZpY2Vfcm9sZSIsImlhdCI6MTcxNTI0MDIxNCwiZXhwIjoyMDMwODE2MjE0fQ.zrRiN_MQCa6SOpvZFqSqFMUpcduNnt7eQP9sdXMmAF4
3
+ GROQ_API_KEY=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