File size: 6,140 Bytes
0dda2a1
 
4005ffd
0dda2a1
 
 
 
40d15f0
0dda2a1
40d15f0
0dda2a1
 
 
 
 
 
064943c
0dda2a1
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
064943c
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
0dda2a1
 
 
 
 
 
 
 
 
064943c
 
 
 
 
 
0dda2a1
 
 
064943c
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
0dda2a1
40d15f0
064943c
 
 
 
 
 
 
 
 
 
 
 
011040f
064943c
 
 
 
 
 
 
 
 
0dda2a1
 
 
 
 
 
 
 
 
 
27f5d4b
 
 
 
 
 
 
 
 
 
 
681223f
27f5d4b
 
 
0dda2a1
27f5d4b
 
 
0dda2a1
 
 
064943c
 
 
 
 
 
 
 
 
 
40d15f0
 
064943c
 
 
 
fe0b51f
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
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
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
from langchain_core.runnables import RunnablePassthrough, RunnableLambda
from langchain_text_splitters import RecursiveCharacterTextSplitter
from langchain_qdrant import QdrantVectorStore
from langchain_core.prompts.chat import ChatPromptTemplate
from langchain_core.output_parsers import StrOutputParser
from langchain_huggingface import HuggingFaceEmbeddings
from supabase.client import create_client
from qdrant_client import QdrantClient
from langchain_groq import ChatGroq
from supabase import create_client
from dotenv import load_dotenv
import pandas as pd
import os

load_dotenv("secrets.env")
client = create_client(os.environ["SUPABASE_URL"], os.environ["SUPABASE_KEY"])
qdrantClient = QdrantClient(url=os.environ["QDRANT_URL"], api_key=os.environ["QDRANT_API_KEY"])
model_kwargs = {"device": "cuda"}
encode_kwargs = {"normalize_embeddings": True}
embeddings = HuggingFaceEmbeddings(
    model_name = "BAAI/bge-m3",
    model_kwargs = model_kwargs,
    encode_kwargs = encode_kwargs
)
prompt = """
### Role
    - **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.
### Constraints
    1. **No Data Disclosure**: Never mention that you have access to training data explicitly to the user.
    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.
    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.
    4. **Restrictive Role Focus**: Do not answer questions or perform tasks unrelated to your role and training data.
DO NOT ADD ANYTHING BY YOURSELF OR ANSWER ON YOUR OWN!
Based on the context answer the following question.
Context:
=====================================
{context}
=====================================
{question}
NOTE: generate responses WITHOUT prepending phrases like "Response:", "Output:", or "Answer:", etc
"""
prompt = ChatPromptTemplate.from_template(prompt)


def createUser(username: str, password: str) -> None:
    try:
        userData = client.table("ConversAI_UserInfo").select("*").execute().data
        if username not in [userData[x]["username"] for x in range(len(userData))]:
            response = (
            client.table("ConversAI_UserInfo")
            .insert({"username": username, "password": password})
            .execute()
            )
            return {
                "output": "SUCCESS"
            }
        else: 
            return {
                "output": "USER ALREADY EXISTS"
            }
    except Exception as e:
        return {
            "error": e
        } 


def matchPassword(username: str, password: str) -> str:
    response = (
    client.table("ConversAI_UserInfo")
    .select("*")
    .eq("username", username)
    .execute()
    )
    try: return {
        "output": password == response.data[0]["password"]
        }
    except: return {
        "output": "USER DOESN'T EXIST"
        }


def createTable(tablename: str):
    try:
        qdrant = QdrantVectorStore.from_documents(
            [],
            embeddings,
            url=os.environ["QDRANT_URL"],
            prefer_grpc=True,
            api_key=os.environ["QDRANT_API_KEY"],
            collection_name=tablename
        )
        return {
            "output": "SUCCESS"
        }
    except Exception as e: 
        return {
            "error": e 
        }

def addDocuments(text: str, vectorstore: str):
    try:
        global embeddings
        text_splitter = RecursiveCharacterTextSplitter(
            chunk_size = 1024,
            chunk_overlap = 200,
            add_start_index = True
        )
        texts = text_splitter.create_documents([text])
        vectorstore = QdrantVectorStore.from_existing_collection(
            embedding = embeddings,
            collection_name=vectorstore,
            url=os.environ["QDRANT_URL"],
            api_key=os.environ["QDRANT_API_KEY"]
        )
        vectorstore.add_documents(documents = texts)
        return {
            "output": "SUCCESS"
        }
    except Exception as e:
        return {
            "error": e
        }


def format_docs(docs: str):
    context = "\n\n".join(doc.page_content for doc in docs)
    if context == "":
        context = "No context found"
    else: pass
    return context

def answerQuery(query: str, vectorstore: str, llmModel: str = "llama3-70b-8192") -> str:
    global prompt 
    global client
    global embeddings
    vectorstore = QdrantVectorStore.from_existing_collection(
        embedding = embeddings,
        collection_name=vectorstore,
        url=os.environ["QDRANT_URL"],
        api_key=os.environ["QDRANT_API_KEY"]
    )
    retriever = vectorstore.as_retriever()
    chain = (
        {"context": retriever | RunnableLambda(format_docs), "question": RunnablePassthrough()}
        | prompt
        | ChatGroq(model = llmModel, temperature = 0.3, max_tokens = 512)
        | StrOutputParser()
        )
    return {
        "output": chain.invoke(query)
    }


def deleteTable(tableName: str):
    try:
        global qdrantClient
        qdrantClient.delete_collection(collection_name=tableName)
        return {
            "output": "SUCCESS"
        }
    except Exception as e:
        return {
            "error": e
        }

def listTables(username: str):
    try:
        global qdrantClient
        qdrantCollections = qdrantClient.get_collections()
        return {
            "output": list(filter(lambda x: True if x.split("-")[1] == username else False, [x.name for x in qdrantCollections.collections]))
        }
    except Exception as e:
        return {
            "error": e
        }