Spaces:
Running
Running
File size: 9,627 Bytes
a21db6e 2dd051b a21db6e 2dd051b a21db6e ba0e04c a21db6e 2dd051b a21db6e 2dd051b a21db6e 2dd051b a21db6e 2dd051b a21db6e 2dd051b a21db6e 2dd051b a21db6e 2dd051b a21db6e 2dd051b a21db6e 2dd051b a21db6e 2dd051b a21db6e 2dd051b a21db6e 2dd051b a21db6e 2dd051b a21db6e 2dd051b a21db6e 2dd051b a21db6e 2dd051b a21db6e 2dd051b a21db6e 2dd051b a21db6e 2dd051b a21db6e 2dd051b a21db6e 2dd051b a21db6e 2dd051b a21db6e 2dd051b a21db6e 2dd051b a21db6e 2dd051b |
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 177 178 179 180 181 182 183 184 185 186 187 188 189 190 191 192 193 194 195 196 197 198 199 200 201 202 203 204 205 206 207 208 209 210 211 212 213 214 215 216 217 218 219 220 221 222 223 224 225 226 227 228 229 230 231 232 233 234 235 236 237 238 239 240 241 242 243 244 245 246 247 248 249 250 251 252 253 254 255 256 257 258 259 260 261 262 263 264 265 266 267 268 269 270 271 272 273 274 275 276 277 278 279 280 281 282 283 284 285 286 287 288 |
import os
from getpass import getpass
from langchain_text_splitters import RecursiveCharacterTextSplitter
from langchain_huggingface import HuggingFaceEmbeddings
import torch
from langchain_huggingface import HuggingFaceEndpoint
from langchain_core.caches import InMemoryCache
from langchain_core.globals import set_llm_cache
from langchain_chroma import Chroma
from langchain.chains import RetrievalQA
import gradio
import PyPDF2
import json
import re
import time
import threading
from langchain_core.runnables import RunnableConfig, RunnablePassthrough
from langchain_core.output_parsers import StrOutputParser
from langchain_core.prompts import PromptTemplate
from langchain_core.runnables import RunnableLambda
#hfapi_key = getpass("Enter you HuggingFace access token:")
#os.environ["HF_TOKEN"] = hfapi_key
#os.environ["HUGGINGFACEHUB_API_TOKEN"] = hfapi_key
hfapi_key = os.getenv("Mytoken")
set_llm_cache(InMemoryCache()) # Set cache globally
persist_directory = 'docs/chroma/'
pdf_path = 'AIML.pdf'
####################################
def get_documents():
print("$$$$$ ENTER INTO get_documents $$$$$")
with open(pdf_path, 'rb') as file:
# Create a PDF reader object
pdf_reader = PyPDF2.PdfReader(file)
# Extract text from all pages
full_text = ""
for page in pdf_reader.pages:
full_text += page.extract_text() + "\n"
print("@@@@@@ EXIT FROM get_documents @@@@@")
return full_text
####################################
def getTextSplits():
print("$$$$$ ENTER INTO getDocSplitter $$$$$")
text_splitter = RecursiveCharacterTextSplitter(
chunk_size = 512,
chunk_overlap = 128
)
texts = text_splitter.split_text(get_documents())
#print("Page content ", texts)
print("@@@@@@ EXIT FROM getDocSplitter @@@@@")
return texts
####################################
def getEmbeddings():
print("$$$$$ ENTER INTO getEmbeddings $$$$$")
modelPath="mixedbread-ai/mxbai-embed-large-v1"
device = "cuda" if torch.cuda.is_available() else "cpu"
# Create a dictionary with model configuration options, specifying to use the CPU for computations
model_kwargs = {'device': device} # cuda/cpu
# Create a dictionary with encoding options, specifically setting 'normalize_embeddings' to False
encode_kwargs = {'normalize_embeddings': False}
embedding = HuggingFaceEmbeddings(
model_name=modelPath, # Provide the pre-trained model's path
model_kwargs=model_kwargs, # Pass the model configuration options
encode_kwargs=encode_kwargs # Pass the encoding options
)
print("@@@@@@ EXIT FROM getEmbeddings @@@@@")
return embedding
####################################
def getLLM():
print("$$$$$ ENTER INTO getLLM $$$$$")
model_kwargs = {
'device': "cuda" if torch.cuda.is_available() else "cpu",
'stream': True # Ensure streaming is enabled
}
llm = HuggingFaceEndpoint(
repo_id="HuggingFaceH4/zephyr-7b-beta",
task="text-generation",
max_new_tokens= 512,
do_sample= True,
temperature = 0.7,
repetition_penalty= 1.2,
top_k = 10
#model_kwargs=model_kwargs # Pass the model configuration options
)
print("@@@@@@ EXIT FROM getLLM @@@@@")
return llm
####################################
def is_chroma_db_present(directory: str):
#Check if the directory exists and contains any files.
return os.path.exists(directory) and len(os.listdir(directory)) > 0
####################################
def getRetiriver(query, metadata_filter:None):
print("$$$$$ ENTER INTO getRetiriver $$$$$")
# Classify query
query_type = classify_query(query)
print("Query classification", query_type)
k_default = 2
fetch_k_default = 5
search_type_default = "mmr"
# Routing logic
if query_type == 'concept':
# For conceptual queries, prioritize comprehensive context
k_default = 5
fetch_k_default = 10
search_type_default = "mmr"
elif query_type == 'example':
# For example queries, focus on more specific, relevant contexts
search_type_default = "similarity"
elif query_type == 'code':
# For code-related queries, use a more targeted retrieval
search_type_default = "similarity"
if is_chroma_db_present(persist_directory):
print(f"Chroma vector DB found in '{persist_directory}' and will be loaded.")
# Load vector store from the local directory
vectordb = Chroma(
persist_directory=persist_directory,
embedding_function=getEmbeddings(),
collection_name="ai_tutor")
else:
vectordb = Chroma.from_texts(
collection_name="ai_tutor",
texts=getTextSplits(),
embedding=getEmbeddings(),
persist_directory=persist_directory, # save the directory
)
print("metadata_filter", metadata_filter)
if(metadata_filter):
metadata_filter_dict = {
"result": metadata_filter # ChromaDB will perform a substring search
}
print("@@@@@@ EXIT FROM getRetiriver with metadata_filter @@@@@")
if search_type_default == "similarity":
return vectordb.as_retriever(search_type=search_type_default, search_kwargs={"k": k_default, "filter": metadata_filter_dict})
return vectordb.as_retriever(search_type=search_type_default, search_kwargs={"k": k_default, "fetch_k":fetch_k_default, "filter": metadata_filter_dict})
print("@@@@@@ EXIT FROM getRetiriver without metadata_filter @@@@@")
if search_type_default == "similarity":
return vectordb.as_retriever(search_type=search_type_default, search_kwargs={"k": k_default})
return vectordb.as_retriever(search_type=search_type_default, search_kwargs={"k": k_default, "fetch_k":fetch_k_default})
####################################
def classify_query(query):
"""
Classify the type of query to determine routing strategy.
Query Types:
- 'concept': Theoretical or conceptual questions
- 'example': Requests for practical examples
- 'code': Coding or implementation-related queries
- 'general': Default catch-all category
"""
query = query.lower()
# Concept detection patterns
concept_patterns = [
r'what is',
r'define',
r'explain',
r'describe',
r'theory of',
r'concept of'
]
# Example detection patterns
example_patterns = [
r'give an example',
r'show me an example',
r'demonstrate',
r'illustrate'
]
# Code-related detection patterns
code_patterns = [
r'how to implement',
r'code for',
r'python code',
r'algorithm implementation',
r'write a program'
]
# Check patterns
for pattern in concept_patterns:
if re.search(pattern, query):
return 'concept'
for pattern in example_patterns:
if re.search(pattern, query):
return 'example'
for pattern in code_patterns:
if re.search(pattern, query):
return 'code'
return 'general'
####################################
def get_rag_response(query, metadata_filter=None):
print("$$$$$ ENTER INTO get_rag_response $$$$$")
# Create the retriever
retriever = getRetiriver(query, metadata_filter)
# Get the LLM
llm = getLLM()
# Create a prompt template
template = """Use the following pieces of context to answer the question at the end.
If you don't know the answer, just say that you don't know, don't try to make up an answer.
Context: {context}
Question: {question}
Helpful Answer:"""
prompt = PromptTemplate.from_template(template)
# Function to prepare input for the chain
def prepare_inputs(inputs):
retrieved_docs = retriever.invoke(inputs["question"])
context = format_docs(retrieved_docs)
return {
"context": context,
"question": inputs["question"]
}
# Construct the RAG chain with streaming
rag_chain = (
RunnablePassthrough()
| RunnableLambda(prepare_inputs)
| prompt
| llm
| StrOutputParser()
)
# Stream the response
full_response = ""
for chunk in rag_chain.stream({"question": query}):
full_response += chunk
# Add a small delay to create a streaming effect
time.sleep(0.05) # 50 milliseconds between chunk updates
yield full_response
####################################
# Utility function to format documents
def format_docs(docs):
return "\n\n".join(doc.page_content for doc in docs)
####################################
# Input from user
in_question = gradio.Textbox(lines=10, placeholder=None, value="What are Artificial Intelligence and Machine Learning?", label='Ask a question to your AI Tutor')
# Optional metadata filter input
in_metadata_filter = gradio.Textbox(lines=2, placeholder=None, label='Optionally add a filter word')
# Output prediction
out_response = gradio.Textbox(label='Response', interactive=False, show_copy_button=True)
# Gradio interface to generate UI
iface = gradio.Interface(
fn = get_rag_response,
inputs=[in_question, in_metadata_filter],
outputs=out_response,
title="Your AI Tutor",
description="Ask a question, optionally add metadata filters.",
allow_flagging='never',
stream_every=0.5
)
iface.launch(share = True) |