ConversAI / functions.py
Rauhan's picture
UPDATE: QDRANT
02f4796
raw
history blame
5.06 kB
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"])
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:
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 "done"
else:
return "already exists"
def matchPassword(username: str, password: str) -> str:
response = (
client.table("ConversAI_UserInfo")
.select("*")
.eq("username", username)
.execute()
)
try: return password == response.data[0]["password"]
except: return "user doesn't exist"
def createTable(tablename: str):
qdrant = QdrantVectorStore.from_documents(
[],
embeddings,
url=os.environ["QDRANT_URL"],
prefer_grpc=True,
api_key=os.environ["QDRANT_API_KEY"],
collection_name=tablename
)
def addDocuments(text: str, vectorstore: str):
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"],
)
vectorstore.add_documents(documents = texts)
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"],
)
retriever = vectorstore.as_retriever()
chain = (
{"context": retriever | RunnableLambda(format_docs), "question": RunnablePassthrough(query)}
| prompt
| ChatGroq(model = llmModel, temperature = 0.3, max_tokens = 512)
| StrOutputParser()
)
return chain.invoke(query)
def deleteTable(tableName: str):
global qdrantClient
qdrantClient.delete_collection(collection_name=tableName)
def listTables(username: str):
global qdrantClient
qdrantCollections = qdrantClient.get_collections()
return [qdrantCollections.collections[x].name for x in qdrantCollections if qdrantCollections.collections[x].name.split("-")[1] == username]